variant.cpp 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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::operator==(const Variant &p_variant) const {
  510. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  511. return false;
  512. }
  513. bool v;
  514. Variant r;
  515. evaluate(OP_EQUAL, *this, p_variant, r, v);
  516. return r;
  517. }
  518. bool Variant::operator!=(const Variant &p_variant) const {
  519. if (type != p_variant.type) { //evaluation of operator== needs to be more strict
  520. return true;
  521. }
  522. bool v;
  523. Variant r;
  524. evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
  525. return r;
  526. }
  527. bool Variant::operator<(const Variant &p_variant) const {
  528. if (type != p_variant.type) { //if types differ, then order by type first
  529. return type < p_variant.type;
  530. }
  531. bool v;
  532. Variant r;
  533. evaluate(OP_LESS, *this, p_variant, r, v);
  534. return r;
  535. }
  536. bool Variant::is_zero() const {
  537. switch (type) {
  538. case NIL: {
  539. return true;
  540. } break;
  541. // atomic types
  542. case BOOL: {
  543. return !(_data._bool);
  544. } break;
  545. case INT: {
  546. return _data._int == 0;
  547. } break;
  548. case REAL: {
  549. return _data._real == 0;
  550. } break;
  551. case STRING: {
  552. return *reinterpret_cast<const String *>(_data._mem) == String();
  553. } break;
  554. // math types
  555. case VECTOR2: {
  556. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
  557. } break;
  558. case RECT2: {
  559. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
  560. } break;
  561. case TRANSFORM2D: {
  562. return *_data._transform2d == Transform2D();
  563. } break;
  564. case VECTOR3: {
  565. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
  566. } break;
  567. case PLANE: {
  568. return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
  569. } break;
  570. /*
  571. case QUAT: {
  572. } break;*/
  573. case AABB: {
  574. return *_data._aabb == ::AABB();
  575. } break;
  576. case QUAT: {
  577. return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
  578. } break;
  579. case BASIS: {
  580. return *_data._basis == Basis();
  581. } break;
  582. case TRANSFORM: {
  583. return *_data._transform == Transform();
  584. } break;
  585. // misc types
  586. case COLOR: {
  587. return *reinterpret_cast<const Color *>(_data._mem) == Color();
  588. } break;
  589. case _RID: {
  590. return *reinterpret_cast<const RID *>(_data._mem) == RID();
  591. } break;
  592. case OBJECT: {
  593. return _UNSAFE_OBJ_PROXY_PTR(*this) == nullptr;
  594. } break;
  595. case NODE_PATH: {
  596. return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
  597. } break;
  598. case DICTIONARY: {
  599. return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
  600. } break;
  601. case ARRAY: {
  602. return reinterpret_cast<const Array *>(_data._mem)->empty();
  603. } break;
  604. // arrays
  605. case POOL_BYTE_ARRAY: {
  606. return reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem)->size() == 0;
  607. } break;
  608. case POOL_INT_ARRAY: {
  609. return reinterpret_cast<const PoolVector<int> *>(_data._mem)->size() == 0;
  610. } break;
  611. case POOL_REAL_ARRAY: {
  612. return reinterpret_cast<const PoolVector<real_t> *>(_data._mem)->size() == 0;
  613. } break;
  614. case POOL_STRING_ARRAY: {
  615. return reinterpret_cast<const PoolVector<String> *>(_data._mem)->size() == 0;
  616. } break;
  617. case POOL_VECTOR2_ARRAY: {
  618. return reinterpret_cast<const PoolVector<Vector2> *>(_data._mem)->size() == 0;
  619. } break;
  620. case POOL_VECTOR3_ARRAY: {
  621. return reinterpret_cast<const PoolVector<Vector3> *>(_data._mem)->size() == 0;
  622. } break;
  623. case POOL_COLOR_ARRAY: {
  624. return reinterpret_cast<const PoolVector<Color> *>(_data._mem)->size() == 0;
  625. } break;
  626. default: {
  627. }
  628. }
  629. return false;
  630. }
  631. bool Variant::is_one() const {
  632. switch (type) {
  633. case NIL: {
  634. return true;
  635. } break;
  636. // atomic types
  637. case BOOL: {
  638. return _data._bool;
  639. } break;
  640. case INT: {
  641. return _data._int == 1;
  642. } break;
  643. case REAL: {
  644. return _data._real == 1;
  645. } break;
  646. case VECTOR2: {
  647. return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
  648. } break;
  649. case RECT2: {
  650. return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
  651. } break;
  652. case VECTOR3: {
  653. return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
  654. } break;
  655. case PLANE: {
  656. return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
  657. } break;
  658. case COLOR: {
  659. return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
  660. } break;
  661. default: {
  662. return !is_zero();
  663. }
  664. }
  665. return false;
  666. }
  667. ObjectID Variant::get_object_instance_id() const {
  668. if (type != OBJECT) {
  669. return 0;
  670. }
  671. #ifdef DEBUG_ENABLED
  672. if (is_ref()) {
  673. return !_get_obj().ref.is_null() ? _REF_OBJ_PTR(*this)->get_instance_id() : 0;
  674. } else {
  675. return _get_obj().rc->instance_id;
  676. }
  677. #else
  678. if (is_ref() && _get_obj().ref.is_null()) {
  679. return 0;
  680. } else {
  681. return _get_obj().obj->get_instance_id();
  682. }
  683. #endif
  684. }
  685. void Variant::reference(const Variant &p_variant) {
  686. switch (type) {
  687. case NIL:
  688. case BOOL:
  689. case INT:
  690. case REAL:
  691. break;
  692. default:
  693. clear();
  694. }
  695. type = p_variant.type;
  696. switch (p_variant.type) {
  697. case NIL: {
  698. // none
  699. } break;
  700. // atomic types
  701. case BOOL: {
  702. _data._bool = p_variant._data._bool;
  703. } break;
  704. case INT: {
  705. _data._int = p_variant._data._int;
  706. } break;
  707. case REAL: {
  708. _data._real = p_variant._data._real;
  709. } break;
  710. case STRING: {
  711. memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
  712. } break;
  713. // math types
  714. case VECTOR2: {
  715. memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
  716. } break;
  717. case RECT2: {
  718. memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
  719. } break;
  720. case TRANSFORM2D: {
  721. _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d));
  722. } break;
  723. case VECTOR3: {
  724. memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
  725. } break;
  726. case PLANE: {
  727. memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
  728. } break;
  729. case AABB: {
  730. _data._aabb = memnew(::AABB(*p_variant._data._aabb));
  731. } break;
  732. case QUAT: {
  733. memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
  734. } break;
  735. case BASIS: {
  736. _data._basis = memnew(Basis(*p_variant._data._basis));
  737. } break;
  738. case TRANSFORM: {
  739. _data._transform = memnew(Transform(*p_variant._data._transform));
  740. } break;
  741. // misc types
  742. case COLOR: {
  743. memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
  744. } break;
  745. case _RID: {
  746. memnew_placement(_data._mem, RID(*reinterpret_cast<const RID *>(p_variant._data._mem)));
  747. } break;
  748. case OBJECT: {
  749. memnew_placement(_data._mem, ObjData(p_variant._get_obj()));
  750. #ifdef DEBUG_ENABLED
  751. if (_get_obj().rc) {
  752. _get_obj().rc->increment();
  753. }
  754. #endif
  755. } break;
  756. case NODE_PATH: {
  757. memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
  758. } break;
  759. case DICTIONARY: {
  760. memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
  761. } break;
  762. case ARRAY: {
  763. memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
  764. } break;
  765. // arrays
  766. case POOL_BYTE_ARRAY: {
  767. memnew_placement(_data._mem, PoolVector<uint8_t>(*reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem)));
  768. } break;
  769. case POOL_INT_ARRAY: {
  770. memnew_placement(_data._mem, PoolVector<int>(*reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem)));
  771. } break;
  772. case POOL_REAL_ARRAY: {
  773. memnew_placement(_data._mem, PoolVector<real_t>(*reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem)));
  774. } break;
  775. case POOL_STRING_ARRAY: {
  776. memnew_placement(_data._mem, PoolVector<String>(*reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem)));
  777. } break;
  778. case POOL_VECTOR2_ARRAY: {
  779. memnew_placement(_data._mem, PoolVector<Vector2>(*reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem)));
  780. } break;
  781. case POOL_VECTOR3_ARRAY: {
  782. memnew_placement(_data._mem, PoolVector<Vector3>(*reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem)));
  783. } break;
  784. case POOL_COLOR_ARRAY: {
  785. memnew_placement(_data._mem, PoolVector<Color>(*reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem)));
  786. } break;
  787. default: {
  788. }
  789. }
  790. }
  791. void Variant::zero() {
  792. switch (type) {
  793. case NIL:
  794. break;
  795. case BOOL:
  796. this->_data._bool = false;
  797. break;
  798. case INT:
  799. this->_data._int = 0;
  800. break;
  801. case REAL:
  802. this->_data._real = 0;
  803. break;
  804. case VECTOR2:
  805. *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2();
  806. break;
  807. case RECT2:
  808. *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2();
  809. break;
  810. case VECTOR3:
  811. *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3();
  812. break;
  813. case PLANE:
  814. *reinterpret_cast<Plane *>(this->_data._mem) = Plane();
  815. break;
  816. case QUAT:
  817. *reinterpret_cast<Quat *>(this->_data._mem) = Quat();
  818. break;
  819. case COLOR:
  820. *reinterpret_cast<Color *>(this->_data._mem) = Color();
  821. break;
  822. default:
  823. this->clear();
  824. break;
  825. }
  826. }
  827. void Variant::clear() {
  828. switch (type) {
  829. case STRING: {
  830. reinterpret_cast<String *>(_data._mem)->~String();
  831. } break;
  832. /*
  833. // no point, they don't allocate memory
  834. VECTOR3,
  835. PLANE,
  836. QUAT,
  837. COLOR,
  838. VECTOR2,
  839. RECT2
  840. */
  841. case TRANSFORM2D: {
  842. memdelete(_data._transform2d);
  843. } break;
  844. case AABB: {
  845. memdelete(_data._aabb);
  846. } break;
  847. case BASIS: {
  848. memdelete(_data._basis);
  849. } break;
  850. case TRANSFORM: {
  851. memdelete(_data._transform);
  852. } break;
  853. // misc types
  854. case NODE_PATH: {
  855. reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
  856. } break;
  857. case OBJECT: {
  858. #ifdef DEBUG_ENABLED
  859. if (likely(_get_obj().rc)) {
  860. if (unlikely(_get_obj().rc->decrement())) {
  861. memdelete(_get_obj().rc);
  862. }
  863. } else {
  864. _get_obj().ref.unref();
  865. }
  866. #else
  867. _get_obj().obj = NULL;
  868. _get_obj().ref.unref();
  869. #endif
  870. } break;
  871. case _RID: {
  872. // not much need probably
  873. reinterpret_cast<RID *>(_data._mem)->~RID();
  874. } break;
  875. case DICTIONARY: {
  876. reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
  877. } break;
  878. case ARRAY: {
  879. reinterpret_cast<Array *>(_data._mem)->~Array();
  880. } break;
  881. // arrays
  882. case POOL_BYTE_ARRAY: {
  883. reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>();
  884. } break;
  885. case POOL_INT_ARRAY: {
  886. reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>();
  887. } break;
  888. case POOL_REAL_ARRAY: {
  889. reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>();
  890. } break;
  891. case POOL_STRING_ARRAY: {
  892. reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>();
  893. } break;
  894. case POOL_VECTOR2_ARRAY: {
  895. reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>();
  896. } break;
  897. case POOL_VECTOR3_ARRAY: {
  898. reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>();
  899. } break;
  900. case POOL_COLOR_ARRAY: {
  901. reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>();
  902. } break;
  903. default: {
  904. } /* not needed */
  905. }
  906. type = NIL;
  907. }
  908. Variant::operator signed int() const {
  909. switch (type) {
  910. case NIL:
  911. return 0;
  912. case BOOL:
  913. return _data._bool ? 1 : 0;
  914. case INT:
  915. return _data._int;
  916. case REAL:
  917. return _data._real;
  918. case STRING:
  919. return operator String().to_int();
  920. default: {
  921. return 0;
  922. }
  923. }
  924. }
  925. Variant::operator unsigned int() const {
  926. switch (type) {
  927. case NIL:
  928. return 0;
  929. case BOOL:
  930. return _data._bool ? 1 : 0;
  931. case INT:
  932. return _data._int;
  933. case REAL:
  934. return _data._real;
  935. case STRING:
  936. return operator String().to_int();
  937. default: {
  938. return 0;
  939. }
  940. }
  941. }
  942. Variant::operator int64_t() const {
  943. switch (type) {
  944. case NIL:
  945. return 0;
  946. case BOOL:
  947. return _data._bool ? 1 : 0;
  948. case INT:
  949. return _data._int;
  950. case REAL:
  951. return _data._real;
  952. case STRING:
  953. return operator String().to_int64();
  954. default: {
  955. return 0;
  956. }
  957. }
  958. }
  959. /*
  960. Variant::operator long unsigned int() const {
  961. switch( type ) {
  962. case NIL: return 0;
  963. case BOOL: return _data._bool ? 1 : 0;
  964. case INT: return _data._int;
  965. case REAL: return _data._real;
  966. case STRING: return operator String().to_int();
  967. default: {
  968. return 0;
  969. }
  970. }
  971. return 0;
  972. };
  973. */
  974. Variant::operator uint64_t() const {
  975. switch (type) {
  976. case NIL:
  977. return 0;
  978. case BOOL:
  979. return _data._bool ? 1 : 0;
  980. case INT:
  981. return _data._int;
  982. case REAL:
  983. return _data._real;
  984. case STRING:
  985. return operator String().to_int();
  986. default: {
  987. return 0;
  988. }
  989. }
  990. }
  991. #ifdef NEED_LONG_INT
  992. Variant::operator signed long() const {
  993. switch (type) {
  994. case NIL:
  995. return 0;
  996. case BOOL:
  997. return _data._bool ? 1 : 0;
  998. case INT:
  999. return _data._int;
  1000. case REAL:
  1001. return _data._real;
  1002. case STRING:
  1003. return operator String().to_int();
  1004. default: {
  1005. return 0;
  1006. }
  1007. }
  1008. return 0;
  1009. };
  1010. Variant::operator unsigned long() const {
  1011. switch (type) {
  1012. case NIL:
  1013. return 0;
  1014. case BOOL:
  1015. return _data._bool ? 1 : 0;
  1016. case INT:
  1017. return _data._int;
  1018. case REAL:
  1019. return _data._real;
  1020. case STRING:
  1021. return operator String().to_int();
  1022. default: {
  1023. return 0;
  1024. }
  1025. }
  1026. return 0;
  1027. };
  1028. #endif
  1029. Variant::operator signed short() const {
  1030. switch (type) {
  1031. case NIL:
  1032. return 0;
  1033. case BOOL:
  1034. return _data._bool ? 1 : 0;
  1035. case INT:
  1036. return _data._int;
  1037. case REAL:
  1038. return _data._real;
  1039. case STRING:
  1040. return operator String().to_int();
  1041. default: {
  1042. return 0;
  1043. }
  1044. }
  1045. }
  1046. Variant::operator unsigned short() const {
  1047. switch (type) {
  1048. case NIL:
  1049. return 0;
  1050. case BOOL:
  1051. return _data._bool ? 1 : 0;
  1052. case INT:
  1053. return _data._int;
  1054. case REAL:
  1055. return _data._real;
  1056. case STRING:
  1057. return operator String().to_int();
  1058. default: {
  1059. return 0;
  1060. }
  1061. }
  1062. }
  1063. Variant::operator signed char() const {
  1064. switch (type) {
  1065. case NIL:
  1066. return 0;
  1067. case BOOL:
  1068. return _data._bool ? 1 : 0;
  1069. case INT:
  1070. return _data._int;
  1071. case REAL:
  1072. return _data._real;
  1073. case STRING:
  1074. return operator String().to_int();
  1075. default: {
  1076. return 0;
  1077. }
  1078. }
  1079. }
  1080. Variant::operator unsigned char() const {
  1081. switch (type) {
  1082. case NIL:
  1083. return 0;
  1084. case BOOL:
  1085. return _data._bool ? 1 : 0;
  1086. case INT:
  1087. return _data._int;
  1088. case REAL:
  1089. return _data._real;
  1090. case STRING:
  1091. return operator String().to_int();
  1092. default: {
  1093. return 0;
  1094. }
  1095. }
  1096. }
  1097. Variant::operator CharType() const {
  1098. return operator unsigned int();
  1099. }
  1100. Variant::operator float() const {
  1101. switch (type) {
  1102. case NIL:
  1103. return 0;
  1104. case BOOL:
  1105. return _data._bool ? 1.0 : 0.0;
  1106. case INT:
  1107. return (float)_data._int;
  1108. case REAL:
  1109. return _data._real;
  1110. case STRING:
  1111. return operator String().to_double();
  1112. default: {
  1113. return 0;
  1114. }
  1115. }
  1116. }
  1117. Variant::operator double() const {
  1118. switch (type) {
  1119. case NIL:
  1120. return 0;
  1121. case BOOL:
  1122. return _data._bool ? 1.0 : 0.0;
  1123. case INT:
  1124. return (double)_data._int;
  1125. case REAL:
  1126. return _data._real;
  1127. case STRING:
  1128. return operator String().to_double();
  1129. default: {
  1130. return 0;
  1131. }
  1132. }
  1133. }
  1134. Variant::operator StringName() const {
  1135. if (type == NODE_PATH) {
  1136. return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
  1137. }
  1138. return StringName(operator String());
  1139. }
  1140. struct _VariantStrPair {
  1141. String key;
  1142. String value;
  1143. bool operator<(const _VariantStrPair &p) const {
  1144. return key < p.key;
  1145. }
  1146. };
  1147. Variant::operator String() const {
  1148. List<const void *> stack;
  1149. return stringify(stack);
  1150. }
  1151. String Variant::stringify(List<const void *> &stack) const {
  1152. switch (type) {
  1153. case NIL:
  1154. return "Null";
  1155. case BOOL:
  1156. return _data._bool ? "True" : "False";
  1157. case INT:
  1158. return itos(_data._int);
  1159. case REAL:
  1160. return rtos(_data._real);
  1161. case STRING:
  1162. return *reinterpret_cast<const String *>(_data._mem);
  1163. case VECTOR2:
  1164. return "(" + operator Vector2() + ")";
  1165. case RECT2:
  1166. return "(" + operator Rect2() + ")";
  1167. case TRANSFORM2D: {
  1168. Transform2D mat32 = operator Transform2D();
  1169. return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
  1170. } break;
  1171. case VECTOR3:
  1172. return "(" + operator Vector3() + ")";
  1173. case PLANE:
  1174. return operator Plane();
  1175. //case QUAT:
  1176. case AABB:
  1177. return operator ::AABB();
  1178. case QUAT:
  1179. return "(" + operator Quat() + ")";
  1180. case BASIS: {
  1181. Basis mat3 = operator Basis();
  1182. String mtx("(");
  1183. for (int i = 0; i < 3; i++) {
  1184. if (i != 0) {
  1185. mtx += ", ";
  1186. }
  1187. mtx += "(";
  1188. for (int j = 0; j < 3; j++) {
  1189. if (j != 0) {
  1190. mtx += ", ";
  1191. }
  1192. mtx += Variant(mat3.elements[i][j]).operator String();
  1193. }
  1194. mtx += ")";
  1195. }
  1196. return mtx + ")";
  1197. } break;
  1198. case TRANSFORM:
  1199. return operator Transform();
  1200. case NODE_PATH:
  1201. return operator NodePath();
  1202. case COLOR:
  1203. return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
  1204. case DICTIONARY: {
  1205. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1206. if (stack.find(d.id())) {
  1207. return "{...}";
  1208. }
  1209. stack.push_back(d.id());
  1210. //const String *K=NULL;
  1211. String str("{");
  1212. List<Variant> keys;
  1213. d.get_key_list(&keys);
  1214. Vector<_VariantStrPair> pairs;
  1215. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1216. _VariantStrPair sp;
  1217. sp.key = E->get().stringify(stack);
  1218. sp.value = d[E->get()].stringify(stack);
  1219. pairs.push_back(sp);
  1220. }
  1221. pairs.sort();
  1222. for (int i = 0; i < pairs.size(); i++) {
  1223. if (i > 0) {
  1224. str += ", ";
  1225. }
  1226. str += pairs[i].key + ":" + pairs[i].value;
  1227. }
  1228. str += "}";
  1229. stack.erase(d.id());
  1230. return str;
  1231. } break;
  1232. case POOL_VECTOR2_ARRAY: {
  1233. PoolVector<Vector2> vec = operator PoolVector<Vector2>();
  1234. String str("[");
  1235. for (int i = 0; i < vec.size(); i++) {
  1236. if (i > 0) {
  1237. str += ", ";
  1238. }
  1239. str = str + Variant(vec[i]);
  1240. }
  1241. str += "]";
  1242. return str;
  1243. } break;
  1244. case POOL_VECTOR3_ARRAY: {
  1245. PoolVector<Vector3> vec = operator PoolVector<Vector3>();
  1246. String str("[");
  1247. for (int i = 0; i < vec.size(); i++) {
  1248. if (i > 0) {
  1249. str += ", ";
  1250. }
  1251. str = str + Variant(vec[i]);
  1252. }
  1253. str += "]";
  1254. return str;
  1255. } break;
  1256. case POOL_STRING_ARRAY: {
  1257. PoolVector<String> vec = operator PoolVector<String>();
  1258. String str("[");
  1259. for (int i = 0; i < vec.size(); i++) {
  1260. if (i > 0) {
  1261. str += ", ";
  1262. }
  1263. str = str + vec[i];
  1264. }
  1265. str += "]";
  1266. return str;
  1267. } break;
  1268. case POOL_INT_ARRAY: {
  1269. PoolVector<int> vec = operator PoolVector<int>();
  1270. String str("[");
  1271. for (int i = 0; i < vec.size(); i++) {
  1272. if (i > 0) {
  1273. str += ", ";
  1274. }
  1275. str = str + itos(vec[i]);
  1276. }
  1277. str += "]";
  1278. return str;
  1279. } break;
  1280. case POOL_REAL_ARRAY: {
  1281. PoolVector<real_t> vec = operator PoolVector<real_t>();
  1282. String str("[");
  1283. for (int i = 0; i < vec.size(); i++) {
  1284. if (i > 0) {
  1285. str += ", ";
  1286. }
  1287. str = str + rtos(vec[i]);
  1288. }
  1289. str += "]";
  1290. return str;
  1291. } break;
  1292. case ARRAY: {
  1293. Array arr = operator Array();
  1294. if (stack.find(arr.id())) {
  1295. return "[...]";
  1296. }
  1297. stack.push_back(arr.id());
  1298. String str("[");
  1299. for (int i = 0; i < arr.size(); i++) {
  1300. if (i) {
  1301. str += ", ";
  1302. }
  1303. str += arr[i].stringify(stack);
  1304. }
  1305. str += "]";
  1306. stack.erase(arr.id());
  1307. return str;
  1308. } break;
  1309. case OBJECT: {
  1310. Object *obj = _OBJ_PTR(*this);
  1311. if (obj) {
  1312. if (_get_obj().ref.is_null() && !ObjectDB::get_instance(obj->get_instance_id())) {
  1313. return "[Deleted Object]";
  1314. }
  1315. return obj->to_string();
  1316. } else {
  1317. #ifdef DEBUG_ENABLED
  1318. if (ScriptDebugger::get_singleton() && _get_obj().rc && !ObjectDB::get_instance(_get_obj().rc->instance_id)) {
  1319. return "[Deleted Object]";
  1320. }
  1321. #endif
  1322. return "[Object:null]";
  1323. }
  1324. } break;
  1325. default: {
  1326. return "[" + get_type_name(type) + "]";
  1327. }
  1328. }
  1329. return "";
  1330. }
  1331. Variant::operator Vector2() const {
  1332. if (type == VECTOR2) {
  1333. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1334. } else if (type == VECTOR3) {
  1335. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1336. } else {
  1337. return Vector2();
  1338. }
  1339. }
  1340. Variant::operator Rect2() const {
  1341. if (type == RECT2) {
  1342. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1343. } else {
  1344. return Rect2();
  1345. }
  1346. }
  1347. Variant::operator Vector3() const {
  1348. if (type == VECTOR3) {
  1349. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1350. } else if (type == VECTOR2) {
  1351. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1352. } else {
  1353. return Vector3();
  1354. }
  1355. }
  1356. Variant::operator Plane() const {
  1357. if (type == PLANE) {
  1358. return *reinterpret_cast<const Plane *>(_data._mem);
  1359. } else {
  1360. return Plane();
  1361. }
  1362. }
  1363. Variant::operator ::AABB() const {
  1364. if (type == AABB) {
  1365. return *_data._aabb;
  1366. } else {
  1367. return ::AABB();
  1368. }
  1369. }
  1370. Variant::operator Basis() const {
  1371. if (type == BASIS) {
  1372. return *_data._basis;
  1373. } else if (type == QUAT) {
  1374. return *reinterpret_cast<const Quat *>(_data._mem);
  1375. } else if (type == VECTOR3) {
  1376. return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
  1377. } else if (type == TRANSFORM) { // unexposed in Variant::can_convert?
  1378. return _data._transform->basis;
  1379. } else {
  1380. return Basis();
  1381. }
  1382. }
  1383. Variant::operator Quat() const {
  1384. if (type == QUAT) {
  1385. return *reinterpret_cast<const Quat *>(_data._mem);
  1386. } else if (type == BASIS) {
  1387. return *_data._basis;
  1388. } else if (type == TRANSFORM) {
  1389. return _data._transform->basis;
  1390. } else {
  1391. return Quat();
  1392. }
  1393. }
  1394. Variant::operator Transform() const {
  1395. if (type == TRANSFORM) {
  1396. return *_data._transform;
  1397. } else if (type == BASIS) {
  1398. return Transform(*_data._basis, Vector3());
  1399. } else if (type == QUAT) {
  1400. return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
  1401. } else if (type == TRANSFORM2D) {
  1402. const Transform2D &t = *_data._transform2d;
  1403. Transform m;
  1404. m.basis.elements[0][0] = t.elements[0][0];
  1405. m.basis.elements[1][0] = t.elements[0][1];
  1406. m.basis.elements[0][1] = t.elements[1][0];
  1407. m.basis.elements[1][1] = t.elements[1][1];
  1408. m.origin[0] = t.elements[2][0];
  1409. m.origin[1] = t.elements[2][1];
  1410. return m;
  1411. } else {
  1412. return Transform();
  1413. }
  1414. }
  1415. Variant::operator Transform2D() const {
  1416. if (type == TRANSFORM2D) {
  1417. return *_data._transform2d;
  1418. } else if (type == TRANSFORM) {
  1419. const Transform &t = *_data._transform;
  1420. Transform2D m;
  1421. m.elements[0][0] = t.basis.elements[0][0];
  1422. m.elements[0][1] = t.basis.elements[1][0];
  1423. m.elements[1][0] = t.basis.elements[0][1];
  1424. m.elements[1][1] = t.basis.elements[1][1];
  1425. m.elements[2][0] = t.origin[0];
  1426. m.elements[2][1] = t.origin[1];
  1427. return m;
  1428. } else {
  1429. return Transform2D();
  1430. }
  1431. }
  1432. Variant::operator Color() const {
  1433. if (type == COLOR) {
  1434. return *reinterpret_cast<const Color *>(_data._mem);
  1435. } else if (type == STRING) {
  1436. return Color::html(operator String());
  1437. } else if (type == INT) {
  1438. return Color::hex(operator int());
  1439. } else {
  1440. return Color();
  1441. }
  1442. }
  1443. Variant::operator NodePath() const {
  1444. if (type == NODE_PATH) {
  1445. return *reinterpret_cast<const NodePath *>(_data._mem);
  1446. } else if (type == STRING) {
  1447. return NodePath(operator String());
  1448. } else {
  1449. return NodePath();
  1450. }
  1451. }
  1452. Variant::operator RefPtr() const {
  1453. if (type == OBJECT) {
  1454. return _get_obj().ref;
  1455. } else {
  1456. return RefPtr();
  1457. }
  1458. }
  1459. Variant::operator RID() const {
  1460. if (type == _RID) {
  1461. return *reinterpret_cast<const RID *>(_data._mem);
  1462. } else if (type == OBJECT) {
  1463. if (!_get_obj().ref.is_null()) {
  1464. return _get_obj().ref.get_rid();
  1465. } else {
  1466. #ifdef DEBUG_ENABLED
  1467. Object *obj = likely(_get_obj().rc) ? _get_obj().rc->get_ptr() : nullptr;
  1468. if (unlikely(!obj)) {
  1469. if (ScriptDebugger::get_singleton() && _get_obj().rc && !ObjectDB::get_instance(_get_obj().rc->instance_id)) {
  1470. ERR_PRINT("Attempted get RID on a deleted object.");
  1471. }
  1472. return RID();
  1473. }
  1474. #else
  1475. Object *obj = _get_obj().obj;
  1476. if (unlikely(!obj)) {
  1477. return RID();
  1478. }
  1479. #endif
  1480. Variant::CallError ce;
  1481. Variant ret = obj->call(CoreStringNames::get_singleton()->get_rid, nullptr, 0, ce);
  1482. if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
  1483. return ret;
  1484. } else {
  1485. return RID();
  1486. }
  1487. }
  1488. } else {
  1489. return RID();
  1490. }
  1491. }
  1492. Variant::operator Object *() const {
  1493. if (type == OBJECT) {
  1494. return _OBJ_PTR(*this);
  1495. } else {
  1496. return nullptr;
  1497. }
  1498. }
  1499. Variant::operator Node *() const {
  1500. if (type == OBJECT) {
  1501. #ifdef DEBUG_ENABLED
  1502. Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
  1503. #else
  1504. Object *obj = _get_obj().obj;
  1505. #endif
  1506. return Object::cast_to<Node>(obj);
  1507. }
  1508. return nullptr;
  1509. }
  1510. Variant::operator Control *() const {
  1511. if (type == OBJECT) {
  1512. #ifdef DEBUG_ENABLED
  1513. Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
  1514. #else
  1515. Object *obj = _get_obj().obj;
  1516. #endif
  1517. return Object::cast_to<Control>(obj);
  1518. }
  1519. return nullptr;
  1520. }
  1521. Variant::operator Dictionary() const {
  1522. if (type == DICTIONARY) {
  1523. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1524. } else {
  1525. return Dictionary();
  1526. }
  1527. }
  1528. template <class DA, class SA>
  1529. inline DA _convert_array(const SA &p_array) {
  1530. DA da;
  1531. da.resize(p_array.size());
  1532. for (int i = 0; i < p_array.size(); i++) {
  1533. da.set(i, Variant(p_array.get(i)));
  1534. }
  1535. return da;
  1536. }
  1537. template <class DA>
  1538. inline DA _convert_array_from_variant(const Variant &p_variant) {
  1539. switch (p_variant.get_type()) {
  1540. case Variant::ARRAY: {
  1541. return _convert_array<DA, Array>(p_variant.operator Array());
  1542. }
  1543. case Variant::POOL_BYTE_ARRAY: {
  1544. return _convert_array<DA, PoolVector<uint8_t>>(p_variant.operator PoolVector<uint8_t>());
  1545. }
  1546. case Variant::POOL_INT_ARRAY: {
  1547. return _convert_array<DA, PoolVector<int>>(p_variant.operator PoolVector<int>());
  1548. }
  1549. case Variant::POOL_REAL_ARRAY: {
  1550. return _convert_array<DA, PoolVector<real_t>>(p_variant.operator PoolVector<real_t>());
  1551. }
  1552. case Variant::POOL_STRING_ARRAY: {
  1553. return _convert_array<DA, PoolVector<String>>(p_variant.operator PoolVector<String>());
  1554. }
  1555. case Variant::POOL_VECTOR2_ARRAY: {
  1556. return _convert_array<DA, PoolVector<Vector2>>(p_variant.operator PoolVector<Vector2>());
  1557. }
  1558. case Variant::POOL_VECTOR3_ARRAY: {
  1559. return _convert_array<DA, PoolVector<Vector3>>(p_variant.operator PoolVector<Vector3>());
  1560. }
  1561. case Variant::POOL_COLOR_ARRAY: {
  1562. return _convert_array<DA, PoolVector<Color>>(p_variant.operator PoolVector<Color>());
  1563. }
  1564. default: {
  1565. return DA();
  1566. }
  1567. }
  1568. }
  1569. Variant::operator Array() const {
  1570. if (type == ARRAY) {
  1571. return *reinterpret_cast<const Array *>(_data._mem);
  1572. } else {
  1573. return _convert_array_from_variant<Array>(*this);
  1574. }
  1575. }
  1576. Variant::operator PoolVector<uint8_t>() const {
  1577. if (type == POOL_BYTE_ARRAY) {
  1578. return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  1579. } else {
  1580. return _convert_array_from_variant<PoolVector<uint8_t>>(*this);
  1581. }
  1582. }
  1583. Variant::operator PoolVector<int>() const {
  1584. if (type == POOL_INT_ARRAY) {
  1585. return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  1586. } else {
  1587. return _convert_array_from_variant<PoolVector<int>>(*this);
  1588. }
  1589. }
  1590. Variant::operator PoolVector<real_t>() const {
  1591. if (type == POOL_REAL_ARRAY) {
  1592. return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  1593. } else {
  1594. return _convert_array_from_variant<PoolVector<real_t>>(*this);
  1595. }
  1596. }
  1597. Variant::operator PoolVector<String>() const {
  1598. if (type == POOL_STRING_ARRAY) {
  1599. return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  1600. } else {
  1601. return _convert_array_from_variant<PoolVector<String>>(*this);
  1602. }
  1603. }
  1604. Variant::operator PoolVector<Vector3>() const {
  1605. if (type == POOL_VECTOR3_ARRAY) {
  1606. return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  1607. } else {
  1608. return _convert_array_from_variant<PoolVector<Vector3>>(*this);
  1609. }
  1610. }
  1611. Variant::operator PoolVector<Vector2>() const {
  1612. if (type == POOL_VECTOR2_ARRAY) {
  1613. return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  1614. } else {
  1615. return _convert_array_from_variant<PoolVector<Vector2>>(*this);
  1616. }
  1617. }
  1618. Variant::operator PoolVector<Color>() const {
  1619. if (type == POOL_COLOR_ARRAY) {
  1620. return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  1621. } else {
  1622. return _convert_array_from_variant<PoolVector<Color>>(*this);
  1623. }
  1624. }
  1625. /* helpers */
  1626. Variant::operator Vector<RID>() const {
  1627. Array va = operator Array();
  1628. Vector<RID> rids;
  1629. rids.resize(va.size());
  1630. for (int i = 0; i < rids.size(); i++) {
  1631. rids.write[i] = va[i];
  1632. }
  1633. return rids;
  1634. }
  1635. Variant::operator Vector<Vector2>() const {
  1636. PoolVector<Vector2> from = operator PoolVector<Vector2>();
  1637. Vector<Vector2> to;
  1638. int len = from.size();
  1639. if (len == 0) {
  1640. return Vector<Vector2>();
  1641. }
  1642. to.resize(len);
  1643. PoolVector<Vector2>::Read r = from.read();
  1644. Vector2 *w = to.ptrw();
  1645. for (int i = 0; i < len; i++) {
  1646. w[i] = r[i];
  1647. }
  1648. return to;
  1649. }
  1650. Variant::operator PoolVector<Plane>() const {
  1651. Array va = operator Array();
  1652. PoolVector<Plane> planes;
  1653. int va_size = va.size();
  1654. if (va_size == 0) {
  1655. return planes;
  1656. }
  1657. planes.resize(va_size);
  1658. PoolVector<Plane>::Write w = planes.write();
  1659. for (int i = 0; i < va_size; i++) {
  1660. w[i] = va[i];
  1661. }
  1662. return planes;
  1663. }
  1664. Variant::operator PoolVector<Face3>() const {
  1665. PoolVector<Vector3> va = operator PoolVector<Vector3>();
  1666. PoolVector<Face3> faces;
  1667. int va_size = va.size();
  1668. if (va_size == 0) {
  1669. return faces;
  1670. }
  1671. faces.resize(va_size / 3);
  1672. PoolVector<Face3>::Write w = faces.write();
  1673. PoolVector<Vector3>::Read r = va.read();
  1674. for (int i = 0; i < va_size; i++) {
  1675. w[i / 3].vertex[i % 3] = r[i];
  1676. }
  1677. return faces;
  1678. }
  1679. Variant::operator Vector<Plane>() const {
  1680. Array va = operator Array();
  1681. Vector<Plane> planes;
  1682. int va_size = va.size();
  1683. if (va_size == 0) {
  1684. return planes;
  1685. }
  1686. planes.resize(va_size);
  1687. for (int i = 0; i < va_size; i++) {
  1688. planes.write[i] = va[i];
  1689. }
  1690. return planes;
  1691. }
  1692. Variant::operator Vector<Variant>() const {
  1693. Array from = operator Array();
  1694. Vector<Variant> 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<uint8_t>() const {
  1703. PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
  1704. Vector<uint8_t> 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<int>() const {
  1713. PoolVector<int> from = operator PoolVector<int>();
  1714. Vector<int> to;
  1715. int len = from.size();
  1716. to.resize(len);
  1717. for (int i = 0; i < len; i++) {
  1718. to.write[i] = from[i];
  1719. }
  1720. return to;
  1721. }
  1722. Variant::operator Vector<real_t>() const {
  1723. PoolVector<real_t> from = operator PoolVector<real_t>();
  1724. Vector<real_t> to;
  1725. int len = from.size();
  1726. to.resize(len);
  1727. for (int i = 0; i < len; i++) {
  1728. to.write[i] = from[i];
  1729. }
  1730. return to;
  1731. }
  1732. Variant::operator Vector<String>() const {
  1733. PoolVector<String> from = operator PoolVector<String>();
  1734. Vector<String> to;
  1735. int len = from.size();
  1736. to.resize(len);
  1737. for (int i = 0; i < len; i++) {
  1738. to.write[i] = from[i];
  1739. }
  1740. return to;
  1741. }
  1742. Variant::operator Vector<StringName>() const {
  1743. PoolVector<String> from = operator PoolVector<String>();
  1744. Vector<StringName> to;
  1745. int len = from.size();
  1746. to.resize(len);
  1747. for (int i = 0; i < len; i++) {
  1748. to.write[i] = from[i];
  1749. }
  1750. return to;
  1751. }
  1752. Variant::operator Vector<Vector3>() const {
  1753. PoolVector<Vector3> from = operator PoolVector<Vector3>();
  1754. Vector<Vector3> to;
  1755. int len = from.size();
  1756. if (len == 0) {
  1757. return Vector<Vector3>();
  1758. }
  1759. to.resize(len);
  1760. PoolVector<Vector3>::Read r = from.read();
  1761. Vector3 *w = to.ptrw();
  1762. for (int i = 0; i < len; i++) {
  1763. w[i] = r[i];
  1764. }
  1765. return to;
  1766. }
  1767. Variant::operator Vector<Color>() const {
  1768. PoolVector<Color> from = operator PoolVector<Color>();
  1769. Vector<Color> to;
  1770. int len = from.size();
  1771. if (len == 0) {
  1772. return Vector<Color>();
  1773. }
  1774. to.resize(len);
  1775. PoolVector<Color>::Read r = from.read();
  1776. Color *w = to.ptrw();
  1777. for (int i = 0; i < len; i++) {
  1778. w[i] = r[i];
  1779. }
  1780. return to;
  1781. }
  1782. Variant::operator Margin() const {
  1783. return (Margin) operator int();
  1784. }
  1785. Variant::operator Orientation() const {
  1786. return (Orientation) operator int();
  1787. }
  1788. Variant::operator IP_Address() const {
  1789. if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
  1790. PoolVector<int> addr = operator PoolVector<int>();
  1791. if (addr.size() == 4) {
  1792. return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  1793. }
  1794. }
  1795. return IP_Address(operator String());
  1796. }
  1797. Variant::Variant(bool p_bool) {
  1798. type = BOOL;
  1799. _data._bool = p_bool;
  1800. }
  1801. /*
  1802. Variant::Variant(long unsigned int p_long) {
  1803. type=INT;
  1804. _data._int=p_long;
  1805. };
  1806. */
  1807. Variant::Variant(signed int p_int) {
  1808. type = INT;
  1809. _data._int = p_int;
  1810. }
  1811. Variant::Variant(unsigned int p_int) {
  1812. type = INT;
  1813. _data._int = p_int;
  1814. }
  1815. #ifdef NEED_LONG_INT
  1816. Variant::Variant(signed long p_int) {
  1817. type = INT;
  1818. _data._int = p_int;
  1819. }
  1820. Variant::Variant(unsigned long p_int) {
  1821. type = INT;
  1822. _data._int = p_int;
  1823. }
  1824. #endif
  1825. Variant::Variant(int64_t p_int) {
  1826. type = INT;
  1827. _data._int = p_int;
  1828. }
  1829. Variant::Variant(uint64_t p_int) {
  1830. type = INT;
  1831. _data._int = p_int;
  1832. }
  1833. Variant::Variant(signed short p_short) {
  1834. type = INT;
  1835. _data._int = p_short;
  1836. }
  1837. Variant::Variant(unsigned short p_short) {
  1838. type = INT;
  1839. _data._int = p_short;
  1840. }
  1841. Variant::Variant(signed char p_char) {
  1842. type = INT;
  1843. _data._int = p_char;
  1844. }
  1845. Variant::Variant(unsigned char p_char) {
  1846. type = INT;
  1847. _data._int = p_char;
  1848. }
  1849. Variant::Variant(float p_float) {
  1850. type = REAL;
  1851. _data._real = p_float;
  1852. }
  1853. Variant::Variant(double p_double) {
  1854. type = REAL;
  1855. _data._real = p_double;
  1856. }
  1857. Variant::Variant(const StringName &p_string) {
  1858. type = STRING;
  1859. memnew_placement(_data._mem, String(p_string.operator String()));
  1860. }
  1861. Variant::Variant(const String &p_string) {
  1862. type = STRING;
  1863. memnew_placement(_data._mem, String(p_string));
  1864. }
  1865. Variant::Variant(const char *const p_cstring) {
  1866. type = STRING;
  1867. memnew_placement(_data._mem, String((const char *)p_cstring));
  1868. }
  1869. Variant::Variant(const CharType *p_wstring) {
  1870. type = STRING;
  1871. memnew_placement(_data._mem, String(p_wstring));
  1872. }
  1873. Variant::Variant(const Vector3 &p_vector3) {
  1874. type = VECTOR3;
  1875. memnew_placement(_data._mem, Vector3(p_vector3));
  1876. }
  1877. Variant::Variant(const Vector2 &p_vector2) {
  1878. type = VECTOR2;
  1879. memnew_placement(_data._mem, Vector2(p_vector2));
  1880. }
  1881. Variant::Variant(const Rect2 &p_rect2) {
  1882. type = RECT2;
  1883. memnew_placement(_data._mem, Rect2(p_rect2));
  1884. }
  1885. Variant::Variant(const Plane &p_plane) {
  1886. type = PLANE;
  1887. memnew_placement(_data._mem, Plane(p_plane));
  1888. }
  1889. Variant::Variant(const ::AABB &p_aabb) {
  1890. type = AABB;
  1891. _data._aabb = memnew(::AABB(p_aabb));
  1892. }
  1893. Variant::Variant(const Basis &p_matrix) {
  1894. type = BASIS;
  1895. _data._basis = memnew(Basis(p_matrix));
  1896. }
  1897. Variant::Variant(const Quat &p_quat) {
  1898. type = QUAT;
  1899. memnew_placement(_data._mem, Quat(p_quat));
  1900. }
  1901. Variant::Variant(const Transform &p_transform) {
  1902. type = TRANSFORM;
  1903. _data._transform = memnew(Transform(p_transform));
  1904. }
  1905. Variant::Variant(const Transform2D &p_transform) {
  1906. type = TRANSFORM2D;
  1907. _data._transform2d = memnew(Transform2D(p_transform));
  1908. }
  1909. Variant::Variant(const Color &p_color) {
  1910. type = COLOR;
  1911. memnew_placement(_data._mem, Color(p_color));
  1912. }
  1913. Variant::Variant(const NodePath &p_node_path) {
  1914. type = NODE_PATH;
  1915. memnew_placement(_data._mem, NodePath(p_node_path));
  1916. }
  1917. Variant::Variant(const RefPtr &p_resource) {
  1918. type = OBJECT;
  1919. memnew_placement(_data._mem, ObjData);
  1920. #ifdef DEBUG_ENABLED
  1921. _get_obj().rc = nullptr;
  1922. #else
  1923. REF *ref = reinterpret_cast<REF *>(p_resource.get_data());
  1924. _get_obj().obj = ref->ptr();
  1925. #endif
  1926. _get_obj().ref = p_resource;
  1927. }
  1928. Variant::Variant(const RID &p_rid) {
  1929. type = _RID;
  1930. memnew_placement(_data._mem, RID(p_rid));
  1931. }
  1932. Variant::Variant(const Object *p_object) {
  1933. type = OBJECT;
  1934. Object *obj = const_cast<Object *>(p_object);
  1935. memnew_placement(_data._mem, ObjData);
  1936. Reference *ref = Object::cast_to<Reference>(obj);
  1937. if (unlikely(ref)) {
  1938. *reinterpret_cast<Ref<Reference> *>(_get_obj().ref.get_data()) = Ref<Reference>(ref);
  1939. #ifdef DEBUG_ENABLED
  1940. _get_obj().rc = nullptr;
  1941. } else {
  1942. _get_obj().rc = likely(obj) ? obj->_use_rc() : nullptr;
  1943. #endif
  1944. }
  1945. #if !defined(DEBUG_ENABLED)
  1946. _get_obj().obj = obj;
  1947. #endif
  1948. }
  1949. Variant::Variant(const Dictionary &p_dictionary) {
  1950. type = DICTIONARY;
  1951. memnew_placement(_data._mem, Dictionary(p_dictionary));
  1952. }
  1953. Variant::Variant(const Array &p_array) {
  1954. type = ARRAY;
  1955. memnew_placement(_data._mem, Array(p_array));
  1956. }
  1957. Variant::Variant(const PoolVector<Plane> &p_array) {
  1958. type = ARRAY;
  1959. Array *plane_array = memnew_placement(_data._mem, Array);
  1960. plane_array->resize(p_array.size());
  1961. for (int i = 0; i < p_array.size(); i++) {
  1962. plane_array->operator[](i) = Variant(p_array[i]);
  1963. }
  1964. }
  1965. Variant::Variant(const Vector<Plane> &p_array) {
  1966. type = ARRAY;
  1967. Array *plane_array = memnew_placement(_data._mem, Array);
  1968. plane_array->resize(p_array.size());
  1969. for (int i = 0; i < p_array.size(); i++) {
  1970. plane_array->operator[](i) = Variant(p_array[i]);
  1971. }
  1972. }
  1973. Variant::Variant(const Vector<RID> &p_array) {
  1974. type = ARRAY;
  1975. Array *rid_array = memnew_placement(_data._mem, Array);
  1976. rid_array->resize(p_array.size());
  1977. for (int i = 0; i < p_array.size(); i++) {
  1978. rid_array->set(i, Variant(p_array[i]));
  1979. }
  1980. }
  1981. Variant::Variant(const Vector<Vector2> &p_array) {
  1982. type = NIL;
  1983. PoolVector<Vector2> v;
  1984. int len = p_array.size();
  1985. if (len > 0) {
  1986. v.resize(len);
  1987. PoolVector<Vector2>::Write w = v.write();
  1988. const Vector2 *r = p_array.ptr();
  1989. for (int i = 0; i < len; i++) {
  1990. w[i] = r[i];
  1991. }
  1992. }
  1993. *this = v;
  1994. }
  1995. Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
  1996. type = POOL_BYTE_ARRAY;
  1997. memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
  1998. }
  1999. Variant::Variant(const PoolVector<int> &p_int_array) {
  2000. type = POOL_INT_ARRAY;
  2001. memnew_placement(_data._mem, PoolVector<int>(p_int_array));
  2002. }
  2003. Variant::Variant(const PoolVector<real_t> &p_real_array) {
  2004. type = POOL_REAL_ARRAY;
  2005. memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
  2006. }
  2007. Variant::Variant(const PoolVector<String> &p_string_array) {
  2008. type = POOL_STRING_ARRAY;
  2009. memnew_placement(_data._mem, PoolVector<String>(p_string_array));
  2010. }
  2011. Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
  2012. type = POOL_VECTOR3_ARRAY;
  2013. memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
  2014. }
  2015. Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
  2016. type = POOL_VECTOR2_ARRAY;
  2017. memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
  2018. }
  2019. Variant::Variant(const PoolVector<Color> &p_color_array) {
  2020. type = POOL_COLOR_ARRAY;
  2021. memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
  2022. }
  2023. Variant::Variant(const PoolVector<Face3> &p_face_array) {
  2024. PoolVector<Vector3> vertices;
  2025. int face_count = p_face_array.size();
  2026. vertices.resize(face_count * 3);
  2027. if (face_count) {
  2028. PoolVector<Face3>::Read r = p_face_array.read();
  2029. PoolVector<Vector3>::Write w = vertices.write();
  2030. for (int i = 0; i < face_count; i++) {
  2031. for (int j = 0; j < 3; j++) {
  2032. w[i * 3 + j] = r[i].vertex[j];
  2033. }
  2034. }
  2035. }
  2036. type = NIL;
  2037. *this = vertices;
  2038. }
  2039. /* helpers */
  2040. Variant::Variant(const Vector<Variant> &p_array) {
  2041. type = NIL;
  2042. Array 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<uint8_t> &p_array) {
  2051. type = NIL;
  2052. PoolVector<uint8_t> v;
  2053. int len = p_array.size();
  2054. v.resize(len);
  2055. for (int i = 0; i < len; i++) {
  2056. v.set(i, p_array[i]);
  2057. }
  2058. *this = v;
  2059. }
  2060. Variant::Variant(const Vector<int> &p_array) {
  2061. type = NIL;
  2062. PoolVector<int> v;
  2063. int len = p_array.size();
  2064. v.resize(len);
  2065. for (int i = 0; i < len; i++) {
  2066. v.set(i, p_array[i]);
  2067. }
  2068. *this = v;
  2069. }
  2070. Variant::Variant(const Vector<real_t> &p_array) {
  2071. type = NIL;
  2072. PoolVector<real_t> v;
  2073. int len = p_array.size();
  2074. v.resize(len);
  2075. for (int i = 0; i < len; i++) {
  2076. v.set(i, p_array[i]);
  2077. }
  2078. *this = v;
  2079. }
  2080. Variant::Variant(const Vector<String> &p_array) {
  2081. type = NIL;
  2082. PoolVector<String> v;
  2083. int len = p_array.size();
  2084. v.resize(len);
  2085. for (int i = 0; i < len; i++) {
  2086. v.set(i, p_array[i]);
  2087. }
  2088. *this = v;
  2089. }
  2090. Variant::Variant(const Vector<StringName> &p_array) {
  2091. type = NIL;
  2092. PoolVector<String> v;
  2093. int len = p_array.size();
  2094. v.resize(len);
  2095. for (int i = 0; i < len; i++) {
  2096. v.set(i, p_array[i]);
  2097. }
  2098. *this = v;
  2099. }
  2100. Variant::Variant(const Vector<Vector3> &p_array) {
  2101. type = NIL;
  2102. PoolVector<Vector3> v;
  2103. int len = p_array.size();
  2104. if (len > 0) {
  2105. v.resize(len);
  2106. PoolVector<Vector3>::Write w = v.write();
  2107. const Vector3 *r = p_array.ptr();
  2108. for (int i = 0; i < len; i++) {
  2109. w[i] = r[i];
  2110. }
  2111. }
  2112. *this = v;
  2113. }
  2114. Variant::Variant(const Vector<Color> &p_array) {
  2115. type = NIL;
  2116. PoolVector<Color> v;
  2117. int len = p_array.size();
  2118. v.resize(len);
  2119. for (int i = 0; i < len; i++) {
  2120. v.set(i, p_array[i]);
  2121. }
  2122. *this = v;
  2123. }
  2124. void Variant::operator=(const Variant &p_variant) {
  2125. if (unlikely(this == &p_variant)) {
  2126. return;
  2127. }
  2128. if (unlikely(type != p_variant.type)) {
  2129. reference(p_variant);
  2130. return;
  2131. }
  2132. switch (p_variant.type) {
  2133. case NIL: {
  2134. // none
  2135. } break;
  2136. // atomic types
  2137. case BOOL: {
  2138. _data._bool = p_variant._data._bool;
  2139. } break;
  2140. case INT: {
  2141. _data._int = p_variant._data._int;
  2142. } break;
  2143. case REAL: {
  2144. _data._real = p_variant._data._real;
  2145. } break;
  2146. case STRING: {
  2147. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2148. } break;
  2149. // math types
  2150. case VECTOR2: {
  2151. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2152. } break;
  2153. case RECT2: {
  2154. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2155. } break;
  2156. case TRANSFORM2D: {
  2157. *_data._transform2d = *(p_variant._data._transform2d);
  2158. } break;
  2159. case VECTOR3: {
  2160. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2161. } break;
  2162. case PLANE: {
  2163. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2164. } break;
  2165. case AABB: {
  2166. *_data._aabb = *(p_variant._data._aabb);
  2167. } break;
  2168. case QUAT: {
  2169. *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
  2170. } break;
  2171. case BASIS: {
  2172. *_data._basis = *(p_variant._data._basis);
  2173. } break;
  2174. case TRANSFORM: {
  2175. *_data._transform = *(p_variant._data._transform);
  2176. } break;
  2177. // misc types
  2178. case COLOR: {
  2179. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2180. } break;
  2181. case _RID: {
  2182. *reinterpret_cast<RID *>(_data._mem) = *reinterpret_cast<const RID *>(p_variant._data._mem);
  2183. } break;
  2184. case OBJECT: {
  2185. #ifdef DEBUG_ENABLED
  2186. if (likely(_get_obj().rc)) {
  2187. if (unlikely(_get_obj().rc->decrement())) {
  2188. memdelete(_get_obj().rc);
  2189. }
  2190. }
  2191. #endif
  2192. *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
  2193. #ifdef DEBUG_ENABLED
  2194. if (likely(_get_obj().rc)) {
  2195. _get_obj().rc->increment();
  2196. }
  2197. #endif
  2198. } break;
  2199. case NODE_PATH: {
  2200. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2201. } break;
  2202. case DICTIONARY: {
  2203. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2204. } break;
  2205. case ARRAY: {
  2206. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2207. } break;
  2208. // arrays
  2209. case POOL_BYTE_ARRAY: {
  2210. *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
  2211. } break;
  2212. case POOL_INT_ARRAY: {
  2213. *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
  2214. } break;
  2215. case POOL_REAL_ARRAY: {
  2216. *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
  2217. } break;
  2218. case POOL_STRING_ARRAY: {
  2219. *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
  2220. } break;
  2221. case POOL_VECTOR2_ARRAY: {
  2222. *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
  2223. } break;
  2224. case POOL_VECTOR3_ARRAY: {
  2225. *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
  2226. } break;
  2227. case POOL_COLOR_ARRAY: {
  2228. *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
  2229. } break;
  2230. default: {
  2231. }
  2232. }
  2233. }
  2234. Variant::Variant(const IP_Address &p_address) {
  2235. type = STRING;
  2236. memnew_placement(_data._mem, String(p_address));
  2237. }
  2238. Variant::Variant(const Variant &p_variant) {
  2239. type = NIL;
  2240. reference(p_variant);
  2241. }
  2242. /*
  2243. Variant::~Variant() {
  2244. clear();
  2245. }*/
  2246. uint32_t Variant::hash() const {
  2247. switch (type) {
  2248. case NIL: {
  2249. return 0;
  2250. } break;
  2251. case BOOL: {
  2252. return _data._bool ? 1 : 0;
  2253. } break;
  2254. case INT: {
  2255. return _data._int;
  2256. } break;
  2257. case REAL: {
  2258. return hash_djb2_one_float(_data._real);
  2259. } break;
  2260. case STRING: {
  2261. return reinterpret_cast<const String *>(_data._mem)->hash();
  2262. } break;
  2263. // math types
  2264. case VECTOR2: {
  2265. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
  2266. return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
  2267. } break;
  2268. case RECT2: {
  2269. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
  2270. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
  2271. hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
  2272. return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
  2273. } break;
  2274. case TRANSFORM2D: {
  2275. uint32_t hash = 5831;
  2276. for (int i = 0; i < 3; i++) {
  2277. for (int j = 0; j < 2; j++) {
  2278. hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
  2279. }
  2280. }
  2281. return hash;
  2282. } break;
  2283. case VECTOR3: {
  2284. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
  2285. hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
  2286. return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
  2287. } break;
  2288. case PLANE: {
  2289. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
  2290. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
  2291. hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
  2292. return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
  2293. } break;
  2294. /*
  2295. case QUAT: {
  2296. } break;*/
  2297. case AABB: {
  2298. uint32_t hash = 5831;
  2299. for (int i = 0; i < 3; i++) {
  2300. hash = hash_djb2_one_float(_data._aabb->position[i], hash);
  2301. hash = hash_djb2_one_float(_data._aabb->size[i], hash);
  2302. }
  2303. return hash;
  2304. } break;
  2305. case QUAT: {
  2306. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
  2307. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
  2308. hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
  2309. return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
  2310. } break;
  2311. case BASIS: {
  2312. uint32_t hash = 5831;
  2313. for (int i = 0; i < 3; i++) {
  2314. for (int j = 0; j < 3; j++) {
  2315. hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
  2316. }
  2317. }
  2318. return hash;
  2319. } break;
  2320. case TRANSFORM: {
  2321. uint32_t hash = 5831;
  2322. for (int i = 0; i < 3; i++) {
  2323. for (int j = 0; j < 3; j++) {
  2324. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
  2325. }
  2326. hash = hash_djb2_one_float(_data._transform->origin[i], hash);
  2327. }
  2328. return hash;
  2329. } break;
  2330. // misc types
  2331. case COLOR: {
  2332. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
  2333. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
  2334. hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
  2335. return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
  2336. } break;
  2337. case _RID: {
  2338. return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
  2339. } break;
  2340. case OBJECT: {
  2341. return hash_djb2_one_64(make_uint64_t(_UNSAFE_OBJ_PROXY_PTR(*this)));
  2342. } break;
  2343. case NODE_PATH: {
  2344. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2345. } break;
  2346. case DICTIONARY: {
  2347. return reinterpret_cast<const Dictionary *>(_data._mem)->hash();
  2348. } break;
  2349. case ARRAY: {
  2350. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2351. return arr.hash();
  2352. } break;
  2353. case POOL_BYTE_ARRAY: {
  2354. const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
  2355. int len = arr.size();
  2356. if (likely(len)) {
  2357. PoolVector<uint8_t>::Read r = arr.read();
  2358. return hash_djb2_buffer((uint8_t *)&r[0], len);
  2359. } else {
  2360. return hash_djb2_one_64(0);
  2361. }
  2362. } break;
  2363. case POOL_INT_ARRAY: {
  2364. const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
  2365. int len = arr.size();
  2366. if (likely(len)) {
  2367. PoolVector<int>::Read r = arr.read();
  2368. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
  2369. } else {
  2370. return hash_djb2_one_64(0);
  2371. }
  2372. } break;
  2373. case POOL_REAL_ARRAY: {
  2374. const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
  2375. int len = arr.size();
  2376. if (likely(len)) {
  2377. PoolVector<real_t>::Read r = arr.read();
  2378. return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
  2379. } else {
  2380. return hash_djb2_one_float(0.0);
  2381. }
  2382. } break;
  2383. case POOL_STRING_ARRAY: {
  2384. uint32_t hash = 5831;
  2385. const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
  2386. int len = arr.size();
  2387. if (likely(len)) {
  2388. PoolVector<String>::Read r = arr.read();
  2389. for (int i = 0; i < len; i++) {
  2390. hash = hash_djb2_one_32(r[i].hash(), hash);
  2391. }
  2392. }
  2393. return hash;
  2394. } break;
  2395. case POOL_VECTOR2_ARRAY: {
  2396. uint32_t hash = 5831;
  2397. const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
  2398. int len = arr.size();
  2399. if (likely(len)) {
  2400. PoolVector<Vector2>::Read r = arr.read();
  2401. for (int i = 0; i < len; i++) {
  2402. hash = hash_djb2_one_float(r[i].x, hash);
  2403. hash = hash_djb2_one_float(r[i].y, hash);
  2404. }
  2405. }
  2406. return hash;
  2407. } break;
  2408. case POOL_VECTOR3_ARRAY: {
  2409. uint32_t hash = 5831;
  2410. const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
  2411. int len = arr.size();
  2412. if (likely(len)) {
  2413. PoolVector<Vector3>::Read r = arr.read();
  2414. for (int i = 0; i < len; i++) {
  2415. hash = hash_djb2_one_float(r[i].x, hash);
  2416. hash = hash_djb2_one_float(r[i].y, hash);
  2417. hash = hash_djb2_one_float(r[i].z, hash);
  2418. }
  2419. }
  2420. return hash;
  2421. } break;
  2422. case POOL_COLOR_ARRAY: {
  2423. uint32_t hash = 5831;
  2424. const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
  2425. int len = arr.size();
  2426. if (likely(len)) {
  2427. PoolVector<Color>::Read r = arr.read();
  2428. for (int i = 0; i < len; i++) {
  2429. hash = hash_djb2_one_float(r[i].r, hash);
  2430. hash = hash_djb2_one_float(r[i].g, hash);
  2431. hash = hash_djb2_one_float(r[i].b, hash);
  2432. hash = hash_djb2_one_float(r[i].a, hash);
  2433. }
  2434. }
  2435. return hash;
  2436. } break;
  2437. default: {
  2438. }
  2439. }
  2440. return 0;
  2441. }
  2442. #define hash_compare_scalar(p_lhs, p_rhs) \
  2443. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
  2444. #define hash_compare_vector2(p_lhs, p_rhs) \
  2445. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2446. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2447. #define hash_compare_vector3(p_lhs, p_rhs) \
  2448. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2449. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2450. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2451. #define hash_compare_quat(p_lhs, p_rhs) \
  2452. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2453. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2454. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2455. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2456. #define hash_compare_color(p_lhs, p_rhs) \
  2457. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2458. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2459. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2460. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2461. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2462. const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
  2463. const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
  2464. \
  2465. if (l.size() != r.size()) \
  2466. return false; \
  2467. \
  2468. PoolVector<p_type>::Read lr = l.read(); \
  2469. PoolVector<p_type>::Read rr = r.read(); \
  2470. \
  2471. for (int i = 0; i < l.size(); ++i) { \
  2472. if (!p_compare_func((lr[i]), (rr[i]))) \
  2473. return false; \
  2474. } \
  2475. \
  2476. return true
  2477. bool Variant::hash_compare(const Variant &p_variant) const {
  2478. if (type != p_variant.type) {
  2479. return false;
  2480. }
  2481. switch (type) {
  2482. case REAL: {
  2483. return hash_compare_scalar(_data._real, p_variant._data._real);
  2484. } break;
  2485. case VECTOR2: {
  2486. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2487. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2488. return hash_compare_vector2(*l, *r);
  2489. } break;
  2490. case RECT2: {
  2491. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2492. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2493. return (hash_compare_vector2(l->position, r->position)) &&
  2494. (hash_compare_vector2(l->size, r->size));
  2495. } break;
  2496. case TRANSFORM2D: {
  2497. Transform2D *l = _data._transform2d;
  2498. Transform2D *r = p_variant._data._transform2d;
  2499. for (int i = 0; i < 3; i++) {
  2500. if (!(hash_compare_vector2(l->elements[i], r->elements[i]))) {
  2501. return false;
  2502. }
  2503. }
  2504. return true;
  2505. } break;
  2506. case VECTOR3: {
  2507. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2508. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2509. return hash_compare_vector3(*l, *r);
  2510. } break;
  2511. case PLANE: {
  2512. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  2513. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  2514. return (hash_compare_vector3(l->normal, r->normal)) &&
  2515. (hash_compare_scalar(l->d, r->d));
  2516. } break;
  2517. case AABB: {
  2518. const ::AABB *l = _data._aabb;
  2519. const ::AABB *r = p_variant._data._aabb;
  2520. return (hash_compare_vector3(l->position, r->position) &&
  2521. (hash_compare_vector3(l->size, r->size)));
  2522. } break;
  2523. case QUAT: {
  2524. const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
  2525. const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
  2526. return hash_compare_quat(*l, *r);
  2527. } break;
  2528. case BASIS: {
  2529. const Basis *l = _data._basis;
  2530. const Basis *r = p_variant._data._basis;
  2531. for (int i = 0; i < 3; i++) {
  2532. if (!(hash_compare_vector3(l->elements[i], r->elements[i]))) {
  2533. return false;
  2534. }
  2535. }
  2536. return true;
  2537. } break;
  2538. case TRANSFORM: {
  2539. const Transform *l = _data._transform;
  2540. const Transform *r = p_variant._data._transform;
  2541. for (int i = 0; i < 3; i++) {
  2542. if (!(hash_compare_vector3(l->basis.elements[i], r->basis.elements[i]))) {
  2543. return false;
  2544. }
  2545. }
  2546. return hash_compare_vector3(l->origin, r->origin);
  2547. } break;
  2548. case COLOR: {
  2549. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  2550. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  2551. return hash_compare_color(*l, *r);
  2552. } break;
  2553. case ARRAY: {
  2554. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  2555. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  2556. if (l.size() != r.size()) {
  2557. return false;
  2558. }
  2559. for (int i = 0; i < l.size(); ++i) {
  2560. if (!l[i].hash_compare(r[i])) {
  2561. return false;
  2562. }
  2563. }
  2564. return true;
  2565. } break;
  2566. case POOL_REAL_ARRAY: {
  2567. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2568. } break;
  2569. case POOL_VECTOR2_ARRAY: {
  2570. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2571. } break;
  2572. case POOL_VECTOR3_ARRAY: {
  2573. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2574. } break;
  2575. case POOL_COLOR_ARRAY: {
  2576. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2577. } break;
  2578. default:
  2579. bool v;
  2580. Variant r;
  2581. evaluate(OP_EQUAL, *this, p_variant, r, v);
  2582. return r;
  2583. }
  2584. return false;
  2585. }
  2586. bool Variant::is_ref() const {
  2587. return type == OBJECT && !_get_obj().ref.is_null();
  2588. }
  2589. Vector<Variant> varray() {
  2590. return Vector<Variant>();
  2591. }
  2592. Vector<Variant> varray(const Variant &p_arg1) {
  2593. Vector<Variant> v;
  2594. v.push_back(p_arg1);
  2595. return v;
  2596. }
  2597. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
  2598. Vector<Variant> v;
  2599. v.push_back(p_arg1);
  2600. v.push_back(p_arg2);
  2601. return v;
  2602. }
  2603. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
  2604. Vector<Variant> v;
  2605. v.push_back(p_arg1);
  2606. v.push_back(p_arg2);
  2607. v.push_back(p_arg3);
  2608. return v;
  2609. }
  2610. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
  2611. Vector<Variant> v;
  2612. v.push_back(p_arg1);
  2613. v.push_back(p_arg2);
  2614. v.push_back(p_arg3);
  2615. v.push_back(p_arg4);
  2616. return v;
  2617. }
  2618. Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
  2619. Vector<Variant> v;
  2620. v.push_back(p_arg1);
  2621. v.push_back(p_arg2);
  2622. v.push_back(p_arg3);
  2623. v.push_back(p_arg4);
  2624. v.push_back(p_arg5);
  2625. return v;
  2626. }
  2627. void Variant::static_assign(const Variant &p_variant) {
  2628. }
  2629. bool Variant::is_shared() const {
  2630. switch (type) {
  2631. case OBJECT:
  2632. return true;
  2633. case ARRAY:
  2634. return true;
  2635. case DICTIONARY:
  2636. return true;
  2637. default: {
  2638. }
  2639. }
  2640. return false;
  2641. }
  2642. Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
  2643. VARIANT_ARGPTRS;
  2644. int argc = 0;
  2645. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  2646. if (argptr[i]->get_type() == Variant::NIL) {
  2647. break;
  2648. }
  2649. argc++;
  2650. }
  2651. CallError error;
  2652. Variant ret = call(p_method, argptr, argc, error);
  2653. switch (error.error) {
  2654. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2655. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
  2656. ERR_PRINT(err.utf8().get_data());
  2657. } break;
  2658. case CallError::CALL_ERROR_INVALID_METHOD: {
  2659. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  2660. ERR_PRINT(err.utf8().get_data());
  2661. } break;
  2662. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2663. String err = "Too many arguments for method '" + p_method + "'";
  2664. ERR_PRINT(err.utf8().get_data());
  2665. } break;
  2666. default: {
  2667. }
  2668. }
  2669. return ret;
  2670. }
  2671. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  2672. r_value = Variant();
  2673. }
  2674. String Variant::get_construct_string() const {
  2675. String vars;
  2676. VariantWriter::write_to_string(*this, vars);
  2677. return vars;
  2678. }
  2679. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
  2680. String err_text;
  2681. if (ce.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2682. int errorarg = ce.argument;
  2683. if (p_argptrs) {
  2684. 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) + ".";
  2685. } else {
  2686. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
  2687. }
  2688. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2689. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2690. } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2691. err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
  2692. } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2693. err_text = "Method not found.";
  2694. } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2695. err_text = "Instance is null";
  2696. } else if (ce.error == Variant::CallError::CALL_OK) {
  2697. return "Call OK";
  2698. }
  2699. String class_name = p_base->get_class();
  2700. Ref<Script> script = p_base->get_script();
  2701. if (script.is_valid() && script->get_path().is_resource_file()) {
  2702. class_name += "(" + script->get_path().get_file() + ")";
  2703. }
  2704. return "'" + class_name + "::" + String(p_method) + "': " + err_text;
  2705. }
  2706. String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
  2707. Array args;
  2708. if (p1.get_type() != Variant::NIL) {
  2709. args.push_back(p1);
  2710. if (p2.get_type() != Variant::NIL) {
  2711. args.push_back(p2);
  2712. if (p3.get_type() != Variant::NIL) {
  2713. args.push_back(p3);
  2714. if (p4.get_type() != Variant::NIL) {
  2715. args.push_back(p4);
  2716. if (p5.get_type() != Variant::NIL) {
  2717. args.push_back(p5);
  2718. }
  2719. }
  2720. }
  2721. }
  2722. }
  2723. bool error = false;
  2724. String fmt = p_text.sprintf(args, &error);
  2725. ERR_FAIL_COND_V_MSG(error, String(), fmt);
  2726. return fmt;
  2727. }