variant.cpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103
  1. /**************************************************************************/
  2. /* variant.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/object_rc.h"
  35. #include "core/print_string.h"
  36. #include "core/resource.h"
  37. #include "core/variant_parser.h"
  38. #include "scene/gui/control.h"
  39. #include "scene/main/node.h"
  40. String Variant::get_type_name(Variant::Type p_type) {
  41. switch (p_type) {
  42. case NIL: {
  43. return "Nil";
  44. } break;
  45. // atomic types
  46. case BOOL: {
  47. return "bool";
  48. } break;
  49. case INT: {
  50. return "int";
  51. } break;
  52. case REAL: {
  53. return "float";
  54. } break;
  55. case STRING: {
  56. return "String";
  57. } break;
  58. // math types
  59. case VECTOR2: {
  60. return "Vector2";
  61. } break;
  62. case RECT2: {
  63. return "Rect2";
  64. } break;
  65. case TRANSFORM2D: {
  66. return "Transform2D";
  67. } break;
  68. case VECTOR3: {
  69. return "Vector3";
  70. } break;
  71. case PLANE: {
  72. return "Plane";
  73. } break;
  74. /*
  75. case QUAT: {
  76. } break;*/
  77. case AABB: {
  78. return "AABB";
  79. } break;
  80. case QUAT: {
  81. return "Quat";
  82. } break;
  83. case BASIS: {
  84. return "Basis";
  85. } break;
  86. case TRANSFORM: {
  87. return "Transform";
  88. } break;
  89. // misc types
  90. case COLOR: {
  91. return "Color";
  92. } break;
  93. case _RID: {
  94. return "RID";
  95. } break;
  96. case OBJECT: {
  97. return "Object";
  98. } break;
  99. case NODE_PATH: {
  100. return "NodePath";
  101. } break;
  102. case DICTIONARY: {
  103. return "Dictionary";
  104. } break;
  105. case ARRAY: {
  106. return "Array";
  107. } break;
  108. // arrays
  109. case POOL_BYTE_ARRAY: {
  110. return "PoolByteArray";
  111. } break;
  112. case POOL_INT_ARRAY: {
  113. return "PoolIntArray";
  114. } break;
  115. case POOL_REAL_ARRAY: {
  116. return "PoolRealArray";
  117. } break;
  118. case POOL_STRING_ARRAY: {
  119. return "PoolStringArray";
  120. } break;
  121. case POOL_VECTOR2_ARRAY: {
  122. return "PoolVector2Array";
  123. } break;
  124. case POOL_VECTOR3_ARRAY: {
  125. return "PoolVector3Array";
  126. } break;
  127. case POOL_COLOR_ARRAY: {
  128. return "PoolColorArray";
  129. } break;
  130. default: {
  131. }
  132. }
  133. return "";
  134. }
  135. bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
  136. if (p_type_from == p_type_to) {
  137. return true;
  138. }
  139. if (p_type_to == NIL && p_type_from != NIL) { //nil can convert to anything
  140. return true;
  141. }
  142. if (p_type_from == NIL) {
  143. return (p_type_to == OBJECT);
  144. };
  145. const Type *valid_types = nullptr;
  146. const Type *invalid_types = nullptr;
  147. switch (p_type_to) {
  148. case BOOL: {
  149. static const Type valid[] = {
  150. INT,
  151. REAL,
  152. STRING,
  153. NIL,
  154. };
  155. valid_types = valid;
  156. } break;
  157. case INT: {
  158. static const Type valid[] = {
  159. BOOL,
  160. REAL,
  161. STRING,
  162. NIL,
  163. };
  164. valid_types = valid;
  165. } break;
  166. case REAL: {
  167. static const Type valid[] = {
  168. BOOL,
  169. INT,
  170. STRING,
  171. NIL,
  172. };
  173. valid_types = valid;
  174. } break;
  175. case STRING: {
  176. static const Type invalid[] = {
  177. OBJECT,
  178. NIL
  179. };
  180. invalid_types = invalid;
  181. } break;
  182. case TRANSFORM2D: {
  183. static const Type valid[] = {
  184. TRANSFORM,
  185. NIL
  186. };
  187. valid_types = valid;
  188. } break;
  189. case QUAT: {
  190. static const Type valid[] = {
  191. BASIS,
  192. NIL
  193. };
  194. valid_types = valid;
  195. } break;
  196. case BASIS: {
  197. static const Type valid[] = {
  198. QUAT,
  199. VECTOR3,
  200. NIL
  201. };
  202. valid_types = valid;
  203. } break;
  204. case TRANSFORM: {
  205. static const Type valid[] = {
  206. TRANSFORM2D,
  207. QUAT,
  208. BASIS,
  209. NIL
  210. };
  211. valid_types = valid;
  212. } break;
  213. case COLOR: {
  214. static const Type valid[] = {
  215. STRING,
  216. INT,
  217. NIL,
  218. };
  219. valid_types = valid;
  220. } break;
  221. case _RID: {
  222. static const Type valid[] = {
  223. OBJECT,
  224. NIL
  225. };
  226. valid_types = valid;
  227. } break;
  228. case OBJECT: {
  229. static const Type valid[] = {
  230. NIL
  231. };
  232. valid_types = valid;
  233. } break;
  234. case NODE_PATH: {
  235. static const Type valid[] = {
  236. STRING,
  237. NIL
  238. };
  239. valid_types = valid;
  240. } break;
  241. case ARRAY: {
  242. static const Type valid[] = {
  243. POOL_BYTE_ARRAY,
  244. POOL_INT_ARRAY,
  245. POOL_STRING_ARRAY,
  246. POOL_REAL_ARRAY,
  247. POOL_COLOR_ARRAY,
  248. POOL_VECTOR2_ARRAY,
  249. POOL_VECTOR3_ARRAY,
  250. NIL
  251. };
  252. valid_types = valid;
  253. } break;
  254. // arrays
  255. case POOL_BYTE_ARRAY: {
  256. static const Type valid[] = {
  257. ARRAY,
  258. NIL
  259. };
  260. valid_types = valid;
  261. } break;
  262. case POOL_INT_ARRAY: {
  263. static const Type valid[] = {
  264. ARRAY,
  265. NIL
  266. };
  267. valid_types = valid;
  268. } break;
  269. case POOL_REAL_ARRAY: {
  270. static const Type valid[] = {
  271. ARRAY,
  272. NIL
  273. };
  274. valid_types = valid;
  275. } break;
  276. case POOL_STRING_ARRAY: {
  277. static const Type valid[] = {
  278. ARRAY,
  279. NIL
  280. };
  281. valid_types = valid;
  282. } break;
  283. case POOL_VECTOR2_ARRAY: {
  284. static const Type valid[] = {
  285. ARRAY,
  286. NIL
  287. };
  288. valid_types = valid;
  289. } break;
  290. case POOL_VECTOR3_ARRAY: {
  291. static const Type valid[] = {
  292. ARRAY,
  293. NIL
  294. };
  295. valid_types = valid;
  296. } break;
  297. case POOL_COLOR_ARRAY: {
  298. static const Type valid[] = {
  299. ARRAY,
  300. NIL
  301. };
  302. valid_types = valid;
  303. } break;
  304. default: {
  305. }
  306. }
  307. if (valid_types) {
  308. int i = 0;
  309. while (valid_types[i] != NIL) {
  310. if (p_type_from == valid_types[i]) {
  311. return true;
  312. }
  313. i++;
  314. }
  315. } else if (invalid_types) {
  316. int i = 0;
  317. while (invalid_types[i] != NIL) {
  318. if (p_type_from == invalid_types[i]) {
  319. return false;
  320. }
  321. i++;
  322. }
  323. return true;
  324. }
  325. return false;
  326. }
  327. bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
  328. if (p_type_from == p_type_to) {
  329. return true;
  330. }
  331. if (p_type_to == NIL && p_type_from != NIL) { //nil can convert to anything
  332. return true;
  333. }
  334. if (p_type_from == NIL) {
  335. return (p_type_to == OBJECT);
  336. };
  337. const Type *valid_types = nullptr;
  338. switch (p_type_to) {
  339. case BOOL: {
  340. static const Type valid[] = {
  341. INT,
  342. REAL,
  343. //STRING,
  344. NIL,
  345. };
  346. valid_types = valid;
  347. } break;
  348. case INT: {
  349. static const Type valid[] = {
  350. BOOL,
  351. REAL,
  352. //STRING,
  353. NIL,
  354. };
  355. valid_types = valid;
  356. } break;
  357. case REAL: {
  358. static const Type valid[] = {
  359. BOOL,
  360. INT,
  361. //STRING,
  362. NIL,
  363. };
  364. valid_types = valid;
  365. } break;
  366. case STRING: {
  367. static const Type valid[] = {
  368. NODE_PATH,
  369. NIL
  370. };
  371. valid_types = valid;
  372. } break;
  373. case TRANSFORM2D: {
  374. static const Type valid[] = {
  375. TRANSFORM,
  376. NIL
  377. };
  378. valid_types = valid;
  379. } break;
  380. case QUAT: {
  381. static const Type valid[] = {
  382. BASIS,
  383. NIL
  384. };
  385. valid_types = valid;
  386. } break;
  387. case BASIS: {
  388. static const Type valid[] = {
  389. QUAT,
  390. VECTOR3,
  391. NIL
  392. };
  393. valid_types = valid;
  394. } break;
  395. case TRANSFORM: {
  396. static const Type valid[] = {
  397. TRANSFORM2D,
  398. QUAT,
  399. BASIS,
  400. NIL
  401. };
  402. valid_types = valid;
  403. } break;
  404. case COLOR: {
  405. static const Type valid[] = {
  406. STRING,
  407. INT,
  408. NIL,
  409. };
  410. valid_types = valid;
  411. } break;
  412. case _RID: {
  413. static const Type valid[] = {
  414. OBJECT,
  415. NIL
  416. };
  417. valid_types = valid;
  418. } break;
  419. case OBJECT: {
  420. static const Type valid[] = {
  421. NIL
  422. };
  423. valid_types = valid;
  424. } break;
  425. case NODE_PATH: {
  426. static const Type valid[] = {
  427. STRING,
  428. NIL
  429. };
  430. valid_types = valid;
  431. } break;
  432. case ARRAY: {
  433. static const Type valid[] = {
  434. POOL_BYTE_ARRAY,
  435. POOL_INT_ARRAY,
  436. POOL_STRING_ARRAY,
  437. POOL_REAL_ARRAY,
  438. POOL_COLOR_ARRAY,
  439. POOL_VECTOR2_ARRAY,
  440. POOL_VECTOR3_ARRAY,
  441. NIL
  442. };
  443. valid_types = valid;
  444. } break;
  445. // arrays
  446. case POOL_BYTE_ARRAY: {
  447. static const Type valid[] = {
  448. ARRAY,
  449. NIL
  450. };
  451. valid_types = valid;
  452. } break;
  453. case POOL_INT_ARRAY: {
  454. static const Type valid[] = {
  455. ARRAY,
  456. NIL
  457. };
  458. valid_types = valid;
  459. } break;
  460. case POOL_REAL_ARRAY: {
  461. static const Type valid[] = {
  462. ARRAY,
  463. NIL
  464. };
  465. valid_types = valid;
  466. } break;
  467. case POOL_STRING_ARRAY: {
  468. static const Type valid[] = {
  469. ARRAY,
  470. NIL
  471. };
  472. valid_types = valid;
  473. } break;
  474. case POOL_VECTOR2_ARRAY: {
  475. static const Type valid[] = {
  476. ARRAY,
  477. NIL
  478. };
  479. valid_types = valid;
  480. } break;
  481. case POOL_VECTOR3_ARRAY: {
  482. static const Type valid[] = {
  483. ARRAY,
  484. NIL
  485. };
  486. valid_types = valid;
  487. } break;
  488. case POOL_COLOR_ARRAY: {
  489. static const Type valid[] = {
  490. ARRAY,
  491. NIL
  492. };
  493. valid_types = valid;
  494. } break;
  495. default: {
  496. }
  497. }
  498. if (valid_types) {
  499. int i = 0;
  500. while (valid_types[i] != NIL) {
  501. if (p_type_from == valid_types[i]) {
  502. return true;
  503. }
  504. i++;
  505. }
  506. }
  507. return false;
  508. }
  509. bool Variant::deep_equal(const Variant &p_variant, int p_recursion_count) const {
  510. ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, true, "Max recursion reached");
  511. // Containers must be handled with recursivity checks
  512. switch (type) {
  513. case Variant::Type::DICTIONARY: {
  514. if (p_variant.type != Variant::Type::DICTIONARY) {
  515. return false;
  516. }
  517. const Dictionary v1_as_d = Dictionary(*this);
  518. const Dictionary v2_as_d = Dictionary(p_variant);
  519. return v1_as_d.deep_equal(v2_as_d, p_recursion_count + 1);
  520. } break;
  521. case Variant::Type::ARRAY: {
  522. if (p_variant.type != Variant::Type::ARRAY) {
  523. return false;
  524. }
  525. const Array v1_as_a = Array(*this);
  526. const Array v2_as_a = Array(p_variant);
  527. return v1_as_a.deep_equal(v2_as_a, p_recursion_count + 1);
  528. } break;
  529. default: {
  530. return *this == p_variant;
  531. } break;
  532. }
  533. }
  534. bool Variant::operator==(const Variant &p_variant) const {
  535. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  536. return false;
  537. }
  538. bool v;
  539. Variant r;
  540. evaluate(OP_EQUAL, *this, p_variant, r, v);
  541. return r;
  542. }
  543. bool Variant::operator!=(const Variant &p_variant) const {
  544. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  545. return true;
  546. }
  547. bool v;
  548. Variant r;
  549. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  550. return r;
  551. }
  552. bool Variant::operator<(const Variant &p_variant) const {
  553. if (type != p_variant.type) { //if types differ, then order by type first
  554. return type < p_variant.type;
  555. }
  556. bool v;
  557. Variant r;
  558. evaluate(OP_LESS, *this, p_variant, r, v);
  559. return r;
  560. }
  561. bool Variant::is_zero() const {
  562. switch (type) {
  563. case NIL: {
  564. return true;
  565. } break;
  566. // atomic types
  567. case BOOL: {
  568. return !(_data._bool);
  569. } break;
  570. case INT: {
  571. return _data._int == 0;
  572. } break;
  573. case REAL: {
  574. return _data._real == 0;
  575. } break;
  576. case STRING: {
  577. return *reinterpret_cast<const String *>(_data._mem) == String();
  578. } break;
  579. // math types
  580. case VECTOR2: {
  581. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  582. } break;
  583. case RECT2: {
  584. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  585. } break;
  586. case TRANSFORM2D: {
  587. return *_data._transform2d == Transform2D();
  588. } break;
  589. case VECTOR3: {
  590. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  591. } break;
  592. case PLANE: {
  593. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  594. } break;
  595. /*
  596. case QUAT: {
  597. } break;*/
  598. case AABB: {
  599. return *_data._aabb == ::AABB();
  600. } break;
  601. case QUAT: {
  602. return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
  603. } break;
  604. case BASIS: {
  605. return *_data._basis == Basis();
  606. } break;
  607. case TRANSFORM: {
  608. return *_data._transform == Transform();
  609. } break;
  610. // misc types
  611. case COLOR: {
  612. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  613. } break;
  614. case _RID: {
  615. return *reinterpret_cast<const RID *>(_data._mem) == RID();
  616. } break;
  617. case OBJECT: {
  618. return _UNSAFE_OBJ_PROXY_PTR(*this) == nullptr;
  619. } break;
  620. case NODE_PATH: {
  621. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  622. } break;
  623. case DICTIONARY: {
  624. return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
  625. } break;
  626. case ARRAY: {
  627. return reinterpret_cast<const Array *>(_data._mem)->empty();
  628. } break;
  629. // arrays
  630. case POOL_BYTE_ARRAY: {
  631. return reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem)->size() == 0;
  632. } break;
  633. case POOL_INT_ARRAY: {
  634. return reinterpret_cast<const PoolVector<int> *>(_data._mem)->size() == 0;
  635. } break;
  636. case POOL_REAL_ARRAY: {
  637. return reinterpret_cast<const PoolVector<real_t> *>(_data._mem)->size() == 0;
  638. } break;
  639. case POOL_STRING_ARRAY: {
  640. return reinterpret_cast<const PoolVector<String> *>(_data._mem)->size() == 0;
  641. } break;
  642. case POOL_VECTOR2_ARRAY: {
  643. return reinterpret_cast<const PoolVector<Vector2> *>(_data._mem)->size() == 0;
  644. } break;
  645. case POOL_VECTOR3_ARRAY: {
  646. return reinterpret_cast<const PoolVector<Vector3> *>(_data._mem)->size() == 0;
  647. } break;
  648. case POOL_COLOR_ARRAY: {
  649. return reinterpret_cast<const PoolVector<Color> *>(_data._mem)->size() == 0;
  650. } break;
  651. default: {
  652. }
  653. }
  654. return false;
  655. }
  656. bool Variant::is_one() const {
  657. switch (type) {
  658. case NIL: {
  659. return true;
  660. } break;
  661. // atomic types
  662. case BOOL: {
  663. return _data._bool;
  664. } break;
  665. case INT: {
  666. return _data._int == 1;
  667. } break;
  668. case REAL: {
  669. return _data._real == 1;
  670. } break;
  671. case VECTOR2: {
  672. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  673. } break;
  674. case RECT2: {
  675. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  676. } break;
  677. case VECTOR3: {
  678. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  679. } break;
  680. case PLANE: {
  681. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  682. } break;
  683. case COLOR: {
  684. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  685. } break;
  686. default: {
  687. return !is_zero();
  688. }
  689. }
  690. return false;
  691. }
  692. ObjectID Variant::get_object_instance_id() const {
  693. if (unlikely(type != OBJECT)) {
  694. return 0;
  695. } else if (likely(_get_obj().rc)) {
  696. return _get_obj().rc->instance_id;
  697. } else if (likely(!_get_obj().ref.is_null())) {
  698. return _REF_OBJ_PTR(*this)->get_instance_id();
  699. } else {
  700. return 0;
  701. }
  702. }
  703. bool Variant::is_invalid_object() const {
  704. return type == OBJECT && _get_obj().rc && !_get_obj().rc->get_ptr();
  705. }
  706. void Variant::reference(const Variant &p_variant) {
  707. switch (type) {
  708. case NIL:
  709. case BOOL:
  710. case INT:
  711. case REAL:
  712. break;
  713. default:
  714. clear();
  715. }
  716. type = p_variant.type;
  717. switch (p_variant.type) {
  718. case NIL: {
  719. // none
  720. } break;
  721. // atomic types
  722. case BOOL: {
  723. _data._bool = p_variant._data._bool;
  724. } break;
  725. case INT: {
  726. _data._int = p_variant._data._int;
  727. } break;
  728. case REAL: {
  729. _data._real = p_variant._data._real;
  730. } break;
  731. case STRING: {
  732. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  733. } break;
  734. // math types
  735. case VECTOR2: {
  736. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  737. } break;
  738. case RECT2: {
  739. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  740. } break;
  741. case TRANSFORM2D: {
  742. _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d));
  743. } break;
  744. case VECTOR3: {
  745. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  746. } break;
  747. case PLANE: {
  748. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  749. } break;
  750. case AABB: {
  751. _data._aabb = memnew(::AABB(*p_variant._data._aabb));
  752. } break;
  753. case QUAT: {
  754. memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
  755. } break;
  756. case BASIS: {
  757. _data._basis = memnew(Basis(*p_variant._data._basis));
  758. } break;
  759. case TRANSFORM: {
  760. _data._transform = memnew(Transform(*p_variant._data._transform));
  761. } break;
  762. // misc types
  763. case COLOR: {
  764. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  765. } break;
  766. case _RID: {
  767. memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem)));
  768. } break;
  769. case OBJECT: {
  770. memnew_placement(_data._mem, ObjData(p_variant._get_obj()));
  771. if (likely(_get_obj().rc)) {
  772. _get_obj().rc->increment();
  773. }
  774. } break;
  775. case NODE_PATH: {
  776. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  777. } break;
  778. case DICTIONARY: {
  779. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  780. } break;
  781. case ARRAY: {
  782. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  783. } break;
  784. // arrays
  785. case POOL_BYTE_ARRAY: {
  786. memnew_placement(_data._mem, PoolVector<uint8_t>(*reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem)));
  787. } break;
  788. case POOL_INT_ARRAY: {
  789. memnew_placement(_data._mem, PoolVector<int>(*reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem)));
  790. } break;
  791. case POOL_REAL_ARRAY: {
  792. memnew_placement(_data._mem, PoolVector<real_t>(*reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem)));
  793. } break;
  794. case POOL_STRING_ARRAY: {
  795. memnew_placement(_data._mem, PoolVector<String>(*reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem)));
  796. } break;
  797. case POOL_VECTOR2_ARRAY: {
  798. memnew_placement(_data._mem, PoolVector<Vector2>(*reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem)));
  799. } break;
  800. case POOL_VECTOR3_ARRAY: {
  801. memnew_placement(_data._mem, PoolVector<Vector3>(*reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem)));
  802. } break;
  803. case POOL_COLOR_ARRAY: {
  804. memnew_placement(_data._mem, PoolVector<Color>(*reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem)));
  805. } break;
  806. default: {
  807. }
  808. }
  809. }
  810. void Variant::zero() {
  811. switch (type) {
  812. case NIL:
  813. break;
  814. case BOOL:
  815. this->_data._bool = false;
  816. break;
  817. case INT:
  818. this->_data._int = 0;
  819. break;
  820. case REAL:
  821. this->_data._real = 0;
  822. break;
  823. case VECTOR2:
  824. *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2();
  825. break;
  826. case RECT2:
  827. *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2();
  828. break;
  829. case VECTOR3:
  830. *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3();
  831. break;
  832. case PLANE:
  833. *reinterpret_cast<Plane *>(this->_data._mem) = Plane();
  834. break;
  835. case QUAT:
  836. *reinterpret_cast<Quat *>(this->_data._mem) = Quat();
  837. break;
  838. case COLOR:
  839. *reinterpret_cast<Color *>(this->_data._mem) = Color();
  840. break;
  841. default:
  842. this->clear();
  843. break;
  844. }
  845. }
  846. void Variant::clear() {
  847. switch (type) {
  848. case STRING: {
  849. reinterpret_cast<String *>(_data._mem)->~String();
  850. } break;
  851. /*
  852. // no point, they don't allocate memory
  853. VECTOR3,
  854. PLANE,
  855. QUAT,
  856. COLOR,
  857. VECTOR2,
  858. RECT2
  859. */
  860. case TRANSFORM2D: {
  861. memdelete(_data._transform2d);
  862. } break;
  863. case AABB: {
  864. memdelete(_data._aabb);
  865. } break;
  866. case BASIS: {
  867. memdelete(_data._basis);
  868. } break;
  869. case TRANSFORM: {
  870. memdelete(_data._transform);
  871. } break;
  872. // misc types
  873. case NODE_PATH: {
  874. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  875. } break;
  876. case OBJECT: {
  877. if (likely(_get_obj().rc)) {
  878. if (unlikely(_get_obj().rc->decrement())) {
  879. memdelete(_get_obj().rc);
  880. }
  881. } else {
  882. _get_obj().ref.unref();
  883. }
  884. } break;
  885. case _RID: {
  886. // not much need probably
  887. reinterpret_cast<RID *>(_data._mem)->~RID();
  888. } break;
  889. case DICTIONARY: {
  890. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  891. } break;
  892. case ARRAY: {
  893. reinterpret_cast<Array *>(_data._mem)->~Array();
  894. } break;
  895. // arrays
  896. case POOL_BYTE_ARRAY: {
  897. reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>();
  898. } break;
  899. case POOL_INT_ARRAY: {
  900. reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>();
  901. } break;
  902. case POOL_REAL_ARRAY: {
  903. reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>();
  904. } break;
  905. case POOL_STRING_ARRAY: {
  906. reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>();
  907. } break;
  908. case POOL_VECTOR2_ARRAY: {
  909. reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>();
  910. } break;
  911. case POOL_VECTOR3_ARRAY: {
  912. reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>();
  913. } break;
  914. case POOL_COLOR_ARRAY: {
  915. reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>();
  916. } break;
  917. default: {
  918. } /* not needed */
  919. }
  920. type = NIL;
  921. }
  922. Variant::operator signed int() const {
  923. switch (type) {
  924. case NIL:
  925. return 0;
  926. case BOOL:
  927. return _data._bool ? 1 : 0;
  928. case INT:
  929. return _data._int;
  930. case REAL:
  931. return _data._real;
  932. case STRING:
  933. return operator String().to_int();
  934. default: {
  935. return 0;
  936. }
  937. }
  938. }
  939. Variant::operator unsigned int() const {
  940. switch (type) {
  941. case NIL:
  942. return 0;
  943. case BOOL:
  944. return _data._bool ? 1 : 0;
  945. case INT:
  946. return _data._int;
  947. case REAL:
  948. return _data._real;
  949. case STRING:
  950. return operator String().to_int();
  951. default: {
  952. return 0;
  953. }
  954. }
  955. }
  956. Variant::operator int64_t() const {
  957. switch (type) {
  958. case NIL:
  959. return 0;
  960. case BOOL:
  961. return _data._bool ? 1 : 0;
  962. case INT:
  963. return _data._int;
  964. case REAL:
  965. return _data._real;
  966. case STRING:
  967. return operator String().to_int64();
  968. default: {
  969. return 0;
  970. }
  971. }
  972. }
  973. /*
  974. Variant::operator long unsigned int() const {
  975. switch( type ) {
  976. case NIL: return 0;
  977. case BOOL: return _data._bool ? 1 : 0;
  978. case INT: return _data._int;
  979. case REAL: return _data._real;
  980. case STRING: return operator String().to_int();
  981. default: {
  982. return 0;
  983. }
  984. }
  985. return 0;
  986. };
  987. */
  988. Variant::operator uint64_t() const {
  989. switch (type) {
  990. case NIL:
  991. return 0;
  992. case BOOL:
  993. return _data._bool ? 1 : 0;
  994. case INT:
  995. return _data._int;
  996. case REAL:
  997. return _data._real;
  998. case STRING:
  999. return operator String().to_int();
  1000. default: {
  1001. return 0;
  1002. }
  1003. }
  1004. }
  1005. #ifdef NEED_LONG_INT
  1006. Variant::operator signed long() const {
  1007. switch (type) {
  1008. case NIL:
  1009. return 0;
  1010. case BOOL:
  1011. return _data._bool ? 1 : 0;
  1012. case INT:
  1013. return _data._int;
  1014. case REAL:
  1015. return _data._real;
  1016. case STRING:
  1017. return operator String().to_int();
  1018. default: {
  1019. return 0;
  1020. }
  1021. }
  1022. return 0;
  1023. };
  1024. Variant::operator unsigned long() const {
  1025. switch (type) {
  1026. case NIL:
  1027. return 0;
  1028. case BOOL:
  1029. return _data._bool ? 1 : 0;
  1030. case INT:
  1031. return _data._int;
  1032. case REAL:
  1033. return _data._real;
  1034. case STRING:
  1035. return operator String().to_int();
  1036. default: {
  1037. return 0;
  1038. }
  1039. }
  1040. return 0;
  1041. };
  1042. #endif
  1043. Variant::operator signed short() const {
  1044. switch (type) {
  1045. case NIL:
  1046. return 0;
  1047. case BOOL:
  1048. return _data._bool ? 1 : 0;
  1049. case INT:
  1050. return _data._int;
  1051. case REAL:
  1052. return _data._real;
  1053. case STRING:
  1054. return operator String().to_int();
  1055. default: {
  1056. return 0;
  1057. }
  1058. }
  1059. }
  1060. Variant::operator unsigned short() const {
  1061. switch (type) {
  1062. case NIL:
  1063. return 0;
  1064. case BOOL:
  1065. return _data._bool ? 1 : 0;
  1066. case INT:
  1067. return _data._int;
  1068. case REAL:
  1069. return _data._real;
  1070. case STRING:
  1071. return operator String().to_int();
  1072. default: {
  1073. return 0;
  1074. }
  1075. }
  1076. }
  1077. Variant::operator signed char() const {
  1078. switch (type) {
  1079. case NIL:
  1080. return 0;
  1081. case BOOL:
  1082. return _data._bool ? 1 : 0;
  1083. case INT:
  1084. return _data._int;
  1085. case REAL:
  1086. return _data._real;
  1087. case STRING:
  1088. return operator String().to_int();
  1089. default: {
  1090. return 0;
  1091. }
  1092. }
  1093. }
  1094. Variant::operator unsigned char() const {
  1095. switch (type) {
  1096. case NIL:
  1097. return 0;
  1098. case BOOL:
  1099. return _data._bool ? 1 : 0;
  1100. case INT:
  1101. return _data._int;
  1102. case REAL:
  1103. return _data._real;
  1104. case STRING:
  1105. return operator String().to_int();
  1106. default: {
  1107. return 0;
  1108. }
  1109. }
  1110. }
  1111. Variant::operator CharType() const {
  1112. return operator unsigned int();
  1113. }
  1114. Variant::operator float() const {
  1115. switch (type) {
  1116. case NIL:
  1117. return 0;
  1118. case BOOL:
  1119. return _data._bool ? 1.0 : 0.0;
  1120. case INT:
  1121. return (float)_data._int;
  1122. case REAL:
  1123. return _data._real;
  1124. case STRING:
  1125. return operator String().to_double();
  1126. default: {
  1127. return 0;
  1128. }
  1129. }
  1130. }
  1131. Variant::operator double() const {
  1132. switch (type) {
  1133. case NIL:
  1134. return 0;
  1135. case BOOL:
  1136. return _data._bool ? 1.0 : 0.0;
  1137. case INT:
  1138. return (double)_data._int;
  1139. case REAL:
  1140. return _data._real;
  1141. case STRING:
  1142. return operator String().to_double();
  1143. default: {
  1144. return 0;
  1145. }
  1146. }
  1147. }
  1148. Variant::operator StringName() const {
  1149. if (type == NODE_PATH) {
  1150. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1151. }
  1152. return StringName(operator String());
  1153. }
  1154. struct _VariantStrPair {
  1155. String key;
  1156. String value;
  1157. bool operator<(const _VariantStrPair &p) const {
  1158. return key < p.key;
  1159. }
  1160. };
  1161. Variant::operator String() const {
  1162. List<const void *> stack;
  1163. return stringify(stack);
  1164. }
  1165. template <class T>
  1166. String stringify_vector(const T &vec, List<const void *> &stack) {
  1167. String str("[");
  1168. for (int i = 0; i < vec.size(); i++) {
  1169. if (i > 0) {
  1170. str += ", ";
  1171. }
  1172. str = str + Variant(vec[i]).stringify(stack);
  1173. }
  1174. str += "]";
  1175. return str;
  1176. }
  1177. String Variant::stringify(List<const void *> &stack) const {
  1178. switch (type) {
  1179. case NIL:
  1180. return "Null";
  1181. case BOOL:
  1182. return _data._bool ? "True" : "False";
  1183. case INT:
  1184. return itos(_data._int);
  1185. case REAL:
  1186. return rtos(_data._real);
  1187. case STRING:
  1188. return *reinterpret_cast<const String *>(_data._mem);
  1189. case VECTOR2:
  1190. return "(" + operator Vector2() + ")";
  1191. case RECT2:
  1192. return "(" + operator Rect2() + ")";
  1193. case TRANSFORM2D: {
  1194. Transform2D mat32 = operator Transform2D();
  1195. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1196. } break;
  1197. case VECTOR3:
  1198. return "(" + operator Vector3() + ")";
  1199. case PLANE:
  1200. return operator Plane();
  1201. //case QUAT:
  1202. case AABB:
  1203. return operator ::AABB();
  1204. case QUAT:
  1205. return "(" + operator Quat() + ")";
  1206. case BASIS: {
  1207. Basis mat3 = operator Basis();
  1208. String mtx("(");
  1209. for (int i = 0; i < 3; i++) {
  1210. if (i != 0) {
  1211. mtx += ", ";
  1212. }
  1213. mtx += "(";
  1214. for (int j = 0; j < 3; j++) {
  1215. if (j != 0) {
  1216. mtx += ", ";
  1217. }
  1218. mtx += Variant(mat3.elements[i][j]).operator String();
  1219. }
  1220. mtx += ")";
  1221. }
  1222. return mtx + ")";
  1223. } break;
  1224. case TRANSFORM:
  1225. return operator Transform();
  1226. case NODE_PATH:
  1227. return operator NodePath();
  1228. case COLOR:
  1229. return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1230. case DICTIONARY: {
  1231. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1232. if (stack.find(d.id())) {
  1233. return "{...}";
  1234. }
  1235. stack.push_back(d.id());
  1236. //const String *K=NULL;
  1237. String str("{");
  1238. List<Variant> keys;
  1239. d.get_key_list(&keys);
  1240. Vector<_VariantStrPair> pairs;
  1241. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1242. _VariantStrPair sp;
  1243. sp.key = E->get().stringify(stack);
  1244. sp.value = d[E->get()].stringify(stack);
  1245. pairs.push_back(sp);
  1246. }
  1247. pairs.sort();
  1248. for (int i = 0; i < pairs.size(); i++) {
  1249. if (i > 0) {
  1250. str += ", ";
  1251. }
  1252. str += pairs[i].key + ":" + pairs[i].value;
  1253. }
  1254. str += "}";
  1255. stack.erase(d.id());
  1256. return str;
  1257. } break;
  1258. case POOL_VECTOR2_ARRAY: {
  1259. return stringify_vector(operator PoolVector<Vector2>(), stack);
  1260. } break;
  1261. case POOL_VECTOR3_ARRAY: {
  1262. return stringify_vector(operator PoolVector<Vector3>(), stack);
  1263. } break;
  1264. case POOL_COLOR_ARRAY: {
  1265. return stringify_vector(operator PoolVector<Color>(), stack);
  1266. } break;
  1267. case POOL_STRING_ARRAY: {
  1268. return stringify_vector(operator PoolVector<String>(), stack);
  1269. } break;
  1270. case POOL_BYTE_ARRAY: {
  1271. return stringify_vector(operator PoolVector<uint8_t>(), stack);
  1272. } break;
  1273. case POOL_INT_ARRAY: {
  1274. return stringify_vector(operator PoolVector<int>(), stack);
  1275. } break;
  1276. case POOL_REAL_ARRAY: {
  1277. return stringify_vector(operator PoolVector<real_t>(), stack);
  1278. } break;
  1279. case ARRAY: {
  1280. Array arr = operator Array();
  1281. if (stack.find(arr.id())) {
  1282. return "[...]";
  1283. }
  1284. stack.push_back(arr.id());
  1285. String str = stringify_vector(arr, stack);
  1286. stack.erase(arr.id());
  1287. return str;
  1288. } break;
  1289. case OBJECT: {
  1290. Object *obj = _OBJ_PTR(*this);
  1291. if (likely(obj)) {
  1292. return obj->to_string();
  1293. } else {
  1294. if (_get_obj().rc) {
  1295. return "[Deleted Object]";
  1296. }
  1297. return "[Object:null]";
  1298. }
  1299. } break;
  1300. default: {
  1301. return "[" + get_type_name(type) + "]";
  1302. }
  1303. }
  1304. return "";
  1305. }
  1306. Variant::operator Vector2() const {
  1307. if (type == VECTOR2) {
  1308. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1309. } else if (type == VECTOR3) {
  1310. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1311. } else {
  1312. return Vector2();
  1313. }
  1314. }
  1315. Variant::operator Rect2() const {
  1316. if (type == RECT2) {
  1317. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1318. } else {
  1319. return Rect2();
  1320. }
  1321. }
  1322. Variant::operator Vector3() const {
  1323. if (type == VECTOR3) {
  1324. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1325. } else if (type == VECTOR2) {
  1326. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1327. } else {
  1328. return Vector3();
  1329. }
  1330. }
  1331. Variant::operator Plane() const {
  1332. if (type == PLANE) {
  1333. return *reinterpret_cast<const Plane *>(_data._mem);
  1334. } else {
  1335. return Plane();
  1336. }
  1337. }
  1338. Variant::operator ::AABB() const {
  1339. if (type == AABB) {
  1340. return *_data._aabb;
  1341. } else {
  1342. return ::AABB();
  1343. }
  1344. }
  1345. Variant::operator Basis() const {
  1346. if (type == BASIS) {
  1347. return *_data._basis;
  1348. } else if (type == QUAT) {
  1349. return *reinterpret_cast<const Quat *>(_data._mem);
  1350. } else if (type == VECTOR3) {
  1351. return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
  1352. } else if (type == TRANSFORM) { // unexposed in Variant::can_convert?
  1353. return _data._transform->basis;
  1354. } else {
  1355. return Basis();
  1356. }
  1357. }
  1358. Variant::operator Quat() const {
  1359. if (type == QUAT) {
  1360. return *reinterpret_cast<const Quat *>(_data._mem);
  1361. } else if (type == BASIS) {
  1362. return *_data._basis;
  1363. } else if (type == TRANSFORM) {
  1364. return _data._transform->basis;
  1365. } else {
  1366. return Quat();
  1367. }
  1368. }
  1369. Variant::operator Transform() const {
  1370. if (type == TRANSFORM) {
  1371. return *_data._transform;
  1372. } else if (type == BASIS) {
  1373. return Transform(*_data._basis, Vector3());
  1374. } else if (type == QUAT) {
  1375. return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1376. } else if (type == TRANSFORM2D) {
  1377. const Transform2D &t = *_data._transform2d;
  1378. Transform m;
  1379. m.basis.elements[0][0] = t.elements[0][0];
  1380. m.basis.elements[1][0] = t.elements[0][1];
  1381. m.basis.elements[0][1] = t.elements[1][0];
  1382. m.basis.elements[1][1] = t.elements[1][1];
  1383. m.origin[0] = t.elements[2][0];
  1384. m.origin[1] = t.elements[2][1];
  1385. return m;
  1386. } else {
  1387. return Transform();
  1388. }
  1389. }
  1390. Variant::operator Transform2D() const {
  1391. if (type == TRANSFORM2D) {
  1392. return *_data._transform2d;
  1393. } else if (type == TRANSFORM) {
  1394. const Transform &t = *_data._transform;
  1395. Transform2D m;
  1396. m.elements[0][0] = t.basis.elements[0][0];
  1397. m.elements[0][1] = t.basis.elements[1][0];
  1398. m.elements[1][0] = t.basis.elements[0][1];
  1399. m.elements[1][1] = t.basis.elements[1][1];
  1400. m.elements[2][0] = t.origin[0];
  1401. m.elements[2][1] = t.origin[1];
  1402. return m;
  1403. } else {
  1404. return Transform2D();
  1405. }
  1406. }
  1407. Variant::operator Color() const {
  1408. if (type == COLOR) {
  1409. return *reinterpret_cast<const Color *>(_data._mem);
  1410. } else if (type == STRING) {
  1411. return Color::html(operator String());
  1412. } else if (type == INT) {
  1413. return Color::hex(operator int());
  1414. } else {
  1415. return Color();
  1416. }
  1417. }
  1418. Variant::operator NodePath() const {
  1419. if (type == NODE_PATH) {
  1420. return *reinterpret_cast<const NodePath *>(_data._mem);
  1421. } else if (type == STRING) {
  1422. return NodePath(operator String());
  1423. } else {
  1424. return NodePath();
  1425. }
  1426. }
  1427. Variant::operator RefPtr() const {
  1428. if (type == OBJECT) {
  1429. return _get_obj().ref;
  1430. } else {
  1431. return RefPtr();
  1432. }
  1433. }
  1434. Variant::operator RID() const {
  1435. if (type == _RID) {
  1436. return *reinterpret_cast<const RID *>(_data._mem);
  1437. } else if (type == OBJECT) {
  1438. if (!_get_obj().ref.is_null()) {
  1439. return _get_obj().ref.get_rid();
  1440. } else {
  1441. Object *obj = likely(_get_obj().rc) ? _get_obj().rc->get_ptr() : nullptr;
  1442. if (unlikely(!obj)) {
  1443. if (_get_obj().rc) {
  1444. ERR_PRINT("Attempted get RID on a deleted object.");
  1445. }
  1446. return RID();
  1447. }
  1448. Variant::CallError ce;
  1449. Variant ret = obj->call(CoreStringNames::get_singleton()->get_rid, nullptr, 0, ce);
  1450. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1451. return ret;
  1452. } else {
  1453. return RID();
  1454. }
  1455. }
  1456. } else {
  1457. return RID();
  1458. }
  1459. }
  1460. Variant::operator Object *() const {
  1461. if (type == OBJECT) {
  1462. return _OBJ_PTR(*this);
  1463. } else {
  1464. return nullptr;
  1465. }
  1466. }
  1467. Variant::operator Node *() const {
  1468. if (type == OBJECT) {
  1469. Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
  1470. return Object::cast_to<Node>(obj);
  1471. }
  1472. return nullptr;
  1473. }
  1474. Variant::operator Control *() const {
  1475. if (type == OBJECT) {
  1476. Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
  1477. return Object::cast_to<Control>(obj);
  1478. }
  1479. return nullptr;
  1480. }
  1481. Variant::operator Dictionary() const {
  1482. if (type == DICTIONARY) {
  1483. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1484. } else {
  1485. return Dictionary();
  1486. }
  1487. }
  1488. template <class DA, class SA>
  1489. inline DA _convert_array(const SA &p_array) {
  1490. DA da;
  1491. da.resize(p_array.size());
  1492. for (int i = 0; i < p_array.size(); i++) {
  1493. da.set(i, Variant(p_array.get(i)));
  1494. }
  1495. return da;
  1496. }
  1497. template <class DA>
  1498. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1499. switch (p_variant.get_type()) {
  1500. case Variant::ARRAY: {
  1501. return _convert_array<DA, Array>(p_variant.operator Array());
  1502. }
  1503. case Variant::POOL_BYTE_ARRAY: {
  1504. return _convert_array<DA, PoolVector<uint8_t>>(p_variant.operator PoolVector<uint8_t>());
  1505. }
  1506. case Variant::POOL_INT_ARRAY: {
  1507. return _convert_array<DA, PoolVector<int>>(p_variant.operator PoolVector<int>());
  1508. }
  1509. case Variant::POOL_REAL_ARRAY: {
  1510. return _convert_array<DA, PoolVector<real_t>>(p_variant.operator PoolVector<real_t>());
  1511. }
  1512. case Variant::POOL_STRING_ARRAY: {
  1513. return _convert_array<DA, PoolVector<String>>(p_variant.operator PoolVector<String>());
  1514. }
  1515. case Variant::POOL_VECTOR2_ARRAY: {
  1516. return _convert_array<DA, PoolVector<Vector2>>(p_variant.operator PoolVector<Vector2>());
  1517. }
  1518. case Variant::POOL_VECTOR3_ARRAY: {
  1519. return _convert_array<DA, PoolVector<Vector3>>(p_variant.operator PoolVector<Vector3>());
  1520. }
  1521. case Variant::POOL_COLOR_ARRAY: {
  1522. return _convert_array<DA, PoolVector<Color>>(p_variant.operator PoolVector<Color>());
  1523. }
  1524. default: {
  1525. return DA();
  1526. }
  1527. }
  1528. }
  1529. Variant::operator Array() const {
  1530. if (type == ARRAY) {
  1531. return *reinterpret_cast<const Array *>(_data._mem);
  1532. } else {
  1533. return _convert_array_from_variant<Array>(*this);
  1534. }
  1535. }
  1536. Variant::operator PoolVector<uint8_t>() const {
  1537. if (type == POOL_BYTE_ARRAY) {
  1538. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1539. } else {
  1540. return _convert_array_from_variant<PoolVector<uint8_t>>(*this);
  1541. }
  1542. }
  1543. Variant::operator PoolVector<int>() const {
  1544. if (type == POOL_INT_ARRAY) {
  1545. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1546. } else {
  1547. return _convert_array_from_variant<PoolVector<int>>(*this);
  1548. }
  1549. }
  1550. Variant::operator PoolVector<real_t>() const {
  1551. if (type == POOL_REAL_ARRAY) {
  1552. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1553. } else {
  1554. return _convert_array_from_variant<PoolVector<real_t>>(*this);
  1555. }
  1556. }
  1557. Variant::operator PoolVector<String>() const {
  1558. if (type == POOL_STRING_ARRAY) {
  1559. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1560. } else {
  1561. return _convert_array_from_variant<PoolVector<String>>(*this);
  1562. }
  1563. }
  1564. Variant::operator PoolVector<Vector3>() const {
  1565. if (type == POOL_VECTOR3_ARRAY) {
  1566. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1567. } else {
  1568. return _convert_array_from_variant<PoolVector<Vector3>>(*this);
  1569. }
  1570. }
  1571. Variant::operator PoolVector<Vector2>() const {
  1572. if (type == POOL_VECTOR2_ARRAY) {
  1573. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1574. } else {
  1575. return _convert_array_from_variant<PoolVector<Vector2>>(*this);
  1576. }
  1577. }
  1578. Variant::operator PoolVector<Color>() const {
  1579. if (type == POOL_COLOR_ARRAY) {
  1580. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1581. } else {
  1582. return _convert_array_from_variant<PoolVector<Color>>(*this);
  1583. }
  1584. }
  1585. /* helpers */
  1586. Variant::operator Vector<RID>() const {
  1587. Array va = operator Array();
  1588. Vector<RID> rids;
  1589. rids.resize(va.size());
  1590. for (int i = 0; i < rids.size(); i++) {
  1591. rids.write[i] = va[i];
  1592. }
  1593. return rids;
  1594. }
  1595. Variant::operator Vector<Vector2>() const {
  1596. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1597. Vector<Vector2> to;
  1598. int len = from.size();
  1599. if (len == 0) {
  1600. return Vector<Vector2>();
  1601. }
  1602. to.resize(len);
  1603. PoolVector<Vector2>::Read r = from.read();
  1604. Vector2 *w = to.ptrw();
  1605. for (int i = 0; i < len; i++) {
  1606. w[i] = r[i];
  1607. }
  1608. return to;
  1609. }
  1610. Variant::operator PoolVector<Plane>() const {
  1611. Array va = operator Array();
  1612. PoolVector<Plane> planes;
  1613. int va_size = va.size();
  1614. if (va_size == 0) {
  1615. return planes;
  1616. }
  1617. planes.resize(va_size);
  1618. PoolVector<Plane>::Write w = planes.write();
  1619. for (int i = 0; i < va_size; i++) {
  1620. w[i] = va[i];
  1621. }
  1622. return planes;
  1623. }
  1624. Variant::operator PoolVector<Face3>() const {
  1625. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1626. PoolVector<Face3> faces;
  1627. int va_size = va.size();
  1628. if (va_size == 0) {
  1629. return faces;
  1630. }
  1631. faces.resize(va_size / 3);
  1632. PoolVector<Face3>::Write w = faces.write();
  1633. PoolVector<Vector3>::Read r = va.read();
  1634. for (int i = 0; i < va_size; i++) {
  1635. w[i / 3].vertex[i % 3] = r[i];
  1636. }
  1637. return faces;
  1638. }
  1639. Variant::operator Vector<Plane>() const {
  1640. Array va = operator Array();
  1641. Vector<Plane> planes;
  1642. int va_size = va.size();
  1643. if (va_size == 0) {
  1644. return planes;
  1645. }
  1646. planes.resize(va_size);
  1647. for (int i = 0; i < va_size; i++) {
  1648. planes.write[i] = va[i];
  1649. }
  1650. return planes;
  1651. }
  1652. Variant::operator Vector<Variant>() const {
  1653. Array from = operator Array();
  1654. Vector<Variant> to;
  1655. int len = from.size();
  1656. to.resize(len);
  1657. for (int i = 0; i < len; i++) {
  1658. to.write[i] = from[i];
  1659. }
  1660. return to;
  1661. }
  1662. Variant::operator Vector<uint8_t>() const {
  1663. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1664. Vector<uint8_t> to;
  1665. int len = from.size();
  1666. to.resize(len);
  1667. for (int i = 0; i < len; i++) {
  1668. to.write[i] = from[i];
  1669. }
  1670. return to;
  1671. }
  1672. Variant::operator Vector<int>() const {
  1673. PoolVector<int> from = operator PoolVector<int>();
  1674. Vector<int> to;
  1675. int len = from.size();
  1676. to.resize(len);
  1677. for (int i = 0; i < len; i++) {
  1678. to.write[i] = from[i];
  1679. }
  1680. return to;
  1681. }
  1682. Variant::operator Vector<real_t>() const {
  1683. PoolVector<real_t> from = operator PoolVector<real_t>();
  1684. Vector<real_t> to;
  1685. int len = from.size();
  1686. to.resize(len);
  1687. for (int i = 0; i < len; i++) {
  1688. to.write[i] = from[i];
  1689. }
  1690. return to;
  1691. }
  1692. Variant::operator Vector<String>() const {
  1693. PoolVector<String> from = operator PoolVector<String>();
  1694. Vector<String> to;
  1695. int len = from.size();
  1696. to.resize(len);
  1697. for (int i = 0; i < len; i++) {
  1698. to.write[i] = from[i];
  1699. }
  1700. return to;
  1701. }
  1702. Variant::operator Vector<StringName>() const {
  1703. PoolVector<String> from = operator PoolVector<String>();
  1704. Vector<StringName> to;
  1705. int len = from.size();
  1706. to.resize(len);
  1707. for (int i = 0; i < len; i++) {
  1708. to.write[i] = from[i];
  1709. }
  1710. return to;
  1711. }
  1712. Variant::operator Vector<Vector3>() const {
  1713. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1714. Vector<Vector3> to;
  1715. int len = from.size();
  1716. if (len == 0) {
  1717. return Vector<Vector3>();
  1718. }
  1719. to.resize(len);
  1720. PoolVector<Vector3>::Read r = from.read();
  1721. Vector3 *w = to.ptrw();
  1722. for (int i = 0; i < len; i++) {
  1723. w[i] = r[i];
  1724. }
  1725. return to;
  1726. }
  1727. Variant::operator Vector<Color>() const {
  1728. PoolVector<Color> from = operator PoolVector<Color>();
  1729. Vector<Color> to;
  1730. int len = from.size();
  1731. if (len == 0) {
  1732. return Vector<Color>();
  1733. }
  1734. to.resize(len);
  1735. PoolVector<Color>::Read r = from.read();
  1736. Color *w = to.ptrw();
  1737. for (int i = 0; i < len; i++) {
  1738. w[i] = r[i];
  1739. }
  1740. return to;
  1741. }
  1742. Variant::operator Margin() const {
  1743. return (Margin) operator int();
  1744. }
  1745. Variant::operator Orientation() const {
  1746. return (Orientation) operator int();
  1747. }
  1748. Variant::operator IP_Address() const {
  1749. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1750. PoolVector<int> addr = operator PoolVector<int>();
  1751. if (addr.size() == 4) {
  1752. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1753. }
  1754. }
  1755. return IP_Address(operator String());
  1756. }
  1757. Variant::Variant(bool p_bool) {
  1758. type = BOOL;
  1759. _data._bool = p_bool;
  1760. }
  1761. /*
  1762. Variant::Variant(long unsigned int p_long) {
  1763. type=INT;
  1764. _data._int=p_long;
  1765. };
  1766. */
  1767. Variant::Variant(signed int p_int) {
  1768. type = INT;
  1769. _data._int = p_int;
  1770. }
  1771. Variant::Variant(unsigned int p_int) {
  1772. type = INT;
  1773. _data._int = p_int;
  1774. }
  1775. #ifdef NEED_LONG_INT
  1776. Variant::Variant(signed long p_int) {
  1777. type = INT;
  1778. _data._int = p_int;
  1779. }
  1780. Variant::Variant(unsigned long p_int) {
  1781. type = INT;
  1782. _data._int = p_int;
  1783. }
  1784. #endif
  1785. Variant::Variant(int64_t p_int) {
  1786. type = INT;
  1787. _data._int = p_int;
  1788. }
  1789. Variant::Variant(uint64_t p_int) {
  1790. type = INT;
  1791. _data._int = p_int;
  1792. }
  1793. Variant::Variant(signed short p_short) {
  1794. type = INT;
  1795. _data._int = p_short;
  1796. }
  1797. Variant::Variant(unsigned short p_short) {
  1798. type = INT;
  1799. _data._int = p_short;
  1800. }
  1801. Variant::Variant(signed char p_char) {
  1802. type = INT;
  1803. _data._int = p_char;
  1804. }
  1805. Variant::Variant(unsigned char p_char) {
  1806. type = INT;
  1807. _data._int = p_char;
  1808. }
  1809. Variant::Variant(float p_float) {
  1810. type = REAL;
  1811. _data._real = p_float;
  1812. }
  1813. Variant::Variant(double p_double) {
  1814. type = REAL;
  1815. _data._real = p_double;
  1816. }
  1817. Variant::Variant(const StringName &p_string) {
  1818. type = STRING;
  1819. memnew_placement(_data._mem, String(p_string.operator String()));
  1820. }
  1821. Variant::Variant(const String &p_string) {
  1822. type = STRING;
  1823. memnew_placement(_data._mem, String(p_string));
  1824. }
  1825. Variant::Variant(const char *const p_cstring) {
  1826. type = STRING;
  1827. memnew_placement(_data._mem, String((const char *)p_cstring));
  1828. }
  1829. Variant::Variant(const CharType *p_wstring) {
  1830. type = STRING;
  1831. memnew_placement(_data._mem, String(p_wstring));
  1832. }
  1833. Variant::Variant(const Vector3 &p_vector3) {
  1834. type = VECTOR3;
  1835. memnew_placement(_data._mem, Vector3(p_vector3));
  1836. }
  1837. Variant::Variant(const Vector2 &p_vector2) {
  1838. type = VECTOR2;
  1839. memnew_placement(_data._mem, Vector2(p_vector2));
  1840. }
  1841. Variant::Variant(const Rect2 &p_rect2) {
  1842. type = RECT2;
  1843. memnew_placement(_data._mem, Rect2(p_rect2));
  1844. }
  1845. Variant::Variant(const Plane &p_plane) {
  1846. type = PLANE;
  1847. memnew_placement(_data._mem, Plane(p_plane));
  1848. }
  1849. Variant::Variant(const ::AABB &p_aabb) {
  1850. type = AABB;
  1851. _data._aabb = memnew(::AABB(p_aabb));
  1852. }
  1853. Variant::Variant(const Basis &p_matrix) {
  1854. type = BASIS;
  1855. _data._basis = memnew(Basis(p_matrix));
  1856. }
  1857. Variant::Variant(const Quat &p_quat) {
  1858. type = QUAT;
  1859. memnew_placement(_data._mem, Quat(p_quat));
  1860. }
  1861. Variant::Variant(const Transform &p_transform) {
  1862. type = TRANSFORM;
  1863. _data._transform = memnew(Transform(p_transform));
  1864. }
  1865. Variant::Variant(const Transform2D &p_transform) {
  1866. type = TRANSFORM2D;
  1867. _data._transform2d = memnew(Transform2D(p_transform));
  1868. }
  1869. Variant::Variant(const Color &p_color) {
  1870. type = COLOR;
  1871. memnew_placement(_data._mem, Color(p_color));
  1872. }
  1873. Variant::Variant(const NodePath &p_node_path) {
  1874. type = NODE_PATH;
  1875. memnew_placement(_data._mem, NodePath(p_node_path));
  1876. }
  1877. Variant::Variant(const RefPtr &p_resource) {
  1878. type = OBJECT;
  1879. memnew_placement(_data._mem, ObjData);
  1880. _get_obj().rc = nullptr;
  1881. _get_obj().ref = p_resource;
  1882. }
  1883. Variant::Variant(const RID &p_rid) {
  1884. type = _RID;
  1885. memnew_placement(_data._mem, RID(p_rid));
  1886. }
  1887. Variant::Variant(const Object *p_object) {
  1888. type = OBJECT;
  1889. Object *obj = const_cast<Object *>(p_object);
  1890. memnew_placement(_data._mem, ObjData);
  1891. Reference *ref = Object::cast_to<Reference>(obj);
  1892. if (unlikely(ref)) {
  1893. *reinterpret_cast<Ref<Reference> *>(_get_obj().ref.get_data()) = Ref<Reference>(ref);
  1894. _get_obj().rc = nullptr;
  1895. } else {
  1896. _get_obj().rc = likely(obj) ? obj->_use_rc() : nullptr;
  1897. }
  1898. }
  1899. Variant::Variant(const Dictionary &p_dictionary) {
  1900. type = DICTIONARY;
  1901. memnew_placement(_data._mem, Dictionary(p_dictionary));
  1902. }
  1903. Variant::Variant(const Array &p_array) {
  1904. type = ARRAY;
  1905. memnew_placement(_data._mem, Array(p_array));
  1906. }
  1907. Variant::Variant(const PoolVector<Plane> &p_array) {
  1908. type = ARRAY;
  1909. Array *plane_array = memnew_placement(_data._mem, Array);
  1910. plane_array->resize(p_array.size());
  1911. for (int i = 0; i < p_array.size(); i++) {
  1912. plane_array->operator[](i) = Variant(p_array[i]);
  1913. }
  1914. }
  1915. Variant::Variant(const Vector<Plane> &p_array) {
  1916. type = ARRAY;
  1917. Array *plane_array = memnew_placement(_data._mem, Array);
  1918. plane_array->resize(p_array.size());
  1919. for (int i = 0; i < p_array.size(); i++) {
  1920. plane_array->operator[](i) = Variant(p_array[i]);
  1921. }
  1922. }
  1923. Variant::Variant(const Vector<RID> &p_array) {
  1924. type = ARRAY;
  1925. Array *rid_array = memnew_placement(_data._mem, Array);
  1926. rid_array->resize(p_array.size());
  1927. for (int i = 0; i < p_array.size(); i++) {
  1928. rid_array->set(i, Variant(p_array[i]));
  1929. }
  1930. }
  1931. Variant::Variant(const Vector<Vector2> &p_array) {
  1932. type = NIL;
  1933. PoolVector<Vector2> v;
  1934. int len = p_array.size();
  1935. if (len > 0) {
  1936. v.resize(len);
  1937. PoolVector<Vector2>::Write w = v.write();
  1938. const Vector2 *r = p_array.ptr();
  1939. for (int i = 0; i < len; i++) {
  1940. w[i] = r[i];
  1941. }
  1942. }
  1943. *this = v;
  1944. }
  1945. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1946. type = POOL_BYTE_ARRAY;
  1947. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1948. }
  1949. Variant::Variant(const PoolVector<int> &p_int_array) {
  1950. type = POOL_INT_ARRAY;
  1951. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  1952. }
  1953. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  1954. type = POOL_REAL_ARRAY;
  1955. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  1956. }
  1957. Variant::Variant(const PoolVector<String> &p_string_array) {
  1958. type = POOL_STRING_ARRAY;
  1959. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  1960. }
  1961. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  1962. type = POOL_VECTOR3_ARRAY;
  1963. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  1964. }
  1965. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  1966. type = POOL_VECTOR2_ARRAY;
  1967. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  1968. }
  1969. Variant::Variant(const PoolVector<Color> &p_color_array) {
  1970. type = POOL_COLOR_ARRAY;
  1971. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  1972. }
  1973. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  1974. PoolVector<Vector3> vertices;
  1975. int face_count = p_face_array.size();
  1976. vertices.resize(face_count * 3);
  1977. if (face_count) {
  1978. PoolVector<Face3>::Read r = p_face_array.read();
  1979. PoolVector<Vector3>::Write w = vertices.write();
  1980. for (int i = 0; i < face_count; i++) {
  1981. for (int j = 0; j < 3; j++) {
  1982. w[i * 3 + j] = r[i].vertex[j];
  1983. }
  1984. }
  1985. }
  1986. type = NIL;
  1987. *this = vertices;
  1988. }
  1989. /* helpers */
  1990. Variant::Variant(const Vector<Variant> &p_array) {
  1991. type = NIL;
  1992. Array v;
  1993. int len = p_array.size();
  1994. v.resize(len);
  1995. for (int i = 0; i < len; i++) {
  1996. v.set(i, p_array[i]);
  1997. }
  1998. *this = v;
  1999. }
  2000. Variant::Variant(const Vector<uint8_t> &p_array) {
  2001. type = NIL;
  2002. PoolVector<uint8_t> v;
  2003. int len = p_array.size();
  2004. v.resize(len);
  2005. for (int i = 0; i < len; i++) {
  2006. v.set(i, p_array[i]);
  2007. }
  2008. *this = v;
  2009. }
  2010. Variant::Variant(const Vector<int> &p_array) {
  2011. type = NIL;
  2012. PoolVector<int> v;
  2013. int len = p_array.size();
  2014. v.resize(len);
  2015. for (int i = 0; i < len; i++) {
  2016. v.set(i, p_array[i]);
  2017. }
  2018. *this = v;
  2019. }
  2020. Variant::Variant(const Vector<real_t> &p_array) {
  2021. type = NIL;
  2022. PoolVector<real_t> v;
  2023. int len = p_array.size();
  2024. v.resize(len);
  2025. for (int i = 0; i < len; i++) {
  2026. v.set(i, p_array[i]);
  2027. }
  2028. *this = v;
  2029. }
  2030. Variant::Variant(const Vector<String> &p_array) {
  2031. type = NIL;
  2032. PoolVector<String> v;
  2033. int len = p_array.size();
  2034. v.resize(len);
  2035. for (int i = 0; i < len; i++) {
  2036. v.set(i, p_array[i]);
  2037. }
  2038. *this = v;
  2039. }
  2040. Variant::Variant(const Vector<StringName> &p_array) {
  2041. type = NIL;
  2042. PoolVector<String> v;
  2043. int len = p_array.size();
  2044. v.resize(len);
  2045. for (int i = 0; i < len; i++) {
  2046. v.set(i, p_array[i]);
  2047. }
  2048. *this = v;
  2049. }
  2050. Variant::Variant(const Vector<Vector3> &p_array) {
  2051. type = NIL;
  2052. PoolVector<Vector3> v;
  2053. int len = p_array.size();
  2054. if (len > 0) {
  2055. v.resize(len);
  2056. PoolVector<Vector3>::Write w = v.write();
  2057. const Vector3 *r = p_array.ptr();
  2058. for (int i = 0; i < len; i++) {
  2059. w[i] = r[i];
  2060. }
  2061. }
  2062. *this = v;
  2063. }
  2064. Variant::Variant(const Vector<Color> &p_array) {
  2065. type = NIL;
  2066. PoolVector<Color> v;
  2067. int len = p_array.size();
  2068. v.resize(len);
  2069. for (int i = 0; i < len; i++) {
  2070. v.set(i, p_array[i]);
  2071. }
  2072. *this = v;
  2073. }
  2074. void Variant::operator=(const Variant &p_variant) {
  2075. if (unlikely(this == &p_variant)) {
  2076. return;
  2077. }
  2078. if (unlikely(type != p_variant.type)) {
  2079. reference(p_variant);
  2080. return;
  2081. }
  2082. switch (p_variant.type) {
  2083. case NIL: {
  2084. // none
  2085. } break;
  2086. // atomic types
  2087. case BOOL: {
  2088. _data._bool = p_variant._data._bool;
  2089. } break;
  2090. case INT: {
  2091. _data._int = p_variant._data._int;
  2092. } break;
  2093. case REAL: {
  2094. _data._real = p_variant._data._real;
  2095. } break;
  2096. case STRING: {
  2097. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2098. } break;
  2099. // math types
  2100. case VECTOR2: {
  2101. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2102. } break;
  2103. case RECT2: {
  2104. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2105. } break;
  2106. case TRANSFORM2D: {
  2107. *_data._transform2d = *(p_variant._data._transform2d);
  2108. } break;
  2109. case VECTOR3: {
  2110. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2111. } break;
  2112. case PLANE: {
  2113. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2114. } break;
  2115. case AABB: {
  2116. *_data._aabb = *(p_variant._data._aabb);
  2117. } break;
  2118. case QUAT: {
  2119. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  2120. } break;
  2121. case BASIS: {
  2122. *_data._basis = *(p_variant._data._basis);
  2123. } break;
  2124. case TRANSFORM: {
  2125. *_data._transform = *(p_variant._data._transform);
  2126. } break;
  2127. // misc types
  2128. case COLOR: {
  2129. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2130. } break;
  2131. case _RID: {
  2132. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  2133. } break;
  2134. case OBJECT: {
  2135. if (likely(_get_obj().rc)) {
  2136. if (unlikely(_get_obj().rc->decrement())) {
  2137. memdelete(_get_obj().rc);
  2138. }
  2139. }
  2140. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  2141. if (likely(_get_obj().rc)) {
  2142. _get_obj().rc->increment();
  2143. }
  2144. } break;
  2145. case NODE_PATH: {
  2146. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2147. } break;
  2148. case DICTIONARY: {
  2149. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2150. } break;
  2151. case ARRAY: {
  2152. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2153. } break;
  2154. // arrays
  2155. case POOL_BYTE_ARRAY: {
  2156. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  2157. } break;
  2158. case POOL_INT_ARRAY: {
  2159. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  2160. } break;
  2161. case POOL_REAL_ARRAY: {
  2162. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  2163. } break;
  2164. case POOL_STRING_ARRAY: {
  2165. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  2166. } break;
  2167. case POOL_VECTOR2_ARRAY: {
  2168. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  2169. } break;
  2170. case POOL_VECTOR3_ARRAY: {
  2171. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  2172. } break;
  2173. case POOL_COLOR_ARRAY: {
  2174. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  2175. } break;
  2176. default: {
  2177. }
  2178. }
  2179. }
  2180. Variant::Variant(const IP_Address &p_address) {
  2181. type = STRING;
  2182. memnew_placement(_data._mem, String(p_address));
  2183. }
  2184. Variant::Variant(const Variant &p_variant) {
  2185. type = NIL;
  2186. reference(p_variant);
  2187. }
  2188. /*
  2189. Variant::~Variant() {
  2190. clear();
  2191. }*/
  2192. uint32_t Variant::hash() const {
  2193. return recursive_hash(0);
  2194. }
  2195. uint32_t Variant::recursive_hash(int p_recursion_count) const {
  2196. switch (type) {
  2197. case NIL: {
  2198. return 0;
  2199. } break;
  2200. case BOOL: {
  2201. return _data._bool ? 1 : 0;
  2202. } break;
  2203. case INT: {
  2204. return _data._int;
  2205. } break;
  2206. case REAL: {
  2207. return hash_djb2_one_float(_data._real);
  2208. } break;
  2209. case STRING: {
  2210. return reinterpret_cast<const String *>(_data._mem)->hash();
  2211. } break;
  2212. // math types
  2213. case VECTOR2: {
  2214. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  2215. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  2216. } break;
  2217. case RECT2: {
  2218. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  2219. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  2220. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  2221. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  2222. } break;
  2223. case TRANSFORM2D: {
  2224. uint32_t hash = 5831;
  2225. for (int i = 0; i < 3; i++) {
  2226. for (int j = 0; j < 2; j++) {
  2227. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2228. }
  2229. }
  2230. return hash;
  2231. } break;
  2232. case VECTOR3: {
  2233. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2234. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2235. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2236. } break;
  2237. case PLANE: {
  2238. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2239. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2240. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2241. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2242. } break;
  2243. /*
  2244. case QUAT: {
  2245. } break;*/
  2246. case AABB: {
  2247. uint32_t hash = 5831;
  2248. for (int i = 0; i < 3; i++) {
  2249. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2250. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2251. }
  2252. return hash;
  2253. } break;
  2254. case QUAT: {
  2255. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2256. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2257. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2258. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2259. } break;
  2260. case BASIS: {
  2261. uint32_t hash = 5831;
  2262. for (int i = 0; i < 3; i++) {
  2263. for (int j = 0; j < 3; j++) {
  2264. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2265. }
  2266. }
  2267. return hash;
  2268. } break;
  2269. case TRANSFORM: {
  2270. uint32_t hash = 5831;
  2271. for (int i = 0; i < 3; i++) {
  2272. for (int j = 0; j < 3; j++) {
  2273. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2274. }
  2275. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2276. }
  2277. return hash;
  2278. } break;
  2279. // misc types
  2280. case COLOR: {
  2281. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2282. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2283. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2284. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2285. } break;
  2286. case _RID: {
  2287. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2288. } break;
  2289. case OBJECT: {
  2290. return hash_djb2_one_64(make_uint64_t(_UNSAFE_OBJ_PROXY_PTR(*this)));
  2291. } break;
  2292. case NODE_PATH: {
  2293. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2294. } break;
  2295. case DICTIONARY: {
  2296. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(p_recursion_count);
  2297. } break;
  2298. case ARRAY: {
  2299. return reinterpret_cast<const Array *>(_data._mem)->recursive_hash(p_recursion_count);
  2300. } break;
  2301. case POOL_BYTE_ARRAY: {
  2302. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2303. int len = arr.size();
  2304. if (likely(len)) {
  2305. PoolVector<uint8_t>::Read r = arr.read();
  2306. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2307. } else {
  2308. return hash_djb2_one_64(0);
  2309. }
  2310. } break;
  2311. case POOL_INT_ARRAY: {
  2312. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2313. int len = arr.size();
  2314. if (likely(len)) {
  2315. PoolVector<int>::Read r = arr.read();
  2316. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2317. } else {
  2318. return hash_djb2_one_64(0);
  2319. }
  2320. } break;
  2321. case POOL_REAL_ARRAY: {
  2322. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2323. int len = arr.size();
  2324. if (likely(len)) {
  2325. PoolVector<real_t>::Read r = arr.read();
  2326. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2327. } else {
  2328. return hash_djb2_one_float(0.0);
  2329. }
  2330. } break;
  2331. case POOL_STRING_ARRAY: {
  2332. uint32_t hash = 5831;
  2333. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2334. int len = arr.size();
  2335. if (likely(len)) {
  2336. PoolVector<String>::Read r = arr.read();
  2337. for (int i = 0; i < len; i++) {
  2338. hash = hash_djb2_one_32(r[i].hash(), hash);
  2339. }
  2340. }
  2341. return hash;
  2342. } break;
  2343. case POOL_VECTOR2_ARRAY: {
  2344. uint32_t hash = 5831;
  2345. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2346. int len = arr.size();
  2347. if (likely(len)) {
  2348. PoolVector<Vector2>::Read r = arr.read();
  2349. for (int i = 0; i < len; i++) {
  2350. hash = hash_djb2_one_float(r[i].x, hash);
  2351. hash = hash_djb2_one_float(r[i].y, hash);
  2352. }
  2353. }
  2354. return hash;
  2355. } break;
  2356. case POOL_VECTOR3_ARRAY: {
  2357. uint32_t hash = 5831;
  2358. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2359. int len = arr.size();
  2360. if (likely(len)) {
  2361. PoolVector<Vector3>::Read r = arr.read();
  2362. for (int i = 0; i < len; i++) {
  2363. hash = hash_djb2_one_float(r[i].x, hash);
  2364. hash = hash_djb2_one_float(r[i].y, hash);
  2365. hash = hash_djb2_one_float(r[i].z, hash);
  2366. }
  2367. }
  2368. return hash;
  2369. } break;
  2370. case POOL_COLOR_ARRAY: {
  2371. uint32_t hash = 5831;
  2372. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2373. int len = arr.size();
  2374. if (likely(len)) {
  2375. PoolVector<Color>::Read r = arr.read();
  2376. for (int i = 0; i < len; i++) {
  2377. hash = hash_djb2_one_float(r[i].r, hash);
  2378. hash = hash_djb2_one_float(r[i].g, hash);
  2379. hash = hash_djb2_one_float(r[i].b, hash);
  2380. hash = hash_djb2_one_float(r[i].a, hash);
  2381. }
  2382. }
  2383. return hash;
  2384. } break;
  2385. default: {
  2386. }
  2387. }
  2388. return 0;
  2389. }
  2390. #define hash_compare_scalar(p_lhs, p_rhs) \
  2391. (((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2392. #define hash_compare_vector2(p_lhs, p_rhs) \
  2393. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2394. hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2395. #define hash_compare_vector3(p_lhs, p_rhs) \
  2396. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2397. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2398. hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2399. #define hash_compare_quat(p_lhs, p_rhs) \
  2400. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2401. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2402. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2403. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2404. #define hash_compare_color(p_lhs, p_rhs) \
  2405. (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
  2406. hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
  2407. hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
  2408. hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2409. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2410. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2411. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2412. \
  2413. if (l.size() != r.size()) \
  2414. return false; \
  2415. \
  2416. PoolVector<p_type>::Read lr = l.read(); \
  2417. PoolVector<p_type>::Read rr = r.read(); \
  2418. \
  2419. for (int i = 0; i < l.size(); ++i) { \
  2420. if (!p_compare_func((lr[i]), (rr[i]))) \
  2421. return false; \
  2422. } \
  2423. \
  2424. return true
  2425. bool Variant::hash_compare(const Variant &p_variant) const {
  2426. if (type != p_variant.type) {
  2427. return false;
  2428. }
  2429. switch (type) {
  2430. case INT: {
  2431. return _data._int == p_variant._data._int;
  2432. } break;
  2433. case REAL: {
  2434. return hash_compare_scalar(_data._real, p_variant._data._real);
  2435. } break;
  2436. case STRING: {
  2437. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2438. } break;
  2439. case VECTOR2: {
  2440. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2441. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2442. return hash_compare_vector2(*l, *r);
  2443. } break;
  2444. case RECT2: {
  2445. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2446. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2447. return hash_compare_vector2(l->position, r->position) &&
  2448. hash_compare_vector2(l->size, r->size);
  2449. } break;
  2450. case TRANSFORM2D: {
  2451. Transform2D *l = _data._transform2d;
  2452. Transform2D *r = p_variant._data._transform2d;
  2453. for (int i = 0; i < 3; i++) {
  2454. if (!hash_compare_vector2(l->elements[i], r->elements[i])) {
  2455. return false;
  2456. }
  2457. }
  2458. return true;
  2459. } break;
  2460. case VECTOR3: {
  2461. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2462. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2463. return hash_compare_vector3(*l, *r);
  2464. } break;
  2465. case PLANE: {
  2466. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2467. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2468. return hash_compare_vector3(l->normal, r->normal) &&
  2469. hash_compare_scalar(l->d, r->d);
  2470. } break;
  2471. case AABB: {
  2472. const ::AABB *l = _data._aabb;
  2473. const ::AABB *r = p_variant._data._aabb;
  2474. return hash_compare_vector3(l->position, r->position) &&
  2475. hash_compare_vector3(l->size, r->size);
  2476. } break;
  2477. case QUAT: {
  2478. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2479. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2480. return hash_compare_quat(*l, *r);
  2481. } break;
  2482. case BASIS: {
  2483. const Basis *l = _data._basis;
  2484. const Basis *r = p_variant._data._basis;
  2485. for (int i = 0; i < 3; i++) {
  2486. if (!hash_compare_vector3(l->elements[i], r->elements[i])) {
  2487. return false;
  2488. }
  2489. }
  2490. return true;
  2491. } break;
  2492. case TRANSFORM: {
  2493. const Transform *l = _data._transform;
  2494. const Transform *r = p_variant._data._transform;
  2495. for (int i = 0; i < 3; i++) {
  2496. if (!hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])) {
  2497. return false;
  2498. }
  2499. }
  2500. return hash_compare_vector3(l->origin, r->origin);
  2501. } break;
  2502. case COLOR: {
  2503. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2504. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2505. return hash_compare_color(*l, *r);
  2506. } break;
  2507. case ARRAY: {
  2508. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2509. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2510. if (l.size() != r.size()) {
  2511. return false;
  2512. }
  2513. for (int i = 0; i < l.size(); ++i) {
  2514. if (!l[i].hash_compare(r[i])) {
  2515. return false;
  2516. }
  2517. }
  2518. return true;
  2519. } break;
  2520. case POOL_REAL_ARRAY: {
  2521. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2522. } break;
  2523. case POOL_VECTOR2_ARRAY: {
  2524. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2525. } break;
  2526. case POOL_VECTOR3_ARRAY: {
  2527. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2528. } break;
  2529. case POOL_COLOR_ARRAY: {
  2530. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2531. } break;
  2532. default:
  2533. bool v;
  2534. Variant r;
  2535. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2536. return r;
  2537. }
  2538. return false;
  2539. }
  2540. bool Variant::is_ref() const {
  2541. return type == OBJECT && !_get_obj().ref.is_null();
  2542. }
  2543. Vector<Variant> varray() {
  2544. return Vector<Variant>();
  2545. }
  2546. Vector<Variant> varray(const Variant &p_arg1) {
  2547. Vector<Variant> v;
  2548. v.push_back(p_arg1);
  2549. return v;
  2550. }
  2551. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2552. Vector<Variant> v;
  2553. v.push_back(p_arg1);
  2554. v.push_back(p_arg2);
  2555. return v;
  2556. }
  2557. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2558. Vector<Variant> v;
  2559. v.push_back(p_arg1);
  2560. v.push_back(p_arg2);
  2561. v.push_back(p_arg3);
  2562. return v;
  2563. }
  2564. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2565. Vector<Variant> v;
  2566. v.push_back(p_arg1);
  2567. v.push_back(p_arg2);
  2568. v.push_back(p_arg3);
  2569. v.push_back(p_arg4);
  2570. return v;
  2571. }
  2572. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2573. Vector<Variant> v;
  2574. v.push_back(p_arg1);
  2575. v.push_back(p_arg2);
  2576. v.push_back(p_arg3);
  2577. v.push_back(p_arg4);
  2578. v.push_back(p_arg5);
  2579. return v;
  2580. }
  2581. void Variant::static_assign(const Variant &p_variant) {
  2582. }
  2583. bool Variant::is_shared() const {
  2584. switch (type) {
  2585. case OBJECT:
  2586. return true;
  2587. case ARRAY:
  2588. return true;
  2589. case DICTIONARY:
  2590. return true;
  2591. default: {
  2592. }
  2593. }
  2594. return false;
  2595. }
  2596. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2597. VARIANT_ARGPTRS;
  2598. int argc = 0;
  2599. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2600. if (argptr[i]->get_type() == Variant::NIL) {
  2601. break;
  2602. }
  2603. argc++;
  2604. }
  2605. CallError error;
  2606. Variant ret = call(p_method, argptr, argc, error);
  2607. switch (error.error) {
  2608. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2609. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2610. ERR_PRINT(err.utf8().get_data());
  2611. } break;
  2612. case CallError::CALL_ERROR_INVALID_METHOD: {
  2613. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2614. ERR_PRINT(err.utf8().get_data());
  2615. } break;
  2616. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2617. String err = "Too many arguments for method '" + p_method + "'";
  2618. ERR_PRINT(err.utf8().get_data());
  2619. } break;
  2620. default: {
  2621. }
  2622. }
  2623. return ret;
  2624. }
  2625. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2626. r_value = Variant();
  2627. }
  2628. String Variant::get_construct_string() const {
  2629. String vars;
  2630. VariantWriter::write_to_string(*this, vars);
  2631. return vars;
  2632. }
  2633. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2634. String err_text;
  2635. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2636. int errorarg = ce.argument;
  2637. if (p_argptrs) {
  2638. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(ce.expected) + ".";
  2639. } else {
  2640. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2641. }
  2642. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2643. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2644. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2645. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2646. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2647. err_text = "Method not found.";
  2648. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2649. err_text = "Instance is null";
  2650. } else if (ce.error == Variant::CallError::CALL_OK) {
  2651. return "Call OK";
  2652. }
  2653. String class_name = p_base->get_class();
  2654. Ref<Script> script = p_base->get_script();
  2655. if (script.is_valid() && script->get_path().is_resource_file()) {
  2656. class_name += "(" + script->get_path().get_file() + ")";
  2657. }
  2658. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2659. }
  2660. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2661. Array args;
  2662. if (p1.get_type() != Variant::NIL) {
  2663. args.push_back(p1);
  2664. if (p2.get_type() != Variant::NIL) {
  2665. args.push_back(p2);
  2666. if (p3.get_type() != Variant::NIL) {
  2667. args.push_back(p3);
  2668. if (p4.get_type() != Variant::NIL) {
  2669. args.push_back(p4);
  2670. if (p5.get_type() != Variant::NIL) {
  2671. args.push_back(p5);
  2672. }
  2673. }
  2674. }
  2675. }
  2676. }
  2677. bool error = false;
  2678. String fmt = p_text.sprintf(args, &error);
  2679. ERR_FAIL_COND_V_MSG(error, String(), fmt);
  2680. return fmt;
  2681. }