variant.cpp 93 KB

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