variant.cpp 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708
  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. switch (type) {
  1326. case NIL:
  1327. return 0;
  1328. case BOOL:
  1329. return _data._bool ? 1 : 0;
  1330. case INT:
  1331. return int64_t(_data._int);
  1332. case FLOAT:
  1333. return int64_t(_data._float);
  1334. case STRING:
  1335. return int64_t(operator String().to_int());
  1336. default: {
  1337. return 0;
  1338. }
  1339. }
  1340. }
  1341. Variant::operator int32_t() const {
  1342. switch (type) {
  1343. case NIL:
  1344. return 0;
  1345. case BOOL:
  1346. return _data._bool ? 1 : 0;
  1347. case INT:
  1348. return int32_t(_data._int);
  1349. case FLOAT:
  1350. return int32_t(_data._float);
  1351. case STRING:
  1352. return int32_t(operator String().to_int());
  1353. default: {
  1354. return 0;
  1355. }
  1356. }
  1357. }
  1358. Variant::operator int16_t() const {
  1359. switch (type) {
  1360. case NIL:
  1361. return 0;
  1362. case BOOL:
  1363. return _data._bool ? 1 : 0;
  1364. case INT:
  1365. return int16_t(_data._int);
  1366. case FLOAT:
  1367. return int16_t(_data._float);
  1368. case STRING:
  1369. return int16_t(operator String().to_int());
  1370. default: {
  1371. return 0;
  1372. }
  1373. }
  1374. }
  1375. Variant::operator int8_t() const {
  1376. switch (type) {
  1377. case NIL:
  1378. return 0;
  1379. case BOOL:
  1380. return _data._bool ? 1 : 0;
  1381. case INT:
  1382. return int8_t(_data._int);
  1383. case FLOAT:
  1384. return int8_t(_data._float);
  1385. case STRING:
  1386. return int8_t(operator String().to_int());
  1387. default: {
  1388. return 0;
  1389. }
  1390. }
  1391. }
  1392. Variant::operator uint64_t() const {
  1393. switch (type) {
  1394. case NIL:
  1395. return 0;
  1396. case BOOL:
  1397. return _data._bool ? 1 : 0;
  1398. case INT:
  1399. return uint64_t(_data._int);
  1400. case FLOAT:
  1401. return uint64_t(_data._float);
  1402. case STRING:
  1403. return uint64_t(operator String().to_int());
  1404. default: {
  1405. return 0;
  1406. }
  1407. }
  1408. }
  1409. Variant::operator uint32_t() const {
  1410. switch (type) {
  1411. case NIL:
  1412. return 0;
  1413. case BOOL:
  1414. return _data._bool ? 1 : 0;
  1415. case INT:
  1416. return uint32_t(_data._int);
  1417. case FLOAT:
  1418. return uint32_t(_data._float);
  1419. case STRING:
  1420. return uint32_t(operator String().to_int());
  1421. default: {
  1422. return 0;
  1423. }
  1424. }
  1425. }
  1426. Variant::operator uint16_t() const {
  1427. switch (type) {
  1428. case NIL:
  1429. return 0;
  1430. case BOOL:
  1431. return _data._bool ? 1 : 0;
  1432. case INT:
  1433. return uint16_t(_data._int);
  1434. case FLOAT:
  1435. return uint16_t(_data._float);
  1436. case STRING:
  1437. return uint16_t(operator String().to_int());
  1438. default: {
  1439. return 0;
  1440. }
  1441. }
  1442. }
  1443. Variant::operator uint8_t() const {
  1444. switch (type) {
  1445. case NIL:
  1446. return 0;
  1447. case BOOL:
  1448. return _data._bool ? 1 : 0;
  1449. case INT:
  1450. return uint8_t(_data._int);
  1451. case FLOAT:
  1452. return uint8_t(_data._float);
  1453. case STRING:
  1454. return uint8_t(operator String().to_int());
  1455. default: {
  1456. return 0;
  1457. }
  1458. }
  1459. }
  1460. Variant::operator ObjectID() const {
  1461. if (type == INT) {
  1462. return ObjectID(_data._int);
  1463. } else if (type == OBJECT) {
  1464. return _get_obj().id;
  1465. } else {
  1466. return ObjectID();
  1467. }
  1468. }
  1469. Variant::operator char32_t() const {
  1470. return operator uint32_t();
  1471. }
  1472. Variant::operator float() const {
  1473. switch (type) {
  1474. case NIL:
  1475. return 0;
  1476. case BOOL:
  1477. return _data._bool ? 1.0 : 0.0;
  1478. case INT:
  1479. return (float)_data._int;
  1480. case FLOAT:
  1481. return _data._float;
  1482. case STRING:
  1483. return operator String().to_float();
  1484. default: {
  1485. return 0;
  1486. }
  1487. }
  1488. }
  1489. Variant::operator double() const {
  1490. switch (type) {
  1491. case NIL:
  1492. return 0;
  1493. case BOOL:
  1494. return _data._bool ? 1.0 : 0.0;
  1495. case INT:
  1496. return (double)_data._int;
  1497. case FLOAT:
  1498. return _data._float;
  1499. case STRING:
  1500. return operator String().to_float();
  1501. default: {
  1502. return 0;
  1503. }
  1504. }
  1505. }
  1506. Variant::operator StringName() const {
  1507. if (type == STRING_NAME) {
  1508. return *reinterpret_cast<const StringName *>(_data._mem);
  1509. } else if (type == STRING) {
  1510. return *reinterpret_cast<const String *>(_data._mem);
  1511. }
  1512. return StringName();
  1513. }
  1514. struct _VariantStrPair {
  1515. String key;
  1516. String value;
  1517. bool operator<(const _VariantStrPair &p) const {
  1518. return key < p.key;
  1519. }
  1520. };
  1521. Variant::operator String() const {
  1522. return stringify(0);
  1523. }
  1524. String stringify_variant_clean(const Variant &p_variant, int recursion_count) {
  1525. String s = p_variant.stringify(recursion_count);
  1526. // Wrap strings in quotes to avoid ambiguity.
  1527. switch (p_variant.get_type()) {
  1528. case Variant::STRING: {
  1529. s = s.c_escape().quote();
  1530. } break;
  1531. case Variant::STRING_NAME: {
  1532. s = "&" + s.c_escape().quote();
  1533. } break;
  1534. case Variant::NODE_PATH: {
  1535. s = "^" + s.c_escape().quote();
  1536. } break;
  1537. default: {
  1538. } break;
  1539. }
  1540. return s;
  1541. }
  1542. template <typename T>
  1543. String stringify_vector(const T &vec, int recursion_count) {
  1544. String str("[");
  1545. for (int i = 0; i < vec.size(); i++) {
  1546. if (i > 0) {
  1547. str += ", ";
  1548. }
  1549. str += stringify_variant_clean(vec[i], recursion_count);
  1550. }
  1551. str += "]";
  1552. return str;
  1553. }
  1554. String Variant::stringify(int recursion_count) const {
  1555. switch (type) {
  1556. case NIL:
  1557. return "<null>";
  1558. case BOOL:
  1559. return _data._bool ? "true" : "false";
  1560. case INT:
  1561. return itos(_data._int);
  1562. case FLOAT:
  1563. return String::num_real(_data._float, true);
  1564. case STRING:
  1565. return *reinterpret_cast<const String *>(_data._mem);
  1566. case VECTOR2:
  1567. return operator Vector2();
  1568. case VECTOR2I:
  1569. return operator Vector2i();
  1570. case RECT2:
  1571. return operator Rect2();
  1572. case RECT2I:
  1573. return operator Rect2i();
  1574. case TRANSFORM2D:
  1575. return operator Transform2D();
  1576. case VECTOR3:
  1577. return operator Vector3();
  1578. case VECTOR3I:
  1579. return operator Vector3i();
  1580. case VECTOR4:
  1581. return operator Vector4();
  1582. case VECTOR4I:
  1583. return operator Vector4i();
  1584. case PLANE:
  1585. return operator Plane();
  1586. case AABB:
  1587. return operator ::AABB();
  1588. case QUATERNION:
  1589. return operator Quaternion();
  1590. case BASIS:
  1591. return operator Basis();
  1592. case TRANSFORM3D:
  1593. return operator Transform3D();
  1594. case PROJECTION:
  1595. return operator Projection();
  1596. case STRING_NAME:
  1597. return operator StringName();
  1598. case NODE_PATH:
  1599. return operator NodePath();
  1600. case COLOR:
  1601. return operator Color();
  1602. case DICTIONARY: {
  1603. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "{ ... }", "Maximum dictionary recursion reached!");
  1604. recursion_count++;
  1605. const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
  1606. // Add leading and trailing space to Dictionary printing. This distinguishes it
  1607. // from array printing on fonts that have similar-looking {} and [] characters.
  1608. String str("{ ");
  1609. List<Variant> keys;
  1610. d.get_key_list(&keys);
  1611. Vector<_VariantStrPair> pairs;
  1612. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1613. _VariantStrPair sp;
  1614. sp.key = stringify_variant_clean(E->get(), recursion_count);
  1615. sp.value = stringify_variant_clean(d[E->get()], recursion_count);
  1616. pairs.push_back(sp);
  1617. }
  1618. for (int i = 0; i < pairs.size(); i++) {
  1619. if (i > 0) {
  1620. str += ", ";
  1621. }
  1622. str += pairs[i].key + ": " + pairs[i].value;
  1623. }
  1624. str += " }";
  1625. return str;
  1626. }
  1627. // Packed arrays cannot contain recursive structures, the recursion_count increment is not needed.
  1628. case PACKED_VECTOR2_ARRAY: {
  1629. return stringify_vector(operator PackedVector2Array(), recursion_count);
  1630. }
  1631. case PACKED_VECTOR3_ARRAY: {
  1632. return stringify_vector(operator PackedVector3Array(), recursion_count);
  1633. }
  1634. case PACKED_COLOR_ARRAY: {
  1635. return stringify_vector(operator PackedColorArray(), recursion_count);
  1636. }
  1637. case PACKED_VECTOR4_ARRAY: {
  1638. return stringify_vector(operator PackedVector4Array(), recursion_count);
  1639. }
  1640. case PACKED_STRING_ARRAY: {
  1641. return stringify_vector(operator PackedStringArray(), recursion_count);
  1642. }
  1643. case PACKED_BYTE_ARRAY: {
  1644. return stringify_vector(operator PackedByteArray(), recursion_count);
  1645. }
  1646. case PACKED_INT32_ARRAY: {
  1647. return stringify_vector(operator PackedInt32Array(), recursion_count);
  1648. }
  1649. case PACKED_INT64_ARRAY: {
  1650. return stringify_vector(operator PackedInt64Array(), recursion_count);
  1651. }
  1652. case PACKED_FLOAT32_ARRAY: {
  1653. return stringify_vector(operator PackedFloat32Array(), recursion_count);
  1654. }
  1655. case PACKED_FLOAT64_ARRAY: {
  1656. return stringify_vector(operator PackedFloat64Array(), recursion_count);
  1657. }
  1658. case ARRAY: {
  1659. ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "[...]", "Maximum array recursion reached!");
  1660. recursion_count++;
  1661. return stringify_vector(operator Array(), recursion_count);
  1662. }
  1663. case OBJECT: {
  1664. if (_get_obj().obj) {
  1665. if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
  1666. return "<Freed Object>";
  1667. }
  1668. return _get_obj().obj->to_string();
  1669. } else {
  1670. return "<Object#null>";
  1671. }
  1672. }
  1673. case CALLABLE: {
  1674. const Callable &c = *reinterpret_cast<const Callable *>(_data._mem);
  1675. return c;
  1676. }
  1677. case SIGNAL: {
  1678. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  1679. return s;
  1680. }
  1681. case RID: {
  1682. const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem);
  1683. return "RID(" + itos(s.get_id()) + ")";
  1684. }
  1685. default: {
  1686. return "<" + get_type_name(type) + ">";
  1687. }
  1688. }
  1689. }
  1690. String Variant::to_json_string() const {
  1691. return JSON::stringify(*this);
  1692. }
  1693. Variant::operator Vector2() const {
  1694. if (type == VECTOR2) {
  1695. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1696. } else if (type == VECTOR2I) {
  1697. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1698. } else if (type == VECTOR3) {
  1699. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1700. } else if (type == VECTOR3I) {
  1701. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1702. } else if (type == VECTOR4) {
  1703. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1704. } else if (type == VECTOR4I) {
  1705. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1706. } else {
  1707. return Vector2();
  1708. }
  1709. }
  1710. Variant::operator Vector2i() const {
  1711. if (type == VECTOR2I) {
  1712. return *reinterpret_cast<const Vector2i *>(_data._mem);
  1713. } else if (type == VECTOR2) {
  1714. return *reinterpret_cast<const Vector2 *>(_data._mem);
  1715. } else if (type == VECTOR3) {
  1716. return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
  1717. } else if (type == VECTOR3I) {
  1718. return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
  1719. } else if (type == VECTOR4) {
  1720. return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
  1721. } else if (type == VECTOR4I) {
  1722. return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
  1723. } else {
  1724. return Vector2i();
  1725. }
  1726. }
  1727. Variant::operator Rect2() const {
  1728. if (type == RECT2) {
  1729. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1730. } else if (type == RECT2I) {
  1731. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1732. } else {
  1733. return Rect2();
  1734. }
  1735. }
  1736. Variant::operator Rect2i() const {
  1737. if (type == RECT2I) {
  1738. return *reinterpret_cast<const Rect2i *>(_data._mem);
  1739. } else if (type == RECT2) {
  1740. return *reinterpret_cast<const Rect2 *>(_data._mem);
  1741. } else {
  1742. return Rect2i();
  1743. }
  1744. }
  1745. Variant::operator Vector3() const {
  1746. if (type == VECTOR3) {
  1747. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1748. } else if (type == VECTOR3I) {
  1749. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1750. } else if (type == VECTOR2) {
  1751. return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1752. } else if (type == VECTOR2I) {
  1753. return Vector3(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1754. } else if (type == VECTOR4) {
  1755. return Vector3(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1756. } else if (type == VECTOR4I) {
  1757. return Vector3(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1758. } else {
  1759. return Vector3();
  1760. }
  1761. }
  1762. Variant::operator Vector3i() const {
  1763. if (type == VECTOR3I) {
  1764. return *reinterpret_cast<const Vector3i *>(_data._mem);
  1765. } else if (type == VECTOR3) {
  1766. return *reinterpret_cast<const Vector3 *>(_data._mem);
  1767. } else if (type == VECTOR2) {
  1768. return Vector3i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
  1769. } else if (type == VECTOR2I) {
  1770. return Vector3i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
  1771. } else if (type == VECTOR4) {
  1772. return Vector3i(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
  1773. } else if (type == VECTOR4I) {
  1774. return Vector3i(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
  1775. } else {
  1776. return Vector3i();
  1777. }
  1778. }
  1779. Variant::operator Vector4() const {
  1780. if (type == VECTOR4) {
  1781. return *reinterpret_cast<const Vector4 *>(_data._mem);
  1782. } else if (type == VECTOR4I) {
  1783. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1784. } else if (type == VECTOR2) {
  1785. return Vector4(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1786. } else if (type == VECTOR2I) {
  1787. return Vector4(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1788. } else if (type == VECTOR3) {
  1789. 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);
  1790. } else if (type == VECTOR3I) {
  1791. 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);
  1792. } else {
  1793. return Vector4();
  1794. }
  1795. }
  1796. Variant::operator Vector4i() const {
  1797. if (type == VECTOR4I) {
  1798. return *reinterpret_cast<const Vector4i *>(_data._mem);
  1799. } else if (type == VECTOR4) {
  1800. const Vector4 &v4 = *reinterpret_cast<const Vector4 *>(_data._mem);
  1801. return Vector4i(v4.x, v4.y, v4.z, v4.w);
  1802. } else if (type == VECTOR2) {
  1803. return Vector4i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
  1804. } else if (type == VECTOR2I) {
  1805. return Vector4i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
  1806. } else if (type == VECTOR3) {
  1807. 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);
  1808. } else if (type == VECTOR3I) {
  1809. 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);
  1810. } else {
  1811. return Vector4i();
  1812. }
  1813. }
  1814. Variant::operator Plane() const {
  1815. if (type == PLANE) {
  1816. return *reinterpret_cast<const Plane *>(_data._mem);
  1817. } else {
  1818. return Plane();
  1819. }
  1820. }
  1821. Variant::operator ::AABB() const {
  1822. if (type == AABB) {
  1823. return *_data._aabb;
  1824. } else {
  1825. return ::AABB();
  1826. }
  1827. }
  1828. Variant::operator Basis() const {
  1829. if (type == BASIS) {
  1830. return *_data._basis;
  1831. } else if (type == QUATERNION) {
  1832. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1833. } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert?
  1834. return _data._transform3d->basis;
  1835. } else {
  1836. return Basis();
  1837. }
  1838. }
  1839. Variant::operator Quaternion() const {
  1840. if (type == QUATERNION) {
  1841. return *reinterpret_cast<const Quaternion *>(_data._mem);
  1842. } else if (type == BASIS) {
  1843. return *_data._basis;
  1844. } else if (type == TRANSFORM3D) {
  1845. return _data._transform3d->basis;
  1846. } else {
  1847. return Quaternion();
  1848. }
  1849. }
  1850. Variant::operator Transform3D() const {
  1851. if (type == TRANSFORM3D) {
  1852. return *_data._transform3d;
  1853. } else if (type == BASIS) {
  1854. return Transform3D(*_data._basis, Vector3());
  1855. } else if (type == QUATERNION) {
  1856. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1857. } else if (type == TRANSFORM2D) {
  1858. const Transform2D &t = *_data._transform2d;
  1859. Transform3D m;
  1860. m.basis.rows[0][0] = t.columns[0][0];
  1861. m.basis.rows[1][0] = t.columns[0][1];
  1862. m.basis.rows[0][1] = t.columns[1][0];
  1863. m.basis.rows[1][1] = t.columns[1][1];
  1864. m.origin[0] = t.columns[2][0];
  1865. m.origin[1] = t.columns[2][1];
  1866. return m;
  1867. } else if (type == PROJECTION) {
  1868. return *_data._projection;
  1869. } else {
  1870. return Transform3D();
  1871. }
  1872. }
  1873. Variant::operator Projection() const {
  1874. if (type == TRANSFORM3D) {
  1875. return *_data._transform3d;
  1876. } else if (type == BASIS) {
  1877. return Transform3D(*_data._basis, Vector3());
  1878. } else if (type == QUATERNION) {
  1879. return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
  1880. } else if (type == TRANSFORM2D) {
  1881. const Transform2D &t = *_data._transform2d;
  1882. Transform3D m;
  1883. m.basis.rows[0][0] = t.columns[0][0];
  1884. m.basis.rows[1][0] = t.columns[0][1];
  1885. m.basis.rows[0][1] = t.columns[1][0];
  1886. m.basis.rows[1][1] = t.columns[1][1];
  1887. m.origin[0] = t.columns[2][0];
  1888. m.origin[1] = t.columns[2][1];
  1889. return m;
  1890. } else if (type == PROJECTION) {
  1891. return *_data._projection;
  1892. } else {
  1893. return Projection();
  1894. }
  1895. }
  1896. Variant::operator Transform2D() const {
  1897. if (type == TRANSFORM2D) {
  1898. return *_data._transform2d;
  1899. } else if (type == TRANSFORM3D) {
  1900. const Transform3D &t = *_data._transform3d;
  1901. Transform2D m;
  1902. m.columns[0][0] = t.basis.rows[0][0];
  1903. m.columns[0][1] = t.basis.rows[1][0];
  1904. m.columns[1][0] = t.basis.rows[0][1];
  1905. m.columns[1][1] = t.basis.rows[1][1];
  1906. m.columns[2][0] = t.origin[0];
  1907. m.columns[2][1] = t.origin[1];
  1908. return m;
  1909. } else {
  1910. return Transform2D();
  1911. }
  1912. }
  1913. Variant::operator Color() const {
  1914. if (type == COLOR) {
  1915. return *reinterpret_cast<const Color *>(_data._mem);
  1916. } else if (type == STRING) {
  1917. return Color(operator String());
  1918. } else if (type == INT) {
  1919. return Color::hex(operator int());
  1920. } else {
  1921. return Color();
  1922. }
  1923. }
  1924. Variant::operator NodePath() const {
  1925. if (type == NODE_PATH) {
  1926. return *reinterpret_cast<const NodePath *>(_data._mem);
  1927. } else if (type == STRING) {
  1928. return NodePath(operator String());
  1929. } else {
  1930. return NodePath();
  1931. }
  1932. }
  1933. Variant::operator ::RID() const {
  1934. if (type == RID) {
  1935. return *reinterpret_cast<const ::RID *>(_data._mem);
  1936. } else if (type == OBJECT && _get_obj().obj == nullptr) {
  1937. return ::RID();
  1938. } else if (type == OBJECT && _get_obj().obj) {
  1939. #ifdef DEBUG_ENABLED
  1940. if (EngineDebugger::is_active()) {
  1941. ERR_FAIL_NULL_V_MSG(ObjectDB::get_instance(_get_obj().id), ::RID(), "Invalid pointer (object was freed).");
  1942. }
  1943. #endif
  1944. Callable::CallError ce;
  1945. const Variant ret = _get_obj().obj->callp(CoreStringName(get_rid), nullptr, 0, ce);
  1946. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) {
  1947. return ret;
  1948. }
  1949. return ::RID();
  1950. } else {
  1951. return ::RID();
  1952. }
  1953. }
  1954. Variant::operator Object *() const {
  1955. if (type == OBJECT) {
  1956. return _get_obj().obj;
  1957. } else {
  1958. return nullptr;
  1959. }
  1960. }
  1961. Object *Variant::get_validated_object_with_check(bool &r_previously_freed) const {
  1962. if (type == OBJECT) {
  1963. Object *instance = ObjectDB::get_instance(_get_obj().id);
  1964. r_previously_freed = !instance && _get_obj().id != ObjectID();
  1965. return instance;
  1966. } else {
  1967. r_previously_freed = false;
  1968. return nullptr;
  1969. }
  1970. }
  1971. Object *Variant::get_validated_object() const {
  1972. if (type == OBJECT) {
  1973. return ObjectDB::get_instance(_get_obj().id);
  1974. } else {
  1975. return nullptr;
  1976. }
  1977. }
  1978. Variant::operator Dictionary() const {
  1979. if (type == DICTIONARY) {
  1980. return *reinterpret_cast<const Dictionary *>(_data._mem);
  1981. } else {
  1982. return Dictionary();
  1983. }
  1984. }
  1985. Variant::operator Callable() const {
  1986. if (type == CALLABLE) {
  1987. return *reinterpret_cast<const Callable *>(_data._mem);
  1988. } else {
  1989. return Callable();
  1990. }
  1991. }
  1992. Variant::operator Signal() const {
  1993. if (type == SIGNAL) {
  1994. return *reinterpret_cast<const Signal *>(_data._mem);
  1995. } else {
  1996. return Signal();
  1997. }
  1998. }
  1999. template <typename DA, typename SA>
  2000. inline DA _convert_array(const SA &p_array) {
  2001. DA da;
  2002. da.resize(p_array.size());
  2003. for (int i = 0; i < p_array.size(); i++) {
  2004. da.set(i, Variant(p_array.get(i)));
  2005. }
  2006. return da;
  2007. }
  2008. template <typename DA>
  2009. inline DA _convert_array_from_variant(const Variant &p_variant) {
  2010. switch (p_variant.get_type()) {
  2011. case Variant::ARRAY: {
  2012. return _convert_array<DA, Array>(p_variant.operator Array());
  2013. }
  2014. case Variant::PACKED_BYTE_ARRAY: {
  2015. return _convert_array<DA, PackedByteArray>(p_variant.operator PackedByteArray());
  2016. }
  2017. case Variant::PACKED_INT32_ARRAY: {
  2018. return _convert_array<DA, PackedInt32Array>(p_variant.operator PackedInt32Array());
  2019. }
  2020. case Variant::PACKED_INT64_ARRAY: {
  2021. return _convert_array<DA, PackedInt64Array>(p_variant.operator PackedInt64Array());
  2022. }
  2023. case Variant::PACKED_FLOAT32_ARRAY: {
  2024. return _convert_array<DA, PackedFloat32Array>(p_variant.operator PackedFloat32Array());
  2025. }
  2026. case Variant::PACKED_FLOAT64_ARRAY: {
  2027. return _convert_array<DA, PackedFloat64Array>(p_variant.operator PackedFloat64Array());
  2028. }
  2029. case Variant::PACKED_STRING_ARRAY: {
  2030. return _convert_array<DA, PackedStringArray>(p_variant.operator PackedStringArray());
  2031. }
  2032. case Variant::PACKED_VECTOR2_ARRAY: {
  2033. return _convert_array<DA, PackedVector2Array>(p_variant.operator PackedVector2Array());
  2034. }
  2035. case Variant::PACKED_VECTOR3_ARRAY: {
  2036. return _convert_array<DA, PackedVector3Array>(p_variant.operator PackedVector3Array());
  2037. }
  2038. case Variant::PACKED_COLOR_ARRAY: {
  2039. return _convert_array<DA, PackedColorArray>(p_variant.operator PackedColorArray());
  2040. }
  2041. case Variant::PACKED_VECTOR4_ARRAY: {
  2042. return _convert_array<DA, PackedVector4Array>(p_variant.operator PackedVector4Array());
  2043. }
  2044. default: {
  2045. return DA();
  2046. }
  2047. }
  2048. }
  2049. Variant::operator Array() const {
  2050. if (type == ARRAY) {
  2051. return *reinterpret_cast<const Array *>(_data._mem);
  2052. } else {
  2053. return _convert_array_from_variant<Array>(*this);
  2054. }
  2055. }
  2056. Variant::operator PackedByteArray() const {
  2057. if (type == PACKED_BYTE_ARRAY) {
  2058. return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array;
  2059. } else {
  2060. return _convert_array_from_variant<PackedByteArray>(*this);
  2061. }
  2062. }
  2063. Variant::operator PackedInt32Array() const {
  2064. if (type == PACKED_INT32_ARRAY) {
  2065. return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array;
  2066. } else {
  2067. return _convert_array_from_variant<PackedInt32Array>(*this);
  2068. }
  2069. }
  2070. Variant::operator PackedInt64Array() const {
  2071. if (type == PACKED_INT64_ARRAY) {
  2072. return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array;
  2073. } else {
  2074. return _convert_array_from_variant<PackedInt64Array>(*this);
  2075. }
  2076. }
  2077. Variant::operator PackedFloat32Array() const {
  2078. if (type == PACKED_FLOAT32_ARRAY) {
  2079. return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array;
  2080. } else {
  2081. return _convert_array_from_variant<PackedFloat32Array>(*this);
  2082. }
  2083. }
  2084. Variant::operator PackedFloat64Array() const {
  2085. if (type == PACKED_FLOAT64_ARRAY) {
  2086. return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array;
  2087. } else {
  2088. return _convert_array_from_variant<PackedFloat64Array>(*this);
  2089. }
  2090. }
  2091. Variant::operator PackedStringArray() const {
  2092. if (type == PACKED_STRING_ARRAY) {
  2093. return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array;
  2094. } else {
  2095. return _convert_array_from_variant<PackedStringArray>(*this);
  2096. }
  2097. }
  2098. Variant::operator PackedVector2Array() const {
  2099. if (type == PACKED_VECTOR2_ARRAY) {
  2100. return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array;
  2101. } else {
  2102. return _convert_array_from_variant<PackedVector2Array>(*this);
  2103. }
  2104. }
  2105. Variant::operator PackedVector3Array() const {
  2106. if (type == PACKED_VECTOR3_ARRAY) {
  2107. return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array;
  2108. } else {
  2109. return _convert_array_from_variant<PackedVector3Array>(*this);
  2110. }
  2111. }
  2112. Variant::operator PackedColorArray() const {
  2113. if (type == PACKED_COLOR_ARRAY) {
  2114. return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array;
  2115. } else {
  2116. return _convert_array_from_variant<PackedColorArray>(*this);
  2117. }
  2118. }
  2119. Variant::operator PackedVector4Array() const {
  2120. if (type == PACKED_VECTOR4_ARRAY) {
  2121. return static_cast<PackedArrayRef<Vector4> *>(_data.packed_array)->array;
  2122. } else {
  2123. return _convert_array_from_variant<PackedVector4Array>(*this);
  2124. }
  2125. }
  2126. /* helpers */
  2127. Variant::operator Vector<::RID>() const {
  2128. Array va = operator Array();
  2129. Vector<::RID> rids;
  2130. rids.resize(va.size());
  2131. for (int i = 0; i < rids.size(); i++) {
  2132. rids.write[i] = va[i];
  2133. }
  2134. return rids;
  2135. }
  2136. Variant::operator Vector<Plane>() const {
  2137. Array va = operator Array();
  2138. Vector<Plane> planes;
  2139. int va_size = va.size();
  2140. if (va_size == 0) {
  2141. return planes;
  2142. }
  2143. planes.resize(va_size);
  2144. Plane *w = planes.ptrw();
  2145. for (int i = 0; i < va_size; i++) {
  2146. w[i] = va[i];
  2147. }
  2148. return planes;
  2149. }
  2150. Variant::operator Vector<Face3>() const {
  2151. PackedVector3Array va = operator PackedVector3Array();
  2152. Vector<Face3> faces;
  2153. int va_size = va.size();
  2154. if (va_size == 0) {
  2155. return faces;
  2156. }
  2157. faces.resize(va_size / 3);
  2158. Face3 *w = faces.ptrw();
  2159. const Vector3 *r = va.ptr();
  2160. for (int i = 0; i < va_size; i++) {
  2161. w[i / 3].vertex[i % 3] = r[i];
  2162. }
  2163. return faces;
  2164. }
  2165. Variant::operator Vector<Variant>() const {
  2166. Array va = operator Array();
  2167. Vector<Variant> variants;
  2168. int va_size = va.size();
  2169. if (va_size == 0) {
  2170. return variants;
  2171. }
  2172. variants.resize(va_size);
  2173. Variant *w = variants.ptrw();
  2174. for (int i = 0; i < va_size; i++) {
  2175. w[i] = va[i];
  2176. }
  2177. return variants;
  2178. }
  2179. Variant::operator Vector<StringName>() const {
  2180. PackedStringArray from = operator PackedStringArray();
  2181. Vector<StringName> to;
  2182. int len = from.size();
  2183. to.resize(len);
  2184. for (int i = 0; i < len; i++) {
  2185. to.write[i] = from[i];
  2186. }
  2187. return to;
  2188. }
  2189. Variant::operator Side() const {
  2190. return (Side) operator int();
  2191. }
  2192. Variant::operator Orientation() const {
  2193. return (Orientation) operator int();
  2194. }
  2195. Variant::operator IPAddress() const {
  2196. if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
  2197. Vector<int> addr = operator Vector<int>();
  2198. if (addr.size() == 4) {
  2199. return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
  2200. }
  2201. }
  2202. return IPAddress(operator String());
  2203. }
  2204. Variant::Variant(bool p_bool) :
  2205. type(BOOL) {
  2206. _data._bool = p_bool;
  2207. }
  2208. Variant::Variant(int64_t p_int64) :
  2209. type(INT) {
  2210. _data._int = p_int64;
  2211. }
  2212. Variant::Variant(int32_t p_int32) :
  2213. type(INT) {
  2214. _data._int = p_int32;
  2215. }
  2216. Variant::Variant(int16_t p_int16) :
  2217. type(INT) {
  2218. _data._int = p_int16;
  2219. }
  2220. Variant::Variant(int8_t p_int8) :
  2221. type(INT) {
  2222. _data._int = p_int8;
  2223. }
  2224. Variant::Variant(uint64_t p_uint64) :
  2225. type(INT) {
  2226. _data._int = int64_t(p_uint64);
  2227. }
  2228. Variant::Variant(uint32_t p_uint32) :
  2229. type(INT) {
  2230. _data._int = int64_t(p_uint32);
  2231. }
  2232. Variant::Variant(uint16_t p_uint16) :
  2233. type(INT) {
  2234. _data._int = int64_t(p_uint16);
  2235. }
  2236. Variant::Variant(uint8_t p_uint8) :
  2237. type(INT) {
  2238. _data._int = int64_t(p_uint8);
  2239. }
  2240. Variant::Variant(float p_float) :
  2241. type(FLOAT) {
  2242. _data._float = p_float;
  2243. }
  2244. Variant::Variant(double p_double) :
  2245. type(FLOAT) {
  2246. _data._float = p_double;
  2247. }
  2248. Variant::Variant(const ObjectID &p_id) :
  2249. type(INT) {
  2250. _data._int = int64_t(p_id);
  2251. }
  2252. Variant::Variant(const StringName &p_string) :
  2253. type(STRING_NAME) {
  2254. memnew_placement(_data._mem, StringName(p_string));
  2255. }
  2256. Variant::Variant(const String &p_string) :
  2257. type(STRING) {
  2258. memnew_placement(_data._mem, String(p_string));
  2259. }
  2260. Variant::Variant(const char *const p_cstring) :
  2261. type(STRING) {
  2262. memnew_placement(_data._mem, String((const char *)p_cstring));
  2263. }
  2264. Variant::Variant(const char32_t *p_wstring) :
  2265. type(STRING) {
  2266. memnew_placement(_data._mem, String(p_wstring));
  2267. }
  2268. Variant::Variant(const Vector3 &p_vector3) :
  2269. type(VECTOR3) {
  2270. memnew_placement(_data._mem, Vector3(p_vector3));
  2271. }
  2272. Variant::Variant(const Vector3i &p_vector3i) :
  2273. type(VECTOR3I) {
  2274. memnew_placement(_data._mem, Vector3i(p_vector3i));
  2275. }
  2276. Variant::Variant(const Vector4 &p_vector4) :
  2277. type(VECTOR4) {
  2278. memnew_placement(_data._mem, Vector4(p_vector4));
  2279. }
  2280. Variant::Variant(const Vector4i &p_vector4i) :
  2281. type(VECTOR4I) {
  2282. memnew_placement(_data._mem, Vector4i(p_vector4i));
  2283. }
  2284. Variant::Variant(const Vector2 &p_vector2) :
  2285. type(VECTOR2) {
  2286. memnew_placement(_data._mem, Vector2(p_vector2));
  2287. }
  2288. Variant::Variant(const Vector2i &p_vector2i) :
  2289. type(VECTOR2I) {
  2290. memnew_placement(_data._mem, Vector2i(p_vector2i));
  2291. }
  2292. Variant::Variant(const Rect2 &p_rect2) :
  2293. type(RECT2) {
  2294. memnew_placement(_data._mem, Rect2(p_rect2));
  2295. }
  2296. Variant::Variant(const Rect2i &p_rect2i) :
  2297. type(RECT2I) {
  2298. memnew_placement(_data._mem, Rect2i(p_rect2i));
  2299. }
  2300. Variant::Variant(const Plane &p_plane) :
  2301. type(PLANE) {
  2302. memnew_placement(_data._mem, Plane(p_plane));
  2303. }
  2304. Variant::Variant(const ::AABB &p_aabb) :
  2305. type(AABB) {
  2306. _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
  2307. memnew_placement(_data._aabb, ::AABB(p_aabb));
  2308. }
  2309. Variant::Variant(const Basis &p_matrix) :
  2310. type(BASIS) {
  2311. _data._basis = (Basis *)Pools::_bucket_medium.alloc();
  2312. memnew_placement(_data._basis, Basis(p_matrix));
  2313. }
  2314. Variant::Variant(const Quaternion &p_quaternion) :
  2315. type(QUATERNION) {
  2316. memnew_placement(_data._mem, Quaternion(p_quaternion));
  2317. }
  2318. Variant::Variant(const Transform3D &p_transform) :
  2319. type(TRANSFORM3D) {
  2320. _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
  2321. memnew_placement(_data._transform3d, Transform3D(p_transform));
  2322. }
  2323. Variant::Variant(const Projection &pp_projection) :
  2324. type(PROJECTION) {
  2325. _data._projection = (Projection *)Pools::_bucket_large.alloc();
  2326. memnew_placement(_data._projection, Projection(pp_projection));
  2327. }
  2328. Variant::Variant(const Transform2D &p_transform) :
  2329. type(TRANSFORM2D) {
  2330. _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
  2331. memnew_placement(_data._transform2d, Transform2D(p_transform));
  2332. }
  2333. Variant::Variant(const Color &p_color) :
  2334. type(COLOR) {
  2335. memnew_placement(_data._mem, Color(p_color));
  2336. }
  2337. Variant::Variant(const NodePath &p_node_path) :
  2338. type(NODE_PATH) {
  2339. memnew_placement(_data._mem, NodePath(p_node_path));
  2340. }
  2341. Variant::Variant(const ::RID &p_rid) :
  2342. type(RID) {
  2343. memnew_placement(_data._mem, ::RID(p_rid));
  2344. }
  2345. Variant::Variant(const Object *p_object) :
  2346. type(OBJECT) {
  2347. _get_obj() = ObjData();
  2348. _get_obj().ref_pointer(const_cast<Object *>(p_object));
  2349. }
  2350. Variant::Variant(const Callable &p_callable) :
  2351. type(CALLABLE) {
  2352. memnew_placement(_data._mem, Callable(p_callable));
  2353. }
  2354. Variant::Variant(const Signal &p_callable) :
  2355. type(SIGNAL) {
  2356. memnew_placement(_data._mem, Signal(p_callable));
  2357. }
  2358. Variant::Variant(const Dictionary &p_dictionary) :
  2359. type(DICTIONARY) {
  2360. memnew_placement(_data._mem, Dictionary(p_dictionary));
  2361. }
  2362. Variant::Variant(const Array &p_array) :
  2363. type(ARRAY) {
  2364. memnew_placement(_data._mem, Array(p_array));
  2365. }
  2366. Variant::Variant(const PackedByteArray &p_byte_array) :
  2367. type(PACKED_BYTE_ARRAY) {
  2368. _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
  2369. }
  2370. Variant::Variant(const PackedInt32Array &p_int32_array) :
  2371. type(PACKED_INT32_ARRAY) {
  2372. _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
  2373. }
  2374. Variant::Variant(const PackedInt64Array &p_int64_array) :
  2375. type(PACKED_INT64_ARRAY) {
  2376. _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
  2377. }
  2378. Variant::Variant(const PackedFloat32Array &p_float32_array) :
  2379. type(PACKED_FLOAT32_ARRAY) {
  2380. _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
  2381. }
  2382. Variant::Variant(const PackedFloat64Array &p_float64_array) :
  2383. type(PACKED_FLOAT64_ARRAY) {
  2384. _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
  2385. }
  2386. Variant::Variant(const PackedStringArray &p_string_array) :
  2387. type(PACKED_STRING_ARRAY) {
  2388. _data.packed_array = PackedArrayRef<String>::create(p_string_array);
  2389. }
  2390. Variant::Variant(const PackedVector2Array &p_vector2_array) :
  2391. type(PACKED_VECTOR2_ARRAY) {
  2392. _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
  2393. }
  2394. Variant::Variant(const PackedVector3Array &p_vector3_array) :
  2395. type(PACKED_VECTOR3_ARRAY) {
  2396. _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
  2397. }
  2398. Variant::Variant(const PackedColorArray &p_color_array) :
  2399. type(PACKED_COLOR_ARRAY) {
  2400. _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
  2401. }
  2402. Variant::Variant(const PackedVector4Array &p_vector4_array) :
  2403. type(PACKED_VECTOR4_ARRAY) {
  2404. _data.packed_array = PackedArrayRef<Vector4>::create(p_vector4_array);
  2405. }
  2406. /* helpers */
  2407. Variant::Variant(const Vector<::RID> &p_array) :
  2408. type(ARRAY) {
  2409. Array *rid_array = memnew_placement(_data._mem, Array);
  2410. rid_array->resize(p_array.size());
  2411. for (int i = 0; i < p_array.size(); i++) {
  2412. rid_array->set(i, Variant(p_array[i]));
  2413. }
  2414. }
  2415. Variant::Variant(const Vector<Plane> &p_array) :
  2416. type(ARRAY) {
  2417. Array *plane_array = memnew_placement(_data._mem, Array);
  2418. plane_array->resize(p_array.size());
  2419. for (int i = 0; i < p_array.size(); i++) {
  2420. plane_array->operator[](i) = Variant(p_array[i]);
  2421. }
  2422. }
  2423. Variant::Variant(const Vector<Face3> &p_face_array) {
  2424. PackedVector3Array vertices;
  2425. int face_count = p_face_array.size();
  2426. vertices.resize(face_count * 3);
  2427. if (face_count) {
  2428. const Face3 *r = p_face_array.ptr();
  2429. Vector3 *w = vertices.ptrw();
  2430. for (int i = 0; i < face_count; i++) {
  2431. for (int j = 0; j < 3; j++) {
  2432. w[i * 3 + j] = r[i].vertex[j];
  2433. }
  2434. }
  2435. }
  2436. *this = vertices;
  2437. }
  2438. Variant::Variant(const Vector<Variant> &p_array) {
  2439. Array arr;
  2440. arr.resize(p_array.size());
  2441. for (int i = 0; i < p_array.size(); i++) {
  2442. arr[i] = p_array[i];
  2443. }
  2444. *this = arr;
  2445. }
  2446. Variant::Variant(const Vector<StringName> &p_array) {
  2447. PackedStringArray v;
  2448. int len = p_array.size();
  2449. v.resize(len);
  2450. for (int i = 0; i < len; i++) {
  2451. v.set(i, p_array[i]);
  2452. }
  2453. *this = v;
  2454. }
  2455. void Variant::operator=(const Variant &p_variant) {
  2456. if (unlikely(this == &p_variant)) {
  2457. return;
  2458. }
  2459. if (unlikely(type != p_variant.type)) {
  2460. reference(p_variant);
  2461. return;
  2462. }
  2463. switch (p_variant.type) {
  2464. case NIL: {
  2465. // none
  2466. } break;
  2467. // atomic types
  2468. case BOOL: {
  2469. _data._bool = p_variant._data._bool;
  2470. } break;
  2471. case INT: {
  2472. _data._int = p_variant._data._int;
  2473. } break;
  2474. case FLOAT: {
  2475. _data._float = p_variant._data._float;
  2476. } break;
  2477. case STRING: {
  2478. *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
  2479. } break;
  2480. // math types
  2481. case VECTOR2: {
  2482. *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2483. } break;
  2484. case VECTOR2I: {
  2485. *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2486. } break;
  2487. case RECT2: {
  2488. *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2489. } break;
  2490. case RECT2I: {
  2491. *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2492. } break;
  2493. case TRANSFORM2D: {
  2494. *_data._transform2d = *(p_variant._data._transform2d);
  2495. } break;
  2496. case VECTOR3: {
  2497. *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2498. } break;
  2499. case VECTOR3I: {
  2500. *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2501. } break;
  2502. case VECTOR4: {
  2503. *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  2504. } break;
  2505. case VECTOR4I: {
  2506. *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  2507. } break;
  2508. case PLANE: {
  2509. *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
  2510. } break;
  2511. case AABB: {
  2512. *_data._aabb = *(p_variant._data._aabb);
  2513. } break;
  2514. case QUATERNION: {
  2515. *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  2516. } break;
  2517. case BASIS: {
  2518. *_data._basis = *(p_variant._data._basis);
  2519. } break;
  2520. case TRANSFORM3D: {
  2521. *_data._transform3d = *(p_variant._data._transform3d);
  2522. } break;
  2523. case PROJECTION: {
  2524. *_data._projection = *(p_variant._data._projection);
  2525. } break;
  2526. // misc types
  2527. case COLOR: {
  2528. *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
  2529. } break;
  2530. case RID: {
  2531. *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
  2532. } break;
  2533. case OBJECT: {
  2534. _get_obj().ref(p_variant._get_obj());
  2535. } break;
  2536. case CALLABLE: {
  2537. *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
  2538. } break;
  2539. case SIGNAL: {
  2540. *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
  2541. } break;
  2542. case STRING_NAME: {
  2543. *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2544. } break;
  2545. case NODE_PATH: {
  2546. *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
  2547. } break;
  2548. case DICTIONARY: {
  2549. *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
  2550. } break;
  2551. case ARRAY: {
  2552. *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
  2553. } break;
  2554. // arrays
  2555. case PACKED_BYTE_ARRAY: {
  2556. _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2557. } break;
  2558. case PACKED_INT32_ARRAY: {
  2559. _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2560. } break;
  2561. case PACKED_INT64_ARRAY: {
  2562. _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2563. } break;
  2564. case PACKED_FLOAT32_ARRAY: {
  2565. _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2566. } break;
  2567. case PACKED_FLOAT64_ARRAY: {
  2568. _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2569. } break;
  2570. case PACKED_STRING_ARRAY: {
  2571. _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2572. } break;
  2573. case PACKED_VECTOR2_ARRAY: {
  2574. _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2575. } break;
  2576. case PACKED_VECTOR3_ARRAY: {
  2577. _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2578. } break;
  2579. case PACKED_COLOR_ARRAY: {
  2580. _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2581. } break;
  2582. case PACKED_VECTOR4_ARRAY: {
  2583. _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
  2584. } break;
  2585. default: {
  2586. }
  2587. }
  2588. }
  2589. Variant::Variant(const IPAddress &p_address) :
  2590. type(STRING) {
  2591. memnew_placement(_data._mem, String(p_address));
  2592. }
  2593. Variant::Variant(const Variant &p_variant) {
  2594. reference(p_variant);
  2595. }
  2596. uint32_t Variant::hash() const {
  2597. return recursive_hash(0);
  2598. }
  2599. uint32_t Variant::recursive_hash(int recursion_count) const {
  2600. switch (type) {
  2601. case NIL: {
  2602. return 0;
  2603. } break;
  2604. case BOOL: {
  2605. return _data._bool ? 1 : 0;
  2606. } break;
  2607. case INT: {
  2608. return hash_one_uint64((uint64_t)_data._int);
  2609. } break;
  2610. case FLOAT: {
  2611. return hash_murmur3_one_double(_data._float);
  2612. } break;
  2613. case STRING: {
  2614. return reinterpret_cast<const String *>(_data._mem)->hash();
  2615. } break;
  2616. // math types
  2617. case VECTOR2: {
  2618. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
  2619. } break;
  2620. case VECTOR2I: {
  2621. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
  2622. } break;
  2623. case RECT2: {
  2624. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
  2625. } break;
  2626. case RECT2I: {
  2627. return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
  2628. } break;
  2629. case TRANSFORM2D: {
  2630. uint32_t h = HASH_MURMUR3_SEED;
  2631. const Transform2D &t = *_data._transform2d;
  2632. h = hash_murmur3_one_real(t[0].x, h);
  2633. h = hash_murmur3_one_real(t[0].y, h);
  2634. h = hash_murmur3_one_real(t[1].x, h);
  2635. h = hash_murmur3_one_real(t[1].y, h);
  2636. h = hash_murmur3_one_real(t[2].x, h);
  2637. h = hash_murmur3_one_real(t[2].y, h);
  2638. return hash_fmix32(h);
  2639. } break;
  2640. case VECTOR3: {
  2641. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
  2642. } break;
  2643. case VECTOR3I: {
  2644. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
  2645. } break;
  2646. case VECTOR4: {
  2647. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
  2648. } break;
  2649. case VECTOR4I: {
  2650. return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
  2651. } break;
  2652. case PLANE: {
  2653. uint32_t h = HASH_MURMUR3_SEED;
  2654. const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
  2655. h = hash_murmur3_one_real(p.normal.x, h);
  2656. h = hash_murmur3_one_real(p.normal.y, h);
  2657. h = hash_murmur3_one_real(p.normal.z, h);
  2658. h = hash_murmur3_one_real(p.d, h);
  2659. return hash_fmix32(h);
  2660. } break;
  2661. case AABB: {
  2662. return HashMapHasherDefault::hash(*_data._aabb);
  2663. } break;
  2664. case QUATERNION: {
  2665. uint32_t h = HASH_MURMUR3_SEED;
  2666. const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
  2667. h = hash_murmur3_one_real(q.x, h);
  2668. h = hash_murmur3_one_real(q.y, h);
  2669. h = hash_murmur3_one_real(q.z, h);
  2670. h = hash_murmur3_one_real(q.w, h);
  2671. return hash_fmix32(h);
  2672. } break;
  2673. case BASIS: {
  2674. uint32_t h = HASH_MURMUR3_SEED;
  2675. const Basis &b = *_data._basis;
  2676. h = hash_murmur3_one_real(b[0].x, h);
  2677. h = hash_murmur3_one_real(b[0].y, h);
  2678. h = hash_murmur3_one_real(b[0].z, h);
  2679. h = hash_murmur3_one_real(b[1].x, h);
  2680. h = hash_murmur3_one_real(b[1].y, h);
  2681. h = hash_murmur3_one_real(b[1].z, h);
  2682. h = hash_murmur3_one_real(b[2].x, h);
  2683. h = hash_murmur3_one_real(b[2].y, h);
  2684. h = hash_murmur3_one_real(b[2].z, h);
  2685. return hash_fmix32(h);
  2686. } break;
  2687. case TRANSFORM3D: {
  2688. uint32_t h = HASH_MURMUR3_SEED;
  2689. const Transform3D &t = *_data._transform3d;
  2690. h = hash_murmur3_one_real(t.basis[0].x, h);
  2691. h = hash_murmur3_one_real(t.basis[0].y, h);
  2692. h = hash_murmur3_one_real(t.basis[0].z, h);
  2693. h = hash_murmur3_one_real(t.basis[1].x, h);
  2694. h = hash_murmur3_one_real(t.basis[1].y, h);
  2695. h = hash_murmur3_one_real(t.basis[1].z, h);
  2696. h = hash_murmur3_one_real(t.basis[2].x, h);
  2697. h = hash_murmur3_one_real(t.basis[2].y, h);
  2698. h = hash_murmur3_one_real(t.basis[2].z, h);
  2699. h = hash_murmur3_one_real(t.origin.x, h);
  2700. h = hash_murmur3_one_real(t.origin.y, h);
  2701. h = hash_murmur3_one_real(t.origin.z, h);
  2702. return hash_fmix32(h);
  2703. } break;
  2704. case PROJECTION: {
  2705. uint32_t h = HASH_MURMUR3_SEED;
  2706. const Projection &t = *_data._projection;
  2707. h = hash_murmur3_one_real(t.columns[0].x, h);
  2708. h = hash_murmur3_one_real(t.columns[0].y, h);
  2709. h = hash_murmur3_one_real(t.columns[0].z, h);
  2710. h = hash_murmur3_one_real(t.columns[0].w, h);
  2711. h = hash_murmur3_one_real(t.columns[1].x, h);
  2712. h = hash_murmur3_one_real(t.columns[1].y, h);
  2713. h = hash_murmur3_one_real(t.columns[1].z, h);
  2714. h = hash_murmur3_one_real(t.columns[1].w, h);
  2715. h = hash_murmur3_one_real(t.columns[2].x, h);
  2716. h = hash_murmur3_one_real(t.columns[2].y, h);
  2717. h = hash_murmur3_one_real(t.columns[2].z, h);
  2718. h = hash_murmur3_one_real(t.columns[2].w, h);
  2719. h = hash_murmur3_one_real(t.columns[3].x, h);
  2720. h = hash_murmur3_one_real(t.columns[3].y, h);
  2721. h = hash_murmur3_one_real(t.columns[3].z, h);
  2722. h = hash_murmur3_one_real(t.columns[3].w, h);
  2723. return hash_fmix32(h);
  2724. } break;
  2725. // misc types
  2726. case COLOR: {
  2727. uint32_t h = HASH_MURMUR3_SEED;
  2728. const Color &c = *reinterpret_cast<const Color *>(_data._mem);
  2729. h = hash_murmur3_one_float(c.r, h);
  2730. h = hash_murmur3_one_float(c.g, h);
  2731. h = hash_murmur3_one_float(c.b, h);
  2732. h = hash_murmur3_one_float(c.a, h);
  2733. return hash_fmix32(h);
  2734. } break;
  2735. case RID: {
  2736. return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
  2737. } break;
  2738. case OBJECT: {
  2739. return hash_one_uint64(hash_make_uint64_t(_get_obj().obj));
  2740. } break;
  2741. case STRING_NAME: {
  2742. return reinterpret_cast<const StringName *>(_data._mem)->hash();
  2743. } break;
  2744. case NODE_PATH: {
  2745. return reinterpret_cast<const NodePath *>(_data._mem)->hash();
  2746. } break;
  2747. case DICTIONARY: {
  2748. return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
  2749. } break;
  2750. case CALLABLE: {
  2751. return reinterpret_cast<const Callable *>(_data._mem)->hash();
  2752. } break;
  2753. case SIGNAL: {
  2754. const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
  2755. uint32_t hash = s.get_name().hash();
  2756. return hash_murmur3_one_64(s.get_object_id(), hash);
  2757. } break;
  2758. case ARRAY: {
  2759. const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
  2760. return arr.recursive_hash(recursion_count);
  2761. } break;
  2762. case PACKED_BYTE_ARRAY: {
  2763. const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
  2764. int len = arr.size();
  2765. if (likely(len)) {
  2766. const uint8_t *r = arr.ptr();
  2767. return hash_murmur3_buffer((uint8_t *)&r[0], len);
  2768. } else {
  2769. return hash_murmur3_one_64(0);
  2770. }
  2771. } break;
  2772. case PACKED_INT32_ARRAY: {
  2773. const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
  2774. int len = arr.size();
  2775. if (likely(len)) {
  2776. const int32_t *r = arr.ptr();
  2777. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
  2778. } else {
  2779. return hash_murmur3_one_64(0);
  2780. }
  2781. } break;
  2782. case PACKED_INT64_ARRAY: {
  2783. const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
  2784. int len = arr.size();
  2785. if (likely(len)) {
  2786. const int64_t *r = arr.ptr();
  2787. return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
  2788. } else {
  2789. return hash_murmur3_one_64(0);
  2790. }
  2791. } break;
  2792. case PACKED_FLOAT32_ARRAY: {
  2793. const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
  2794. int len = arr.size();
  2795. if (likely(len)) {
  2796. const float *r = arr.ptr();
  2797. uint32_t h = HASH_MURMUR3_SEED;
  2798. for (int32_t i = 0; i < len; i++) {
  2799. h = hash_murmur3_one_float(r[i], h);
  2800. }
  2801. return hash_fmix32(h);
  2802. } else {
  2803. return hash_murmur3_one_float(0.0);
  2804. }
  2805. } break;
  2806. case PACKED_FLOAT64_ARRAY: {
  2807. const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
  2808. int len = arr.size();
  2809. if (likely(len)) {
  2810. const double *r = arr.ptr();
  2811. uint32_t h = HASH_MURMUR3_SEED;
  2812. for (int32_t i = 0; i < len; i++) {
  2813. h = hash_murmur3_one_double(r[i], h);
  2814. }
  2815. return hash_fmix32(h);
  2816. } else {
  2817. return hash_murmur3_one_double(0.0);
  2818. }
  2819. } break;
  2820. case PACKED_STRING_ARRAY: {
  2821. uint32_t hash = HASH_MURMUR3_SEED;
  2822. const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
  2823. int len = arr.size();
  2824. if (likely(len)) {
  2825. const String *r = arr.ptr();
  2826. for (int i = 0; i < len; i++) {
  2827. hash = hash_murmur3_one_32(r[i].hash(), hash);
  2828. }
  2829. hash = hash_fmix32(hash);
  2830. }
  2831. return hash;
  2832. } break;
  2833. case PACKED_VECTOR2_ARRAY: {
  2834. uint32_t hash = HASH_MURMUR3_SEED;
  2835. const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
  2836. int len = arr.size();
  2837. if (likely(len)) {
  2838. const Vector2 *r = arr.ptr();
  2839. for (int i = 0; i < len; i++) {
  2840. hash = hash_murmur3_one_real(r[i].x, hash);
  2841. hash = hash_murmur3_one_real(r[i].y, hash);
  2842. }
  2843. hash = hash_fmix32(hash);
  2844. }
  2845. return hash;
  2846. } break;
  2847. case PACKED_VECTOR3_ARRAY: {
  2848. uint32_t hash = HASH_MURMUR3_SEED;
  2849. const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
  2850. int len = arr.size();
  2851. if (likely(len)) {
  2852. const Vector3 *r = arr.ptr();
  2853. for (int i = 0; i < len; i++) {
  2854. hash = hash_murmur3_one_real(r[i].x, hash);
  2855. hash = hash_murmur3_one_real(r[i].y, hash);
  2856. hash = hash_murmur3_one_real(r[i].z, hash);
  2857. }
  2858. hash = hash_fmix32(hash);
  2859. }
  2860. return hash;
  2861. } break;
  2862. case PACKED_COLOR_ARRAY: {
  2863. uint32_t hash = HASH_MURMUR3_SEED;
  2864. const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
  2865. int len = arr.size();
  2866. if (likely(len)) {
  2867. const Color *r = arr.ptr();
  2868. for (int i = 0; i < len; i++) {
  2869. hash = hash_murmur3_one_float(r[i].r, hash);
  2870. hash = hash_murmur3_one_float(r[i].g, hash);
  2871. hash = hash_murmur3_one_float(r[i].b, hash);
  2872. hash = hash_murmur3_one_float(r[i].a, hash);
  2873. }
  2874. hash = hash_fmix32(hash);
  2875. }
  2876. return hash;
  2877. } break;
  2878. case PACKED_VECTOR4_ARRAY: {
  2879. uint32_t hash = HASH_MURMUR3_SEED;
  2880. const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
  2881. int len = arr.size();
  2882. if (likely(len)) {
  2883. const Vector4 *r = arr.ptr();
  2884. for (int i = 0; i < len; i++) {
  2885. hash = hash_murmur3_one_real(r[i].x, hash);
  2886. hash = hash_murmur3_one_real(r[i].y, hash);
  2887. hash = hash_murmur3_one_real(r[i].z, hash);
  2888. hash = hash_murmur3_one_real(r[i].w, hash);
  2889. }
  2890. hash = hash_fmix32(hash);
  2891. }
  2892. return hash;
  2893. } break;
  2894. default: {
  2895. }
  2896. }
  2897. return 0;
  2898. }
  2899. #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
  2900. (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
  2901. #define hash_compare_scalar(p_lhs, p_rhs) \
  2902. (hash_compare_scalar_base(p_lhs, p_rhs, true))
  2903. #define hash_compare_vector2(p_lhs, p_rhs) \
  2904. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2905. hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2906. #define hash_compare_vector3(p_lhs, p_rhs) \
  2907. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2908. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2909. hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2910. #define hash_compare_vector4(p_lhs, p_rhs) \
  2911. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2912. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2913. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2914. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2915. #define hash_compare_quaternion(p_lhs, p_rhs) \
  2916. (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
  2917. hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
  2918. hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
  2919. hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2920. #define hash_compare_color(p_lhs, p_rhs) \
  2921. (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
  2922. hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
  2923. hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
  2924. hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2925. #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
  2926. const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
  2927. const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
  2928. \
  2929. if (l.size() != r.size()) \
  2930. return false; \
  2931. \
  2932. const p_type *lr = l.ptr(); \
  2933. const p_type *rr = r.ptr(); \
  2934. \
  2935. for (int i = 0; i < l.size(); ++i) { \
  2936. if (!p_compare_func((lr[i]), (rr[i]))) \
  2937. return false; \
  2938. } \
  2939. \
  2940. return true
  2941. bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
  2942. if (type != p_variant.type) {
  2943. return false;
  2944. }
  2945. switch (type) {
  2946. case INT: {
  2947. return _data._int == p_variant._data._int;
  2948. } break;
  2949. case FLOAT: {
  2950. return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
  2951. } break;
  2952. case STRING: {
  2953. return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
  2954. } break;
  2955. case STRING_NAME: {
  2956. return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
  2957. } break;
  2958. case VECTOR2: {
  2959. const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
  2960. const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
  2961. return hash_compare_vector2(*l, *r);
  2962. } break;
  2963. case VECTOR2I: {
  2964. const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
  2965. const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
  2966. return *l == *r;
  2967. } break;
  2968. case RECT2: {
  2969. const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
  2970. const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
  2971. return hash_compare_vector2(l->position, r->position) &&
  2972. hash_compare_vector2(l->size, r->size);
  2973. } break;
  2974. case RECT2I: {
  2975. const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
  2976. const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
  2977. return *l == *r;
  2978. } break;
  2979. case TRANSFORM2D: {
  2980. Transform2D *l = _data._transform2d;
  2981. Transform2D *r = p_variant._data._transform2d;
  2982. for (int i = 0; i < 3; i++) {
  2983. if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
  2984. return false;
  2985. }
  2986. }
  2987. return true;
  2988. } break;
  2989. case VECTOR3: {
  2990. const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
  2991. const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
  2992. return hash_compare_vector3(*l, *r);
  2993. } break;
  2994. case VECTOR3I: {
  2995. const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
  2996. const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
  2997. return *l == *r;
  2998. } break;
  2999. case VECTOR4: {
  3000. const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
  3001. const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
  3002. return hash_compare_vector4(*l, *r);
  3003. } break;
  3004. case VECTOR4I: {
  3005. const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
  3006. const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
  3007. return *l == *r;
  3008. } break;
  3009. case PLANE: {
  3010. const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
  3011. const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
  3012. return hash_compare_vector3(l->normal, r->normal) &&
  3013. hash_compare_scalar(l->d, r->d);
  3014. } break;
  3015. case AABB: {
  3016. const ::AABB *l = _data._aabb;
  3017. const ::AABB *r = p_variant._data._aabb;
  3018. return hash_compare_vector3(l->position, r->position) &&
  3019. hash_compare_vector3(l->size, r->size);
  3020. } break;
  3021. case QUATERNION: {
  3022. const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
  3023. const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
  3024. return hash_compare_quaternion(*l, *r);
  3025. } break;
  3026. case BASIS: {
  3027. const Basis *l = _data._basis;
  3028. const Basis *r = p_variant._data._basis;
  3029. for (int i = 0; i < 3; i++) {
  3030. if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
  3031. return false;
  3032. }
  3033. }
  3034. return true;
  3035. } break;
  3036. case TRANSFORM3D: {
  3037. const Transform3D *l = _data._transform3d;
  3038. const Transform3D *r = p_variant._data._transform3d;
  3039. for (int i = 0; i < 3; i++) {
  3040. if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
  3041. return false;
  3042. }
  3043. }
  3044. return hash_compare_vector3(l->origin, r->origin);
  3045. } break;
  3046. case PROJECTION: {
  3047. const Projection *l = _data._projection;
  3048. const Projection *r = p_variant._data._projection;
  3049. for (int i = 0; i < 4; i++) {
  3050. if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
  3051. return false;
  3052. }
  3053. }
  3054. return true;
  3055. } break;
  3056. case COLOR: {
  3057. const Color *l = reinterpret_cast<const Color *>(_data._mem);
  3058. const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
  3059. return hash_compare_color(*l, *r);
  3060. } break;
  3061. case ARRAY: {
  3062. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3063. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3064. if (!l.recursive_equal(r, recursion_count + 1)) {
  3065. return false;
  3066. }
  3067. return true;
  3068. } break;
  3069. case DICTIONARY: {
  3070. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3071. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3072. if (!l.recursive_equal(r, recursion_count + 1)) {
  3073. return false;
  3074. }
  3075. return true;
  3076. } break;
  3077. // This is for floating point comparisons only.
  3078. case PACKED_FLOAT32_ARRAY: {
  3079. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
  3080. } break;
  3081. case PACKED_FLOAT64_ARRAY: {
  3082. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
  3083. } break;
  3084. case PACKED_VECTOR2_ARRAY: {
  3085. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
  3086. } break;
  3087. case PACKED_VECTOR3_ARRAY: {
  3088. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
  3089. } break;
  3090. case PACKED_COLOR_ARRAY: {
  3091. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
  3092. } break;
  3093. case PACKED_VECTOR4_ARRAY: {
  3094. hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
  3095. } break;
  3096. default:
  3097. bool v;
  3098. Variant r;
  3099. evaluate(OP_EQUAL, *this, p_variant, r, v);
  3100. return r;
  3101. }
  3102. }
  3103. bool Variant::identity_compare(const Variant &p_variant) const {
  3104. if (type != p_variant.type) {
  3105. return false;
  3106. }
  3107. switch (type) {
  3108. case OBJECT: {
  3109. return _get_obj().id == p_variant._get_obj().id;
  3110. } break;
  3111. case DICTIONARY: {
  3112. const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
  3113. const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
  3114. return l.id() == r.id();
  3115. } break;
  3116. case ARRAY: {
  3117. const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
  3118. const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
  3119. return l.id() == r.id();
  3120. } break;
  3121. case PACKED_BYTE_ARRAY:
  3122. case PACKED_INT32_ARRAY:
  3123. case PACKED_INT64_ARRAY:
  3124. case PACKED_FLOAT32_ARRAY:
  3125. case PACKED_FLOAT64_ARRAY:
  3126. case PACKED_STRING_ARRAY:
  3127. case PACKED_VECTOR2_ARRAY:
  3128. case PACKED_VECTOR3_ARRAY:
  3129. case PACKED_COLOR_ARRAY:
  3130. case PACKED_VECTOR4_ARRAY: {
  3131. return _data.packed_array == p_variant._data.packed_array;
  3132. } break;
  3133. default: {
  3134. return hash_compare(p_variant);
  3135. }
  3136. }
  3137. }
  3138. bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
  3139. if (p_lhs.hash_compare(p_rhs)) {
  3140. return true;
  3141. }
  3142. if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
  3143. return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
  3144. }
  3145. if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
  3146. return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
  3147. }
  3148. return false;
  3149. }
  3150. bool Variant::is_ref_counted() const {
  3151. return type == OBJECT && _get_obj().id.is_ref_counted();
  3152. }
  3153. bool Variant::is_type_shared(Variant::Type p_type) {
  3154. switch (p_type) {
  3155. case OBJECT:
  3156. case ARRAY:
  3157. case DICTIONARY:
  3158. return true;
  3159. default: {
  3160. }
  3161. }
  3162. return false;
  3163. }
  3164. bool Variant::is_shared() const {
  3165. return is_type_shared(type);
  3166. }
  3167. bool Variant::is_read_only() const {
  3168. switch (type) {
  3169. case ARRAY:
  3170. return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
  3171. case DICTIONARY:
  3172. return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
  3173. default:
  3174. return false;
  3175. }
  3176. }
  3177. void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
  3178. switch (error.error) {
  3179. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  3180. String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
  3181. ERR_PRINT(err.utf8().get_data());
  3182. } break;
  3183. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  3184. String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
  3185. ERR_PRINT(err.utf8().get_data());
  3186. } break;
  3187. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  3188. String err = "Too many arguments for method '" + p_method + "'";
  3189. ERR_PRINT(err.utf8().get_data());
  3190. } break;
  3191. default: {
  3192. }
  3193. }
  3194. }
  3195. void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
  3196. r_value = Variant();
  3197. }
  3198. String Variant::get_construct_string() const {
  3199. String vars;
  3200. VariantWriter::write_to_string(*this, vars);
  3201. return vars;
  3202. }
  3203. String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3204. return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
  3205. }
  3206. String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3207. String err_text;
  3208. if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  3209. int errorarg = ce.argument;
  3210. if (p_argptrs) {
  3211. 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));
  3212. } else {
  3213. err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
  3214. }
  3215. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  3216. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3217. } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  3218. err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
  3219. } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  3220. err_text = "Method not found";
  3221. } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  3222. err_text = "Instance is null";
  3223. } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
  3224. err_text = "Method not const in const instance";
  3225. } else if (ce.error == Callable::CallError::CALL_OK) {
  3226. return "Call OK";
  3227. }
  3228. String base_text;
  3229. if (p_base) {
  3230. base_text = p_base->get_class();
  3231. Ref<Resource> script = p_base->get_script();
  3232. if (script.is_valid() && script->get_path().is_resource_file()) {
  3233. base_text += "(" + script->get_path().get_file() + ")";
  3234. }
  3235. base_text += "::";
  3236. }
  3237. return "'" + base_text + String(p_method) + "': " + err_text;
  3238. }
  3239. String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
  3240. Vector<Variant> binds;
  3241. p_callable.get_bound_arguments_ref(binds);
  3242. int args_unbound = p_callable.get_unbound_arguments_count();
  3243. if (p_argcount - args_unbound < 0) {
  3244. return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
  3245. } else {
  3246. Vector<const Variant *> argptrs;
  3247. argptrs.resize(p_argcount - args_unbound + binds.size());
  3248. for (int i = 0; i < p_argcount - args_unbound; i++) {
  3249. argptrs.write[i] = p_argptrs[i];
  3250. }
  3251. for (int i = 0; i < binds.size(); i++) {
  3252. argptrs.write[i + p_argcount - args_unbound] = &binds[i];
  3253. }
  3254. return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
  3255. }
  3256. }
  3257. void Variant::register_types() {
  3258. _register_variant_operators();
  3259. _register_variant_methods();
  3260. _register_variant_setters_getters();
  3261. _register_variant_constructors();
  3262. _register_variant_destructors();
  3263. _register_variant_utility_functions();
  3264. }
  3265. void Variant::unregister_types() {
  3266. _unregister_variant_operators();
  3267. _unregister_variant_methods();
  3268. _unregister_variant_setters_getters();
  3269. _unregister_variant_destructors();
  3270. _unregister_variant_utility_functions();
  3271. }