variant.cpp 95 KB

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