variant.cpp 93 KB

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