variant.cpp 94 KB

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