variant.cpp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589
  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. Variant::Type Variant::get_type_by_name(const String &p_type_name) {
  168. static HashMap<String, Type> type_names;
  169. if (unlikely(type_names.is_empty())) {
  170. for (int i = 0; i < VARIANT_MAX; i++) {
  171. type_names[get_type_name((Type)i)] = (Type)i;
  172. }
  173. }
  174. const Type *ptr = type_names.getptr(p_type_name);
  175. return (ptr == nullptr) ? VARIANT_MAX : *ptr;
  176. }
  177. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  178. if (p_type_from == p_type_to) {
  179. return true;
  180. }
  181. if (p_type_to == NIL) { //nil can convert to anything
  182. return true;
  183. }
  184. if (p_type_from == NIL) {
  185. return (p_type_to == OBJECT);
  186. }
  187. const Type *valid_types = nullptr;
  188. const Type *invalid_types = nullptr;
  189. switch (p_type_to) {
  190. case BOOL: {
  191. static const Type valid[] = {
  192. INT,
  193. FLOAT,
  194. STRING,
  195. NIL,
  196. };
  197. valid_types = valid;
  198. } break;
  199. case INT: {
  200. static const Type valid[] = {
  201. BOOL,
  202. FLOAT,
  203. STRING,
  204. NIL,
  205. };
  206. valid_types = valid;
  207. } break;
  208. case FLOAT: {
  209. static const Type valid[] = {
  210. BOOL,
  211. INT,
  212. STRING,
  213. NIL,
  214. };
  215. valid_types = valid;
  216. } break;
  217. case STRING: {
  218. static const Type invalid[] = {
  219. OBJECT,
  220. NIL
  221. };
  222. invalid_types = invalid;
  223. } break;
  224. case VECTOR2: {
  225. static const Type valid[] = {
  226. VECTOR2I,
  227. NIL,
  228. };
  229. valid_types = valid;
  230. } break;
  231. case VECTOR2I: {
  232. static const Type valid[] = {
  233. VECTOR2,
  234. NIL,
  235. };
  236. valid_types = valid;
  237. } break;
  238. case RECT2: {
  239. static const Type valid[] = {
  240. RECT2I,
  241. NIL,
  242. };
  243. valid_types = valid;
  244. } break;
  245. case RECT2I: {
  246. static const Type valid[] = {
  247. RECT2,
  248. NIL,
  249. };
  250. valid_types = valid;
  251. } break;
  252. case TRANSFORM2D: {
  253. static const Type valid[] = {
  254. TRANSFORM3D,
  255. NIL
  256. };
  257. valid_types = valid;
  258. } break;
  259. case VECTOR3: {
  260. static const Type valid[] = {
  261. VECTOR3I,
  262. NIL,
  263. };
  264. valid_types = valid;
  265. } break;
  266. case VECTOR3I: {
  267. static const Type valid[] = {
  268. VECTOR3,
  269. NIL,
  270. };
  271. valid_types = valid;
  272. } break;
  273. case VECTOR4: {
  274. static const Type valid[] = {
  275. VECTOR4I,
  276. NIL,
  277. };
  278. valid_types = valid;
  279. } break;
  280. case VECTOR4I: {
  281. static const Type valid[] = {
  282. VECTOR4,
  283. NIL,
  284. };
  285. valid_types = valid;
  286. } break;
  287. case QUATERNION: {
  288. static const Type valid[] = {
  289. BASIS,
  290. NIL
  291. };
  292. valid_types = valid;
  293. } break;
  294. case BASIS: {
  295. static const Type valid[] = {
  296. QUATERNION,
  297. NIL
  298. };
  299. valid_types = valid;
  300. } break;
  301. case TRANSFORM3D: {
  302. static const Type valid[] = {
  303. TRANSFORM2D,
  304. QUATERNION,
  305. BASIS,
  306. PROJECTION,
  307. NIL
  308. };
  309. valid_types = valid;
  310. } break;
  311. case PROJECTION: {
  312. static const Type valid[] = {
  313. TRANSFORM3D,
  314. NIL
  315. };
  316. valid_types = valid;
  317. } break;
  318. case COLOR: {
  319. static const Type valid[] = {
  320. STRING,
  321. INT,
  322. NIL,
  323. };
  324. valid_types = valid;
  325. } break;
  326. case RID: {
  327. static const Type valid[] = {
  328. OBJECT,
  329. NIL
  330. };
  331. valid_types = valid;
  332. } break;
  333. case OBJECT: {
  334. static const Type valid[] = {
  335. NIL
  336. };
  337. valid_types = valid;
  338. } break;
  339. case STRING_NAME: {
  340. static const Type valid[] = {
  341. STRING,
  342. NIL
  343. };
  344. valid_types = valid;
  345. } break;
  346. case NODE_PATH: {
  347. static const Type valid[] = {
  348. STRING,
  349. NIL
  350. };
  351. valid_types = valid;
  352. } break;
  353. case ARRAY: {
  354. static const Type valid[] = {
  355. PACKED_BYTE_ARRAY,
  356. PACKED_INT32_ARRAY,
  357. PACKED_INT64_ARRAY,
  358. PACKED_FLOAT32_ARRAY,
  359. PACKED_FLOAT64_ARRAY,
  360. PACKED_STRING_ARRAY,
  361. PACKED_COLOR_ARRAY,
  362. PACKED_VECTOR2_ARRAY,
  363. PACKED_VECTOR3_ARRAY,
  364. PACKED_VECTOR4_ARRAY,
  365. NIL
  366. };
  367. valid_types = valid;
  368. } break;
  369. // arrays
  370. case PACKED_BYTE_ARRAY: {
  371. static const Type valid[] = {
  372. ARRAY,
  373. NIL
  374. };
  375. valid_types = valid;
  376. } break;
  377. case PACKED_INT32_ARRAY: {
  378. static const Type valid[] = {
  379. ARRAY,
  380. NIL
  381. };
  382. valid_types = valid;
  383. } break;
  384. case PACKED_INT64_ARRAY: {
  385. static const Type valid[] = {
  386. ARRAY,
  387. NIL
  388. };
  389. valid_types = valid;
  390. } break;
  391. case PACKED_FLOAT32_ARRAY: {
  392. static const Type valid[] = {
  393. ARRAY,
  394. NIL
  395. };
  396. valid_types = valid;
  397. } break;
  398. case PACKED_FLOAT64_ARRAY: {
  399. static const Type valid[] = {
  400. ARRAY,
  401. NIL
  402. };
  403. valid_types = valid;
  404. } break;
  405. case PACKED_STRING_ARRAY: {
  406. static const Type valid[] = {
  407. ARRAY,
  408. NIL
  409. };
  410. valid_types = valid;
  411. } break;
  412. case PACKED_VECTOR2_ARRAY: {
  413. static const Type valid[] = {
  414. ARRAY,
  415. NIL
  416. };
  417. valid_types = valid;
  418. } break;
  419. case PACKED_VECTOR3_ARRAY: {
  420. static const Type valid[] = {
  421. ARRAY,
  422. NIL
  423. };
  424. valid_types = valid;
  425. } break;
  426. case PACKED_COLOR_ARRAY: {
  427. static const Type valid[] = {
  428. ARRAY,
  429. NIL
  430. };
  431. valid_types = valid;
  432. } break;
  433. case PACKED_VECTOR4_ARRAY: {
  434. static const Type valid[] = {
  435. ARRAY,
  436. NIL
  437. };
  438. valid_types = valid;
  439. } break;
  440. default: {
  441. }
  442. }
  443. if (valid_types) {
  444. int i = 0;
  445. while (valid_types[i] != NIL) {
  446. if (p_type_from == valid_types[i]) {
  447. return true;
  448. }
  449. i++;
  450. }
  451. } else if (invalid_types) {
  452. int i = 0;
  453. while (invalid_types[i] != NIL) {
  454. if (p_type_from == invalid_types[i]) {
  455. return false;
  456. }
  457. i++;
  458. }
  459. return true;
  460. }
  461. return false;
  462. }
  463. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  464. if (p_type_from == p_type_to) {
  465. return true;
  466. }
  467. if (p_type_to == NIL) { //nil can convert to anything
  468. return true;
  469. }
  470. if (p_type_from == NIL) {
  471. return (p_type_to == OBJECT);
  472. }
  473. const Type *valid_types = nullptr;
  474. switch (p_type_to) {
  475. case BOOL: {
  476. static const Type valid[] = {
  477. INT,
  478. FLOAT,
  479. //STRING,
  480. NIL,
  481. };
  482. valid_types = valid;
  483. } break;
  484. case INT: {
  485. static const Type valid[] = {
  486. BOOL,
  487. FLOAT,
  488. //STRING,
  489. NIL,
  490. };
  491. valid_types = valid;
  492. } break;
  493. case FLOAT: {
  494. static const Type valid[] = {
  495. BOOL,
  496. INT,
  497. //STRING,
  498. NIL,
  499. };
  500. valid_types = valid;
  501. } break;
  502. case STRING: {
  503. static const Type valid[] = {
  504. NODE_PATH,
  505. STRING_NAME,
  506. NIL
  507. };
  508. valid_types = valid;
  509. } break;
  510. case VECTOR2: {
  511. static const Type valid[] = {
  512. VECTOR2I,
  513. NIL,
  514. };
  515. valid_types = valid;
  516. } break;
  517. case VECTOR2I: {
  518. static const Type valid[] = {
  519. VECTOR2,
  520. NIL,
  521. };
  522. valid_types = valid;
  523. } break;
  524. case RECT2: {
  525. static const Type valid[] = {
  526. RECT2I,
  527. NIL,
  528. };
  529. valid_types = valid;
  530. } break;
  531. case RECT2I: {
  532. static const Type valid[] = {
  533. RECT2,
  534. NIL,
  535. };
  536. valid_types = valid;
  537. } break;
  538. case TRANSFORM2D: {
  539. static const Type valid[] = {
  540. TRANSFORM3D,
  541. NIL
  542. };
  543. valid_types = valid;
  544. } break;
  545. case VECTOR3: {
  546. static const Type valid[] = {
  547. VECTOR3I,
  548. NIL,
  549. };
  550. valid_types = valid;
  551. } break;
  552. case VECTOR3I: {
  553. static const Type valid[] = {
  554. VECTOR3,
  555. NIL,
  556. };
  557. valid_types = valid;
  558. } break;
  559. case VECTOR4: {
  560. static const Type valid[] = {
  561. VECTOR4I,
  562. NIL,
  563. };
  564. valid_types = valid;
  565. } break;
  566. case VECTOR4I: {
  567. static const Type valid[] = {
  568. VECTOR4,
  569. NIL,
  570. };
  571. valid_types = valid;
  572. } break;
  573. case QUATERNION: {
  574. static const Type valid[] = {
  575. BASIS,
  576. NIL
  577. };
  578. valid_types = valid;
  579. } break;
  580. case BASIS: {
  581. static const Type valid[] = {
  582. QUATERNION,
  583. NIL
  584. };
  585. valid_types = valid;
  586. } break;
  587. case TRANSFORM3D: {
  588. static const Type valid[] = {
  589. TRANSFORM2D,
  590. QUATERNION,
  591. BASIS,
  592. PROJECTION,
  593. NIL
  594. };
  595. valid_types = valid;
  596. } break;
  597. case PROJECTION: {
  598. static const Type valid[] = {
  599. TRANSFORM3D,
  600. NIL
  601. };
  602. valid_types = valid;
  603. } break;
  604. case COLOR: {
  605. static const Type valid[] = {
  606. STRING,
  607. INT,
  608. NIL,
  609. };
  610. valid_types = valid;
  611. } break;
  612. case RID: {
  613. static const Type valid[] = {
  614. OBJECT,
  615. NIL
  616. };
  617. valid_types = valid;
  618. } break;
  619. case OBJECT: {
  620. static const Type valid[] = {
  621. NIL
  622. };
  623. valid_types = valid;
  624. } break;
  625. case STRING_NAME: {
  626. static const Type valid[] = {
  627. STRING,
  628. NIL
  629. };
  630. valid_types = valid;
  631. } break;
  632. case NODE_PATH: {
  633. static const Type valid[] = {
  634. STRING,
  635. NIL
  636. };
  637. valid_types = valid;
  638. } break;
  639. case ARRAY: {
  640. static const Type valid[] = {
  641. PACKED_BYTE_ARRAY,
  642. PACKED_INT32_ARRAY,
  643. PACKED_INT64_ARRAY,
  644. PACKED_FLOAT32_ARRAY,
  645. PACKED_FLOAT64_ARRAY,
  646. PACKED_STRING_ARRAY,
  647. PACKED_COLOR_ARRAY,
  648. PACKED_VECTOR2_ARRAY,
  649. PACKED_VECTOR3_ARRAY,
  650. PACKED_VECTOR4_ARRAY,
  651. NIL
  652. };
  653. valid_types = valid;
  654. } break;
  655. // arrays
  656. case PACKED_BYTE_ARRAY: {
  657. static const Type valid[] = {
  658. ARRAY,
  659. NIL
  660. };
  661. valid_types = valid;
  662. } break;
  663. case PACKED_INT32_ARRAY: {
  664. static const Type valid[] = {
  665. ARRAY,
  666. NIL
  667. };
  668. valid_types = valid;
  669. } break;
  670. case PACKED_INT64_ARRAY: {
  671. static const Type valid[] = {
  672. ARRAY,
  673. NIL
  674. };
  675. valid_types = valid;
  676. } break;
  677. case PACKED_FLOAT32_ARRAY: {
  678. static const Type valid[] = {
  679. ARRAY,
  680. NIL
  681. };
  682. valid_types = valid;
  683. } break;
  684. case PACKED_FLOAT64_ARRAY: {
  685. static const Type valid[] = {
  686. ARRAY,
  687. NIL
  688. };
  689. valid_types = valid;
  690. } break;
  691. case PACKED_STRING_ARRAY: {
  692. static const Type valid[] = {
  693. ARRAY,
  694. NIL
  695. };
  696. valid_types = valid;
  697. } break;
  698. case PACKED_VECTOR2_ARRAY: {
  699. static const Type valid[] = {
  700. ARRAY,
  701. NIL
  702. };
  703. valid_types = valid;
  704. } break;
  705. case PACKED_VECTOR3_ARRAY: {
  706. static const Type valid[] = {
  707. ARRAY,
  708. NIL
  709. };
  710. valid_types = valid;
  711. } break;
  712. case PACKED_COLOR_ARRAY: {
  713. static const Type valid[] = {
  714. ARRAY,
  715. NIL
  716. };
  717. valid_types = valid;
  718. } break;
  719. case PACKED_VECTOR4_ARRAY: {
  720. static const Type valid[] = {
  721. ARRAY,
  722. NIL
  723. };
  724. valid_types = valid;
  725. } break;
  726. default: {
  727. }
  728. }
  729. if (valid_types) {
  730. int i = 0;
  731. while (valid_types[i] != NIL) {
  732. if (p_type_from == valid_types[i]) {
  733. return true;
  734. }
  735. i++;
  736. }
  737. }
  738. return false;
  739. }
  740. bool Variant::operator==(const Variant &p_variant) const {
  741. return hash_compare(p_variant);
  742. }
  743. bool Variant::operator!=(const Variant &p_variant) const {
  744. // Don't use `!hash_compare(p_variant)` given it makes use of OP_EQUAL
  745. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  746. return true;
  747. }
  748. bool v;
  749. Variant r;
  750. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  751. return r;
  752. }
  753. bool Variant::operator<(const Variant &p_variant) const {
  754. if (type != p_variant.type) { //if types differ, then order by type first
  755. return type < p_variant.type;
  756. }
  757. bool v;
  758. Variant r;
  759. evaluate(OP_LESS, *this, p_variant, r, v);
  760. return r;
  761. }
  762. bool Variant::is_zero() const {
  763. switch (type) {
  764. case NIL: {
  765. return true;
  766. }
  767. // Atomic types.
  768. case BOOL: {
  769. return !(_data._bool);
  770. }
  771. case INT: {
  772. return _data._int == 0;
  773. }
  774. case FLOAT: {
  775. return _data._float == 0;
  776. }
  777. case STRING: {
  778. return *reinterpret_cast<const String *>(_data._mem) == String();
  779. }
  780. // Math types.
  781. case VECTOR2: {
  782. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  783. }
  784. case VECTOR2I: {
  785. return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i();
  786. }
  787. case RECT2: {
  788. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  789. }
  790. case RECT2I: {
  791. return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i();
  792. }
  793. case TRANSFORM2D: {
  794. return *_data._transform2d == Transform2D();
  795. }
  796. case VECTOR3: {
  797. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  798. }
  799. case VECTOR3I: {
  800. return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i();
  801. }
  802. case VECTOR4: {
  803. return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4();
  804. }
  805. case VECTOR4I: {
  806. return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i();
  807. }
  808. case PLANE: {
  809. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  810. }
  811. case AABB: {
  812. return *_data._aabb == ::AABB();
  813. }
  814. case QUATERNION: {
  815. return *reinterpret_cast<const Quaternion *>(_data._mem) == Quaternion();
  816. }
  817. case BASIS: {
  818. return *_data._basis == Basis();
  819. }
  820. case TRANSFORM3D: {
  821. return *_data._transform3d == Transform3D();
  822. }
  823. case PROJECTION: {
  824. return *_data._projection == Projection();
  825. }
  826. // Miscellaneous types.
  827. case COLOR: {
  828. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  829. }
  830. case RID: {
  831. return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID();
  832. }
  833. case OBJECT: {
  834. return get_validated_object() == nullptr;
  835. }
  836. case CALLABLE: {
  837. return reinterpret_cast<const Callable *>(_data._mem)->is_null();
  838. }
  839. case SIGNAL: {
  840. return reinterpret_cast<const Signal *>(_data._mem)->is_null();
  841. }
  842. case STRING_NAME: {
  843. return *reinterpret_cast<const StringName *>(_data._mem) == StringName();
  844. }
  845. case NODE_PATH: {
  846. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  847. }
  848. case DICTIONARY: {
  849. return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty();
  850. }
  851. case ARRAY: {
  852. return reinterpret_cast<const Array *>(_data._mem)->is_empty();
  853. }
  854. // Arrays.
  855. case PACKED_BYTE_ARRAY: {
  856. return PackedArrayRef<uint8_t>::get_array(_data.packed_array).size() == 0;
  857. }
  858. case PACKED_INT32_ARRAY: {
  859. return PackedArrayRef<int32_t>::get_array(_data.packed_array).size() == 0;
  860. }
  861. case PACKED_INT64_ARRAY: {
  862. return PackedArrayRef<int64_t>::get_array(_data.packed_array).size() == 0;
  863. }
  864. case PACKED_FLOAT32_ARRAY: {
  865. return PackedArrayRef<float>::get_array(_data.packed_array).size() == 0;
  866. }
  867. case PACKED_FLOAT64_ARRAY: {
  868. return PackedArrayRef<double>::get_array(_data.packed_array).size() == 0;
  869. }
  870. case PACKED_STRING_ARRAY: {
  871. return PackedArrayRef<String>::get_array(_data.packed_array).size() == 0;
  872. }
  873. case PACKED_VECTOR2_ARRAY: {
  874. return PackedArrayRef<Vector2>::get_array(_data.packed_array).size() == 0;
  875. }
  876. case PACKED_VECTOR3_ARRAY: {
  877. return PackedArrayRef<Vector3>::get_array(_data.packed_array).size() == 0;
  878. }
  879. case PACKED_COLOR_ARRAY: {
  880. return PackedArrayRef<Color>::get_array(_data.packed_array).size() == 0;
  881. }
  882. case PACKED_VECTOR4_ARRAY: {
  883. return PackedArrayRef<Vector4>::get_array(_data.packed_array).size() == 0;
  884. }
  885. default: {
  886. }
  887. }
  888. return false;
  889. }
  890. bool Variant::is_one() const {
  891. switch (type) {
  892. case NIL: {
  893. return true;
  894. }
  895. case BOOL: {
  896. return _data._bool;
  897. }
  898. case INT: {
  899. return _data._int == 1;
  900. }
  901. case FLOAT: {
  902. return _data._float == 1;
  903. }
  904. case VECTOR2: {
  905. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  906. }
  907. case VECTOR2I: {
  908. return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(1, 1);
  909. }
  910. case RECT2: {
  911. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  912. }
  913. case RECT2I: {
  914. return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(1, 1, 1, 1);
  915. }
  916. case VECTOR3: {
  917. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  918. }
  919. case VECTOR3I: {
  920. return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(1, 1, 1);
  921. }
  922. case VECTOR4: {
  923. return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(1, 1, 1, 1);
  924. }
  925. case VECTOR4I: {
  926. return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(1, 1, 1, 1);
  927. }
  928. case PLANE: {
  929. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  930. }
  931. case COLOR: {
  932. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  933. }
  934. default: {
  935. return !is_zero();
  936. }
  937. }
  938. }
  939. bool Variant::is_null() const {
  940. if (type == OBJECT && _get_obj().obj) {
  941. return false;
  942. } else {
  943. return true;
  944. }
  945. }
  946. void Variant::ObjData::ref(const ObjData &p_from) {
  947. // Mirrors Ref::ref in refcounted.h
  948. if (p_from.id == id) {
  949. return;
  950. }
  951. ObjData cleanup_ref = *this;
  952. *this = p_from;
  953. if (id.is_ref_counted()) {
  954. RefCounted *reference = static_cast<RefCounted *>(obj);
  955. // Assuming reference is not null because id.is_ref_counted() was true.
  956. if (!reference->reference()) {
  957. *this = ObjData();
  958. }
  959. }
  960. cleanup_ref.unref();
  961. }
  962. void Variant::ObjData::ref_pointer(Object *p_object) {
  963. // Mirrors Ref::ref_pointer in refcounted.h
  964. if (p_object == obj) {
  965. return;
  966. }
  967. ObjData cleanup_ref = *this;
  968. if (p_object) {
  969. *this = ObjData{ p_object->get_instance_id(), p_object };
  970. if (p_object->is_ref_counted()) {
  971. RefCounted *reference = static_cast<RefCounted *>(p_object);
  972. if (!reference->init_ref()) {
  973. *this = ObjData();
  974. }
  975. }
  976. } else {
  977. *this = ObjData();
  978. }
  979. cleanup_ref.unref();
  980. }
  981. void Variant::ObjData::unref() {
  982. // Mirrors Ref::unref in refcounted.h
  983. if (id.is_ref_counted()) {
  984. RefCounted *reference = static_cast<RefCounted *>(obj);
  985. // Assuming reference is not null because id.is_ref_counted() was true.
  986. if (reference->unreference()) {
  987. memdelete(reference);
  988. }
  989. }
  990. *this = ObjData();
  991. }
  992. void Variant::reference(const Variant &p_variant) {
  993. if (type == OBJECT && p_variant.type == OBJECT) {
  994. _get_obj().ref(p_variant._get_obj());
  995. return;
  996. }
  997. clear();
  998. type = p_variant.type;
  999. switch (p_variant.type) {
  1000. case NIL: {
  1001. // None.
  1002. } break;
  1003. // Atomic types.
  1004. case BOOL: {
  1005. _data._bool = p_variant._data._bool;
  1006. } break;
  1007. case INT: {
  1008. _data._int = p_variant._data._int;
  1009. } break;
  1010. case FLOAT: {
  1011. _data._float = p_variant._data._float;
  1012. } break;
  1013. case STRING: {
  1014. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  1015. } break;
  1016. // Math types.
  1017. case VECTOR2: {
  1018. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  1019. } break;
  1020. case VECTOR2I: {
  1021. memnew_placement(_data._mem, Vector2i(*reinterpret_cast<const Vector2i *>(p_variant._data._mem)));
  1022. } break;
  1023. case RECT2: {
  1024. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  1025. } break;
  1026. case RECT2I: {
  1027. memnew_placement(_data._mem, Rect2i(*reinterpret_cast<const Rect2i *>(p_variant._data._mem)));
  1028. } break;
  1029. case TRANSFORM2D: {
  1030. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  1031. memnew_placement(_data._transform2d, Transform2D(*p_variant._data._transform2d));
  1032. } break;
  1033. case VECTOR3: {
  1034. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  1035. } break;
  1036. case VECTOR3I: {
  1037. memnew_placement(_data._mem, Vector3i(*reinterpret_cast<const Vector3i *>(p_variant._data._mem)));
  1038. } break;
  1039. case VECTOR4: {
  1040. memnew_placement(_data._mem, Vector4(*reinterpret_cast<const Vector4 *>(p_variant._data._mem)));
  1041. } break;
  1042. case VECTOR4I: {
  1043. memnew_placement(_data._mem, Vector4i(*reinterpret_cast<const Vector4i *>(p_variant._data._mem)));
  1044. } break;
  1045. case PLANE: {
  1046. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  1047. } break;
  1048. case AABB: {
  1049. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  1050. memnew_placement(_data._aabb, ::AABB(*p_variant._data._aabb));
  1051. } break;
  1052. case QUATERNION: {
  1053. memnew_placement(_data._mem, Quaternion(*reinterpret_cast<const Quaternion *>(p_variant._data._mem)));
  1054. } break;
  1055. case BASIS: {
  1056. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  1057. memnew_placement(_data._basis, Basis(*p_variant._data._basis));
  1058. } break;
  1059. case TRANSFORM3D: {
  1060. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  1061. memnew_placement(_data._transform3d, Transform3D(*p_variant._data._transform3d));
  1062. } break;
  1063. case PROJECTION: {
  1064. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  1065. memnew_placement(_data._projection, Projection(*p_variant._data._projection));
  1066. } break;
  1067. // Miscellaneous types.
  1068. case COLOR: {
  1069. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  1070. } break;
  1071. case RID: {
  1072. memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem)));
  1073. } break;
  1074. case OBJECT: {
  1075. memnew_placement(_data._mem, ObjData);
  1076. _get_obj().ref(p_variant._get_obj());
  1077. } break;
  1078. case CALLABLE: {
  1079. memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem)));
  1080. } break;
  1081. case SIGNAL: {
  1082. memnew_placement(_data._mem, Signal(*reinterpret_cast<const Signal *>(p_variant._data._mem)));
  1083. } break;
  1084. case STRING_NAME: {
  1085. memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem)));
  1086. } break;
  1087. case NODE_PATH: {
  1088. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  1089. } break;
  1090. case DICTIONARY: {
  1091. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  1092. } break;
  1093. case ARRAY: {
  1094. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  1095. } break;
  1096. // Arrays.
  1097. case PACKED_BYTE_ARRAY: {
  1098. _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference();
  1099. if (!_data.packed_array) {
  1100. _data.packed_array = PackedArrayRef<uint8_t>::create();
  1101. }
  1102. } break;
  1103. case PACKED_INT32_ARRAY: {
  1104. _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference();
  1105. if (!_data.packed_array) {
  1106. _data.packed_array = PackedArrayRef<int32_t>::create();
  1107. }
  1108. } break;
  1109. case PACKED_INT64_ARRAY: {
  1110. _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference();
  1111. if (!_data.packed_array) {
  1112. _data.packed_array = PackedArrayRef<int64_t>::create();
  1113. }
  1114. } break;
  1115. case PACKED_FLOAT32_ARRAY: {
  1116. _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference();
  1117. if (!_data.packed_array) {
  1118. _data.packed_array = PackedArrayRef<float>::create();
  1119. }
  1120. } break;
  1121. case PACKED_FLOAT64_ARRAY: {
  1122. _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference();
  1123. if (!_data.packed_array) {
  1124. _data.packed_array = PackedArrayRef<double>::create();
  1125. }
  1126. } break;
  1127. case PACKED_STRING_ARRAY: {
  1128. _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference();
  1129. if (!_data.packed_array) {
  1130. _data.packed_array = PackedArrayRef<String>::create();
  1131. }
  1132. } break;
  1133. case PACKED_VECTOR2_ARRAY: {
  1134. _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference();
  1135. if (!_data.packed_array) {
  1136. _data.packed_array = PackedArrayRef<Vector2>::create();
  1137. }
  1138. } break;
  1139. case PACKED_VECTOR3_ARRAY: {
  1140. _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference();
  1141. if (!_data.packed_array) {
  1142. _data.packed_array = PackedArrayRef<Vector3>::create();
  1143. }
  1144. } break;
  1145. case PACKED_COLOR_ARRAY: {
  1146. _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference();
  1147. if (!_data.packed_array) {
  1148. _data.packed_array = PackedArrayRef<Color>::create();
  1149. }
  1150. } break;
  1151. case PACKED_VECTOR4_ARRAY: {
  1152. _data.packed_array = static_cast<PackedArrayRef<Vector4> *>(p_variant._data.packed_array)->reference();
  1153. if (!_data.packed_array) {
  1154. _data.packed_array = PackedArrayRef<Vector4>::create();
  1155. }
  1156. } break;
  1157. default: {
  1158. }
  1159. }
  1160. }
  1161. void Variant::zero() {
  1162. switch (type) {
  1163. case NIL:
  1164. break;
  1165. case BOOL:
  1166. _data._bool = false;
  1167. break;
  1168. case INT:
  1169. _data._int = 0;
  1170. break;
  1171. case FLOAT:
  1172. _data._float = 0;
  1173. break;
  1174. case VECTOR2:
  1175. *reinterpret_cast<Vector2 *>(_data._mem) = Vector2();
  1176. break;
  1177. case VECTOR2I:
  1178. *reinterpret_cast<Vector2i *>(_data._mem) = Vector2i();
  1179. break;
  1180. case RECT2:
  1181. *reinterpret_cast<Rect2 *>(_data._mem) = Rect2();
  1182. break;
  1183. case RECT2I:
  1184. *reinterpret_cast<Rect2i *>(_data._mem) = Rect2i();
  1185. break;
  1186. case VECTOR3:
  1187. *reinterpret_cast<Vector3 *>(_data._mem) = Vector3();
  1188. break;
  1189. case VECTOR3I:
  1190. *reinterpret_cast<Vector3i *>(_data._mem) = Vector3i();
  1191. break;
  1192. case VECTOR4:
  1193. *reinterpret_cast<Vector4 *>(_data._mem) = Vector4();
  1194. break;
  1195. case VECTOR4I:
  1196. *reinterpret_cast<Vector4i *>(_data._mem) = Vector4i();
  1197. break;
  1198. case PLANE:
  1199. *reinterpret_cast<Plane *>(_data._mem) = Plane();
  1200. break;
  1201. case QUATERNION:
  1202. *reinterpret_cast<Quaternion *>(_data._mem) = Quaternion();
  1203. break;
  1204. case COLOR:
  1205. *reinterpret_cast<Color *>(_data._mem) = Color();
  1206. break;
  1207. default:
  1208. Type prev_type = type;
  1209. clear();
  1210. if (type != prev_type) {
  1211. // clear() changes type to NIL, so it needs to be restored.
  1212. Callable::CallError ce;
  1213. Variant::construct(prev_type, *this, nullptr, 0, ce);
  1214. }
  1215. break;
  1216. }
  1217. }
  1218. void Variant::_clear_internal() {
  1219. switch (type) {
  1220. case STRING: {
  1221. reinterpret_cast<String *>(_data._mem)->~String();
  1222. } break;
  1223. // Math types.
  1224. case TRANSFORM2D: {
  1225. if (_data._transform2d) {
  1226. _data._transform2d->~Transform2D();
  1227. Pools::_bucket_small.free((Pools::BucketSmall *)_data._transform2d);
  1228. _data._transform2d = nullptr;
  1229. }
  1230. } break;
  1231. case AABB: {
  1232. if (_data._aabb) {
  1233. _data._aabb->~AABB();
  1234. Pools::_bucket_small.free((Pools::BucketSmall *)_data._aabb);
  1235. _data._aabb = nullptr;
  1236. }
  1237. } break;
  1238. case BASIS: {
  1239. if (_data._basis) {
  1240. _data._basis->~Basis();
  1241. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._basis);
  1242. _data._basis = nullptr;
  1243. }
  1244. } break;
  1245. case TRANSFORM3D: {
  1246. if (_data._transform3d) {
  1247. _data._transform3d->~Transform3D();
  1248. Pools::_bucket_medium.free((Pools::BucketMedium *)_data._transform3d);
  1249. _data._transform3d = nullptr;
  1250. }
  1251. } break;
  1252. case PROJECTION: {
  1253. if (_data._projection) {
  1254. _data._projection->~Projection();
  1255. Pools::_bucket_large.free((Pools::BucketLarge *)_data._projection);
  1256. _data._projection = nullptr;
  1257. }
  1258. } break;
  1259. // Miscellaneous types.
  1260. case STRING_NAME: {
  1261. reinterpret_cast<StringName *>(_data._mem)->~StringName();
  1262. } break;
  1263. case NODE_PATH: {
  1264. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  1265. } break;
  1266. case OBJECT: {
  1267. _get_obj().unref();
  1268. } break;
  1269. case RID: {
  1270. // Not much need probably.
  1271. // HACK: Can't seem to use destructor + scoping operator, so hack.
  1272. typedef ::RID RID_Class;
  1273. reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class();
  1274. } break;
  1275. case CALLABLE: {
  1276. reinterpret_cast<Callable *>(_data._mem)->~Callable();
  1277. } break;
  1278. case SIGNAL: {
  1279. reinterpret_cast<Signal *>(_data._mem)->~Signal();
  1280. } break;
  1281. case DICTIONARY: {
  1282. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  1283. } break;
  1284. case ARRAY: {
  1285. reinterpret_cast<Array *>(_data._mem)->~Array();
  1286. } break;
  1287. // Arrays.
  1288. case PACKED_BYTE_ARRAY: {
  1289. PackedArrayRefBase::destroy(_data.packed_array);
  1290. } break;
  1291. case PACKED_INT32_ARRAY: {
  1292. PackedArrayRefBase::destroy(_data.packed_array);
  1293. } break;
  1294. case PACKED_INT64_ARRAY: {
  1295. PackedArrayRefBase::destroy(_data.packed_array);
  1296. } break;
  1297. case PACKED_FLOAT32_ARRAY: {
  1298. PackedArrayRefBase::destroy(_data.packed_array);
  1299. } break;
  1300. case PACKED_FLOAT64_ARRAY: {
  1301. PackedArrayRefBase::destroy(_data.packed_array);
  1302. } break;
  1303. case PACKED_STRING_ARRAY: {
  1304. PackedArrayRefBase::destroy(_data.packed_array);
  1305. } break;
  1306. case PACKED_VECTOR2_ARRAY: {
  1307. PackedArrayRefBase::destroy(_data.packed_array);
  1308. } break;
  1309. case PACKED_VECTOR3_ARRAY: {
  1310. PackedArrayRefBase::destroy(_data.packed_array);
  1311. } break;
  1312. case PACKED_COLOR_ARRAY: {
  1313. PackedArrayRefBase::destroy(_data.packed_array);
  1314. } break;
  1315. case PACKED_VECTOR4_ARRAY: {
  1316. PackedArrayRefBase::destroy(_data.packed_array);
  1317. } break;
  1318. default: {
  1319. // Not needed, there is no point. The following do not allocate memory:
  1320. // VECTOR2, VECTOR3, VECTOR4, RECT2, PLANE, QUATERNION, COLOR.
  1321. }
  1322. }
  1323. }
  1324. Variant::operator int64_t() const {
  1325. return _to_int<int64_t>();
  1326. }
  1327. Variant::operator int32_t() const {
  1328. return _to_int<int32_t>();
  1329. }
  1330. Variant::operator int16_t() const {
  1331. return _to_int<int16_t>();
  1332. }
  1333. Variant::operator int8_t() const {
  1334. return _to_int<int8_t>();
  1335. }
  1336. Variant::operator uint64_t() const {
  1337. return _to_int<uint64_t>();
  1338. }
  1339. Variant::operator uint32_t() const {
  1340. return _to_int<uint32_t>();
  1341. }
  1342. Variant::operator uint16_t() const {
  1343. return _to_int<uint16_t>();
  1344. }
  1345. Variant::operator uint8_t() const {
  1346. return _to_int<uint8_t>();
  1347. }
  1348. Variant::operator ObjectID() const {
  1349. if (type == INT) {
  1350. return ObjectID(_data._int);
  1351. } else if (type == OBJECT) {
  1352. return _get_obj().id;
  1353. } else {
  1354. return ObjectID();
  1355. }
  1356. }
  1357. Variant::operator char32_t() const {
  1358. return operator uint32_t();
  1359. }
  1360. Variant::operator float() const {
  1361. return _to_float<float>();
  1362. }
  1363. Variant::operator double() const {
  1364. return _to_float<double>();
  1365. }
  1366. Variant::operator StringName() const {
  1367. if (type == STRING_NAME) {
  1368. return *reinterpret_cast<const StringName *>(_data._mem);
  1369. } else if (type == STRING) {
  1370. return *reinterpret_cast<const String *>(_data._mem);
  1371. }
  1372. return StringName();
  1373. }
  1374. struct _VariantStrPair {
  1375. String key;
  1376. String value;
  1377. bool operator<(const _VariantStrPair &p) const {
  1378. return key < p.key;
  1379. }
  1380. };
  1381. Variant::operator String() const {
  1382. return stringify(0);
  1383. }
  1384. String stringify_variant_clean(const Variant &p_variant, int recursion_count) {
  1385. String s = p_variant.stringify(recursion_count);
  1386. // Wrap strings in quotes to avoid ambiguity.
  1387. switch (p_variant.get_type()) {
  1388. case Variant::STRING: {
  1389. s = s.c_escape().quote();
  1390. } break;
  1391. case Variant::STRING_NAME: {
  1392. s = "&" + s.c_escape().quote();
  1393. } break;
  1394. case Variant::NODE_PATH: {
  1395. s = "^" + s.c_escape().quote();
  1396. } break;
  1397. default: {
  1398. } break;
  1399. }
  1400. return s;
  1401. }
  1402. template <typename T>
  1403. String stringify_vector(const T &vec, int recursion_count) {
  1404. String str("[");
  1405. for (int i = 0; i < vec.size(); i++) {
  1406. if (i > 0) {
  1407. str += ", ";
  1408. }
  1409. str += stringify_variant_clean(vec[i], recursion_count);
  1410. }
  1411. str += "]";
  1412. return str;
  1413. }
  1414. String Variant::stringify(int recursion_count) const {
  1415. switch (type) {
  1416. case NIL:
  1417. return "<null>";
  1418. case BOOL:
  1419. return _data._bool ? "true" : "false";
  1420. case INT:
  1421. return itos(_data._int);
  1422. case FLOAT:
  1423. return String::num_real(_data._float, true);
  1424. case STRING:
  1425. return *reinterpret_cast<const String *>(_data._mem);
  1426. case VECTOR2:
  1427. return operator Vector2();
  1428. case VECTOR2I:
  1429. return operator Vector2i();
  1430. case RECT2:
  1431. return operator Rect2();
  1432. case RECT2I:
  1433. return operator Rect2i();
  1434. case TRANSFORM2D:
  1435. return operator Transform2D();
  1436. case VECTOR3:
  1437. return operator Vector3();
  1438. case VECTOR3I:
  1439. return operator Vector3i();
  1440. case VECTOR4:
  1441. return operator Vector4();
  1442. case VECTOR4I:
  1443. return operator Vector4i();
  1444. case PLANE:
  1445. return operator Plane();
  1446. case AABB:
  1447. return operator ::AABB();
  1448. case QUATERNION:
  1449. return operator Quaternion();
  1450. case BASIS:
  1451. return operator Basis();
  1452. case TRANSFORM3D:
  1453. return operator Transform3D();
  1454. case PROJECTION:
  1455. return operator Projection();
  1456. case STRING_NAME:
  1457. return operator StringName();
  1458. case NODE_PATH:
  1459. return operator NodePath();
  1460. case COLOR:
  1461. return operator Color();
  1462. case DICTIONARY: {
  1463. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "{ ... }", "Maximum dictionary recursion reached!");
  1464. recursion_count++;
  1465. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1466. // Add leading and trailing space to Dictionary printing. This distinguishes it
  1467. // from array printing on fonts that have similar-looking {} and [] characters.
  1468. String str("{ ");
  1469. List<Variant> keys;
  1470. d.get_key_list(&keys);
  1471. Vector<_VariantStrPair> pairs;
  1472. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1473. _VariantStrPair sp;
  1474. sp.key = stringify_variant_clean(E->get(), recursion_count);
  1475. sp.value = stringify_variant_clean(d[E->get()], 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 c;
  1536. }
  1537. case SIGNAL: {
  1538. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  1539. return 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 Side() const {
  2050. return (Side) operator int();
  2051. }
  2052. Variant::operator Orientation() const {
  2053. return (Orientation) operator int();
  2054. }
  2055. Variant::operator IPAddress() const {
  2056. if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
  2057. Vector<int> addr = operator Vector<int>();
  2058. if (addr.size() == 4) {
  2059. return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  2060. }
  2061. }
  2062. return IPAddress(operator String());
  2063. }
  2064. Variant::Variant(bool p_bool) :
  2065. type(BOOL) {
  2066. _data._bool = p_bool;
  2067. }
  2068. Variant::Variant(int64_t p_int64) :
  2069. type(INT) {
  2070. _data._int = p_int64;
  2071. }
  2072. Variant::Variant(int32_t p_int32) :
  2073. type(INT) {
  2074. _data._int = p_int32;
  2075. }
  2076. Variant::Variant(int16_t p_int16) :
  2077. type(INT) {
  2078. _data._int = p_int16;
  2079. }
  2080. Variant::Variant(int8_t p_int8) :
  2081. type(INT) {
  2082. _data._int = p_int8;
  2083. }
  2084. Variant::Variant(uint64_t p_uint64) :
  2085. type(INT) {
  2086. _data._int = int64_t(p_uint64);
  2087. }
  2088. Variant::Variant(uint32_t p_uint32) :
  2089. type(INT) {
  2090. _data._int = int64_t(p_uint32);
  2091. }
  2092. Variant::Variant(uint16_t p_uint16) :
  2093. type(INT) {
  2094. _data._int = int64_t(p_uint16);
  2095. }
  2096. Variant::Variant(uint8_t p_uint8) :
  2097. type(INT) {
  2098. _data._int = int64_t(p_uint8);
  2099. }
  2100. Variant::Variant(float p_float) :
  2101. type(FLOAT) {
  2102. _data._float = p_float;
  2103. }
  2104. Variant::Variant(double p_double) :
  2105. type(FLOAT) {
  2106. _data._float = p_double;
  2107. }
  2108. Variant::Variant(const ObjectID &p_id) :
  2109. type(INT) {
  2110. _data._int = int64_t(p_id);
  2111. }
  2112. Variant::Variant(const StringName &p_string) :
  2113. type(STRING_NAME) {
  2114. memnew_placement(_data._mem, StringName(p_string));
  2115. static_assert(sizeof(StringName) <= sizeof(_data._mem));
  2116. }
  2117. Variant::Variant(const String &p_string) :
  2118. type(STRING) {
  2119. memnew_placement(_data._mem, String(p_string));
  2120. static_assert(sizeof(String) <= sizeof(_data._mem));
  2121. }
  2122. Variant::Variant(const char *const p_cstring) :
  2123. type(STRING) {
  2124. memnew_placement(_data._mem, String((const char *)p_cstring));
  2125. static_assert(sizeof(String) <= sizeof(_data._mem));
  2126. }
  2127. Variant::Variant(const char32_t *p_wstring) :
  2128. type(STRING) {
  2129. memnew_placement(_data._mem, String(p_wstring));
  2130. static_assert(sizeof(String) <= sizeof(_data._mem));
  2131. }
  2132. Variant::Variant(const Vector3 &p_vector3) :
  2133. type(VECTOR3) {
  2134. memnew_placement(_data._mem, Vector3(p_vector3));
  2135. static_assert(sizeof(Vector3) <= sizeof(_data._mem));
  2136. }
  2137. Variant::Variant(const Vector3i &p_vector3i) :
  2138. type(VECTOR3I) {
  2139. memnew_placement(_data._mem, Vector3i(p_vector3i));
  2140. static_assert(sizeof(Vector3i) <= sizeof(_data._mem));
  2141. }
  2142. Variant::Variant(const Vector4 &p_vector4) :
  2143. type(VECTOR4) {
  2144. memnew_placement(_data._mem, Vector4(p_vector4));
  2145. static_assert(sizeof(Vector4) <= sizeof(_data._mem));
  2146. }
  2147. Variant::Variant(const Vector4i &p_vector4i) :
  2148. type(VECTOR4I) {
  2149. memnew_placement(_data._mem, Vector4i(p_vector4i));
  2150. static_assert(sizeof(Vector4i) <= sizeof(_data._mem));
  2151. }
  2152. Variant::Variant(const Vector2 &p_vector2) :
  2153. type(VECTOR2) {
  2154. memnew_placement(_data._mem, Vector2(p_vector2));
  2155. static_assert(sizeof(Vector2) <= sizeof(_data._mem));
  2156. }
  2157. Variant::Variant(const Vector2i &p_vector2i) :
  2158. type(VECTOR2I) {
  2159. memnew_placement(_data._mem, Vector2i(p_vector2i));
  2160. static_assert(sizeof(Vector2i) <= sizeof(_data._mem));
  2161. }
  2162. Variant::Variant(const Rect2 &p_rect2) :
  2163. type(RECT2) {
  2164. memnew_placement(_data._mem, Rect2(p_rect2));
  2165. static_assert(sizeof(Rect2) <= sizeof(_data._mem));
  2166. }
  2167. Variant::Variant(const Rect2i &p_rect2i) :
  2168. type(RECT2I) {
  2169. memnew_placement(_data._mem, Rect2i(p_rect2i));
  2170. static_assert(sizeof(Rect2i) <= sizeof(_data._mem));
  2171. }
  2172. Variant::Variant(const Plane &p_plane) :
  2173. type(PLANE) {
  2174. memnew_placement(_data._mem, Plane(p_plane));
  2175. static_assert(sizeof(Plane) <= sizeof(_data._mem));
  2176. }
  2177. Variant::Variant(const ::AABB &p_aabb) :
  2178. type(AABB) {
  2179. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  2180. memnew_placement(_data._aabb, ::AABB(p_aabb));
  2181. }
  2182. Variant::Variant(const Basis &p_matrix) :
  2183. type(BASIS) {
  2184. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  2185. memnew_placement(_data._basis, Basis(p_matrix));
  2186. }
  2187. Variant::Variant(const Quaternion &p_quaternion) :
  2188. type(QUATERNION) {
  2189. memnew_placement(_data._mem, Quaternion(p_quaternion));
  2190. static_assert(sizeof(Quaternion) <= sizeof(_data._mem));
  2191. }
  2192. Variant::Variant(const Transform3D &p_transform) :
  2193. type(TRANSFORM3D) {
  2194. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  2195. memnew_placement(_data._transform3d, Transform3D(p_transform));
  2196. }
  2197. Variant::Variant(const Projection &pp_projection) :
  2198. type(PROJECTION) {
  2199. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  2200. memnew_placement(_data._projection, Projection(pp_projection));
  2201. }
  2202. Variant::Variant(const Transform2D &p_transform) :
  2203. type(TRANSFORM2D) {
  2204. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  2205. memnew_placement(_data._transform2d, Transform2D(p_transform));
  2206. }
  2207. Variant::Variant(const Color &p_color) :
  2208. type(COLOR) {
  2209. memnew_placement(_data._mem, Color(p_color));
  2210. static_assert(sizeof(Color) <= sizeof(_data._mem));
  2211. }
  2212. Variant::Variant(const NodePath &p_node_path) :
  2213. type(NODE_PATH) {
  2214. memnew_placement(_data._mem, NodePath(p_node_path));
  2215. static_assert(sizeof(NodePath) <= sizeof(_data._mem));
  2216. }
  2217. Variant::Variant(const ::RID &p_rid) :
  2218. type(RID) {
  2219. memnew_placement(_data._mem, ::RID(p_rid));
  2220. static_assert(sizeof(::RID) <= sizeof(_data._mem));
  2221. }
  2222. Variant::Variant(const Object *p_object) :
  2223. type(OBJECT) {
  2224. _get_obj() = ObjData();
  2225. _get_obj().ref_pointer(const_cast<Object *>(p_object));
  2226. }
  2227. Variant::Variant(const Callable &p_callable) :
  2228. type(CALLABLE) {
  2229. memnew_placement(_data._mem, Callable(p_callable));
  2230. static_assert(sizeof(Callable) <= sizeof(_data._mem));
  2231. }
  2232. Variant::Variant(const Signal &p_callable) :
  2233. type(SIGNAL) {
  2234. memnew_placement(_data._mem, Signal(p_callable));
  2235. static_assert(sizeof(Signal) <= sizeof(_data._mem));
  2236. }
  2237. Variant::Variant(const Dictionary &p_dictionary) :
  2238. type(DICTIONARY) {
  2239. memnew_placement(_data._mem, Dictionary(p_dictionary));
  2240. static_assert(sizeof(Dictionary) <= sizeof(_data._mem));
  2241. }
  2242. Variant::Variant(const Array &p_array) :
  2243. type(ARRAY) {
  2244. memnew_placement(_data._mem, Array(p_array));
  2245. static_assert(sizeof(Array) <= sizeof(_data._mem));
  2246. }
  2247. Variant::Variant(const PackedByteArray &p_byte_array) :
  2248. type(PACKED_BYTE_ARRAY) {
  2249. _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
  2250. }
  2251. Variant::Variant(const PackedInt32Array &p_int32_array) :
  2252. type(PACKED_INT32_ARRAY) {
  2253. _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
  2254. }
  2255. Variant::Variant(const PackedInt64Array &p_int64_array) :
  2256. type(PACKED_INT64_ARRAY) {
  2257. _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
  2258. }
  2259. Variant::Variant(const PackedFloat32Array &p_float32_array) :
  2260. type(PACKED_FLOAT32_ARRAY) {
  2261. _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
  2262. }
  2263. Variant::Variant(const PackedFloat64Array &p_float64_array) :
  2264. type(PACKED_FLOAT64_ARRAY) {
  2265. _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
  2266. }
  2267. Variant::Variant(const PackedStringArray &p_string_array) :
  2268. type(PACKED_STRING_ARRAY) {
  2269. _data.packed_array = PackedArrayRef<String>::create(p_string_array);
  2270. }
  2271. Variant::Variant(const PackedVector2Array &p_vector2_array) :
  2272. type(PACKED_VECTOR2_ARRAY) {
  2273. _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
  2274. }
  2275. Variant::Variant(const PackedVector3Array &p_vector3_array) :
  2276. type(PACKED_VECTOR3_ARRAY) {
  2277. _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
  2278. }
  2279. Variant::Variant(const PackedColorArray &p_color_array) :
  2280. type(PACKED_COLOR_ARRAY) {
  2281. _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
  2282. }
  2283. Variant::Variant(const PackedVector4Array &p_vector4_array) :
  2284. type(PACKED_VECTOR4_ARRAY) {
  2285. _data.packed_array = PackedArrayRef<Vector4>::create(p_vector4_array);
  2286. }
  2287. /* helpers */
  2288. Variant::Variant(const Vector<::RID> &p_array) :
  2289. type(ARRAY) {
  2290. Array *rid_array = memnew_placement(_data._mem, Array);
  2291. rid_array->resize(p_array.size());
  2292. for (int i = 0; i < p_array.size(); i++) {
  2293. rid_array->set(i, Variant(p_array[i]));
  2294. }
  2295. }
  2296. Variant::Variant(const Vector<Plane> &p_array) :
  2297. type(ARRAY) {
  2298. Array *plane_array = memnew_placement(_data._mem, Array);
  2299. plane_array->resize(p_array.size());
  2300. for (int i = 0; i < p_array.size(); i++) {
  2301. plane_array->operator[](i) = Variant(p_array[i]);
  2302. }
  2303. }
  2304. Variant::Variant(const Vector<Face3> &p_face_array) {
  2305. PackedVector3Array vertices;
  2306. int face_count = p_face_array.size();
  2307. vertices.resize(face_count * 3);
  2308. if (face_count) {
  2309. const Face3 *r = p_face_array.ptr();
  2310. Vector3 *w = vertices.ptrw();
  2311. for (int i = 0; i < face_count; i++) {
  2312. for (int j = 0; j < 3; j++) {
  2313. w[i * 3 + j] = r[i].vertex[j];
  2314. }
  2315. }
  2316. }
  2317. *this = vertices;
  2318. }
  2319. Variant::Variant(const Vector<Variant> &p_array) {
  2320. Array arr;
  2321. arr.resize(p_array.size());
  2322. for (int i = 0; i < p_array.size(); i++) {
  2323. arr[i] = p_array[i];
  2324. }
  2325. *this = arr;
  2326. }
  2327. Variant::Variant(const Vector<StringName> &p_array) {
  2328. PackedStringArray v;
  2329. int len = p_array.size();
  2330. v.resize(len);
  2331. for (int i = 0; i < len; i++) {
  2332. v.set(i, p_array[i]);
  2333. }
  2334. *this = v;
  2335. }
  2336. void Variant::operator=(const Variant &p_variant) {
  2337. if (unlikely(this == &p_variant)) {
  2338. return;
  2339. }
  2340. if (unlikely(type != p_variant.type)) {
  2341. reference(p_variant);
  2342. return;
  2343. }
  2344. switch (p_variant.type) {
  2345. case NIL: {
  2346. // none
  2347. } break;
  2348. // atomic types
  2349. case BOOL: {
  2350. _data._bool = p_variant._data._bool;
  2351. } break;
  2352. case INT: {
  2353. _data._int = p_variant._data._int;
  2354. } break;
  2355. case FLOAT: {
  2356. _data._float = p_variant._data._float;
  2357. } break;
  2358. case STRING: {
  2359. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2360. } break;
  2361. // math types
  2362. case VECTOR2: {
  2363. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2364. } break;
  2365. case VECTOR2I: {
  2366. *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2367. } break;
  2368. case RECT2: {
  2369. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2370. } break;
  2371. case RECT2I: {
  2372. *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2373. } break;
  2374. case TRANSFORM2D: {
  2375. *_data._transform2d = *(p_variant._data._transform2d);
  2376. } break;
  2377. case VECTOR3: {
  2378. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2379. } break;
  2380. case VECTOR3I: {
  2381. *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2382. } break;
  2383. case VECTOR4: {
  2384. *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2385. } break;
  2386. case VECTOR4I: {
  2387. *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2388. } break;
  2389. case PLANE: {
  2390. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2391. } break;
  2392. case AABB: {
  2393. *_data._aabb = *(p_variant._data._aabb);
  2394. } break;
  2395. case QUATERNION: {
  2396. *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2397. } break;
  2398. case BASIS: {
  2399. *_data._basis = *(p_variant._data._basis);
  2400. } break;
  2401. case TRANSFORM3D: {
  2402. *_data._transform3d = *(p_variant._data._transform3d);
  2403. } break;
  2404. case PROJECTION: {
  2405. *_data._projection = *(p_variant._data._projection);
  2406. } break;
  2407. // misc types
  2408. case COLOR: {
  2409. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2410. } break;
  2411. case RID: {
  2412. *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
  2413. } break;
  2414. case OBJECT: {
  2415. _get_obj().ref(p_variant._get_obj());
  2416. } break;
  2417. case CALLABLE: {
  2418. *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
  2419. } break;
  2420. case SIGNAL: {
  2421. *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
  2422. } break;
  2423. case STRING_NAME: {
  2424. *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2425. } break;
  2426. case NODE_PATH: {
  2427. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2428. } break;
  2429. case DICTIONARY: {
  2430. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2431. } break;
  2432. case ARRAY: {
  2433. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2434. } break;
  2435. // arrays
  2436. case PACKED_BYTE_ARRAY: {
  2437. _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2438. } break;
  2439. case PACKED_INT32_ARRAY: {
  2440. _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2441. } break;
  2442. case PACKED_INT64_ARRAY: {
  2443. _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2444. } break;
  2445. case PACKED_FLOAT32_ARRAY: {
  2446. _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2447. } break;
  2448. case PACKED_FLOAT64_ARRAY: {
  2449. _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2450. } break;
  2451. case PACKED_STRING_ARRAY: {
  2452. _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2453. } break;
  2454. case PACKED_VECTOR2_ARRAY: {
  2455. _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2456. } break;
  2457. case PACKED_VECTOR3_ARRAY: {
  2458. _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2459. } break;
  2460. case PACKED_COLOR_ARRAY: {
  2461. _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2462. } break;
  2463. case PACKED_VECTOR4_ARRAY: {
  2464. _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2465. } break;
  2466. default: {
  2467. }
  2468. }
  2469. }
  2470. Variant::Variant(const IPAddress &p_address) :
  2471. type(STRING) {
  2472. memnew_placement(_data._mem, String(p_address));
  2473. }
  2474. Variant::Variant(const Variant &p_variant) {
  2475. reference(p_variant);
  2476. }
  2477. uint32_t Variant::hash() const {
  2478. return recursive_hash(0);
  2479. }
  2480. uint32_t Variant::recursive_hash(int recursion_count) const {
  2481. switch (type) {
  2482. case NIL: {
  2483. return 0;
  2484. } break;
  2485. case BOOL: {
  2486. return _data._bool ? 1 : 0;
  2487. } break;
  2488. case INT: {
  2489. return hash_one_uint64((uint64_t)_data._int);
  2490. } break;
  2491. case FLOAT: {
  2492. return hash_murmur3_one_double(_data._float);
  2493. } break;
  2494. case STRING: {
  2495. return reinterpret_cast<const String *>(_data._mem)->hash();
  2496. } break;
  2497. // math types
  2498. case VECTOR2: {
  2499. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
  2500. } break;
  2501. case VECTOR2I: {
  2502. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
  2503. } break;
  2504. case RECT2: {
  2505. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
  2506. } break;
  2507. case RECT2I: {
  2508. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
  2509. } break;
  2510. case TRANSFORM2D: {
  2511. uint32_t h = HASH_MURMUR3_SEED;
  2512. const Transform2D &t = *_data._transform2d;
  2513. h = hash_murmur3_one_real(t[0].x, h);
  2514. h = hash_murmur3_one_real(t[0].y, h);
  2515. h = hash_murmur3_one_real(t[1].x, h);
  2516. h = hash_murmur3_one_real(t[1].y, h);
  2517. h = hash_murmur3_one_real(t[2].x, h);
  2518. h = hash_murmur3_one_real(t[2].y, h);
  2519. return hash_fmix32(h);
  2520. } break;
  2521. case VECTOR3: {
  2522. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
  2523. } break;
  2524. case VECTOR3I: {
  2525. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
  2526. } break;
  2527. case VECTOR4: {
  2528. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
  2529. } break;
  2530. case VECTOR4I: {
  2531. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
  2532. } break;
  2533. case PLANE: {
  2534. uint32_t h = HASH_MURMUR3_SEED;
  2535. const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
  2536. h = hash_murmur3_one_real(p.normal.x, h);
  2537. h = hash_murmur3_one_real(p.normal.y, h);
  2538. h = hash_murmur3_one_real(p.normal.z, h);
  2539. h = hash_murmur3_one_real(p.d, h);
  2540. return hash_fmix32(h);
  2541. } break;
  2542. case AABB: {
  2543. return HashMapHasherDefault::hash(*_data._aabb);
  2544. } break;
  2545. case QUATERNION: {
  2546. uint32_t h = HASH_MURMUR3_SEED;
  2547. const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
  2548. h = hash_murmur3_one_real(q.x, h);
  2549. h = hash_murmur3_one_real(q.y, h);
  2550. h = hash_murmur3_one_real(q.z, h);
  2551. h = hash_murmur3_one_real(q.w, h);
  2552. return hash_fmix32(h);
  2553. } break;
  2554. case BASIS: {
  2555. uint32_t h = HASH_MURMUR3_SEED;
  2556. const Basis &b = *_data._basis;
  2557. h = hash_murmur3_one_real(b[0].x, h);
  2558. h = hash_murmur3_one_real(b[0].y, h);
  2559. h = hash_murmur3_one_real(b[0].z, h);
  2560. h = hash_murmur3_one_real(b[1].x, h);
  2561. h = hash_murmur3_one_real(b[1].y, h);
  2562. h = hash_murmur3_one_real(b[1].z, h);
  2563. h = hash_murmur3_one_real(b[2].x, h);
  2564. h = hash_murmur3_one_real(b[2].y, h);
  2565. h = hash_murmur3_one_real(b[2].z, h);
  2566. return hash_fmix32(h);
  2567. } break;
  2568. case TRANSFORM3D: {
  2569. uint32_t h = HASH_MURMUR3_SEED;
  2570. const Transform3D &t = *_data._transform3d;
  2571. h = hash_murmur3_one_real(t.basis[0].x, h);
  2572. h = hash_murmur3_one_real(t.basis[0].y, h);
  2573. h = hash_murmur3_one_real(t.basis[0].z, h);
  2574. h = hash_murmur3_one_real(t.basis[1].x, h);
  2575. h = hash_murmur3_one_real(t.basis[1].y, h);
  2576. h = hash_murmur3_one_real(t.basis[1].z, h);
  2577. h = hash_murmur3_one_real(t.basis[2].x, h);
  2578. h = hash_murmur3_one_real(t.basis[2].y, h);
  2579. h = hash_murmur3_one_real(t.basis[2].z, h);
  2580. h = hash_murmur3_one_real(t.origin.x, h);
  2581. h = hash_murmur3_one_real(t.origin.y, h);
  2582. h = hash_murmur3_one_real(t.origin.z, h);
  2583. return hash_fmix32(h);
  2584. } break;
  2585. case PROJECTION: {
  2586. uint32_t h = HASH_MURMUR3_SEED;
  2587. const Projection &t = *_data._projection;
  2588. h = hash_murmur3_one_real(t.columns[0].x, h);
  2589. h = hash_murmur3_one_real(t.columns[0].y, h);
  2590. h = hash_murmur3_one_real(t.columns[0].z, h);
  2591. h = hash_murmur3_one_real(t.columns[0].w, h);
  2592. h = hash_murmur3_one_real(t.columns[1].x, h);
  2593. h = hash_murmur3_one_real(t.columns[1].y, h);
  2594. h = hash_murmur3_one_real(t.columns[1].z, h);
  2595. h = hash_murmur3_one_real(t.columns[1].w, h);
  2596. h = hash_murmur3_one_real(t.columns[2].x, h);
  2597. h = hash_murmur3_one_real(t.columns[2].y, h);
  2598. h = hash_murmur3_one_real(t.columns[2].z, h);
  2599. h = hash_murmur3_one_real(t.columns[2].w, h);
  2600. h = hash_murmur3_one_real(t.columns[3].x, h);
  2601. h = hash_murmur3_one_real(t.columns[3].y, h);
  2602. h = hash_murmur3_one_real(t.columns[3].z, h);
  2603. h = hash_murmur3_one_real(t.columns[3].w, h);
  2604. return hash_fmix32(h);
  2605. } break;
  2606. // misc types
  2607. case COLOR: {
  2608. uint32_t h = HASH_MURMUR3_SEED;
  2609. const Color &c = *reinterpret_cast<const Color *>(_data._mem);
  2610. h = hash_murmur3_one_float(c.r, h);
  2611. h = hash_murmur3_one_float(c.g, h);
  2612. h = hash_murmur3_one_float(c.b, h);
  2613. h = hash_murmur3_one_float(c.a, h);
  2614. return hash_fmix32(h);
  2615. } break;
  2616. case RID: {
  2617. return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
  2618. } break;
  2619. case OBJECT: {
  2620. return hash_one_uint64(hash_make_uint64_t(_get_obj().obj));
  2621. } break;
  2622. case STRING_NAME: {
  2623. return reinterpret_cast<const StringName *>(_data._mem)->hash();
  2624. } break;
  2625. case NODE_PATH: {
  2626. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2627. } break;
  2628. case DICTIONARY: {
  2629. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
  2630. } break;
  2631. case CALLABLE: {
  2632. return reinterpret_cast<const Callable *>(_data._mem)->hash();
  2633. } break;
  2634. case SIGNAL: {
  2635. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  2636. uint32_t hash = s.get_name().hash();
  2637. return hash_murmur3_one_64(s.get_object_id(), hash);
  2638. } break;
  2639. case ARRAY: {
  2640. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2641. return arr.recursive_hash(recursion_count);
  2642. } break;
  2643. case PACKED_BYTE_ARRAY: {
  2644. const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  2645. int len = arr.size();
  2646. if (likely(len)) {
  2647. const uint8_t *r = arr.ptr();
  2648. return hash_murmur3_buffer((uint8_t *)&r[0], len);
  2649. } else {
  2650. return hash_murmur3_one_64(0);
  2651. }
  2652. } break;
  2653. case PACKED_INT32_ARRAY: {
  2654. const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
  2655. int len = arr.size();
  2656. if (likely(len)) {
  2657. const int32_t *r = arr.ptr();
  2658. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
  2659. } else {
  2660. return hash_murmur3_one_64(0);
  2661. }
  2662. } break;
  2663. case PACKED_INT64_ARRAY: {
  2664. const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
  2665. int len = arr.size();
  2666. if (likely(len)) {
  2667. const int64_t *r = arr.ptr();
  2668. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
  2669. } else {
  2670. return hash_murmur3_one_64(0);
  2671. }
  2672. } break;
  2673. case PACKED_FLOAT32_ARRAY: {
  2674. const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
  2675. int len = arr.size();
  2676. if (likely(len)) {
  2677. const float *r = arr.ptr();
  2678. uint32_t h = HASH_MURMUR3_SEED;
  2679. for (int32_t i = 0; i < len; i++) {
  2680. h = hash_murmur3_one_float(r[i], h);
  2681. }
  2682. return hash_fmix32(h);
  2683. } else {
  2684. return hash_murmur3_one_float(0.0);
  2685. }
  2686. } break;
  2687. case PACKED_FLOAT64_ARRAY: {
  2688. const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
  2689. int len = arr.size();
  2690. if (likely(len)) {
  2691. const double *r = arr.ptr();
  2692. uint32_t h = HASH_MURMUR3_SEED;
  2693. for (int32_t i = 0; i < len; i++) {
  2694. h = hash_murmur3_one_double(r[i], h);
  2695. }
  2696. return hash_fmix32(h);
  2697. } else {
  2698. return hash_murmur3_one_double(0.0);
  2699. }
  2700. } break;
  2701. case PACKED_STRING_ARRAY: {
  2702. uint32_t hash = HASH_MURMUR3_SEED;
  2703. const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
  2704. int len = arr.size();
  2705. if (likely(len)) {
  2706. const String *r = arr.ptr();
  2707. for (int i = 0; i < len; i++) {
  2708. hash = hash_murmur3_one_32(r[i].hash(), hash);
  2709. }
  2710. hash = hash_fmix32(hash);
  2711. }
  2712. return hash;
  2713. } break;
  2714. case PACKED_VECTOR2_ARRAY: {
  2715. uint32_t hash = HASH_MURMUR3_SEED;
  2716. const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
  2717. int len = arr.size();
  2718. if (likely(len)) {
  2719. const Vector2 *r = arr.ptr();
  2720. for (int i = 0; i < len; i++) {
  2721. hash = hash_murmur3_one_real(r[i].x, hash);
  2722. hash = hash_murmur3_one_real(r[i].y, hash);
  2723. }
  2724. hash = hash_fmix32(hash);
  2725. }
  2726. return hash;
  2727. } break;
  2728. case PACKED_VECTOR3_ARRAY: {
  2729. uint32_t hash = HASH_MURMUR3_SEED;
  2730. const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
  2731. int len = arr.size();
  2732. if (likely(len)) {
  2733. const Vector3 *r = arr.ptr();
  2734. for (int i = 0; i < len; i++) {
  2735. hash = hash_murmur3_one_real(r[i].x, hash);
  2736. hash = hash_murmur3_one_real(r[i].y, hash);
  2737. hash = hash_murmur3_one_real(r[i].z, hash);
  2738. }
  2739. hash = hash_fmix32(hash);
  2740. }
  2741. return hash;
  2742. } break;
  2743. case PACKED_COLOR_ARRAY: {
  2744. uint32_t hash = HASH_MURMUR3_SEED;
  2745. const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
  2746. int len = arr.size();
  2747. if (likely(len)) {
  2748. const Color *r = arr.ptr();
  2749. for (int i = 0; i < len; i++) {
  2750. hash = hash_murmur3_one_float(r[i].r, hash);
  2751. hash = hash_murmur3_one_float(r[i].g, hash);
  2752. hash = hash_murmur3_one_float(r[i].b, hash);
  2753. hash = hash_murmur3_one_float(r[i].a, hash);
  2754. }
  2755. hash = hash_fmix32(hash);
  2756. }
  2757. return hash;
  2758. } break;
  2759. case PACKED_VECTOR4_ARRAY: {
  2760. uint32_t hash = HASH_MURMUR3_SEED;
  2761. const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
  2762. int len = arr.size();
  2763. if (likely(len)) {
  2764. const Vector4 *r = arr.ptr();
  2765. for (int i = 0; i < len; i++) {
  2766. hash = hash_murmur3_one_real(r[i].x, hash);
  2767. hash = hash_murmur3_one_real(r[i].y, hash);
  2768. hash = hash_murmur3_one_real(r[i].z, hash);
  2769. hash = hash_murmur3_one_real(r[i].w, hash);
  2770. }
  2771. hash = hash_fmix32(hash);
  2772. }
  2773. return hash;
  2774. } break;
  2775. default: {
  2776. }
  2777. }
  2778. return 0;
  2779. }
  2780. #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
  2781. (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2782. #define hash_compare_scalar(p_lhs, p_rhs) \
  2783. (hash_compare_scalar_base(p_lhs, p_rhs, true))
  2784. #define hash_compare_vector2(p_lhs, p_rhs) \
  2785. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2786. hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2787. #define hash_compare_vector3(p_lhs, p_rhs) \
  2788. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2789. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2790. hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2791. #define hash_compare_vector4(p_lhs, p_rhs) \
  2792. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2793. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2794. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2795. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2796. #define hash_compare_quaternion(p_lhs, p_rhs) \
  2797. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2798. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2799. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2800. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2801. #define hash_compare_color(p_lhs, p_rhs) \
  2802. (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
  2803. hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
  2804. hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
  2805. hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2806. #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2807. const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
  2808. const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
  2809. \
  2810. if (l.size() != r.size()) \
  2811. return false; \
  2812. \
  2813. const p_type *lr = l.ptr(); \
  2814. const p_type *rr = r.ptr(); \
  2815. \
  2816. for (int i = 0; i < l.size(); ++i) { \
  2817. if (!p_compare_func((lr[i]), (rr[i]))) \
  2818. return false; \
  2819. } \
  2820. \
  2821. return true
  2822. bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
  2823. if (type != p_variant.type) {
  2824. return false;
  2825. }
  2826. switch (type) {
  2827. case INT: {
  2828. return _data._int == p_variant._data._int;
  2829. } break;
  2830. case FLOAT: {
  2831. return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
  2832. } break;
  2833. case STRING: {
  2834. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2835. } break;
  2836. case STRING_NAME: {
  2837. return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2838. } break;
  2839. case VECTOR2: {
  2840. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2841. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2842. return hash_compare_vector2(*l, *r);
  2843. } break;
  2844. case VECTOR2I: {
  2845. const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
  2846. const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2847. return *l == *r;
  2848. } break;
  2849. case RECT2: {
  2850. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2851. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2852. return hash_compare_vector2(l->position, r->position) &&
  2853. hash_compare_vector2(l->size, r->size);
  2854. } break;
  2855. case RECT2I: {
  2856. const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
  2857. const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2858. return *l == *r;
  2859. } break;
  2860. case TRANSFORM2D: {
  2861. Transform2D *l = _data._transform2d;
  2862. Transform2D *r = p_variant._data._transform2d;
  2863. for (int i = 0; i < 3; i++) {
  2864. if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
  2865. return false;
  2866. }
  2867. }
  2868. return true;
  2869. } break;
  2870. case VECTOR3: {
  2871. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2872. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2873. return hash_compare_vector3(*l, *r);
  2874. } break;
  2875. case VECTOR3I: {
  2876. const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
  2877. const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2878. return *l == *r;
  2879. } break;
  2880. case VECTOR4: {
  2881. const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
  2882. const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2883. return hash_compare_vector4(*l, *r);
  2884. } break;
  2885. case VECTOR4I: {
  2886. const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
  2887. const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2888. return *l == *r;
  2889. } break;
  2890. case PLANE: {
  2891. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2892. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2893. return hash_compare_vector3(l->normal, r->normal) &&
  2894. hash_compare_scalar(l->d, r->d);
  2895. } break;
  2896. case AABB: {
  2897. const ::AABB *l = _data._aabb;
  2898. const ::AABB *r = p_variant._data._aabb;
  2899. return hash_compare_vector3(l->position, r->position) &&
  2900. hash_compare_vector3(l->size, r->size);
  2901. } break;
  2902. case QUATERNION: {
  2903. const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
  2904. const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2905. return hash_compare_quaternion(*l, *r);
  2906. } break;
  2907. case BASIS: {
  2908. const Basis *l = _data._basis;
  2909. const Basis *r = p_variant._data._basis;
  2910. for (int i = 0; i < 3; i++) {
  2911. if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
  2912. return false;
  2913. }
  2914. }
  2915. return true;
  2916. } break;
  2917. case TRANSFORM3D: {
  2918. const Transform3D *l = _data._transform3d;
  2919. const Transform3D *r = p_variant._data._transform3d;
  2920. for (int i = 0; i < 3; i++) {
  2921. if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
  2922. return false;
  2923. }
  2924. }
  2925. return hash_compare_vector3(l->origin, r->origin);
  2926. } break;
  2927. case PROJECTION: {
  2928. const Projection *l = _data._projection;
  2929. const Projection *r = p_variant._data._projection;
  2930. for (int i = 0; i < 4; i++) {
  2931. if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
  2932. return false;
  2933. }
  2934. }
  2935. return true;
  2936. } break;
  2937. case COLOR: {
  2938. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2939. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2940. return hash_compare_color(*l, *r);
  2941. } break;
  2942. case ARRAY: {
  2943. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2944. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2945. if (!l.recursive_equal(r, recursion_count + 1)) {
  2946. return false;
  2947. }
  2948. return true;
  2949. } break;
  2950. case DICTIONARY: {
  2951. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  2952. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  2953. if (!l.recursive_equal(r, recursion_count + 1)) {
  2954. return false;
  2955. }
  2956. return true;
  2957. } break;
  2958. // This is for floating point comparisons only.
  2959. case PACKED_FLOAT32_ARRAY: {
  2960. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
  2961. } break;
  2962. case PACKED_FLOAT64_ARRAY: {
  2963. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
  2964. } break;
  2965. case PACKED_VECTOR2_ARRAY: {
  2966. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
  2967. } break;
  2968. case PACKED_VECTOR3_ARRAY: {
  2969. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
  2970. } break;
  2971. case PACKED_COLOR_ARRAY: {
  2972. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
  2973. } break;
  2974. case PACKED_VECTOR4_ARRAY: {
  2975. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
  2976. } break;
  2977. default:
  2978. bool v;
  2979. Variant r;
  2980. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2981. return r;
  2982. }
  2983. }
  2984. bool Variant::identity_compare(const Variant &p_variant) const {
  2985. if (type != p_variant.type) {
  2986. return false;
  2987. }
  2988. switch (type) {
  2989. case OBJECT: {
  2990. return _get_obj().id == p_variant._get_obj().id;
  2991. } break;
  2992. case DICTIONARY: {
  2993. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  2994. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  2995. return l.id() == r.id();
  2996. } break;
  2997. case ARRAY: {
  2998. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2999. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3000. return l.id() == r.id();
  3001. } break;
  3002. case PACKED_BYTE_ARRAY:
  3003. case PACKED_INT32_ARRAY:
  3004. case PACKED_INT64_ARRAY:
  3005. case PACKED_FLOAT32_ARRAY:
  3006. case PACKED_FLOAT64_ARRAY:
  3007. case PACKED_STRING_ARRAY:
  3008. case PACKED_VECTOR2_ARRAY:
  3009. case PACKED_VECTOR3_ARRAY:
  3010. case PACKED_COLOR_ARRAY:
  3011. case PACKED_VECTOR4_ARRAY: {
  3012. return _data.packed_array == p_variant._data.packed_array;
  3013. } break;
  3014. default: {
  3015. return hash_compare(p_variant);
  3016. }
  3017. }
  3018. }
  3019. bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
  3020. if (p_lhs.hash_compare(p_rhs)) {
  3021. return true;
  3022. }
  3023. if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
  3024. return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
  3025. }
  3026. if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
  3027. return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
  3028. }
  3029. return false;
  3030. }
  3031. bool Variant::is_ref_counted() const {
  3032. return type == OBJECT && _get_obj().id.is_ref_counted();
  3033. }
  3034. bool Variant::is_type_shared(Variant::Type p_type) {
  3035. switch (p_type) {
  3036. case OBJECT:
  3037. case ARRAY:
  3038. case DICTIONARY:
  3039. return true;
  3040. default: {
  3041. }
  3042. }
  3043. return false;
  3044. }
  3045. bool Variant::is_shared() const {
  3046. return is_type_shared(type);
  3047. }
  3048. bool Variant::is_read_only() const {
  3049. switch (type) {
  3050. case ARRAY:
  3051. return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
  3052. case DICTIONARY:
  3053. return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
  3054. default:
  3055. return false;
  3056. }
  3057. }
  3058. void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
  3059. switch (error.error) {
  3060. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  3061. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
  3062. ERR_PRINT(err.utf8().get_data());
  3063. } break;
  3064. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  3065. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  3066. ERR_PRINT(err.utf8().get_data());
  3067. } break;
  3068. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  3069. String err = "Too many arguments for method '" + p_method + "'";
  3070. ERR_PRINT(err.utf8().get_data());
  3071. } break;
  3072. default: {
  3073. }
  3074. }
  3075. }
  3076. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  3077. r_value = Variant();
  3078. }
  3079. String Variant::get_construct_string() const {
  3080. String vars;
  3081. VariantWriter::write_to_string(*this, vars);
  3082. return vars;
  3083. }
  3084. String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3085. return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
  3086. }
  3087. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3088. String err_text;
  3089. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  3090. int errorarg = ce.argument;
  3091. if (p_argptrs) {
  3092. 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));
  3093. } else {
  3094. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
  3095. }
  3096. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  3097. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3098. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  3099. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3100. } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  3101. err_text = "Method not found";
  3102. } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  3103. err_text = "Instance is null";
  3104. } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
  3105. err_text = "Method not const in const instance";
  3106. } else if (ce.error == Callable::CallError::CALL_OK) {
  3107. return "Call OK";
  3108. }
  3109. String base_text;
  3110. if (p_base) {
  3111. base_text = p_base->get_class();
  3112. Ref<Resource> script = p_base->get_script();
  3113. if (script.is_valid() && script->get_path().is_resource_file()) {
  3114. base_text += "(" + script->get_path().get_file() + ")";
  3115. }
  3116. base_text += "::";
  3117. }
  3118. return "'" + base_text + String(p_method) + "': " + err_text;
  3119. }
  3120. String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3121. Vector<Variant> binds;
  3122. p_callable.get_bound_arguments_ref(binds);
  3123. int args_unbound = p_callable.get_unbound_arguments_count();
  3124. if (p_argcount - args_unbound < 0) {
  3125. return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
  3126. } else {
  3127. Vector<const Variant *> argptrs;
  3128. argptrs.resize(p_argcount - args_unbound + binds.size());
  3129. for (int i = 0; i < p_argcount - args_unbound; i++) {
  3130. argptrs.write[i] = p_argptrs[i];
  3131. }
  3132. for (int i = 0; i < binds.size(); i++) {
  3133. argptrs.write[i + p_argcount - args_unbound] = &binds[i];
  3134. }
  3135. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
  3136. }
  3137. }
  3138. void Variant::register_types() {
  3139. _register_variant_operators();
  3140. _register_variant_methods();
  3141. _register_variant_setters_getters();
  3142. _register_variant_constructors();
  3143. _register_variant_destructors();
  3144. _register_variant_utility_functions();
  3145. }
  3146. void Variant::unregister_types() {
  3147. _unregister_variant_operators();
  3148. _unregister_variant_methods();
  3149. _unregister_variant_setters_getters();
  3150. _unregister_variant_destructors();
  3151. _unregister_variant_utility_functions();
  3152. }