variant.cpp 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567
  1. /**************************************************************************/
  2. /* variant.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "variant.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "core/io/json.h"
  33. #include "core/io/resource.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/variant/variant_parser.h"
  36. PagedAllocator<Variant::Pools::BucketSmall, true> Variant::Pools::_bucket_small;
  37. PagedAllocator<Variant::Pools::BucketMedium, true> Variant::Pools::_bucket_medium;
  38. PagedAllocator<Variant::Pools::BucketLarge, true> Variant::Pools::_bucket_large;
  39. String Variant::get_type_name(Variant::Type p_type) {
  40. switch (p_type) {
  41. case NIL: {
  42. return "Nil";
  43. }
  44. // Atomic types.
  45. case BOOL: {
  46. return "bool";
  47. }
  48. case INT: {
  49. return "int";
  50. }
  51. case FLOAT: {
  52. return "float";
  53. }
  54. case STRING: {
  55. return "String";
  56. }
  57. // Math types.
  58. case VECTOR2: {
  59. return "Vector2";
  60. }
  61. case VECTOR2I: {
  62. return "Vector2i";
  63. }
  64. case RECT2: {
  65. return "Rect2";
  66. }
  67. case RECT2I: {
  68. return "Rect2i";
  69. }
  70. case TRANSFORM2D: {
  71. return "Transform2D";
  72. }
  73. case VECTOR3: {
  74. return "Vector3";
  75. }
  76. case VECTOR3I: {
  77. return "Vector3i";
  78. }
  79. case VECTOR4: {
  80. return "Vector4";
  81. }
  82. case VECTOR4I: {
  83. return "Vector4i";
  84. }
  85. case PLANE: {
  86. return "Plane";
  87. }
  88. case AABB: {
  89. return "AABB";
  90. }
  91. case QUATERNION: {
  92. return "Quaternion";
  93. }
  94. case BASIS: {
  95. return "Basis";
  96. }
  97. case TRANSFORM3D: {
  98. return "Transform3D";
  99. }
  100. case PROJECTION: {
  101. return "Projection";
  102. }
  103. // Miscellaneous types.
  104. case COLOR: {
  105. return "Color";
  106. }
  107. case RID: {
  108. return "RID";
  109. }
  110. case OBJECT: {
  111. return "Object";
  112. }
  113. case CALLABLE: {
  114. return "Callable";
  115. }
  116. case SIGNAL: {
  117. return "Signal";
  118. }
  119. case STRING_NAME: {
  120. return "StringName";
  121. }
  122. case NODE_PATH: {
  123. return "NodePath";
  124. }
  125. case DICTIONARY: {
  126. return "Dictionary";
  127. }
  128. case ARRAY: {
  129. return "Array";
  130. }
  131. // Arrays.
  132. case PACKED_BYTE_ARRAY: {
  133. return "PackedByteArray";
  134. }
  135. case PACKED_INT32_ARRAY: {
  136. return "PackedInt32Array";
  137. }
  138. case PACKED_INT64_ARRAY: {
  139. return "PackedInt64Array";
  140. }
  141. case PACKED_FLOAT32_ARRAY: {
  142. return "PackedFloat32Array";
  143. }
  144. case PACKED_FLOAT64_ARRAY: {
  145. return "PackedFloat64Array";
  146. }
  147. case PACKED_STRING_ARRAY: {
  148. return "PackedStringArray";
  149. }
  150. case PACKED_VECTOR2_ARRAY: {
  151. return "PackedVector2Array";
  152. }
  153. case PACKED_VECTOR3_ARRAY: {
  154. return "PackedVector3Array";
  155. }
  156. case PACKED_COLOR_ARRAY: {
  157. return "PackedColorArray";
  158. }
  159. case PACKED_VECTOR4_ARRAY: {
  160. return "PackedVector4Array";
  161. }
  162. default: {
  163. }
  164. }
  165. return "";
  166. }
  167. static HashMap<String, Variant::Type> _init_type_name_map() {
  168. HashMap<String, Variant::Type> type_names;
  169. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  170. type_names[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
  171. }
  172. return type_names;
  173. }
  174. Variant::Type Variant::get_type_by_name(const String &p_type_name) {
  175. static HashMap<String, Type> type_names = _init_type_name_map();
  176. const Type *ptr = type_names.getptr(p_type_name);
  177. return (ptr == nullptr) ? VARIANT_MAX : *ptr;
  178. }
  179. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  180. if (p_type_from == p_type_to) {
  181. return true;
  182. }
  183. if (p_type_to == NIL) { //nil can convert to anything
  184. return true;
  185. }
  186. if (p_type_from == NIL) {
  187. return (p_type_to == OBJECT);
  188. }
  189. const Type *valid_types = nullptr;
  190. const Type *invalid_types = nullptr;
  191. switch (p_type_to) {
  192. case BOOL: {
  193. static const Type valid[] = {
  194. INT,
  195. FLOAT,
  196. STRING,
  197. NIL,
  198. };
  199. valid_types = valid;
  200. } break;
  201. case INT: {
  202. static const Type valid[] = {
  203. BOOL,
  204. FLOAT,
  205. STRING,
  206. NIL,
  207. };
  208. valid_types = valid;
  209. } break;
  210. case FLOAT: {
  211. static const Type valid[] = {
  212. BOOL,
  213. INT,
  214. STRING,
  215. NIL,
  216. };
  217. valid_types = valid;
  218. } break;
  219. case STRING: {
  220. static const Type invalid[] = {
  221. OBJECT,
  222. NIL
  223. };
  224. invalid_types = invalid;
  225. } break;
  226. case VECTOR2: {
  227. static const Type valid[] = {
  228. VECTOR2I,
  229. NIL,
  230. };
  231. valid_types = valid;
  232. } break;
  233. case VECTOR2I: {
  234. static const Type valid[] = {
  235. VECTOR2,
  236. NIL,
  237. };
  238. valid_types = valid;
  239. } break;
  240. case RECT2: {
  241. static const Type valid[] = {
  242. RECT2I,
  243. NIL,
  244. };
  245. valid_types = valid;
  246. } break;
  247. case RECT2I: {
  248. static const Type valid[] = {
  249. RECT2,
  250. NIL,
  251. };
  252. valid_types = valid;
  253. } break;
  254. case TRANSFORM2D: {
  255. static const Type valid[] = {
  256. TRANSFORM3D,
  257. NIL
  258. };
  259. valid_types = valid;
  260. } break;
  261. case VECTOR3: {
  262. static const Type valid[] = {
  263. VECTOR3I,
  264. NIL,
  265. };
  266. valid_types = valid;
  267. } break;
  268. case VECTOR3I: {
  269. static const Type valid[] = {
  270. VECTOR3,
  271. NIL,
  272. };
  273. valid_types = valid;
  274. } break;
  275. case VECTOR4: {
  276. static const Type valid[] = {
  277. VECTOR4I,
  278. NIL,
  279. };
  280. valid_types = valid;
  281. } break;
  282. case VECTOR4I: {
  283. static const Type valid[] = {
  284. VECTOR4,
  285. NIL,
  286. };
  287. valid_types = valid;
  288. } break;
  289. case QUATERNION: {
  290. static const Type valid[] = {
  291. BASIS,
  292. NIL
  293. };
  294. valid_types = valid;
  295. } break;
  296. case BASIS: {
  297. static const Type valid[] = {
  298. QUATERNION,
  299. NIL
  300. };
  301. valid_types = valid;
  302. } break;
  303. case TRANSFORM3D: {
  304. static const Type valid[] = {
  305. TRANSFORM2D,
  306. QUATERNION,
  307. BASIS,
  308. PROJECTION,
  309. NIL
  310. };
  311. valid_types = valid;
  312. } break;
  313. case PROJECTION: {
  314. static const Type valid[] = {
  315. TRANSFORM3D,
  316. NIL
  317. };
  318. valid_types = valid;
  319. } break;
  320. case COLOR: {
  321. static const Type valid[] = {
  322. STRING,
  323. INT,
  324. NIL,
  325. };
  326. valid_types = valid;
  327. } break;
  328. case RID: {
  329. static const Type valid[] = {
  330. OBJECT,
  331. NIL
  332. };
  333. valid_types = valid;
  334. } break;
  335. case OBJECT: {
  336. static const Type valid[] = {
  337. NIL
  338. };
  339. valid_types = valid;
  340. } break;
  341. case STRING_NAME: {
  342. static const Type valid[] = {
  343. STRING,
  344. NIL
  345. };
  346. valid_types = valid;
  347. } break;
  348. case NODE_PATH: {
  349. static const Type valid[] = {
  350. STRING,
  351. NIL
  352. };
  353. valid_types = valid;
  354. } break;
  355. case ARRAY: {
  356. static const Type valid[] = {
  357. PACKED_BYTE_ARRAY,
  358. PACKED_INT32_ARRAY,
  359. PACKED_INT64_ARRAY,
  360. PACKED_FLOAT32_ARRAY,
  361. PACKED_FLOAT64_ARRAY,
  362. PACKED_STRING_ARRAY,
  363. PACKED_COLOR_ARRAY,
  364. PACKED_VECTOR2_ARRAY,
  365. PACKED_VECTOR3_ARRAY,
  366. PACKED_VECTOR4_ARRAY,
  367. NIL
  368. };
  369. valid_types = valid;
  370. } break;
  371. // arrays
  372. case PACKED_BYTE_ARRAY: {
  373. static const Type valid[] = {
  374. ARRAY,
  375. NIL
  376. };
  377. valid_types = valid;
  378. } break;
  379. case PACKED_INT32_ARRAY: {
  380. static const Type valid[] = {
  381. ARRAY,
  382. NIL
  383. };
  384. valid_types = valid;
  385. } break;
  386. case PACKED_INT64_ARRAY: {
  387. static const Type valid[] = {
  388. ARRAY,
  389. NIL
  390. };
  391. valid_types = valid;
  392. } break;
  393. case PACKED_FLOAT32_ARRAY: {
  394. static const Type valid[] = {
  395. ARRAY,
  396. NIL
  397. };
  398. valid_types = valid;
  399. } break;
  400. case PACKED_FLOAT64_ARRAY: {
  401. static const Type valid[] = {
  402. ARRAY,
  403. NIL
  404. };
  405. valid_types = valid;
  406. } break;
  407. case PACKED_STRING_ARRAY: {
  408. static const Type valid[] = {
  409. ARRAY,
  410. NIL
  411. };
  412. valid_types = valid;
  413. } break;
  414. case PACKED_VECTOR2_ARRAY: {
  415. static const Type valid[] = {
  416. ARRAY,
  417. NIL
  418. };
  419. valid_types = valid;
  420. } break;
  421. case PACKED_VECTOR3_ARRAY: {
  422. static const Type valid[] = {
  423. ARRAY,
  424. NIL
  425. };
  426. valid_types = valid;
  427. } break;
  428. case PACKED_COLOR_ARRAY: {
  429. static const Type valid[] = {
  430. ARRAY,
  431. NIL
  432. };
  433. valid_types = valid;
  434. } break;
  435. case PACKED_VECTOR4_ARRAY: {
  436. static const Type valid[] = {
  437. ARRAY,
  438. NIL
  439. };
  440. valid_types = valid;
  441. } break;
  442. default: {
  443. }
  444. }
  445. if (valid_types) {
  446. int i = 0;
  447. while (valid_types[i] != NIL) {
  448. if (p_type_from == valid_types[i]) {
  449. return true;
  450. }
  451. i++;
  452. }
  453. } else if (invalid_types) {
  454. int i = 0;
  455. while (invalid_types[i] != NIL) {
  456. if (p_type_from == invalid_types[i]) {
  457. return false;
  458. }
  459. i++;
  460. }
  461. return true;
  462. }
  463. return false;
  464. }
  465. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  466. if (p_type_from == p_type_to) {
  467. return true;
  468. }
  469. if (p_type_to == NIL) { //nil can convert to anything
  470. return true;
  471. }
  472. if (p_type_from == NIL) {
  473. return (p_type_to == OBJECT);
  474. }
  475. const Type *valid_types = nullptr;
  476. switch (p_type_to) {
  477. case BOOL: {
  478. static const Type valid[] = {
  479. INT,
  480. FLOAT,
  481. //STRING,
  482. NIL,
  483. };
  484. valid_types = valid;
  485. } break;
  486. case INT: {
  487. static const Type valid[] = {
  488. BOOL,
  489. FLOAT,
  490. //STRING,
  491. NIL,
  492. };
  493. valid_types = valid;
  494. } break;
  495. case FLOAT: {
  496. static const Type valid[] = {
  497. BOOL,
  498. INT,
  499. //STRING,
  500. NIL,
  501. };
  502. valid_types = valid;
  503. } break;
  504. case STRING: {
  505. static const Type valid[] = {
  506. NODE_PATH,
  507. STRING_NAME,
  508. NIL
  509. };
  510. valid_types = valid;
  511. } break;
  512. case VECTOR2: {
  513. static const Type valid[] = {
  514. VECTOR2I,
  515. NIL,
  516. };
  517. valid_types = valid;
  518. } break;
  519. case VECTOR2I: {
  520. static const Type valid[] = {
  521. VECTOR2,
  522. NIL,
  523. };
  524. valid_types = valid;
  525. } break;
  526. case RECT2: {
  527. static const Type valid[] = {
  528. RECT2I,
  529. NIL,
  530. };
  531. valid_types = valid;
  532. } break;
  533. case RECT2I: {
  534. static const Type valid[] = {
  535. RECT2,
  536. NIL,
  537. };
  538. valid_types = valid;
  539. } break;
  540. case TRANSFORM2D: {
  541. static const Type valid[] = {
  542. TRANSFORM3D,
  543. NIL
  544. };
  545. valid_types = valid;
  546. } break;
  547. case VECTOR3: {
  548. static const Type valid[] = {
  549. VECTOR3I,
  550. NIL,
  551. };
  552. valid_types = valid;
  553. } break;
  554. case VECTOR3I: {
  555. static const Type valid[] = {
  556. VECTOR3,
  557. NIL,
  558. };
  559. valid_types = valid;
  560. } break;
  561. case VECTOR4: {
  562. static const Type valid[] = {
  563. VECTOR4I,
  564. NIL,
  565. };
  566. valid_types = valid;
  567. } break;
  568. case VECTOR4I: {
  569. static const Type valid[] = {
  570. VECTOR4,
  571. NIL,
  572. };
  573. valid_types = valid;
  574. } break;
  575. case QUATERNION: {
  576. static const Type valid[] = {
  577. BASIS,
  578. NIL
  579. };
  580. valid_types = valid;
  581. } break;
  582. case BASIS: {
  583. static const Type valid[] = {
  584. QUATERNION,
  585. NIL
  586. };
  587. valid_types = valid;
  588. } break;
  589. case TRANSFORM3D: {
  590. static const Type valid[] = {
  591. TRANSFORM2D,
  592. QUATERNION,
  593. BASIS,
  594. PROJECTION,
  595. NIL
  596. };
  597. valid_types = valid;
  598. } break;
  599. case PROJECTION: {
  600. static const Type valid[] = {
  601. TRANSFORM3D,
  602. NIL
  603. };
  604. valid_types = valid;
  605. } break;
  606. case COLOR: {
  607. static const Type valid[] = {
  608. STRING,
  609. INT,
  610. NIL,
  611. };
  612. valid_types = valid;
  613. } break;
  614. case RID: {
  615. static const Type valid[] = {
  616. OBJECT,
  617. NIL
  618. };
  619. valid_types = valid;
  620. } break;
  621. case OBJECT: {
  622. static const Type valid[] = {
  623. NIL
  624. };
  625. valid_types = valid;
  626. } break;
  627. case STRING_NAME: {
  628. static const Type valid[] = {
  629. STRING,
  630. NIL
  631. };
  632. valid_types = valid;
  633. } break;
  634. case NODE_PATH: {
  635. static const Type valid[] = {
  636. STRING,
  637. NIL
  638. };
  639. valid_types = valid;
  640. } break;
  641. case ARRAY: {
  642. static const Type valid[] = {
  643. PACKED_BYTE_ARRAY,
  644. PACKED_INT32_ARRAY,
  645. PACKED_INT64_ARRAY,
  646. PACKED_FLOAT32_ARRAY,
  647. PACKED_FLOAT64_ARRAY,
  648. PACKED_STRING_ARRAY,
  649. PACKED_COLOR_ARRAY,
  650. PACKED_VECTOR2_ARRAY,
  651. PACKED_VECTOR3_ARRAY,
  652. PACKED_VECTOR4_ARRAY,
  653. NIL
  654. };
  655. valid_types = valid;
  656. } break;
  657. // arrays
  658. case PACKED_BYTE_ARRAY: {
  659. static const Type valid[] = {
  660. ARRAY,
  661. NIL
  662. };
  663. valid_types = valid;
  664. } break;
  665. case PACKED_INT32_ARRAY: {
  666. static const Type valid[] = {
  667. ARRAY,
  668. NIL
  669. };
  670. valid_types = valid;
  671. } break;
  672. case PACKED_INT64_ARRAY: {
  673. static const Type valid[] = {
  674. ARRAY,
  675. NIL
  676. };
  677. valid_types = valid;
  678. } break;
  679. case PACKED_FLOAT32_ARRAY: {
  680. static const Type valid[] = {
  681. ARRAY,
  682. NIL
  683. };
  684. valid_types = valid;
  685. } break;
  686. case PACKED_FLOAT64_ARRAY: {
  687. static const Type valid[] = {
  688. ARRAY,
  689. NIL
  690. };
  691. valid_types = valid;
  692. } break;
  693. case PACKED_STRING_ARRAY: {
  694. static const Type valid[] = {
  695. ARRAY,
  696. NIL
  697. };
  698. valid_types = valid;
  699. } break;
  700. case PACKED_VECTOR2_ARRAY: {
  701. static const Type valid[] = {
  702. ARRAY,
  703. NIL
  704. };
  705. valid_types = valid;
  706. } break;
  707. case PACKED_VECTOR3_ARRAY: {
  708. static const Type valid[] = {
  709. ARRAY,
  710. NIL
  711. };
  712. valid_types = valid;
  713. } break;
  714. case PACKED_COLOR_ARRAY: {
  715. static const Type valid[] = {
  716. ARRAY,
  717. NIL
  718. };
  719. valid_types = valid;
  720. } break;
  721. case PACKED_VECTOR4_ARRAY: {
  722. static const Type valid[] = {
  723. ARRAY,
  724. NIL
  725. };
  726. valid_types = valid;
  727. } break;
  728. default: {
  729. }
  730. }
  731. if (valid_types) {
  732. int i = 0;
  733. while (valid_types[i] != NIL) {
  734. if (p_type_from == valid_types[i]) {
  735. return true;
  736. }
  737. i++;
  738. }
  739. }
  740. return false;
  741. }
  742. bool Variant::operator==(const Variant &p_variant) const {
  743. return hash_compare(p_variant);
  744. }
  745. bool Variant::operator!=(const Variant &p_variant) const {
  746. // Don't use `!hash_compare(p_variant)` given it makes use of OP_EQUAL
  747. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  748. return true;
  749. }
  750. bool v;
  751. Variant r;
  752. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  753. return r;
  754. }
  755. bool Variant::operator<(const Variant &p_variant) const {
  756. if (type != p_variant.type) { //if types differ, then order by type first
  757. return type < p_variant.type;
  758. }
  759. bool v;
  760. Variant r;
  761. evaluate(OP_LESS, *this, p_variant, r, v);
  762. return r;
  763. }
  764. bool Variant::is_zero() const {
  765. switch (type) {
  766. case NIL: {
  767. return true;
  768. }
  769. // Atomic types.
  770. case BOOL: {
  771. return !(_data._bool);
  772. }
  773. case INT: {
  774. return _data._int == 0;
  775. }
  776. case FLOAT: {
  777. return _data._float == 0;
  778. }
  779. case STRING: {
  780. return *reinterpret_cast<const String *>(_data._mem) == String();
  781. }
  782. // Math types.
  783. case VECTOR2: {
  784. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  785. }
  786. case VECTOR2I: {
  787. return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i();
  788. }
  789. case RECT2: {
  790. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  791. }
  792. case RECT2I: {
  793. return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i();
  794. }
  795. case TRANSFORM2D: {
  796. return *_data._transform2d == Transform2D();
  797. }
  798. case VECTOR3: {
  799. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  800. }
  801. case VECTOR3I: {
  802. return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i();
  803. }
  804. case VECTOR4: {
  805. return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4();
  806. }
  807. case VECTOR4I: {
  808. return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i();
  809. }
  810. case PLANE: {
  811. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  812. }
  813. case AABB: {
  814. return *_data._aabb == ::AABB();
  815. }
  816. case QUATERNION: {
  817. return *reinterpret_cast<const Quaternion *>(_data._mem) == Quaternion();
  818. }
  819. case BASIS: {
  820. return *_data._basis == Basis();
  821. }
  822. case TRANSFORM3D: {
  823. return *_data._transform3d == Transform3D();
  824. }
  825. case PROJECTION: {
  826. return *_data._projection == Projection();
  827. }
  828. // Miscellaneous types.
  829. case COLOR: {
  830. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  831. }
  832. case RID: {
  833. return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID();
  834. }
  835. case OBJECT: {
  836. return get_validated_object() == nullptr;
  837. }
  838. case CALLABLE: {
  839. return reinterpret_cast<const Callable *>(_data._mem)->is_null();
  840. }
  841. case SIGNAL: {
  842. return reinterpret_cast<const Signal *>(_data._mem)->is_null();
  843. }
  844. case STRING_NAME: {
  845. return *reinterpret_cast<const StringName *>(_data._mem) == StringName();
  846. }
  847. case NODE_PATH: {
  848. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  849. }
  850. case DICTIONARY: {
  851. return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty();
  852. }
  853. case ARRAY: {
  854. return reinterpret_cast<const Array *>(_data._mem)->is_empty();
  855. }
  856. // Arrays.
  857. case PACKED_BYTE_ARRAY: {
  858. return PackedArrayRef<uint8_t>::get_array(_data.packed_array).is_empty();
  859. }
  860. case PACKED_INT32_ARRAY: {
  861. return PackedArrayRef<int32_t>::get_array(_data.packed_array).is_empty();
  862. }
  863. case PACKED_INT64_ARRAY: {
  864. return PackedArrayRef<int64_t>::get_array(_data.packed_array).is_empty();
  865. }
  866. case PACKED_FLOAT32_ARRAY: {
  867. return PackedArrayRef<float>::get_array(_data.packed_array).is_empty();
  868. }
  869. case PACKED_FLOAT64_ARRAY: {
  870. return PackedArrayRef<double>::get_array(_data.packed_array).is_empty();
  871. }
  872. case PACKED_STRING_ARRAY: {
  873. return PackedArrayRef<String>::get_array(_data.packed_array).is_empty();
  874. }
  875. case PACKED_VECTOR2_ARRAY: {
  876. return PackedArrayRef<Vector2>::get_array(_data.packed_array).is_empty();
  877. }
  878. case PACKED_VECTOR3_ARRAY: {
  879. return PackedArrayRef<Vector3>::get_array(_data.packed_array).is_empty();
  880. }
  881. case PACKED_COLOR_ARRAY: {
  882. return PackedArrayRef<Color>::get_array(_data.packed_array).is_empty();
  883. }
  884. case PACKED_VECTOR4_ARRAY: {
  885. return PackedArrayRef<Vector4>::get_array(_data.packed_array).is_empty();
  886. }
  887. default: {
  888. }
  889. }
  890. return false;
  891. }
  892. bool Variant::is_one() const {
  893. switch (type) {
  894. case NIL: {
  895. return true;
  896. }
  897. case BOOL: {
  898. return _data._bool;
  899. }
  900. case INT: {
  901. return _data._int == 1;
  902. }
  903. case FLOAT: {
  904. return _data._float == 1;
  905. }
  906. case VECTOR2: {
  907. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  908. }
  909. case VECTOR2I: {
  910. return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(1, 1);
  911. }
  912. case RECT2: {
  913. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  914. }
  915. case RECT2I: {
  916. return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(1, 1, 1, 1);
  917. }
  918. case VECTOR3: {
  919. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  920. }
  921. case VECTOR3I: {
  922. return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(1, 1, 1);
  923. }
  924. case VECTOR4: {
  925. return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(1, 1, 1, 1);
  926. }
  927. case VECTOR4I: {
  928. return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(1, 1, 1, 1);
  929. }
  930. case PLANE: {
  931. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  932. }
  933. case COLOR: {
  934. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  935. }
  936. default: {
  937. return !is_zero();
  938. }
  939. }
  940. }
  941. bool Variant::is_null() const {
  942. if (type == OBJECT && _get_obj().obj) {
  943. return false;
  944. } else {
  945. return true;
  946. }
  947. }
  948. void Variant::ObjData::ref(const ObjData &p_from) {
  949. // Mirrors Ref::ref in refcounted.h
  950. if (p_from.id == id) {
  951. return;
  952. }
  953. ObjData cleanup_ref = *this;
  954. *this = p_from;
  955. if (id.is_ref_counted()) {
  956. RefCounted *reference = static_cast<RefCounted *>(obj);
  957. // Assuming reference is not null because id.is_ref_counted() was true.
  958. if (!reference->reference()) {
  959. *this = ObjData();
  960. }
  961. }
  962. cleanup_ref.unref();
  963. }
  964. void Variant::ObjData::ref_pointer(Object *p_object) {
  965. // Mirrors Ref::ref_pointer in refcounted.h
  966. if (p_object == obj) {
  967. return;
  968. }
  969. ObjData cleanup_ref = *this;
  970. if (p_object) {
  971. *this = ObjData{ p_object->get_instance_id(), p_object };
  972. if (p_object->is_ref_counted()) {
  973. RefCounted *reference = static_cast<RefCounted *>(p_object);
  974. if (!reference->init_ref()) {
  975. *this = ObjData();
  976. }
  977. }
  978. } else {
  979. *this = ObjData();
  980. }
  981. cleanup_ref.unref();
  982. }
  983. void Variant::ObjData::unref() {
  984. // Mirrors Ref::unref in refcounted.h
  985. if (id.is_ref_counted()) {
  986. RefCounted *reference = static_cast<RefCounted *>(obj);
  987. // Assuming reference is not null because id.is_ref_counted() was true.
  988. if (reference->unreference()) {
  989. memdelete(reference);
  990. }
  991. }
  992. *this = ObjData();
  993. }
  994. void Variant::reference(const Variant &p_variant) {
  995. if (type == OBJECT && p_variant.type == OBJECT) {
  996. _get_obj().ref(p_variant._get_obj());
  997. return;
  998. }
  999. clear();
  1000. type = p_variant.type;
  1001. switch (p_variant.type) {
  1002. case NIL: {
  1003. // None.
  1004. } break;
  1005. // Atomic types.
  1006. case BOOL: {
  1007. _data._bool = p_variant._data._bool;
  1008. } break;
  1009. case INT: {
  1010. _data._int = p_variant._data._int;
  1011. } break;
  1012. case FLOAT: {
  1013. _data._float = p_variant._data._float;
  1014. } break;
  1015. case STRING: {
  1016. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  1017. } break;
  1018. // Math types.
  1019. case VECTOR2: {
  1020. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  1021. } break;
  1022. case VECTOR2I: {
  1023. memnew_placement(_data._mem, Vector2i(*reinterpret_cast<const Vector2i *>(p_variant._data._mem)));
  1024. } break;
  1025. case RECT2: {
  1026. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  1027. } break;
  1028. case RECT2I: {
  1029. memnew_placement(_data._mem, Rect2i(*reinterpret_cast<const Rect2i *>(p_variant._data._mem)));
  1030. } break;
  1031. case TRANSFORM2D: {
  1032. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  1033. memnew_placement(_data._transform2d, Transform2D(*p_variant._data._transform2d));
  1034. } break;
  1035. case VECTOR3: {
  1036. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  1037. } break;
  1038. case VECTOR3I: {
  1039. memnew_placement(_data._mem, Vector3i(*reinterpret_cast<const Vector3i *>(p_variant._data._mem)));
  1040. } break;
  1041. case VECTOR4: {
  1042. memnew_placement(_data._mem, Vector4(*reinterpret_cast<const Vector4 *>(p_variant._data._mem)));
  1043. } break;
  1044. case VECTOR4I: {
  1045. memnew_placement(_data._mem, Vector4i(*reinterpret_cast<const Vector4i *>(p_variant._data._mem)));
  1046. } break;
  1047. case PLANE: {
  1048. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  1049. } break;
  1050. case AABB: {
  1051. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  1052. memnew_placement(_data._aabb, ::AABB(*p_variant._data._aabb));
  1053. } break;
  1054. case QUATERNION: {
  1055. memnew_placement(_data._mem, Quaternion(*reinterpret_cast<const Quaternion *>(p_variant._data._mem)));
  1056. } break;
  1057. case BASIS: {
  1058. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  1059. memnew_placement(_data._basis, Basis(*p_variant._data._basis));
  1060. } break;
  1061. case TRANSFORM3D: {
  1062. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  1063. memnew_placement(_data._transform3d, Transform3D(*p_variant._data._transform3d));
  1064. } break;
  1065. case PROJECTION: {
  1066. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  1067. memnew_placement(_data._projection, Projection(*p_variant._data._projection));
  1068. } break;
  1069. // Miscellaneous types.
  1070. case COLOR: {
  1071. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  1072. } break;
  1073. case RID: {
  1074. memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem)));
  1075. } break;
  1076. case OBJECT: {
  1077. memnew_placement(_data._mem, ObjData);
  1078. _get_obj().ref(p_variant._get_obj());
  1079. } break;
  1080. case CALLABLE: {
  1081. memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem)));
  1082. } break;
  1083. case SIGNAL: {
  1084. memnew_placement(_data._mem, Signal(*reinterpret_cast<const Signal *>(p_variant._data._mem)));
  1085. } break;
  1086. case STRING_NAME: {
  1087. memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem)));
  1088. } break;
  1089. case NODE_PATH: {
  1090. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  1091. } break;
  1092. case DICTIONARY: {
  1093. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  1094. } break;
  1095. case ARRAY: {
  1096. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  1097. } break;
  1098. // Arrays.
  1099. case PACKED_BYTE_ARRAY: {
  1100. _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference();
  1101. if (!_data.packed_array) {
  1102. _data.packed_array = PackedArrayRef<uint8_t>::create();
  1103. }
  1104. } break;
  1105. case PACKED_INT32_ARRAY: {
  1106. _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference();
  1107. if (!_data.packed_array) {
  1108. _data.packed_array = PackedArrayRef<int32_t>::create();
  1109. }
  1110. } break;
  1111. case PACKED_INT64_ARRAY: {
  1112. _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference();
  1113. if (!_data.packed_array) {
  1114. _data.packed_array = PackedArrayRef<int64_t>::create();
  1115. }
  1116. } break;
  1117. case PACKED_FLOAT32_ARRAY: {
  1118. _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference();
  1119. if (!_data.packed_array) {
  1120. _data.packed_array = PackedArrayRef<float>::create();
  1121. }
  1122. } break;
  1123. case PACKED_FLOAT64_ARRAY: {
  1124. _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference();
  1125. if (!_data.packed_array) {
  1126. _data.packed_array = PackedArrayRef<double>::create();
  1127. }
  1128. } break;
  1129. case PACKED_STRING_ARRAY: {
  1130. _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference();
  1131. if (!_data.packed_array) {
  1132. _data.packed_array = PackedArrayRef<String>::create();
  1133. }
  1134. } break;
  1135. case PACKED_VECTOR2_ARRAY: {
  1136. _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference();
  1137. if (!_data.packed_array) {
  1138. _data.packed_array = PackedArrayRef<Vector2>::create();
  1139. }
  1140. } break;
  1141. case PACKED_VECTOR3_ARRAY: {
  1142. _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference();
  1143. if (!_data.packed_array) {
  1144. _data.packed_array = PackedArrayRef<Vector3>::create();
  1145. }
  1146. } break;
  1147. case PACKED_COLOR_ARRAY: {
  1148. _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference();
  1149. if (!_data.packed_array) {
  1150. _data.packed_array = PackedArrayRef<Color>::create();
  1151. }
  1152. } break;
  1153. case PACKED_VECTOR4_ARRAY: {
  1154. _data.packed_array = static_cast<PackedArrayRef<Vector4> *>(p_variant._data.packed_array)->reference();
  1155. if (!_data.packed_array) {
  1156. _data.packed_array = PackedArrayRef<Vector4>::create();
  1157. }
  1158. } break;
  1159. default: {
  1160. }
  1161. }
  1162. }
  1163. void Variant::zero() {
  1164. switch (type) {
  1165. case NIL:
  1166. break;
  1167. case BOOL:
  1168. _data._bool = false;
  1169. break;
  1170. case INT:
  1171. _data._int = 0;
  1172. break;
  1173. case FLOAT:
  1174. _data._float = 0;
  1175. break;
  1176. case VECTOR2:
  1177. *reinterpret_cast<Vector2 *>(_data._mem) = Vector2();
  1178. break;
  1179. case VECTOR2I:
  1180. *reinterpret_cast<Vector2i *>(_data._mem) = Vector2i();
  1181. break;
  1182. case RECT2:
  1183. *reinterpret_cast<Rect2 *>(_data._mem) = Rect2();
  1184. break;
  1185. case RECT2I:
  1186. *reinterpret_cast<Rect2i *>(_data._mem) = Rect2i();
  1187. break;
  1188. case VECTOR3:
  1189. *reinterpret_cast<Vector3 *>(_data._mem) = Vector3();
  1190. break;
  1191. case VECTOR3I:
  1192. *reinterpret_cast<Vector3i *>(_data._mem) = Vector3i();
  1193. break;
  1194. case VECTOR4:
  1195. *reinterpret_cast<Vector4 *>(_data._mem) = Vector4();
  1196. break;
  1197. case VECTOR4I:
  1198. *reinterpret_cast<Vector4i *>(_data._mem) = Vector4i();
  1199. break;
  1200. case PLANE:
  1201. *reinterpret_cast<Plane *>(_data._mem) = Plane();
  1202. break;
  1203. case QUATERNION:
  1204. *reinterpret_cast<Quaternion *>(_data._mem) = Quaternion();
  1205. break;
  1206. case COLOR:
  1207. *reinterpret_cast<Color *>(_data._mem) = Color();
  1208. break;
  1209. default:
  1210. Type prev_type = type;
  1211. clear();
  1212. if (type != prev_type) {
  1213. // clear() changes type to NIL, so it needs to be restored.
  1214. Callable::CallError ce;
  1215. Variant::construct(prev_type, *this, nullptr, 0, ce);
  1216. }
  1217. break;
  1218. }
  1219. }
  1220. void Variant::_clear_internal() {
  1221. switch (type) {
  1222. case STRING: {
  1223. reinterpret_cast<String *>(_data._mem)->~String();
  1224. } break;
  1225. // Math types.
  1226. case TRANSFORM2D: {
  1227. if (_data._transform2d) {
  1228. _data._transform2d->~Transform2D();
  1229. Pools::_bucket_small.free((Pools::BucketSmall *)_data._transform2d);
  1230. _data._transform2d = nullptr;
  1231. }
  1232. } break;
  1233. case AABB: {
  1234. if (_data._aabb) {
  1235. _data._aabb->~AABB();
  1236. Pools::_bucket_small.free((Pools::BucketSmall *)_data._aabb);
  1237. _data._aabb = nullptr;
  1238. }
  1239. } break;
  1240. case BASIS: {
  1241. if (_data._basis) {
  1242. _data._basis->~Basis();
  1243. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._basis);
  1244. _data._basis = nullptr;
  1245. }
  1246. } break;
  1247. case TRANSFORM3D: {
  1248. if (_data._transform3d) {
  1249. _data._transform3d->~Transform3D();
  1250. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._transform3d);
  1251. _data._transform3d = nullptr;
  1252. }
  1253. } break;
  1254. case PROJECTION: {
  1255. if (_data._projection) {
  1256. _data._projection->~Projection();
  1257. Pools::_bucket_large.free((Pools::BucketLarge *)_data._projection);
  1258. _data._projection = nullptr;
  1259. }
  1260. } break;
  1261. // Miscellaneous types.
  1262. case STRING_NAME: {
  1263. reinterpret_cast<StringName *>(_data._mem)->~StringName();
  1264. } break;
  1265. case NODE_PATH: {
  1266. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  1267. } break;
  1268. case OBJECT: {
  1269. _get_obj().unref();
  1270. } break;
  1271. case RID: {
  1272. // Not much need probably.
  1273. // HACK: Can't seem to use destructor + scoping operator, so hack.
  1274. typedef ::RID RID_Class;
  1275. reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class();
  1276. } break;
  1277. case CALLABLE: {
  1278. reinterpret_cast<Callable *>(_data._mem)->~Callable();
  1279. } break;
  1280. case SIGNAL: {
  1281. reinterpret_cast<Signal *>(_data._mem)->~Signal();
  1282. } break;
  1283. case DICTIONARY: {
  1284. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  1285. } break;
  1286. case ARRAY: {
  1287. reinterpret_cast<Array *>(_data._mem)->~Array();
  1288. } break;
  1289. // Arrays.
  1290. case PACKED_BYTE_ARRAY: {
  1291. PackedArrayRefBase::destroy(_data.packed_array);
  1292. } break;
  1293. case PACKED_INT32_ARRAY: {
  1294. PackedArrayRefBase::destroy(_data.packed_array);
  1295. } break;
  1296. case PACKED_INT64_ARRAY: {
  1297. PackedArrayRefBase::destroy(_data.packed_array);
  1298. } break;
  1299. case PACKED_FLOAT32_ARRAY: {
  1300. PackedArrayRefBase::destroy(_data.packed_array);
  1301. } break;
  1302. case PACKED_FLOAT64_ARRAY: {
  1303. PackedArrayRefBase::destroy(_data.packed_array);
  1304. } break;
  1305. case PACKED_STRING_ARRAY: {
  1306. PackedArrayRefBase::destroy(_data.packed_array);
  1307. } break;
  1308. case PACKED_VECTOR2_ARRAY: {
  1309. PackedArrayRefBase::destroy(_data.packed_array);
  1310. } break;
  1311. case PACKED_VECTOR3_ARRAY: {
  1312. PackedArrayRefBase::destroy(_data.packed_array);
  1313. } break;
  1314. case PACKED_COLOR_ARRAY: {
  1315. PackedArrayRefBase::destroy(_data.packed_array);
  1316. } break;
  1317. case PACKED_VECTOR4_ARRAY: {
  1318. PackedArrayRefBase::destroy(_data.packed_array);
  1319. } break;
  1320. default: {
  1321. // Not needed, there is no point. The following do not allocate memory:
  1322. // VECTOR2, VECTOR3, VECTOR4, RECT2, PLANE, QUATERNION, COLOR.
  1323. }
  1324. }
  1325. }
  1326. Variant::operator int64_t() const {
  1327. return _to_int<int64_t>();
  1328. }
  1329. Variant::operator int32_t() const {
  1330. return _to_int<int32_t>();
  1331. }
  1332. Variant::operator int16_t() const {
  1333. return _to_int<int16_t>();
  1334. }
  1335. Variant::operator int8_t() const {
  1336. return _to_int<int8_t>();
  1337. }
  1338. Variant::operator uint64_t() const {
  1339. return _to_int<uint64_t>();
  1340. }
  1341. Variant::operator uint32_t() const {
  1342. return _to_int<uint32_t>();
  1343. }
  1344. Variant::operator uint16_t() const {
  1345. return _to_int<uint16_t>();
  1346. }
  1347. Variant::operator uint8_t() const {
  1348. return _to_int<uint8_t>();
  1349. }
  1350. Variant::operator ObjectID() const {
  1351. if (type == INT) {
  1352. return ObjectID(_data._int);
  1353. } else if (type == OBJECT) {
  1354. return _get_obj().id;
  1355. } else {
  1356. return ObjectID();
  1357. }
  1358. }
  1359. Variant::operator char32_t() const {
  1360. return operator uint32_t();
  1361. }
  1362. Variant::operator float() const {
  1363. return _to_float<float>();
  1364. }
  1365. Variant::operator double() const {
  1366. return _to_float<double>();
  1367. }
  1368. Variant::operator StringName() const {
  1369. if (type == STRING_NAME) {
  1370. return *reinterpret_cast<const StringName *>(_data._mem);
  1371. } else if (type == STRING) {
  1372. return *reinterpret_cast<const String *>(_data._mem);
  1373. }
  1374. return StringName();
  1375. }
  1376. struct _VariantStrPair {
  1377. String key;
  1378. String value;
  1379. bool operator<(const _VariantStrPair &p) const {
  1380. return key < p.key;
  1381. }
  1382. };
  1383. Variant::operator String() const {
  1384. return stringify(0);
  1385. }
  1386. String stringify_variant_clean(const Variant &p_variant, int recursion_count) {
  1387. String s = p_variant.stringify(recursion_count);
  1388. // Wrap strings in quotes to avoid ambiguity.
  1389. switch (p_variant.get_type()) {
  1390. case Variant::STRING: {
  1391. s = s.c_escape().quote();
  1392. } break;
  1393. case Variant::STRING_NAME: {
  1394. s = "&" + s.c_escape().quote();
  1395. } break;
  1396. case Variant::NODE_PATH: {
  1397. s = "^" + s.c_escape().quote();
  1398. } break;
  1399. default: {
  1400. } break;
  1401. }
  1402. return s;
  1403. }
  1404. template <typename T>
  1405. String stringify_vector(const T &vec, int recursion_count) {
  1406. String str("[");
  1407. for (int i = 0; i < vec.size(); i++) {
  1408. if (i > 0) {
  1409. str += ", ";
  1410. }
  1411. str += stringify_variant_clean(vec[i], recursion_count);
  1412. }
  1413. str += "]";
  1414. return str;
  1415. }
  1416. String Variant::stringify(int recursion_count) const {
  1417. switch (type) {
  1418. case NIL:
  1419. return "<null>";
  1420. case BOOL:
  1421. return _data._bool ? "true" : "false";
  1422. case INT:
  1423. return itos(_data._int);
  1424. case FLOAT:
  1425. return String::num_real(_data._float, true);
  1426. case STRING:
  1427. return *reinterpret_cast<const String *>(_data._mem);
  1428. case VECTOR2:
  1429. return String(operator Vector2());
  1430. case VECTOR2I:
  1431. return String(operator Vector2i());
  1432. case RECT2:
  1433. return String(operator Rect2());
  1434. case RECT2I:
  1435. return String(operator Rect2i());
  1436. case TRANSFORM2D:
  1437. return String(operator Transform2D());
  1438. case VECTOR3:
  1439. return String(operator Vector3());
  1440. case VECTOR3I:
  1441. return String(operator Vector3i());
  1442. case VECTOR4:
  1443. return String(operator Vector4());
  1444. case VECTOR4I:
  1445. return String(operator Vector4i());
  1446. case PLANE:
  1447. return String(operator Plane());
  1448. case AABB:
  1449. return String(operator ::AABB());
  1450. case QUATERNION:
  1451. return String(operator Quaternion());
  1452. case BASIS:
  1453. return String(operator Basis());
  1454. case TRANSFORM3D:
  1455. return String(operator Transform3D());
  1456. case PROJECTION:
  1457. return String(operator Projection());
  1458. case STRING_NAME:
  1459. return operator StringName();
  1460. case NODE_PATH:
  1461. return String(operator NodePath());
  1462. case COLOR:
  1463. return String(operator Color());
  1464. case DICTIONARY: {
  1465. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "{ ... }", "Maximum dictionary recursion reached!");
  1466. recursion_count++;
  1467. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1468. // Add leading and trailing space to Dictionary printing. This distinguishes it
  1469. // from array printing on fonts that have similar-looking {} and [] characters.
  1470. String str("{ ");
  1471. Vector<_VariantStrPair> pairs;
  1472. for (const KeyValue<Variant, Variant> &kv : d) {
  1473. _VariantStrPair sp;
  1474. sp.key = stringify_variant_clean(kv.key, recursion_count);
  1475. sp.value = stringify_variant_clean(kv.value, recursion_count);
  1476. pairs.push_back(sp);
  1477. }
  1478. for (int i = 0; i < pairs.size(); i++) {
  1479. if (i > 0) {
  1480. str += ", ";
  1481. }
  1482. str += pairs[i].key + ": " + pairs[i].value;
  1483. }
  1484. str += " }";
  1485. return str;
  1486. }
  1487. // Packed arrays cannot contain recursive structures, the recursion_count increment is not needed.
  1488. case PACKED_VECTOR2_ARRAY: {
  1489. return stringify_vector(operator PackedVector2Array(), recursion_count);
  1490. }
  1491. case PACKED_VECTOR3_ARRAY: {
  1492. return stringify_vector(operator PackedVector3Array(), recursion_count);
  1493. }
  1494. case PACKED_COLOR_ARRAY: {
  1495. return stringify_vector(operator PackedColorArray(), recursion_count);
  1496. }
  1497. case PACKED_VECTOR4_ARRAY: {
  1498. return stringify_vector(operator PackedVector4Array(), recursion_count);
  1499. }
  1500. case PACKED_STRING_ARRAY: {
  1501. return stringify_vector(operator PackedStringArray(), recursion_count);
  1502. }
  1503. case PACKED_BYTE_ARRAY: {
  1504. return stringify_vector(operator PackedByteArray(), recursion_count);
  1505. }
  1506. case PACKED_INT32_ARRAY: {
  1507. return stringify_vector(operator PackedInt32Array(), recursion_count);
  1508. }
  1509. case PACKED_INT64_ARRAY: {
  1510. return stringify_vector(operator PackedInt64Array(), recursion_count);
  1511. }
  1512. case PACKED_FLOAT32_ARRAY: {
  1513. return stringify_vector(operator PackedFloat32Array(), recursion_count);
  1514. }
  1515. case PACKED_FLOAT64_ARRAY: {
  1516. return stringify_vector(operator PackedFloat64Array(), recursion_count);
  1517. }
  1518. case ARRAY: {
  1519. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "[...]", "Maximum array recursion reached!");
  1520. recursion_count++;
  1521. return stringify_vector(operator Array(), recursion_count);
  1522. }
  1523. case OBJECT: {
  1524. if (_get_obj().obj) {
  1525. if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1526. return "<Freed Object>";
  1527. }
  1528. return _get_obj().obj->to_string();
  1529. } else {
  1530. return "<Object#null>";
  1531. }
  1532. }
  1533. case CALLABLE: {
  1534. const Callable &c = *reinterpret_cast<const Callable *>(_data._mem);
  1535. return String(c);
  1536. }
  1537. case SIGNAL: {
  1538. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  1539. return String(s);
  1540. }
  1541. case RID: {
  1542. const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem);
  1543. return "RID(" + itos(s.get_id()) + ")";
  1544. }
  1545. default: {
  1546. return "<" + get_type_name(type) + ">";
  1547. }
  1548. }
  1549. }
  1550. String Variant::to_json_string() const {
  1551. return JSON::stringify(*this);
  1552. }
  1553. Variant::operator Vector2() const {
  1554. if (type == VECTOR2) {
  1555. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1556. } else if (type == VECTOR2I) {
  1557. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1558. } else if (type == VECTOR3) {
  1559. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1560. } else if (type == VECTOR3I) {
  1561. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1562. } else if (type == VECTOR4) {
  1563. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1564. } else if (type == VECTOR4I) {
  1565. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1566. } else {
  1567. return Vector2();
  1568. }
  1569. }
  1570. Variant::operator Vector2i() const {
  1571. if (type == VECTOR2I) {
  1572. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1573. } else if (type == VECTOR2) {
  1574. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1575. } else if (type == VECTOR3) {
  1576. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1577. } else if (type == VECTOR3I) {
  1578. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1579. } else if (type == VECTOR4) {
  1580. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1581. } else if (type == VECTOR4I) {
  1582. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1583. } else {
  1584. return Vector2i();
  1585. }
  1586. }
  1587. Variant::operator Rect2() const {
  1588. if (type == RECT2) {
  1589. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1590. } else if (type == RECT2I) {
  1591. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1592. } else {
  1593. return Rect2();
  1594. }
  1595. }
  1596. Variant::operator Rect2i() const {
  1597. if (type == RECT2I) {
  1598. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1599. } else if (type == RECT2) {
  1600. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1601. } else {
  1602. return Rect2i();
  1603. }
  1604. }
  1605. Variant::operator Vector3() const {
  1606. if (type == VECTOR3) {
  1607. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1608. } else if (type == VECTOR3I) {
  1609. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1610. } else if (type == VECTOR2) {
  1611. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1612. } else if (type == VECTOR2I) {
  1613. return Vector3(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1614. } else if (type == VECTOR4) {
  1615. return Vector3(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1616. } else if (type == VECTOR4I) {
  1617. return Vector3(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1618. } else {
  1619. return Vector3();
  1620. }
  1621. }
  1622. Variant::operator Vector3i() const {
  1623. if (type == VECTOR3I) {
  1624. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1625. } else if (type == VECTOR3) {
  1626. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1627. } else if (type == VECTOR2) {
  1628. return Vector3i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1629. } else if (type == VECTOR2I) {
  1630. return Vector3i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1631. } else if (type == VECTOR4) {
  1632. return Vector3i(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1633. } else if (type == VECTOR4I) {
  1634. return Vector3i(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1635. } else {
  1636. return Vector3i();
  1637. }
  1638. }
  1639. Variant::operator Vector4() const {
  1640. if (type == VECTOR4) {
  1641. return *reinterpret_cast<const Vector4 *>(_data._mem);
  1642. } else if (type == VECTOR4I) {
  1643. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1644. } else if (type == VECTOR2) {
  1645. return Vector4(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1646. } else if (type == VECTOR2I) {
  1647. return Vector4(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1648. } else if (type == VECTOR3) {
  1649. return Vector4(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y, reinterpret_cast<const Vector3 *>(_data._mem)->z, 0.0);
  1650. } else if (type == VECTOR3I) {
  1651. return Vector4(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y, reinterpret_cast<const Vector3i *>(_data._mem)->z, 0.0);
  1652. } else {
  1653. return Vector4();
  1654. }
  1655. }
  1656. Variant::operator Vector4i() const {
  1657. if (type == VECTOR4I) {
  1658. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1659. } else if (type == VECTOR4) {
  1660. const Vector4 &v4 = *reinterpret_cast<const Vector4 *>(_data._mem);
  1661. return Vector4i(v4.x, v4.y, v4.z, v4.w);
  1662. } else if (type == VECTOR2) {
  1663. return Vector4i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1664. } else if (type == VECTOR2I) {
  1665. return Vector4i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1666. } else if (type == VECTOR3) {
  1667. return Vector4i(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y, reinterpret_cast<const Vector3 *>(_data._mem)->z, 0.0);
  1668. } else if (type == VECTOR3I) {
  1669. return Vector4i(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y, reinterpret_cast<const Vector3i *>(_data._mem)->z, 0.0);
  1670. } else {
  1671. return Vector4i();
  1672. }
  1673. }
  1674. Variant::operator Plane() const {
  1675. if (type == PLANE) {
  1676. return *reinterpret_cast<const Plane *>(_data._mem);
  1677. } else {
  1678. return Plane();
  1679. }
  1680. }
  1681. Variant::operator ::AABB() const {
  1682. if (type == AABB) {
  1683. return *_data._aabb;
  1684. } else {
  1685. return ::AABB();
  1686. }
  1687. }
  1688. Variant::operator Basis() const {
  1689. if (type == BASIS) {
  1690. return *_data._basis;
  1691. } else if (type == QUATERNION) {
  1692. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1693. } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert?
  1694. return _data._transform3d->basis;
  1695. } else {
  1696. return Basis();
  1697. }
  1698. }
  1699. Variant::operator Quaternion() const {
  1700. if (type == QUATERNION) {
  1701. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1702. } else if (type == BASIS) {
  1703. return *_data._basis;
  1704. } else if (type == TRANSFORM3D) {
  1705. return _data._transform3d->basis;
  1706. } else {
  1707. return Quaternion();
  1708. }
  1709. }
  1710. Variant::operator Transform3D() const {
  1711. if (type == TRANSFORM3D) {
  1712. return *_data._transform3d;
  1713. } else if (type == BASIS) {
  1714. return Transform3D(*_data._basis, Vector3());
  1715. } else if (type == QUATERNION) {
  1716. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1717. } else if (type == TRANSFORM2D) {
  1718. const Transform2D &t = *_data._transform2d;
  1719. Transform3D m;
  1720. m.basis.rows[0][0] = t.columns[0][0];
  1721. m.basis.rows[1][0] = t.columns[0][1];
  1722. m.basis.rows[0][1] = t.columns[1][0];
  1723. m.basis.rows[1][1] = t.columns[1][1];
  1724. m.origin[0] = t.columns[2][0];
  1725. m.origin[1] = t.columns[2][1];
  1726. return m;
  1727. } else if (type == PROJECTION) {
  1728. return *_data._projection;
  1729. } else {
  1730. return Transform3D();
  1731. }
  1732. }
  1733. Variant::operator Projection() const {
  1734. if (type == TRANSFORM3D) {
  1735. return *_data._transform3d;
  1736. } else if (type == BASIS) {
  1737. return Transform3D(*_data._basis, Vector3());
  1738. } else if (type == QUATERNION) {
  1739. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1740. } else if (type == TRANSFORM2D) {
  1741. const Transform2D &t = *_data._transform2d;
  1742. Transform3D m;
  1743. m.basis.rows[0][0] = t.columns[0][0];
  1744. m.basis.rows[1][0] = t.columns[0][1];
  1745. m.basis.rows[0][1] = t.columns[1][0];
  1746. m.basis.rows[1][1] = t.columns[1][1];
  1747. m.origin[0] = t.columns[2][0];
  1748. m.origin[1] = t.columns[2][1];
  1749. return m;
  1750. } else if (type == PROJECTION) {
  1751. return *_data._projection;
  1752. } else {
  1753. return Projection();
  1754. }
  1755. }
  1756. Variant::operator Transform2D() const {
  1757. if (type == TRANSFORM2D) {
  1758. return *_data._transform2d;
  1759. } else if (type == TRANSFORM3D) {
  1760. const Transform3D &t = *_data._transform3d;
  1761. Transform2D m;
  1762. m.columns[0][0] = t.basis.rows[0][0];
  1763. m.columns[0][1] = t.basis.rows[1][0];
  1764. m.columns[1][0] = t.basis.rows[0][1];
  1765. m.columns[1][1] = t.basis.rows[1][1];
  1766. m.columns[2][0] = t.origin[0];
  1767. m.columns[2][1] = t.origin[1];
  1768. return m;
  1769. } else {
  1770. return Transform2D();
  1771. }
  1772. }
  1773. Variant::operator Color() const {
  1774. if (type == COLOR) {
  1775. return *reinterpret_cast<const Color *>(_data._mem);
  1776. } else if (type == STRING) {
  1777. return Color(operator String());
  1778. } else if (type == INT) {
  1779. return Color::hex(operator int());
  1780. } else {
  1781. return Color();
  1782. }
  1783. }
  1784. Variant::operator NodePath() const {
  1785. if (type == NODE_PATH) {
  1786. return *reinterpret_cast<const NodePath *>(_data._mem);
  1787. } else if (type == STRING) {
  1788. return NodePath(operator String());
  1789. } else {
  1790. return NodePath();
  1791. }
  1792. }
  1793. Variant::operator ::RID() const {
  1794. if (type == RID) {
  1795. return *reinterpret_cast<const ::RID *>(_data._mem);
  1796. } else if (type == OBJECT && _get_obj().obj == nullptr) {
  1797. return ::RID();
  1798. } else if (type == OBJECT && _get_obj().obj) {
  1799. #ifdef DEBUG_ENABLED
  1800. if (EngineDebugger::is_active()) {
  1801. ERR_FAIL_NULL_V_MSG(ObjectDB::get_instance(_get_obj().id), ::RID(), "Invalid pointer (object was freed).");
  1802. }
  1803. #endif
  1804. Callable::CallError ce;
  1805. const Variant ret = _get_obj().obj->callp(CoreStringName(get_rid), nullptr, 0, ce);
  1806. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) {
  1807. return ret;
  1808. }
  1809. return ::RID();
  1810. } else {
  1811. return ::RID();
  1812. }
  1813. }
  1814. Variant::operator Object *() const {
  1815. if (type == OBJECT) {
  1816. return _get_obj().obj;
  1817. } else {
  1818. return nullptr;
  1819. }
  1820. }
  1821. Object *Variant::get_validated_object_with_check(bool &r_previously_freed) const {
  1822. if (type == OBJECT) {
  1823. Object *instance = ObjectDB::get_instance(_get_obj().id);
  1824. r_previously_freed = !instance && _get_obj().id != ObjectID();
  1825. return instance;
  1826. } else {
  1827. r_previously_freed = false;
  1828. return nullptr;
  1829. }
  1830. }
  1831. Object *Variant::get_validated_object() const {
  1832. if (type == OBJECT) {
  1833. return ObjectDB::get_instance(_get_obj().id);
  1834. } else {
  1835. return nullptr;
  1836. }
  1837. }
  1838. Variant::operator Dictionary() const {
  1839. if (type == DICTIONARY) {
  1840. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1841. } else {
  1842. return Dictionary();
  1843. }
  1844. }
  1845. Variant::operator Callable() const {
  1846. if (type == CALLABLE) {
  1847. return *reinterpret_cast<const Callable *>(_data._mem);
  1848. } else {
  1849. return Callable();
  1850. }
  1851. }
  1852. Variant::operator Signal() const {
  1853. if (type == SIGNAL) {
  1854. return *reinterpret_cast<const Signal *>(_data._mem);
  1855. } else {
  1856. return Signal();
  1857. }
  1858. }
  1859. template <typename DA, typename SA>
  1860. inline DA _convert_array(const SA &p_array) {
  1861. DA da;
  1862. da.resize(p_array.size());
  1863. for (int i = 0; i < p_array.size(); i++) {
  1864. da.set(i, Variant(p_array.get(i)));
  1865. }
  1866. return da;
  1867. }
  1868. template <typename DA>
  1869. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1870. switch (p_variant.get_type()) {
  1871. case Variant::ARRAY: {
  1872. return _convert_array<DA, Array>(p_variant.operator Array());
  1873. }
  1874. case Variant::PACKED_BYTE_ARRAY: {
  1875. return _convert_array<DA, PackedByteArray>(p_variant.operator PackedByteArray());
  1876. }
  1877. case Variant::PACKED_INT32_ARRAY: {
  1878. return _convert_array<DA, PackedInt32Array>(p_variant.operator PackedInt32Array());
  1879. }
  1880. case Variant::PACKED_INT64_ARRAY: {
  1881. return _convert_array<DA, PackedInt64Array>(p_variant.operator PackedInt64Array());
  1882. }
  1883. case Variant::PACKED_FLOAT32_ARRAY: {
  1884. return _convert_array<DA, PackedFloat32Array>(p_variant.operator PackedFloat32Array());
  1885. }
  1886. case Variant::PACKED_FLOAT64_ARRAY: {
  1887. return _convert_array<DA, PackedFloat64Array>(p_variant.operator PackedFloat64Array());
  1888. }
  1889. case Variant::PACKED_STRING_ARRAY: {
  1890. return _convert_array<DA, PackedStringArray>(p_variant.operator PackedStringArray());
  1891. }
  1892. case Variant::PACKED_VECTOR2_ARRAY: {
  1893. return _convert_array<DA, PackedVector2Array>(p_variant.operator PackedVector2Array());
  1894. }
  1895. case Variant::PACKED_VECTOR3_ARRAY: {
  1896. return _convert_array<DA, PackedVector3Array>(p_variant.operator PackedVector3Array());
  1897. }
  1898. case Variant::PACKED_COLOR_ARRAY: {
  1899. return _convert_array<DA, PackedColorArray>(p_variant.operator PackedColorArray());
  1900. }
  1901. case Variant::PACKED_VECTOR4_ARRAY: {
  1902. return _convert_array<DA, PackedVector4Array>(p_variant.operator PackedVector4Array());
  1903. }
  1904. default: {
  1905. return DA();
  1906. }
  1907. }
  1908. }
  1909. Variant::operator Array() const {
  1910. if (type == ARRAY) {
  1911. return *reinterpret_cast<const Array *>(_data._mem);
  1912. } else {
  1913. return _convert_array_from_variant<Array>(*this);
  1914. }
  1915. }
  1916. Variant::operator PackedByteArray() const {
  1917. if (type == PACKED_BYTE_ARRAY) {
  1918. return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array;
  1919. } else {
  1920. return _convert_array_from_variant<PackedByteArray>(*this);
  1921. }
  1922. }
  1923. Variant::operator PackedInt32Array() const {
  1924. if (type == PACKED_INT32_ARRAY) {
  1925. return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array;
  1926. } else {
  1927. return _convert_array_from_variant<PackedInt32Array>(*this);
  1928. }
  1929. }
  1930. Variant::operator PackedInt64Array() const {
  1931. if (type == PACKED_INT64_ARRAY) {
  1932. return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array;
  1933. } else {
  1934. return _convert_array_from_variant<PackedInt64Array>(*this);
  1935. }
  1936. }
  1937. Variant::operator PackedFloat32Array() const {
  1938. if (type == PACKED_FLOAT32_ARRAY) {
  1939. return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array;
  1940. } else {
  1941. return _convert_array_from_variant<PackedFloat32Array>(*this);
  1942. }
  1943. }
  1944. Variant::operator PackedFloat64Array() const {
  1945. if (type == PACKED_FLOAT64_ARRAY) {
  1946. return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array;
  1947. } else {
  1948. return _convert_array_from_variant<PackedFloat64Array>(*this);
  1949. }
  1950. }
  1951. Variant::operator PackedStringArray() const {
  1952. if (type == PACKED_STRING_ARRAY) {
  1953. return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array;
  1954. } else {
  1955. return _convert_array_from_variant<PackedStringArray>(*this);
  1956. }
  1957. }
  1958. Variant::operator PackedVector2Array() const {
  1959. if (type == PACKED_VECTOR2_ARRAY) {
  1960. return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array;
  1961. } else {
  1962. return _convert_array_from_variant<PackedVector2Array>(*this);
  1963. }
  1964. }
  1965. Variant::operator PackedVector3Array() const {
  1966. if (type == PACKED_VECTOR3_ARRAY) {
  1967. return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array;
  1968. } else {
  1969. return _convert_array_from_variant<PackedVector3Array>(*this);
  1970. }
  1971. }
  1972. Variant::operator PackedColorArray() const {
  1973. if (type == PACKED_COLOR_ARRAY) {
  1974. return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array;
  1975. } else {
  1976. return _convert_array_from_variant<PackedColorArray>(*this);
  1977. }
  1978. }
  1979. Variant::operator PackedVector4Array() const {
  1980. if (type == PACKED_VECTOR4_ARRAY) {
  1981. return static_cast<PackedArrayRef<Vector4> *>(_data.packed_array)->array;
  1982. } else {
  1983. return _convert_array_from_variant<PackedVector4Array>(*this);
  1984. }
  1985. }
  1986. /* helpers */
  1987. Variant::operator Vector<::RID>() const {
  1988. Array va = operator Array();
  1989. Vector<::RID> rids;
  1990. rids.resize(va.size());
  1991. for (int i = 0; i < rids.size(); i++) {
  1992. rids.write[i] = va[i];
  1993. }
  1994. return rids;
  1995. }
  1996. Variant::operator Vector<Plane>() const {
  1997. Array va = operator Array();
  1998. Vector<Plane> planes;
  1999. int va_size = va.size();
  2000. if (va_size == 0) {
  2001. return planes;
  2002. }
  2003. planes.resize(va_size);
  2004. Plane *w = planes.ptrw();
  2005. for (int i = 0; i < va_size; i++) {
  2006. w[i] = va[i];
  2007. }
  2008. return planes;
  2009. }
  2010. Variant::operator Vector<Face3>() const {
  2011. PackedVector3Array va = operator PackedVector3Array();
  2012. Vector<Face3> faces;
  2013. int va_size = va.size();
  2014. if (va_size == 0) {
  2015. return faces;
  2016. }
  2017. faces.resize(va_size / 3);
  2018. Face3 *w = faces.ptrw();
  2019. const Vector3 *r = va.ptr();
  2020. for (int i = 0; i < va_size; i++) {
  2021. w[i / 3].vertex[i % 3] = r[i];
  2022. }
  2023. return faces;
  2024. }
  2025. Variant::operator Vector<Variant>() const {
  2026. Array va = operator Array();
  2027. Vector<Variant> variants;
  2028. int va_size = va.size();
  2029. if (va_size == 0) {
  2030. return variants;
  2031. }
  2032. variants.resize(va_size);
  2033. Variant *w = variants.ptrw();
  2034. for (int i = 0; i < va_size; i++) {
  2035. w[i] = va[i];
  2036. }
  2037. return variants;
  2038. }
  2039. Variant::operator Vector<StringName>() const {
  2040. PackedStringArray from = operator PackedStringArray();
  2041. Vector<StringName> to;
  2042. int len = from.size();
  2043. to.resize(len);
  2044. for (int i = 0; i < len; i++) {
  2045. to.write[i] = from[i];
  2046. }
  2047. return to;
  2048. }
  2049. Variant::operator IPAddress() const {
  2050. if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
  2051. Vector<int> addr = operator Vector<int>();
  2052. if (addr.size() == 4) {
  2053. return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  2054. }
  2055. }
  2056. return IPAddress(operator String());
  2057. }
  2058. Variant::Variant(bool p_bool) :
  2059. type(BOOL) {
  2060. _data._bool = p_bool;
  2061. }
  2062. Variant::Variant(int64_t p_int64) :
  2063. type(INT) {
  2064. _data._int = p_int64;
  2065. }
  2066. Variant::Variant(int32_t p_int32) :
  2067. type(INT) {
  2068. _data._int = p_int32;
  2069. }
  2070. Variant::Variant(int16_t p_int16) :
  2071. type(INT) {
  2072. _data._int = p_int16;
  2073. }
  2074. Variant::Variant(int8_t p_int8) :
  2075. type(INT) {
  2076. _data._int = p_int8;
  2077. }
  2078. Variant::Variant(uint64_t p_uint64) :
  2079. type(INT) {
  2080. _data._int = int64_t(p_uint64);
  2081. }
  2082. Variant::Variant(uint32_t p_uint32) :
  2083. type(INT) {
  2084. _data._int = int64_t(p_uint32);
  2085. }
  2086. Variant::Variant(uint16_t p_uint16) :
  2087. type(INT) {
  2088. _data._int = int64_t(p_uint16);
  2089. }
  2090. Variant::Variant(uint8_t p_uint8) :
  2091. type(INT) {
  2092. _data._int = int64_t(p_uint8);
  2093. }
  2094. Variant::Variant(float p_float) :
  2095. type(FLOAT) {
  2096. _data._float = p_float;
  2097. }
  2098. Variant::Variant(double p_double) :
  2099. type(FLOAT) {
  2100. _data._float = p_double;
  2101. }
  2102. Variant::Variant(const ObjectID &p_id) :
  2103. type(INT) {
  2104. _data._int = int64_t(p_id);
  2105. }
  2106. Variant::Variant(const StringName &p_string) :
  2107. type(STRING_NAME) {
  2108. memnew_placement(_data._mem, StringName(p_string));
  2109. static_assert(sizeof(StringName) <= sizeof(_data._mem));
  2110. }
  2111. Variant::Variant(const String &p_string) :
  2112. type(STRING) {
  2113. memnew_placement(_data._mem, String(p_string));
  2114. static_assert(sizeof(String) <= sizeof(_data._mem));
  2115. }
  2116. Variant::Variant(const char *const p_cstring) :
  2117. type(STRING) {
  2118. memnew_placement(_data._mem, String((const char *)p_cstring));
  2119. static_assert(sizeof(String) <= sizeof(_data._mem));
  2120. }
  2121. Variant::Variant(const char32_t *p_wstring) :
  2122. type(STRING) {
  2123. memnew_placement(_data._mem, String(p_wstring));
  2124. static_assert(sizeof(String) <= sizeof(_data._mem));
  2125. }
  2126. Variant::Variant(const Vector3 &p_vector3) :
  2127. type(VECTOR3) {
  2128. memnew_placement(_data._mem, Vector3(p_vector3));
  2129. static_assert(sizeof(Vector3) <= sizeof(_data._mem));
  2130. }
  2131. Variant::Variant(const Vector3i &p_vector3i) :
  2132. type(VECTOR3I) {
  2133. memnew_placement(_data._mem, Vector3i(p_vector3i));
  2134. static_assert(sizeof(Vector3i) <= sizeof(_data._mem));
  2135. }
  2136. Variant::Variant(const Vector4 &p_vector4) :
  2137. type(VECTOR4) {
  2138. memnew_placement(_data._mem, Vector4(p_vector4));
  2139. static_assert(sizeof(Vector4) <= sizeof(_data._mem));
  2140. }
  2141. Variant::Variant(const Vector4i &p_vector4i) :
  2142. type(VECTOR4I) {
  2143. memnew_placement(_data._mem, Vector4i(p_vector4i));
  2144. static_assert(sizeof(Vector4i) <= sizeof(_data._mem));
  2145. }
  2146. Variant::Variant(const Vector2 &p_vector2) :
  2147. type(VECTOR2) {
  2148. memnew_placement(_data._mem, Vector2(p_vector2));
  2149. static_assert(sizeof(Vector2) <= sizeof(_data._mem));
  2150. }
  2151. Variant::Variant(const Vector2i &p_vector2i) :
  2152. type(VECTOR2I) {
  2153. memnew_placement(_data._mem, Vector2i(p_vector2i));
  2154. static_assert(sizeof(Vector2i) <= sizeof(_data._mem));
  2155. }
  2156. Variant::Variant(const Rect2 &p_rect2) :
  2157. type(RECT2) {
  2158. memnew_placement(_data._mem, Rect2(p_rect2));
  2159. static_assert(sizeof(Rect2) <= sizeof(_data._mem));
  2160. }
  2161. Variant::Variant(const Rect2i &p_rect2i) :
  2162. type(RECT2I) {
  2163. memnew_placement(_data._mem, Rect2i(p_rect2i));
  2164. static_assert(sizeof(Rect2i) <= sizeof(_data._mem));
  2165. }
  2166. Variant::Variant(const Plane &p_plane) :
  2167. type(PLANE) {
  2168. memnew_placement(_data._mem, Plane(p_plane));
  2169. static_assert(sizeof(Plane) <= sizeof(_data._mem));
  2170. }
  2171. Variant::Variant(const ::AABB &p_aabb) :
  2172. type(AABB) {
  2173. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  2174. memnew_placement(_data._aabb, ::AABB(p_aabb));
  2175. }
  2176. Variant::Variant(const Basis &p_matrix) :
  2177. type(BASIS) {
  2178. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  2179. memnew_placement(_data._basis, Basis(p_matrix));
  2180. }
  2181. Variant::Variant(const Quaternion &p_quaternion) :
  2182. type(QUATERNION) {
  2183. memnew_placement(_data._mem, Quaternion(p_quaternion));
  2184. static_assert(sizeof(Quaternion) <= sizeof(_data._mem));
  2185. }
  2186. Variant::Variant(const Transform3D &p_transform) :
  2187. type(TRANSFORM3D) {
  2188. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  2189. memnew_placement(_data._transform3d, Transform3D(p_transform));
  2190. }
  2191. Variant::Variant(const Projection &pp_projection) :
  2192. type(PROJECTION) {
  2193. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  2194. memnew_placement(_data._projection, Projection(pp_projection));
  2195. }
  2196. Variant::Variant(const Transform2D &p_transform) :
  2197. type(TRANSFORM2D) {
  2198. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  2199. memnew_placement(_data._transform2d, Transform2D(p_transform));
  2200. }
  2201. Variant::Variant(const Color &p_color) :
  2202. type(COLOR) {
  2203. memnew_placement(_data._mem, Color(p_color));
  2204. static_assert(sizeof(Color) <= sizeof(_data._mem));
  2205. }
  2206. Variant::Variant(const NodePath &p_node_path) :
  2207. type(NODE_PATH) {
  2208. memnew_placement(_data._mem, NodePath(p_node_path));
  2209. static_assert(sizeof(NodePath) <= sizeof(_data._mem));
  2210. }
  2211. Variant::Variant(const ::RID &p_rid) :
  2212. type(RID) {
  2213. memnew_placement(_data._mem, ::RID(p_rid));
  2214. static_assert(sizeof(::RID) <= sizeof(_data._mem));
  2215. }
  2216. Variant::Variant(const Object *p_object) :
  2217. type(OBJECT) {
  2218. _get_obj() = ObjData();
  2219. _get_obj().ref_pointer(const_cast<Object *>(p_object));
  2220. }
  2221. Variant::Variant(const Callable &p_callable) :
  2222. type(CALLABLE) {
  2223. memnew_placement(_data._mem, Callable(p_callable));
  2224. static_assert(sizeof(Callable) <= sizeof(_data._mem));
  2225. }
  2226. Variant::Variant(const Signal &p_callable) :
  2227. type(SIGNAL) {
  2228. memnew_placement(_data._mem, Signal(p_callable));
  2229. static_assert(sizeof(Signal) <= sizeof(_data._mem));
  2230. }
  2231. Variant::Variant(const Dictionary &p_dictionary) :
  2232. type(DICTIONARY) {
  2233. memnew_placement(_data._mem, Dictionary(p_dictionary));
  2234. static_assert(sizeof(Dictionary) <= sizeof(_data._mem));
  2235. }
  2236. Variant::Variant(std::initializer_list<Variant> p_init) :
  2237. type(ARRAY) {
  2238. memnew_placement(_data._mem, Array(p_init));
  2239. }
  2240. Variant::Variant(const Array &p_array) :
  2241. type(ARRAY) {
  2242. memnew_placement(_data._mem, Array(p_array));
  2243. static_assert(sizeof(Array) <= sizeof(_data._mem));
  2244. }
  2245. Variant::Variant(const PackedByteArray &p_byte_array) :
  2246. type(PACKED_BYTE_ARRAY) {
  2247. _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
  2248. }
  2249. Variant::Variant(const PackedInt32Array &p_int32_array) :
  2250. type(PACKED_INT32_ARRAY) {
  2251. _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
  2252. }
  2253. Variant::Variant(const PackedInt64Array &p_int64_array) :
  2254. type(PACKED_INT64_ARRAY) {
  2255. _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
  2256. }
  2257. Variant::Variant(const PackedFloat32Array &p_float32_array) :
  2258. type(PACKED_FLOAT32_ARRAY) {
  2259. _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
  2260. }
  2261. Variant::Variant(const PackedFloat64Array &p_float64_array) :
  2262. type(PACKED_FLOAT64_ARRAY) {
  2263. _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
  2264. }
  2265. Variant::Variant(const PackedStringArray &p_string_array) :
  2266. type(PACKED_STRING_ARRAY) {
  2267. _data.packed_array = PackedArrayRef<String>::create(p_string_array);
  2268. }
  2269. Variant::Variant(const PackedVector2Array &p_vector2_array) :
  2270. type(PACKED_VECTOR2_ARRAY) {
  2271. _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
  2272. }
  2273. Variant::Variant(const PackedVector3Array &p_vector3_array) :
  2274. type(PACKED_VECTOR3_ARRAY) {
  2275. _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
  2276. }
  2277. Variant::Variant(const PackedColorArray &p_color_array) :
  2278. type(PACKED_COLOR_ARRAY) {
  2279. _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
  2280. }
  2281. Variant::Variant(const PackedVector4Array &p_vector4_array) :
  2282. type(PACKED_VECTOR4_ARRAY) {
  2283. _data.packed_array = PackedArrayRef<Vector4>::create(p_vector4_array);
  2284. }
  2285. /* helpers */
  2286. Variant::Variant(const Vector<::RID> &p_array) :
  2287. type(ARRAY) {
  2288. Array *rid_array = memnew_placement(_data._mem, Array);
  2289. rid_array->resize(p_array.size());
  2290. for (int i = 0; i < p_array.size(); i++) {
  2291. rid_array->set(i, Variant(p_array[i]));
  2292. }
  2293. }
  2294. Variant::Variant(const Vector<Plane> &p_array) :
  2295. type(ARRAY) {
  2296. Array *plane_array = memnew_placement(_data._mem, Array);
  2297. plane_array->resize(p_array.size());
  2298. for (int i = 0; i < p_array.size(); i++) {
  2299. plane_array->operator[](i) = Variant(p_array[i]);
  2300. }
  2301. }
  2302. Variant::Variant(const Vector<Face3> &p_face_array) {
  2303. PackedVector3Array vertices;
  2304. int face_count = p_face_array.size();
  2305. vertices.resize(face_count * 3);
  2306. if (face_count) {
  2307. const Face3 *r = p_face_array.ptr();
  2308. Vector3 *w = vertices.ptrw();
  2309. for (int i = 0; i < face_count; i++) {
  2310. for (int j = 0; j < 3; j++) {
  2311. w[i * 3 + j] = r[i].vertex[j];
  2312. }
  2313. }
  2314. }
  2315. *this = vertices;
  2316. }
  2317. Variant::Variant(const Vector<Variant> &p_array) {
  2318. Array arr;
  2319. arr.resize(p_array.size());
  2320. for (int i = 0; i < p_array.size(); i++) {
  2321. arr[i] = p_array[i];
  2322. }
  2323. *this = arr;
  2324. }
  2325. Variant::Variant(const Vector<StringName> &p_array) {
  2326. PackedStringArray v;
  2327. int len = p_array.size();
  2328. v.resize(len);
  2329. for (int i = 0; i < len; i++) {
  2330. v.set(i, p_array[i]);
  2331. }
  2332. *this = v;
  2333. }
  2334. void Variant::operator=(const Variant &p_variant) {
  2335. if (unlikely(this == &p_variant)) {
  2336. return;
  2337. }
  2338. if (unlikely(type != p_variant.type)) {
  2339. reference(p_variant);
  2340. return;
  2341. }
  2342. switch (p_variant.type) {
  2343. case NIL: {
  2344. // none
  2345. } break;
  2346. // atomic types
  2347. case BOOL: {
  2348. _data._bool = p_variant._data._bool;
  2349. } break;
  2350. case INT: {
  2351. _data._int = p_variant._data._int;
  2352. } break;
  2353. case FLOAT: {
  2354. _data._float = p_variant._data._float;
  2355. } break;
  2356. case STRING: {
  2357. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2358. } break;
  2359. // math types
  2360. case VECTOR2: {
  2361. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2362. } break;
  2363. case VECTOR2I: {
  2364. *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2365. } break;
  2366. case RECT2: {
  2367. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2368. } break;
  2369. case RECT2I: {
  2370. *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2371. } break;
  2372. case TRANSFORM2D: {
  2373. *_data._transform2d = *(p_variant._data._transform2d);
  2374. } break;
  2375. case VECTOR3: {
  2376. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2377. } break;
  2378. case VECTOR3I: {
  2379. *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2380. } break;
  2381. case VECTOR4: {
  2382. *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2383. } break;
  2384. case VECTOR4I: {
  2385. *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2386. } break;
  2387. case PLANE: {
  2388. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2389. } break;
  2390. case AABB: {
  2391. *_data._aabb = *(p_variant._data._aabb);
  2392. } break;
  2393. case QUATERNION: {
  2394. *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2395. } break;
  2396. case BASIS: {
  2397. *_data._basis = *(p_variant._data._basis);
  2398. } break;
  2399. case TRANSFORM3D: {
  2400. *_data._transform3d = *(p_variant._data._transform3d);
  2401. } break;
  2402. case PROJECTION: {
  2403. *_data._projection = *(p_variant._data._projection);
  2404. } break;
  2405. // misc types
  2406. case COLOR: {
  2407. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2408. } break;
  2409. case RID: {
  2410. *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
  2411. } break;
  2412. case OBJECT: {
  2413. _get_obj().ref(p_variant._get_obj());
  2414. } break;
  2415. case CALLABLE: {
  2416. *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
  2417. } break;
  2418. case SIGNAL: {
  2419. *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
  2420. } break;
  2421. case STRING_NAME: {
  2422. *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2423. } break;
  2424. case NODE_PATH: {
  2425. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2426. } break;
  2427. case DICTIONARY: {
  2428. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2429. } break;
  2430. case ARRAY: {
  2431. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2432. } break;
  2433. // arrays
  2434. case PACKED_BYTE_ARRAY: {
  2435. _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2436. } break;
  2437. case PACKED_INT32_ARRAY: {
  2438. _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2439. } break;
  2440. case PACKED_INT64_ARRAY: {
  2441. _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2442. } break;
  2443. case PACKED_FLOAT32_ARRAY: {
  2444. _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2445. } break;
  2446. case PACKED_FLOAT64_ARRAY: {
  2447. _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2448. } break;
  2449. case PACKED_STRING_ARRAY: {
  2450. _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2451. } break;
  2452. case PACKED_VECTOR2_ARRAY: {
  2453. _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2454. } break;
  2455. case PACKED_VECTOR3_ARRAY: {
  2456. _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2457. } break;
  2458. case PACKED_COLOR_ARRAY: {
  2459. _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2460. } break;
  2461. case PACKED_VECTOR4_ARRAY: {
  2462. _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2463. } break;
  2464. default: {
  2465. }
  2466. }
  2467. }
  2468. Variant::Variant(const IPAddress &p_address) :
  2469. type(STRING) {
  2470. memnew_placement(_data._mem, String(p_address));
  2471. }
  2472. Variant::Variant(const Variant &p_variant) {
  2473. reference(p_variant);
  2474. }
  2475. uint32_t Variant::hash() const {
  2476. return recursive_hash(0);
  2477. }
  2478. uint32_t Variant::recursive_hash(int recursion_count) const {
  2479. switch (type) {
  2480. case NIL: {
  2481. return 0;
  2482. } break;
  2483. case BOOL: {
  2484. return _data._bool ? 1 : 0;
  2485. } break;
  2486. case INT: {
  2487. return hash_one_uint64((uint64_t)_data._int);
  2488. } break;
  2489. case FLOAT: {
  2490. return hash_murmur3_one_double(_data._float);
  2491. } break;
  2492. case STRING: {
  2493. return reinterpret_cast<const String *>(_data._mem)->hash();
  2494. } break;
  2495. // math types
  2496. case VECTOR2: {
  2497. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
  2498. } break;
  2499. case VECTOR2I: {
  2500. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
  2501. } break;
  2502. case RECT2: {
  2503. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
  2504. } break;
  2505. case RECT2I: {
  2506. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
  2507. } break;
  2508. case TRANSFORM2D: {
  2509. uint32_t h = HASH_MURMUR3_SEED;
  2510. const Transform2D &t = *_data._transform2d;
  2511. h = hash_murmur3_one_real(t[0].x, h);
  2512. h = hash_murmur3_one_real(t[0].y, h);
  2513. h = hash_murmur3_one_real(t[1].x, h);
  2514. h = hash_murmur3_one_real(t[1].y, h);
  2515. h = hash_murmur3_one_real(t[2].x, h);
  2516. h = hash_murmur3_one_real(t[2].y, h);
  2517. return hash_fmix32(h);
  2518. } break;
  2519. case VECTOR3: {
  2520. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
  2521. } break;
  2522. case VECTOR3I: {
  2523. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
  2524. } break;
  2525. case VECTOR4: {
  2526. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
  2527. } break;
  2528. case VECTOR4I: {
  2529. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
  2530. } break;
  2531. case PLANE: {
  2532. uint32_t h = HASH_MURMUR3_SEED;
  2533. const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
  2534. h = hash_murmur3_one_real(p.normal.x, h);
  2535. h = hash_murmur3_one_real(p.normal.y, h);
  2536. h = hash_murmur3_one_real(p.normal.z, h);
  2537. h = hash_murmur3_one_real(p.d, h);
  2538. return hash_fmix32(h);
  2539. } break;
  2540. case AABB: {
  2541. return HashMapHasherDefault::hash(*_data._aabb);
  2542. } break;
  2543. case QUATERNION: {
  2544. uint32_t h = HASH_MURMUR3_SEED;
  2545. const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
  2546. h = hash_murmur3_one_real(q.x, h);
  2547. h = hash_murmur3_one_real(q.y, h);
  2548. h = hash_murmur3_one_real(q.z, h);
  2549. h = hash_murmur3_one_real(q.w, h);
  2550. return hash_fmix32(h);
  2551. } break;
  2552. case BASIS: {
  2553. uint32_t h = HASH_MURMUR3_SEED;
  2554. const Basis &b = *_data._basis;
  2555. h = hash_murmur3_one_real(b[0].x, h);
  2556. h = hash_murmur3_one_real(b[0].y, h);
  2557. h = hash_murmur3_one_real(b[0].z, h);
  2558. h = hash_murmur3_one_real(b[1].x, h);
  2559. h = hash_murmur3_one_real(b[1].y, h);
  2560. h = hash_murmur3_one_real(b[1].z, h);
  2561. h = hash_murmur3_one_real(b[2].x, h);
  2562. h = hash_murmur3_one_real(b[2].y, h);
  2563. h = hash_murmur3_one_real(b[2].z, h);
  2564. return hash_fmix32(h);
  2565. } break;
  2566. case TRANSFORM3D: {
  2567. uint32_t h = HASH_MURMUR3_SEED;
  2568. const Transform3D &t = *_data._transform3d;
  2569. h = hash_murmur3_one_real(t.basis[0].x, h);
  2570. h = hash_murmur3_one_real(t.basis[0].y, h);
  2571. h = hash_murmur3_one_real(t.basis[0].z, h);
  2572. h = hash_murmur3_one_real(t.basis[1].x, h);
  2573. h = hash_murmur3_one_real(t.basis[1].y, h);
  2574. h = hash_murmur3_one_real(t.basis[1].z, h);
  2575. h = hash_murmur3_one_real(t.basis[2].x, h);
  2576. h = hash_murmur3_one_real(t.basis[2].y, h);
  2577. h = hash_murmur3_one_real(t.basis[2].z, h);
  2578. h = hash_murmur3_one_real(t.origin.x, h);
  2579. h = hash_murmur3_one_real(t.origin.y, h);
  2580. h = hash_murmur3_one_real(t.origin.z, h);
  2581. return hash_fmix32(h);
  2582. } break;
  2583. case PROJECTION: {
  2584. uint32_t h = HASH_MURMUR3_SEED;
  2585. const Projection &t = *_data._projection;
  2586. h = hash_murmur3_one_real(t.columns[0].x, h);
  2587. h = hash_murmur3_one_real(t.columns[0].y, h);
  2588. h = hash_murmur3_one_real(t.columns[0].z, h);
  2589. h = hash_murmur3_one_real(t.columns[0].w, h);
  2590. h = hash_murmur3_one_real(t.columns[1].x, h);
  2591. h = hash_murmur3_one_real(t.columns[1].y, h);
  2592. h = hash_murmur3_one_real(t.columns[1].z, h);
  2593. h = hash_murmur3_one_real(t.columns[1].w, h);
  2594. h = hash_murmur3_one_real(t.columns[2].x, h);
  2595. h = hash_murmur3_one_real(t.columns[2].y, h);
  2596. h = hash_murmur3_one_real(t.columns[2].z, h);
  2597. h = hash_murmur3_one_real(t.columns[2].w, h);
  2598. h = hash_murmur3_one_real(t.columns[3].x, h);
  2599. h = hash_murmur3_one_real(t.columns[3].y, h);
  2600. h = hash_murmur3_one_real(t.columns[3].z, h);
  2601. h = hash_murmur3_one_real(t.columns[3].w, h);
  2602. return hash_fmix32(h);
  2603. } break;
  2604. // misc types
  2605. case COLOR: {
  2606. uint32_t h = HASH_MURMUR3_SEED;
  2607. const Color &c = *reinterpret_cast<const Color *>(_data._mem);
  2608. h = hash_murmur3_one_float(c.r, h);
  2609. h = hash_murmur3_one_float(c.g, h);
  2610. h = hash_murmur3_one_float(c.b, h);
  2611. h = hash_murmur3_one_float(c.a, h);
  2612. return hash_fmix32(h);
  2613. } break;
  2614. case RID: {
  2615. return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
  2616. } break;
  2617. case OBJECT: {
  2618. return hash_one_uint64(reinterpret_cast<uint64_t>(_get_obj().obj));
  2619. } break;
  2620. case STRING_NAME: {
  2621. return reinterpret_cast<const StringName *>(_data._mem)->hash();
  2622. } break;
  2623. case NODE_PATH: {
  2624. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2625. } break;
  2626. case DICTIONARY: {
  2627. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
  2628. } break;
  2629. case CALLABLE: {
  2630. return reinterpret_cast<const Callable *>(_data._mem)->hash();
  2631. } break;
  2632. case SIGNAL: {
  2633. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  2634. uint32_t hash = s.get_name().hash();
  2635. return hash_murmur3_one_64(s.get_object_id(), hash);
  2636. } break;
  2637. case ARRAY: {
  2638. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2639. return arr.recursive_hash(recursion_count);
  2640. } break;
  2641. case PACKED_BYTE_ARRAY: {
  2642. const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  2643. int len = arr.size();
  2644. if (likely(len)) {
  2645. const uint8_t *r = arr.ptr();
  2646. return hash_murmur3_buffer((uint8_t *)&r[0], len);
  2647. } else {
  2648. return hash_murmur3_one_64(0);
  2649. }
  2650. } break;
  2651. case PACKED_INT32_ARRAY: {
  2652. const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
  2653. int len = arr.size();
  2654. if (likely(len)) {
  2655. const int32_t *r = arr.ptr();
  2656. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
  2657. } else {
  2658. return hash_murmur3_one_64(0);
  2659. }
  2660. } break;
  2661. case PACKED_INT64_ARRAY: {
  2662. const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
  2663. int len = arr.size();
  2664. if (likely(len)) {
  2665. const int64_t *r = arr.ptr();
  2666. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
  2667. } else {
  2668. return hash_murmur3_one_64(0);
  2669. }
  2670. } break;
  2671. case PACKED_FLOAT32_ARRAY: {
  2672. const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
  2673. int len = arr.size();
  2674. if (likely(len)) {
  2675. const float *r = arr.ptr();
  2676. uint32_t h = HASH_MURMUR3_SEED;
  2677. for (int32_t i = 0; i < len; i++) {
  2678. h = hash_murmur3_one_float(r[i], h);
  2679. }
  2680. return hash_fmix32(h);
  2681. } else {
  2682. return hash_murmur3_one_float(0.0);
  2683. }
  2684. } break;
  2685. case PACKED_FLOAT64_ARRAY: {
  2686. const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
  2687. int len = arr.size();
  2688. if (likely(len)) {
  2689. const double *r = arr.ptr();
  2690. uint32_t h = HASH_MURMUR3_SEED;
  2691. for (int32_t i = 0; i < len; i++) {
  2692. h = hash_murmur3_one_double(r[i], h);
  2693. }
  2694. return hash_fmix32(h);
  2695. } else {
  2696. return hash_murmur3_one_double(0.0);
  2697. }
  2698. } break;
  2699. case PACKED_STRING_ARRAY: {
  2700. uint32_t hash = HASH_MURMUR3_SEED;
  2701. const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
  2702. int len = arr.size();
  2703. if (likely(len)) {
  2704. const String *r = arr.ptr();
  2705. for (int i = 0; i < len; i++) {
  2706. hash = hash_murmur3_one_32(r[i].hash(), hash);
  2707. }
  2708. hash = hash_fmix32(hash);
  2709. }
  2710. return hash;
  2711. } break;
  2712. case PACKED_VECTOR2_ARRAY: {
  2713. uint32_t hash = HASH_MURMUR3_SEED;
  2714. const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
  2715. int len = arr.size();
  2716. if (likely(len)) {
  2717. const Vector2 *r = arr.ptr();
  2718. for (int i = 0; i < len; i++) {
  2719. hash = hash_murmur3_one_real(r[i].x, hash);
  2720. hash = hash_murmur3_one_real(r[i].y, hash);
  2721. }
  2722. hash = hash_fmix32(hash);
  2723. }
  2724. return hash;
  2725. } break;
  2726. case PACKED_VECTOR3_ARRAY: {
  2727. uint32_t hash = HASH_MURMUR3_SEED;
  2728. const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
  2729. int len = arr.size();
  2730. if (likely(len)) {
  2731. const Vector3 *r = arr.ptr();
  2732. for (int i = 0; i < len; i++) {
  2733. hash = hash_murmur3_one_real(r[i].x, hash);
  2734. hash = hash_murmur3_one_real(r[i].y, hash);
  2735. hash = hash_murmur3_one_real(r[i].z, hash);
  2736. }
  2737. hash = hash_fmix32(hash);
  2738. }
  2739. return hash;
  2740. } break;
  2741. case PACKED_COLOR_ARRAY: {
  2742. uint32_t hash = HASH_MURMUR3_SEED;
  2743. const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
  2744. int len = arr.size();
  2745. if (likely(len)) {
  2746. const Color *r = arr.ptr();
  2747. for (int i = 0; i < len; i++) {
  2748. hash = hash_murmur3_one_float(r[i].r, hash);
  2749. hash = hash_murmur3_one_float(r[i].g, hash);
  2750. hash = hash_murmur3_one_float(r[i].b, hash);
  2751. hash = hash_murmur3_one_float(r[i].a, hash);
  2752. }
  2753. hash = hash_fmix32(hash);
  2754. }
  2755. return hash;
  2756. } break;
  2757. case PACKED_VECTOR4_ARRAY: {
  2758. uint32_t hash = HASH_MURMUR3_SEED;
  2759. const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
  2760. int len = arr.size();
  2761. if (likely(len)) {
  2762. const Vector4 *r = arr.ptr();
  2763. for (int i = 0; i < len; i++) {
  2764. hash = hash_murmur3_one_real(r[i].x, hash);
  2765. hash = hash_murmur3_one_real(r[i].y, hash);
  2766. hash = hash_murmur3_one_real(r[i].z, hash);
  2767. hash = hash_murmur3_one_real(r[i].w, hash);
  2768. }
  2769. hash = hash_fmix32(hash);
  2770. }
  2771. return hash;
  2772. } break;
  2773. default: {
  2774. }
  2775. }
  2776. return 0;
  2777. }
  2778. #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
  2779. (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2780. #define hash_compare_scalar(p_lhs, p_rhs) \
  2781. (hash_compare_scalar_base(p_lhs, p_rhs, true))
  2782. #define hash_compare_vector2(p_lhs, p_rhs) \
  2783. (p_lhs).is_same(p_rhs)
  2784. #define hash_compare_vector3(p_lhs, p_rhs) \
  2785. (p_lhs).is_same(p_rhs)
  2786. #define hash_compare_vector4(p_lhs, p_rhs) \
  2787. (p_lhs).is_same(p_rhs)
  2788. #define hash_compare_quaternion(p_lhs, p_rhs) \
  2789. (p_lhs).is_same(p_rhs)
  2790. #define hash_compare_color(p_lhs, p_rhs) \
  2791. (p_lhs).is_same(p_rhs)
  2792. #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2793. const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
  2794. const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
  2795. \
  2796. if (l.size() != r.size()) \
  2797. return false; \
  2798. \
  2799. const p_type *lr = l.ptr(); \
  2800. const p_type *rr = r.ptr(); \
  2801. \
  2802. for (int i = 0; i < l.size(); ++i) { \
  2803. if (!p_compare_func((lr[i]), (rr[i]))) \
  2804. return false; \
  2805. } \
  2806. \
  2807. return true
  2808. bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
  2809. if (type != p_variant.type) {
  2810. return false;
  2811. }
  2812. switch (type) {
  2813. case INT: {
  2814. return _data._int == p_variant._data._int;
  2815. } break;
  2816. case FLOAT: {
  2817. return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
  2818. } break;
  2819. case STRING: {
  2820. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2821. } break;
  2822. case STRING_NAME: {
  2823. return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2824. } break;
  2825. case VECTOR2: {
  2826. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2827. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2828. return hash_compare_vector2(*l, *r);
  2829. } break;
  2830. case VECTOR2I: {
  2831. const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
  2832. const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2833. return *l == *r;
  2834. } break;
  2835. case RECT2: {
  2836. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2837. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2838. return hash_compare_vector2(l->position, r->position) &&
  2839. hash_compare_vector2(l->size, r->size);
  2840. } break;
  2841. case RECT2I: {
  2842. const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
  2843. const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2844. return *l == *r;
  2845. } break;
  2846. case TRANSFORM2D: {
  2847. Transform2D *l = _data._transform2d;
  2848. Transform2D *r = p_variant._data._transform2d;
  2849. return l->is_same(*r);
  2850. } break;
  2851. case VECTOR3: {
  2852. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2853. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2854. return hash_compare_vector3(*l, *r);
  2855. } break;
  2856. case VECTOR3I: {
  2857. const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
  2858. const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2859. return *l == *r;
  2860. } break;
  2861. case VECTOR4: {
  2862. const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
  2863. const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2864. return hash_compare_vector4(*l, *r);
  2865. } break;
  2866. case VECTOR4I: {
  2867. const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
  2868. const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2869. return *l == *r;
  2870. } break;
  2871. case PLANE: {
  2872. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2873. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2874. return l->is_same(*r);
  2875. } break;
  2876. case AABB: {
  2877. const ::AABB *l = _data._aabb;
  2878. const ::AABB *r = p_variant._data._aabb;
  2879. return l->is_same(*r);
  2880. } break;
  2881. case QUATERNION: {
  2882. const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
  2883. const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2884. return hash_compare_quaternion(*l, *r);
  2885. } break;
  2886. case BASIS: {
  2887. const Basis *l = _data._basis;
  2888. const Basis *r = p_variant._data._basis;
  2889. return l->is_same(*r);
  2890. } break;
  2891. case TRANSFORM3D: {
  2892. const Transform3D *l = _data._transform3d;
  2893. const Transform3D *r = p_variant._data._transform3d;
  2894. return l->is_same(*r);
  2895. } break;
  2896. case PROJECTION: {
  2897. const Projection *l = _data._projection;
  2898. const Projection *r = p_variant._data._projection;
  2899. return l->is_same(*r);
  2900. } break;
  2901. case COLOR: {
  2902. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2903. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2904. return hash_compare_color(*l, *r);
  2905. } break;
  2906. case ARRAY: {
  2907. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2908. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2909. if (!l.recursive_equal(r, recursion_count + 1)) {
  2910. return false;
  2911. }
  2912. return true;
  2913. } break;
  2914. case DICTIONARY: {
  2915. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  2916. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  2917. if (!l.recursive_equal(r, recursion_count + 1)) {
  2918. return false;
  2919. }
  2920. return true;
  2921. } break;
  2922. // This is for floating point comparisons only.
  2923. case PACKED_FLOAT32_ARRAY: {
  2924. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
  2925. } break;
  2926. case PACKED_FLOAT64_ARRAY: {
  2927. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
  2928. } break;
  2929. case PACKED_VECTOR2_ARRAY: {
  2930. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
  2931. } break;
  2932. case PACKED_VECTOR3_ARRAY: {
  2933. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
  2934. } break;
  2935. case PACKED_COLOR_ARRAY: {
  2936. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
  2937. } break;
  2938. case PACKED_VECTOR4_ARRAY: {
  2939. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
  2940. } break;
  2941. default:
  2942. bool v;
  2943. Variant r;
  2944. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2945. return r;
  2946. }
  2947. }
  2948. bool Variant::identity_compare(const Variant &p_variant) const {
  2949. if (type != p_variant.type) {
  2950. return false;
  2951. }
  2952. switch (type) {
  2953. case OBJECT: {
  2954. return _get_obj().id == p_variant._get_obj().id;
  2955. } break;
  2956. case DICTIONARY: {
  2957. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  2958. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  2959. return l.id() == r.id();
  2960. } break;
  2961. case ARRAY: {
  2962. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2963. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2964. return l.id() == r.id();
  2965. } break;
  2966. case PACKED_BYTE_ARRAY:
  2967. case PACKED_INT32_ARRAY:
  2968. case PACKED_INT64_ARRAY:
  2969. case PACKED_FLOAT32_ARRAY:
  2970. case PACKED_FLOAT64_ARRAY:
  2971. case PACKED_STRING_ARRAY:
  2972. case PACKED_VECTOR2_ARRAY:
  2973. case PACKED_VECTOR3_ARRAY:
  2974. case PACKED_COLOR_ARRAY:
  2975. case PACKED_VECTOR4_ARRAY: {
  2976. return _data.packed_array == p_variant._data.packed_array;
  2977. } break;
  2978. default: {
  2979. return hash_compare(p_variant);
  2980. }
  2981. }
  2982. }
  2983. bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
  2984. if (p_lhs.hash_compare(p_rhs)) {
  2985. return true;
  2986. }
  2987. if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
  2988. return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
  2989. }
  2990. if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
  2991. return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
  2992. }
  2993. return false;
  2994. }
  2995. bool StringLikeVariantOrder::compare(const Variant &p_lhs, const Variant &p_rhs) {
  2996. if (p_lhs.get_type() == Variant::STRING) {
  2997. const String &lhs = *VariantInternal::get_string(&p_lhs);
  2998. if (p_rhs.get_type() == Variant::STRING) {
  2999. return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string(&p_rhs));
  3000. } else if (p_rhs.get_type() == Variant::STRING_NAME) {
  3001. return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string_name(&p_rhs));
  3002. }
  3003. } else if (p_lhs.get_type() == Variant::STRING_NAME) {
  3004. const StringName &lhs = *VariantInternal::get_string_name(&p_lhs);
  3005. if (p_rhs.get_type() == Variant::STRING) {
  3006. return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string(&p_rhs));
  3007. } else if (p_rhs.get_type() == Variant::STRING_NAME) {
  3008. return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string_name(&p_rhs));
  3009. }
  3010. }
  3011. return p_lhs < p_rhs;
  3012. }
  3013. bool Variant::is_ref_counted() const {
  3014. return type == OBJECT && _get_obj().id.is_ref_counted();
  3015. }
  3016. bool Variant::is_type_shared(Variant::Type p_type) {
  3017. switch (p_type) {
  3018. case OBJECT:
  3019. case ARRAY:
  3020. case DICTIONARY:
  3021. return true;
  3022. default: {
  3023. }
  3024. }
  3025. return false;
  3026. }
  3027. bool Variant::is_shared() const {
  3028. return is_type_shared(type);
  3029. }
  3030. bool Variant::is_read_only() const {
  3031. switch (type) {
  3032. case ARRAY:
  3033. return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
  3034. case DICTIONARY:
  3035. return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
  3036. default:
  3037. return false;
  3038. }
  3039. }
  3040. void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
  3041. switch (error.error) {
  3042. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  3043. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
  3044. ERR_PRINT(err.utf8().get_data());
  3045. } break;
  3046. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  3047. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  3048. ERR_PRINT(err.utf8().get_data());
  3049. } break;
  3050. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  3051. String err = "Too many arguments for method '" + p_method + "'";
  3052. ERR_PRINT(err.utf8().get_data());
  3053. } break;
  3054. default: {
  3055. }
  3056. }
  3057. }
  3058. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  3059. r_value = Variant();
  3060. }
  3061. String Variant::get_construct_string() const {
  3062. String vars;
  3063. VariantWriter::write_to_string(*this, vars);
  3064. return vars;
  3065. }
  3066. String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3067. return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
  3068. }
  3069. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3070. String err_text;
  3071. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  3072. int errorarg = ce.argument;
  3073. if (p_argptrs) {
  3074. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected));
  3075. } else {
  3076. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
  3077. }
  3078. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  3079. err_text = "Method expected " + itos(ce.expected) + " argument(s), but called with " + itos(p_argcount);
  3080. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  3081. err_text = "Method expected " + itos(ce.expected) + " argument(s), but called with " + itos(p_argcount);
  3082. } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  3083. err_text = "Method not found";
  3084. } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  3085. err_text = "Instance is null";
  3086. } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
  3087. err_text = "Method not const in const instance";
  3088. } else if (ce.error == Callable::CallError::CALL_OK) {
  3089. return "Call OK";
  3090. }
  3091. String base_text;
  3092. if (p_base) {
  3093. base_text = p_base->get_class();
  3094. Ref<Resource> script = p_base->get_script();
  3095. if (script.is_valid() && script->get_path().is_resource_file()) {
  3096. base_text += "(" + script->get_path().get_file() + ")";
  3097. }
  3098. base_text += "::";
  3099. }
  3100. return "'" + base_text + String(p_method) + "': " + err_text;
  3101. }
  3102. String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3103. Vector<Variant> binds;
  3104. p_callable.get_bound_arguments_ref(binds);
  3105. int args_unbound = p_callable.get_unbound_arguments_count();
  3106. if (p_argcount - args_unbound < 0) {
  3107. return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
  3108. } else {
  3109. Vector<const Variant *> argptrs;
  3110. argptrs.resize(p_argcount - args_unbound + binds.size());
  3111. for (int i = 0; i < p_argcount - args_unbound; i++) {
  3112. argptrs.write[i] = p_argptrs[i];
  3113. }
  3114. for (int i = 0; i < binds.size(); i++) {
  3115. argptrs.write[i + p_argcount - args_unbound] = &binds[i];
  3116. }
  3117. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
  3118. }
  3119. }
  3120. void Variant::register_types() {
  3121. _register_variant_operators();
  3122. _register_variant_methods();
  3123. _register_variant_setters_getters();
  3124. _register_variant_constructors();
  3125. _register_variant_destructors();
  3126. _register_variant_utility_functions();
  3127. }
  3128. void Variant::unregister_types() {
  3129. _unregister_variant_operators();
  3130. _unregister_variant_methods();
  3131. _unregister_variant_setters_getters();
  3132. _unregister_variant_destructors();
  3133. _unregister_variant_utility_functions();
  3134. }