12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701 |
- /**************************************************************************/
- /* variant.cpp */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #include "variant.h"
- #include "core/debugger/engine_debugger.h"
- #include "core/io/json.h"
- #include "core/io/marshalls.h"
- #include "core/io/resource.h"
- #include "core/math/math_funcs.h"
- #include "core/string/print_string.h"
- #include "core/variant/variant_parser.h"
- PagedAllocator<Variant::Pools::BucketSmall, true> Variant::Pools::_bucket_small;
- PagedAllocator<Variant::Pools::BucketMedium, true> Variant::Pools::_bucket_medium;
- PagedAllocator<Variant::Pools::BucketLarge, true> Variant::Pools::_bucket_large;
- String Variant::get_type_name(Variant::Type p_type) {
- switch (p_type) {
- case NIL: {
- return "Nil";
- }
- // Atomic types.
- case BOOL: {
- return "bool";
- }
- case INT: {
- return "int";
- }
- case FLOAT: {
- return "float";
- }
- case STRING: {
- return "String";
- }
- // Math types.
- case VECTOR2: {
- return "Vector2";
- }
- case VECTOR2I: {
- return "Vector2i";
- }
- case RECT2: {
- return "Rect2";
- }
- case RECT2I: {
- return "Rect2i";
- }
- case TRANSFORM2D: {
- return "Transform2D";
- }
- case VECTOR3: {
- return "Vector3";
- }
- case VECTOR3I: {
- return "Vector3i";
- }
- case VECTOR4: {
- return "Vector4";
- }
- case VECTOR4I: {
- return "Vector4i";
- }
- case PLANE: {
- return "Plane";
- }
- case AABB: {
- return "AABB";
- }
- case QUATERNION: {
- return "Quaternion";
- }
- case BASIS: {
- return "Basis";
- }
- case TRANSFORM3D: {
- return "Transform3D";
- }
- case PROJECTION: {
- return "Projection";
- }
- // Miscellaneous types.
- case COLOR: {
- return "Color";
- }
- case RID: {
- return "RID";
- }
- case OBJECT: {
- return "Object";
- }
- case CALLABLE: {
- return "Callable";
- }
- case SIGNAL: {
- return "Signal";
- }
- case STRING_NAME: {
- return "StringName";
- }
- case NODE_PATH: {
- return "NodePath";
- }
- case DICTIONARY: {
- return "Dictionary";
- }
- case ARRAY: {
- return "Array";
- }
- // Arrays.
- case PACKED_BYTE_ARRAY: {
- return "PackedByteArray";
- }
- case PACKED_INT32_ARRAY: {
- return "PackedInt32Array";
- }
- case PACKED_INT64_ARRAY: {
- return "PackedInt64Array";
- }
- case PACKED_FLOAT32_ARRAY: {
- return "PackedFloat32Array";
- }
- case PACKED_FLOAT64_ARRAY: {
- return "PackedFloat64Array";
- }
- case PACKED_STRING_ARRAY: {
- return "PackedStringArray";
- }
- case PACKED_VECTOR2_ARRAY: {
- return "PackedVector2Array";
- }
- case PACKED_VECTOR3_ARRAY: {
- return "PackedVector3Array";
- }
- case PACKED_COLOR_ARRAY: {
- return "PackedColorArray";
- }
- case PACKED_VECTOR4_ARRAY: {
- return "PackedVector4Array";
- }
- default: {
- }
- }
- return "";
- }
- bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
- if (p_type_from == p_type_to) {
- return true;
- }
- if (p_type_to == NIL) { //nil can convert to anything
- return true;
- }
- if (p_type_from == NIL) {
- return (p_type_to == OBJECT);
- }
- const Type *valid_types = nullptr;
- const Type *invalid_types = nullptr;
- switch (p_type_to) {
- case BOOL: {
- static const Type valid[] = {
- INT,
- FLOAT,
- STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case INT: {
- static const Type valid[] = {
- BOOL,
- FLOAT,
- STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case FLOAT: {
- static const Type valid[] = {
- BOOL,
- INT,
- STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case STRING: {
- static const Type invalid[] = {
- OBJECT,
- NIL
- };
- invalid_types = invalid;
- } break;
- case VECTOR2: {
- static const Type valid[] = {
- VECTOR2I,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR2I: {
- static const Type valid[] = {
- VECTOR2,
- NIL,
- };
- valid_types = valid;
- } break;
- case RECT2: {
- static const Type valid[] = {
- RECT2I,
- NIL,
- };
- valid_types = valid;
- } break;
- case RECT2I: {
- static const Type valid[] = {
- RECT2,
- NIL,
- };
- valid_types = valid;
- } break;
- case TRANSFORM2D: {
- static const Type valid[] = {
- TRANSFORM3D,
- NIL
- };
- valid_types = valid;
- } break;
- case VECTOR3: {
- static const Type valid[] = {
- VECTOR3I,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR3I: {
- static const Type valid[] = {
- VECTOR3,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR4: {
- static const Type valid[] = {
- VECTOR4I,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR4I: {
- static const Type valid[] = {
- VECTOR4,
- NIL,
- };
- valid_types = valid;
- } break;
- case QUATERNION: {
- static const Type valid[] = {
- BASIS,
- NIL
- };
- valid_types = valid;
- } break;
- case BASIS: {
- static const Type valid[] = {
- QUATERNION,
- NIL
- };
- valid_types = valid;
- } break;
- case TRANSFORM3D: {
- static const Type valid[] = {
- TRANSFORM2D,
- QUATERNION,
- BASIS,
- PROJECTION,
- NIL
- };
- valid_types = valid;
- } break;
- case PROJECTION: {
- static const Type valid[] = {
- TRANSFORM3D,
- NIL
- };
- valid_types = valid;
- } break;
- case COLOR: {
- static const Type valid[] = {
- STRING,
- INT,
- NIL,
- };
- valid_types = valid;
- } break;
- case RID: {
- static const Type valid[] = {
- OBJECT,
- NIL
- };
- valid_types = valid;
- } break;
- case OBJECT: {
- static const Type valid[] = {
- NIL
- };
- valid_types = valid;
- } break;
- case STRING_NAME: {
- static const Type valid[] = {
- STRING,
- NIL
- };
- valid_types = valid;
- } break;
- case NODE_PATH: {
- static const Type valid[] = {
- STRING,
- NIL
- };
- valid_types = valid;
- } break;
- case ARRAY: {
- static const Type valid[] = {
- PACKED_BYTE_ARRAY,
- PACKED_INT32_ARRAY,
- PACKED_INT64_ARRAY,
- PACKED_FLOAT32_ARRAY,
- PACKED_FLOAT64_ARRAY,
- PACKED_STRING_ARRAY,
- PACKED_COLOR_ARRAY,
- PACKED_VECTOR2_ARRAY,
- PACKED_VECTOR3_ARRAY,
- PACKED_VECTOR4_ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- // arrays
- case PACKED_BYTE_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_INT32_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_INT64_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_FLOAT32_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_FLOAT64_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_STRING_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_VECTOR2_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_VECTOR3_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_COLOR_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_VECTOR4_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- default: {
- }
- }
- if (valid_types) {
- int i = 0;
- while (valid_types[i] != NIL) {
- if (p_type_from == valid_types[i]) {
- return true;
- }
- i++;
- }
- } else if (invalid_types) {
- int i = 0;
- while (invalid_types[i] != NIL) {
- if (p_type_from == invalid_types[i]) {
- return false;
- }
- i++;
- }
- return true;
- }
- return false;
- }
- bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
- if (p_type_from == p_type_to) {
- return true;
- }
- if (p_type_to == NIL) { //nil can convert to anything
- return true;
- }
- if (p_type_from == NIL) {
- return (p_type_to == OBJECT);
- }
- const Type *valid_types = nullptr;
- switch (p_type_to) {
- case BOOL: {
- static const Type valid[] = {
- INT,
- FLOAT,
- //STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case INT: {
- static const Type valid[] = {
- BOOL,
- FLOAT,
- //STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case FLOAT: {
- static const Type valid[] = {
- BOOL,
- INT,
- //STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case STRING: {
- static const Type valid[] = {
- NODE_PATH,
- STRING_NAME,
- NIL
- };
- valid_types = valid;
- } break;
- case VECTOR2: {
- static const Type valid[] = {
- VECTOR2I,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR2I: {
- static const Type valid[] = {
- VECTOR2,
- NIL,
- };
- valid_types = valid;
- } break;
- case RECT2: {
- static const Type valid[] = {
- RECT2I,
- NIL,
- };
- valid_types = valid;
- } break;
- case RECT2I: {
- static const Type valid[] = {
- RECT2,
- NIL,
- };
- valid_types = valid;
- } break;
- case TRANSFORM2D: {
- static const Type valid[] = {
- TRANSFORM3D,
- NIL
- };
- valid_types = valid;
- } break;
- case VECTOR3: {
- static const Type valid[] = {
- VECTOR3I,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR3I: {
- static const Type valid[] = {
- VECTOR3,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR4: {
- static const Type valid[] = {
- VECTOR4I,
- NIL,
- };
- valid_types = valid;
- } break;
- case VECTOR4I: {
- static const Type valid[] = {
- VECTOR4,
- NIL,
- };
- valid_types = valid;
- } break;
- case QUATERNION: {
- static const Type valid[] = {
- BASIS,
- NIL
- };
- valid_types = valid;
- } break;
- case BASIS: {
- static const Type valid[] = {
- QUATERNION,
- NIL
- };
- valid_types = valid;
- } break;
- case TRANSFORM3D: {
- static const Type valid[] = {
- TRANSFORM2D,
- QUATERNION,
- BASIS,
- PROJECTION,
- NIL
- };
- valid_types = valid;
- } break;
- case PROJECTION: {
- static const Type valid[] = {
- TRANSFORM3D,
- NIL
- };
- valid_types = valid;
- } break;
- case COLOR: {
- static const Type valid[] = {
- STRING,
- INT,
- NIL,
- };
- valid_types = valid;
- } break;
- case RID: {
- static const Type valid[] = {
- OBJECT,
- NIL
- };
- valid_types = valid;
- } break;
- case OBJECT: {
- static const Type valid[] = {
- NIL
- };
- valid_types = valid;
- } break;
- case STRING_NAME: {
- static const Type valid[] = {
- STRING,
- NIL
- };
- valid_types = valid;
- } break;
- case NODE_PATH: {
- static const Type valid[] = {
- STRING,
- NIL
- };
- valid_types = valid;
- } break;
- case ARRAY: {
- static const Type valid[] = {
- PACKED_BYTE_ARRAY,
- PACKED_INT32_ARRAY,
- PACKED_INT64_ARRAY,
- PACKED_FLOAT32_ARRAY,
- PACKED_FLOAT64_ARRAY,
- PACKED_STRING_ARRAY,
- PACKED_COLOR_ARRAY,
- PACKED_VECTOR2_ARRAY,
- PACKED_VECTOR3_ARRAY,
- PACKED_VECTOR4_ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- // arrays
- case PACKED_BYTE_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_INT32_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_INT64_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_FLOAT32_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_FLOAT64_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_STRING_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_VECTOR2_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_VECTOR3_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_COLOR_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case PACKED_VECTOR4_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- default: {
- }
- }
- if (valid_types) {
- int i = 0;
- while (valid_types[i] != NIL) {
- if (p_type_from == valid_types[i]) {
- return true;
- }
- i++;
- }
- }
- return false;
- }
- bool Variant::operator==(const Variant &p_variant) const {
- return hash_compare(p_variant);
- }
- bool Variant::operator!=(const Variant &p_variant) const {
- // Don't use `!hash_compare(p_variant)` given it makes use of OP_EQUAL
- if (type != p_variant.type) { //evaluation of operator== needs to be more strict
- return true;
- }
- bool v;
- Variant r;
- evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
- return r;
- }
- bool Variant::operator<(const Variant &p_variant) const {
- if (type != p_variant.type) { //if types differ, then order by type first
- return type < p_variant.type;
- }
- bool v;
- Variant r;
- evaluate(OP_LESS, *this, p_variant, r, v);
- return r;
- }
- bool Variant::is_zero() const {
- switch (type) {
- case NIL: {
- return true;
- }
- // Atomic types.
- case BOOL: {
- return !(_data._bool);
- }
- case INT: {
- return _data._int == 0;
- }
- case FLOAT: {
- return _data._float == 0;
- }
- case STRING: {
- return *reinterpret_cast<const String *>(_data._mem) == String();
- }
- // Math types.
- case VECTOR2: {
- return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
- }
- case VECTOR2I: {
- return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i();
- }
- case RECT2: {
- return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
- }
- case RECT2I: {
- return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i();
- }
- case TRANSFORM2D: {
- return *_data._transform2d == Transform2D();
- }
- case VECTOR3: {
- return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
- }
- case VECTOR3I: {
- return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i();
- }
- case VECTOR4: {
- return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4();
- }
- case VECTOR4I: {
- return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i();
- }
- case PLANE: {
- return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
- }
- case AABB: {
- return *_data._aabb == ::AABB();
- }
- case QUATERNION: {
- return *reinterpret_cast<const Quaternion *>(_data._mem) == Quaternion();
- }
- case BASIS: {
- return *_data._basis == Basis();
- }
- case TRANSFORM3D: {
- return *_data._transform3d == Transform3D();
- }
- case PROJECTION: {
- return *_data._projection == Projection();
- }
- // Miscellaneous types.
- case COLOR: {
- return *reinterpret_cast<const Color *>(_data._mem) == Color();
- }
- case RID: {
- return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID();
- }
- case OBJECT: {
- return get_validated_object() == nullptr;
- }
- case CALLABLE: {
- return reinterpret_cast<const Callable *>(_data._mem)->is_null();
- }
- case SIGNAL: {
- return reinterpret_cast<const Signal *>(_data._mem)->is_null();
- }
- case STRING_NAME: {
- return *reinterpret_cast<const StringName *>(_data._mem) == StringName();
- }
- case NODE_PATH: {
- return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
- }
- case DICTIONARY: {
- return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty();
- }
- case ARRAY: {
- return reinterpret_cast<const Array *>(_data._mem)->is_empty();
- }
- // Arrays.
- case PACKED_BYTE_ARRAY: {
- return PackedArrayRef<uint8_t>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_INT32_ARRAY: {
- return PackedArrayRef<int32_t>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_INT64_ARRAY: {
- return PackedArrayRef<int64_t>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_FLOAT32_ARRAY: {
- return PackedArrayRef<float>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_FLOAT64_ARRAY: {
- return PackedArrayRef<double>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_STRING_ARRAY: {
- return PackedArrayRef<String>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_VECTOR2_ARRAY: {
- return PackedArrayRef<Vector2>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_VECTOR3_ARRAY: {
- return PackedArrayRef<Vector3>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_COLOR_ARRAY: {
- return PackedArrayRef<Color>::get_array(_data.packed_array).size() == 0;
- }
- case PACKED_VECTOR4_ARRAY: {
- return PackedArrayRef<Vector4>::get_array(_data.packed_array).size() == 0;
- }
- default: {
- }
- }
- return false;
- }
- bool Variant::is_one() const {
- switch (type) {
- case NIL: {
- return true;
- }
- case BOOL: {
- return _data._bool;
- }
- case INT: {
- return _data._int == 1;
- }
- case FLOAT: {
- return _data._float == 1;
- }
- case VECTOR2: {
- return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
- }
- case VECTOR2I: {
- return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(1, 1);
- }
- case RECT2: {
- return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
- }
- case RECT2I: {
- return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(1, 1, 1, 1);
- }
- case VECTOR3: {
- return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
- }
- case VECTOR3I: {
- return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(1, 1, 1);
- }
- case VECTOR4: {
- return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(1, 1, 1, 1);
- }
- case VECTOR4I: {
- return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(1, 1, 1, 1);
- }
- case PLANE: {
- return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
- }
- case COLOR: {
- return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
- }
- default: {
- return !is_zero();
- }
- }
- }
- bool Variant::is_null() const {
- if (type == OBJECT && _get_obj().obj) {
- return false;
- } else {
- return true;
- }
- }
- void Variant::ObjData::ref(const ObjData &p_from) {
- // Mirrors Ref::ref in refcounted.h
- if (p_from.id == id) {
- return;
- }
- ObjData cleanup_ref = *this;
- *this = p_from;
- if (id.is_ref_counted()) {
- RefCounted *reference = static_cast<RefCounted *>(obj);
- // Assuming reference is not null because id.is_ref_counted() was true.
- if (!reference->reference()) {
- *this = ObjData();
- }
- }
- cleanup_ref.unref();
- }
- void Variant::ObjData::ref_pointer(Object *p_object) {
- // Mirrors Ref::ref_pointer in refcounted.h
- if (p_object == obj) {
- return;
- }
- ObjData cleanup_ref = *this;
- if (p_object) {
- *this = ObjData{ p_object->get_instance_id(), p_object };
- if (p_object->is_ref_counted()) {
- RefCounted *reference = static_cast<RefCounted *>(p_object);
- if (!reference->init_ref()) {
- *this = ObjData();
- }
- }
- } else {
- *this = ObjData();
- }
- cleanup_ref.unref();
- }
- void Variant::ObjData::unref() {
- // Mirrors Ref::unref in refcounted.h
- if (id.is_ref_counted()) {
- RefCounted *reference = static_cast<RefCounted *>(obj);
- // Assuming reference is not null because id.is_ref_counted() was true.
- if (reference->unreference()) {
- memdelete(reference);
- }
- }
- *this = ObjData();
- }
- void Variant::reference(const Variant &p_variant) {
- if (type == OBJECT && p_variant.type == OBJECT) {
- _get_obj().ref(p_variant._get_obj());
- return;
- }
- clear();
- type = p_variant.type;
- switch (p_variant.type) {
- case NIL: {
- // None.
- } break;
- // Atomic types.
- case BOOL: {
- _data._bool = p_variant._data._bool;
- } break;
- case INT: {
- _data._int = p_variant._data._int;
- } break;
- case FLOAT: {
- _data._float = p_variant._data._float;
- } break;
- case STRING: {
- memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
- } break;
- // Math types.
- case VECTOR2: {
- memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
- } break;
- case VECTOR2I: {
- memnew_placement(_data._mem, Vector2i(*reinterpret_cast<const Vector2i *>(p_variant._data._mem)));
- } break;
- case RECT2: {
- memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
- } break;
- case RECT2I: {
- memnew_placement(_data._mem, Rect2i(*reinterpret_cast<const Rect2i *>(p_variant._data._mem)));
- } break;
- case TRANSFORM2D: {
- _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
- memnew_placement(_data._transform2d, Transform2D(*p_variant._data._transform2d));
- } break;
- case VECTOR3: {
- memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
- } break;
- case VECTOR3I: {
- memnew_placement(_data._mem, Vector3i(*reinterpret_cast<const Vector3i *>(p_variant._data._mem)));
- } break;
- case VECTOR4: {
- memnew_placement(_data._mem, Vector4(*reinterpret_cast<const Vector4 *>(p_variant._data._mem)));
- } break;
- case VECTOR4I: {
- memnew_placement(_data._mem, Vector4i(*reinterpret_cast<const Vector4i *>(p_variant._data._mem)));
- } break;
- case PLANE: {
- memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
- } break;
- case AABB: {
- _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
- memnew_placement(_data._aabb, ::AABB(*p_variant._data._aabb));
- } break;
- case QUATERNION: {
- memnew_placement(_data._mem, Quaternion(*reinterpret_cast<const Quaternion *>(p_variant._data._mem)));
- } break;
- case BASIS: {
- _data._basis = (Basis *)Pools::_bucket_medium.alloc();
- memnew_placement(_data._basis, Basis(*p_variant._data._basis));
- } break;
- case TRANSFORM3D: {
- _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
- memnew_placement(_data._transform3d, Transform3D(*p_variant._data._transform3d));
- } break;
- case PROJECTION: {
- _data._projection = (Projection *)Pools::_bucket_large.alloc();
- memnew_placement(_data._projection, Projection(*p_variant._data._projection));
- } break;
- // Miscellaneous types.
- case COLOR: {
- memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
- } break;
- case RID: {
- memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem)));
- } break;
- case OBJECT: {
- memnew_placement(_data._mem, ObjData);
- _get_obj().ref(p_variant._get_obj());
- } break;
- case CALLABLE: {
- memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem)));
- } break;
- case SIGNAL: {
- memnew_placement(_data._mem, Signal(*reinterpret_cast<const Signal *>(p_variant._data._mem)));
- } break;
- case STRING_NAME: {
- memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem)));
- } break;
- case NODE_PATH: {
- memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
- } break;
- case DICTIONARY: {
- memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
- } break;
- case ARRAY: {
- memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
- } break;
- // Arrays.
- case PACKED_BYTE_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<uint8_t>::create();
- }
- } break;
- case PACKED_INT32_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<int32_t>::create();
- }
- } break;
- case PACKED_INT64_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<int64_t>::create();
- }
- } break;
- case PACKED_FLOAT32_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<float>::create();
- }
- } break;
- case PACKED_FLOAT64_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<double>::create();
- }
- } break;
- case PACKED_STRING_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<String>::create();
- }
- } break;
- case PACKED_VECTOR2_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<Vector2>::create();
- }
- } break;
- case PACKED_VECTOR3_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<Vector3>::create();
- }
- } break;
- case PACKED_COLOR_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<Color>::create();
- }
- } break;
- case PACKED_VECTOR4_ARRAY: {
- _data.packed_array = static_cast<PackedArrayRef<Vector4> *>(p_variant._data.packed_array)->reference();
- if (!_data.packed_array) {
- _data.packed_array = PackedArrayRef<Vector4>::create();
- }
- } break;
- default: {
- }
- }
- }
- void Variant::zero() {
- switch (type) {
- case NIL:
- break;
- case BOOL:
- _data._bool = false;
- break;
- case INT:
- _data._int = 0;
- break;
- case FLOAT:
- _data._float = 0;
- break;
- case VECTOR2:
- *reinterpret_cast<Vector2 *>(_data._mem) = Vector2();
- break;
- case VECTOR2I:
- *reinterpret_cast<Vector2i *>(_data._mem) = Vector2i();
- break;
- case RECT2:
- *reinterpret_cast<Rect2 *>(_data._mem) = Rect2();
- break;
- case RECT2I:
- *reinterpret_cast<Rect2i *>(_data._mem) = Rect2i();
- break;
- case VECTOR3:
- *reinterpret_cast<Vector3 *>(_data._mem) = Vector3();
- break;
- case VECTOR3I:
- *reinterpret_cast<Vector3i *>(_data._mem) = Vector3i();
- break;
- case VECTOR4:
- *reinterpret_cast<Vector4 *>(_data._mem) = Vector4();
- break;
- case VECTOR4I:
- *reinterpret_cast<Vector4i *>(_data._mem) = Vector4i();
- break;
- case PLANE:
- *reinterpret_cast<Plane *>(_data._mem) = Plane();
- break;
- case QUATERNION:
- *reinterpret_cast<Quaternion *>(_data._mem) = Quaternion();
- break;
- case COLOR:
- *reinterpret_cast<Color *>(_data._mem) = Color();
- break;
- default:
- Type prev_type = type;
- clear();
- if (type != prev_type) {
- // clear() changes type to NIL, so it needs to be restored.
- Callable::CallError ce;
- Variant::construct(prev_type, *this, nullptr, 0, ce);
- }
- break;
- }
- }
- void Variant::_clear_internal() {
- switch (type) {
- case STRING: {
- reinterpret_cast<String *>(_data._mem)->~String();
- } break;
- // Math types.
- case TRANSFORM2D: {
- if (_data._transform2d) {
- _data._transform2d->~Transform2D();
- Pools::_bucket_small.free((Pools::BucketSmall *)_data._transform2d);
- _data._transform2d = nullptr;
- }
- } break;
- case AABB: {
- if (_data._aabb) {
- _data._aabb->~AABB();
- Pools::_bucket_small.free((Pools::BucketSmall *)_data._aabb);
- _data._aabb = nullptr;
- }
- } break;
- case BASIS: {
- if (_data._basis) {
- _data._basis->~Basis();
- Pools::_bucket_medium.free((Pools::BucketMedium *)_data._basis);
- _data._basis = nullptr;
- }
- } break;
- case TRANSFORM3D: {
- if (_data._transform3d) {
- _data._transform3d->~Transform3D();
- Pools::_bucket_medium.free((Pools::BucketMedium *)_data._transform3d);
- _data._transform3d = nullptr;
- }
- } break;
- case PROJECTION: {
- if (_data._projection) {
- _data._projection->~Projection();
- Pools::_bucket_large.free((Pools::BucketLarge *)_data._projection);
- _data._projection = nullptr;
- }
- } break;
- // Miscellaneous types.
- case STRING_NAME: {
- reinterpret_cast<StringName *>(_data._mem)->~StringName();
- } break;
- case NODE_PATH: {
- reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
- } break;
- case OBJECT: {
- _get_obj().unref();
- } break;
- case RID: {
- // Not much need probably.
- // HACK: Can't seem to use destructor + scoping operator, so hack.
- typedef ::RID RID_Class;
- reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class();
- } break;
- case CALLABLE: {
- reinterpret_cast<Callable *>(_data._mem)->~Callable();
- } break;
- case SIGNAL: {
- reinterpret_cast<Signal *>(_data._mem)->~Signal();
- } break;
- case DICTIONARY: {
- reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
- } break;
- case ARRAY: {
- reinterpret_cast<Array *>(_data._mem)->~Array();
- } break;
- // Arrays.
- case PACKED_BYTE_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_INT32_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_INT64_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_FLOAT32_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_FLOAT64_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_STRING_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_VECTOR2_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_VECTOR3_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_COLOR_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- case PACKED_VECTOR4_ARRAY: {
- PackedArrayRefBase::destroy(_data.packed_array);
- } break;
- default: {
- // Not needed, there is no point. The following do not allocate memory:
- // VECTOR2, VECTOR3, VECTOR4, RECT2, PLANE, QUATERNION, COLOR.
- }
- }
- }
- Variant::operator int64_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator int32_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator int16_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator int8_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator uint64_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator uint32_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator uint16_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator uint8_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator ObjectID() const {
- if (type == INT) {
- return ObjectID(_data._int);
- } else if (type == OBJECT) {
- return _get_obj().id;
- } else {
- return ObjectID();
- }
- }
- Variant::operator char32_t() const {
- return operator uint32_t();
- }
- Variant::operator float() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1.0 : 0.0;
- case INT:
- return (float)_data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_float();
- default: {
- return 0;
- }
- }
- }
- Variant::operator double() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1.0 : 0.0;
- case INT:
- return (double)_data._int;
- case FLOAT:
- return _data._float;
- case STRING:
- return operator String().to_float();
- default: {
- return 0;
- }
- }
- }
- Variant::operator StringName() const {
- if (type == STRING_NAME) {
- return *reinterpret_cast<const StringName *>(_data._mem);
- } else if (type == STRING) {
- return *reinterpret_cast<const String *>(_data._mem);
- }
- return StringName();
- }
- struct _VariantStrPair {
- String key;
- String value;
- bool operator<(const _VariantStrPair &p) const {
- return key < p.key;
- }
- };
- Variant::operator String() const {
- return stringify(0);
- }
- String stringify_variant_clean(const Variant &p_variant, int recursion_count) {
- String s = p_variant.stringify(recursion_count);
- // Wrap strings in quotes to avoid ambiguity.
- switch (p_variant.get_type()) {
- case Variant::STRING: {
- s = s.c_escape().quote();
- } break;
- case Variant::STRING_NAME: {
- s = "&" + s.c_escape().quote();
- } break;
- case Variant::NODE_PATH: {
- s = "^" + s.c_escape().quote();
- } break;
- default: {
- } break;
- }
- return s;
- }
- template <typename T>
- String stringify_vector(const T &vec, int recursion_count) {
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str += stringify_variant_clean(vec[i], recursion_count);
- }
- str += "]";
- return str;
- }
- String Variant::stringify(int recursion_count) const {
- switch (type) {
- case NIL:
- return "<null>";
- case BOOL:
- return _data._bool ? "true" : "false";
- case INT:
- return itos(_data._int);
- case FLOAT:
- return String::num_real(_data._float, true);
- case STRING:
- return *reinterpret_cast<const String *>(_data._mem);
- case VECTOR2:
- return operator Vector2();
- case VECTOR2I:
- return operator Vector2i();
- case RECT2:
- return operator Rect2();
- case RECT2I:
- return operator Rect2i();
- case TRANSFORM2D:
- return operator Transform2D();
- case VECTOR3:
- return operator Vector3();
- case VECTOR3I:
- return operator Vector3i();
- case VECTOR4:
- return operator Vector4();
- case VECTOR4I:
- return operator Vector4i();
- case PLANE:
- return operator Plane();
- case AABB:
- return operator ::AABB();
- case QUATERNION:
- return operator Quaternion();
- case BASIS:
- return operator Basis();
- case TRANSFORM3D:
- return operator Transform3D();
- case PROJECTION:
- return operator Projection();
- case STRING_NAME:
- return operator StringName();
- case NODE_PATH:
- return operator NodePath();
- case COLOR:
- return operator Color();
- case DICTIONARY: {
- ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "{ ... }", "Maximum dictionary recursion reached!");
- recursion_count++;
- const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
- // Add leading and trailing space to Dictionary printing. This distinguishes it
- // from array printing on fonts that have similar-looking {} and [] characters.
- String str("{ ");
- List<Variant> keys;
- d.get_key_list(&keys);
- Vector<_VariantStrPair> pairs;
- for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
- _VariantStrPair sp;
- sp.key = stringify_variant_clean(E->get(), recursion_count);
- sp.value = stringify_variant_clean(d[E->get()], recursion_count);
- pairs.push_back(sp);
- }
- for (int i = 0; i < pairs.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str += pairs[i].key + ": " + pairs[i].value;
- }
- str += " }";
- return str;
- }
- // Packed arrays cannot contain recursive structures, the recursion_count increment is not needed.
- case PACKED_VECTOR2_ARRAY: {
- return stringify_vector(operator PackedVector2Array(), recursion_count);
- }
- case PACKED_VECTOR3_ARRAY: {
- return stringify_vector(operator PackedVector3Array(), recursion_count);
- }
- case PACKED_COLOR_ARRAY: {
- return stringify_vector(operator PackedColorArray(), recursion_count);
- }
- case PACKED_VECTOR4_ARRAY: {
- return stringify_vector(operator PackedVector4Array(), recursion_count);
- }
- case PACKED_STRING_ARRAY: {
- return stringify_vector(operator PackedStringArray(), recursion_count);
- }
- case PACKED_BYTE_ARRAY: {
- return stringify_vector(operator PackedByteArray(), recursion_count);
- }
- case PACKED_INT32_ARRAY: {
- return stringify_vector(operator PackedInt32Array(), recursion_count);
- }
- case PACKED_INT64_ARRAY: {
- return stringify_vector(operator PackedInt64Array(), recursion_count);
- }
- case PACKED_FLOAT32_ARRAY: {
- return stringify_vector(operator PackedFloat32Array(), recursion_count);
- }
- case PACKED_FLOAT64_ARRAY: {
- return stringify_vector(operator PackedFloat64Array(), recursion_count);
- }
- case ARRAY: {
- ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "[...]", "Maximum array recursion reached!");
- recursion_count++;
- return stringify_vector(operator Array(), recursion_count);
- }
- case OBJECT: {
- if (_get_obj().obj) {
- if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
- return "<Freed Object>";
- }
- return _get_obj().obj->to_string();
- } else {
- return "<Object#null>";
- }
- }
- case CALLABLE: {
- const Callable &c = *reinterpret_cast<const Callable *>(_data._mem);
- return c;
- }
- case SIGNAL: {
- const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
- return s;
- }
- case RID: {
- const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem);
- return "RID(" + itos(s.get_id()) + ")";
- }
- default: {
- return "<" + get_type_name(type) + ">";
- }
- }
- }
- String Variant::to_json_string() const {
- return JSON::stringify(*this);
- }
- Variant::operator Vector2() const {
- if (type == VECTOR2) {
- return *reinterpret_cast<const Vector2 *>(_data._mem);
- } else if (type == VECTOR2I) {
- return *reinterpret_cast<const Vector2i *>(_data._mem);
- } else if (type == VECTOR3) {
- return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
- } else if (type == VECTOR3I) {
- return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
- } else if (type == VECTOR4) {
- return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
- } else if (type == VECTOR4I) {
- return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
- } else {
- return Vector2();
- }
- }
- Variant::operator Vector2i() const {
- if (type == VECTOR2I) {
- return *reinterpret_cast<const Vector2i *>(_data._mem);
- } else if (type == VECTOR2) {
- return *reinterpret_cast<const Vector2 *>(_data._mem);
- } else if (type == VECTOR3) {
- return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
- } else if (type == VECTOR3I) {
- return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
- } else if (type == VECTOR4) {
- return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
- } else if (type == VECTOR4I) {
- return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
- } else {
- return Vector2i();
- }
- }
- Variant::operator Rect2() const {
- if (type == RECT2) {
- return *reinterpret_cast<const Rect2 *>(_data._mem);
- } else if (type == RECT2I) {
- return *reinterpret_cast<const Rect2i *>(_data._mem);
- } else {
- return Rect2();
- }
- }
- Variant::operator Rect2i() const {
- if (type == RECT2I) {
- return *reinterpret_cast<const Rect2i *>(_data._mem);
- } else if (type == RECT2) {
- return *reinterpret_cast<const Rect2 *>(_data._mem);
- } else {
- return Rect2i();
- }
- }
- Variant::operator Vector3() const {
- if (type == VECTOR3) {
- return *reinterpret_cast<const Vector3 *>(_data._mem);
- } else if (type == VECTOR3I) {
- return *reinterpret_cast<const Vector3i *>(_data._mem);
- } else if (type == VECTOR2) {
- return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
- } else if (type == VECTOR2I) {
- return Vector3(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
- } else if (type == VECTOR4) {
- return Vector3(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
- } else if (type == VECTOR4I) {
- return Vector3(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
- } else {
- return Vector3();
- }
- }
- Variant::operator Vector3i() const {
- if (type == VECTOR3I) {
- return *reinterpret_cast<const Vector3i *>(_data._mem);
- } else if (type == VECTOR3) {
- return *reinterpret_cast<const Vector3 *>(_data._mem);
- } else if (type == VECTOR2) {
- return Vector3i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
- } else if (type == VECTOR2I) {
- return Vector3i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
- } else if (type == VECTOR4) {
- return Vector3i(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
- } else if (type == VECTOR4I) {
- return Vector3i(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
- } else {
- return Vector3i();
- }
- }
- Variant::operator Vector4() const {
- if (type == VECTOR4) {
- return *reinterpret_cast<const Vector4 *>(_data._mem);
- } else if (type == VECTOR4I) {
- return *reinterpret_cast<const Vector4i *>(_data._mem);
- } else if (type == VECTOR2) {
- return Vector4(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
- } else if (type == VECTOR2I) {
- return Vector4(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
- } else if (type == VECTOR3) {
- 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);
- } else if (type == VECTOR3I) {
- 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);
- } else {
- return Vector4();
- }
- }
- Variant::operator Vector4i() const {
- if (type == VECTOR4I) {
- return *reinterpret_cast<const Vector4i *>(_data._mem);
- } else if (type == VECTOR4) {
- const Vector4 &v4 = *reinterpret_cast<const Vector4 *>(_data._mem);
- return Vector4i(v4.x, v4.y, v4.z, v4.w);
- } else if (type == VECTOR2) {
- return Vector4i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
- } else if (type == VECTOR2I) {
- return Vector4i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
- } else if (type == VECTOR3) {
- 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);
- } else if (type == VECTOR3I) {
- 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);
- } else {
- return Vector4i();
- }
- }
- Variant::operator Plane() const {
- if (type == PLANE) {
- return *reinterpret_cast<const Plane *>(_data._mem);
- } else {
- return Plane();
- }
- }
- Variant::operator ::AABB() const {
- if (type == AABB) {
- return *_data._aabb;
- } else {
- return ::AABB();
- }
- }
- Variant::operator Basis() const {
- if (type == BASIS) {
- return *_data._basis;
- } else if (type == QUATERNION) {
- return *reinterpret_cast<const Quaternion *>(_data._mem);
- } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert?
- return _data._transform3d->basis;
- } else {
- return Basis();
- }
- }
- Variant::operator Quaternion() const {
- if (type == QUATERNION) {
- return *reinterpret_cast<const Quaternion *>(_data._mem);
- } else if (type == BASIS) {
- return *_data._basis;
- } else if (type == TRANSFORM3D) {
- return _data._transform3d->basis;
- } else {
- return Quaternion();
- }
- }
- Variant::operator Transform3D() const {
- if (type == TRANSFORM3D) {
- return *_data._transform3d;
- } else if (type == BASIS) {
- return Transform3D(*_data._basis, Vector3());
- } else if (type == QUATERNION) {
- return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
- } else if (type == TRANSFORM2D) {
- const Transform2D &t = *_data._transform2d;
- Transform3D m;
- m.basis.rows[0][0] = t.columns[0][0];
- m.basis.rows[1][0] = t.columns[0][1];
- m.basis.rows[0][1] = t.columns[1][0];
- m.basis.rows[1][1] = t.columns[1][1];
- m.origin[0] = t.columns[2][0];
- m.origin[1] = t.columns[2][1];
- return m;
- } else if (type == PROJECTION) {
- return *_data._projection;
- } else {
- return Transform3D();
- }
- }
- Variant::operator Projection() const {
- if (type == TRANSFORM3D) {
- return *_data._transform3d;
- } else if (type == BASIS) {
- return Transform3D(*_data._basis, Vector3());
- } else if (type == QUATERNION) {
- return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
- } else if (type == TRANSFORM2D) {
- const Transform2D &t = *_data._transform2d;
- Transform3D m;
- m.basis.rows[0][0] = t.columns[0][0];
- m.basis.rows[1][0] = t.columns[0][1];
- m.basis.rows[0][1] = t.columns[1][0];
- m.basis.rows[1][1] = t.columns[1][1];
- m.origin[0] = t.columns[2][0];
- m.origin[1] = t.columns[2][1];
- return m;
- } else if (type == PROJECTION) {
- return *_data._projection;
- } else {
- return Projection();
- }
- }
- Variant::operator Transform2D() const {
- if (type == TRANSFORM2D) {
- return *_data._transform2d;
- } else if (type == TRANSFORM3D) {
- const Transform3D &t = *_data._transform3d;
- Transform2D m;
- m.columns[0][0] = t.basis.rows[0][0];
- m.columns[0][1] = t.basis.rows[1][0];
- m.columns[1][0] = t.basis.rows[0][1];
- m.columns[1][1] = t.basis.rows[1][1];
- m.columns[2][0] = t.origin[0];
- m.columns[2][1] = t.origin[1];
- return m;
- } else {
- return Transform2D();
- }
- }
- Variant::operator Color() const {
- if (type == COLOR) {
- return *reinterpret_cast<const Color *>(_data._mem);
- } else if (type == STRING) {
- return Color(operator String());
- } else if (type == INT) {
- return Color::hex(operator int());
- } else {
- return Color();
- }
- }
- Variant::operator NodePath() const {
- if (type == NODE_PATH) {
- return *reinterpret_cast<const NodePath *>(_data._mem);
- } else if (type == STRING) {
- return NodePath(operator String());
- } else {
- return NodePath();
- }
- }
- Variant::operator ::RID() const {
- if (type == RID) {
- return *reinterpret_cast<const ::RID *>(_data._mem);
- } else if (type == OBJECT && _get_obj().obj == nullptr) {
- return ::RID();
- } else if (type == OBJECT && _get_obj().obj) {
- #ifdef DEBUG_ENABLED
- if (EngineDebugger::is_active()) {
- ERR_FAIL_NULL_V_MSG(ObjectDB::get_instance(_get_obj().id), ::RID(), "Invalid pointer (object was freed).");
- }
- #endif
- Callable::CallError ce;
- const Variant ret = _get_obj().obj->callp(CoreStringName(get_rid), nullptr, 0, ce);
- if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) {
- return ret;
- }
- return ::RID();
- } else {
- return ::RID();
- }
- }
- Variant::operator Object *() const {
- if (type == OBJECT) {
- return _get_obj().obj;
- } else {
- return nullptr;
- }
- }
- Object *Variant::get_validated_object_with_check(bool &r_previously_freed) const {
- if (type == OBJECT) {
- Object *instance = ObjectDB::get_instance(_get_obj().id);
- r_previously_freed = !instance && _get_obj().id != ObjectID();
- return instance;
- } else {
- r_previously_freed = false;
- return nullptr;
- }
- }
- Object *Variant::get_validated_object() const {
- if (type == OBJECT) {
- return ObjectDB::get_instance(_get_obj().id);
- } else {
- return nullptr;
- }
- }
- Variant::operator Dictionary() const {
- if (type == DICTIONARY) {
- return *reinterpret_cast<const Dictionary *>(_data._mem);
- } else {
- return Dictionary();
- }
- }
- Variant::operator Callable() const {
- if (type == CALLABLE) {
- return *reinterpret_cast<const Callable *>(_data._mem);
- } else {
- return Callable();
- }
- }
- Variant::operator Signal() const {
- if (type == SIGNAL) {
- return *reinterpret_cast<const Signal *>(_data._mem);
- } else {
- return Signal();
- }
- }
- template <typename DA, typename SA>
- inline DA _convert_array(const SA &p_array) {
- DA da;
- da.resize(p_array.size());
- for (int i = 0; i < p_array.size(); i++) {
- da.set(i, Variant(p_array.get(i)));
- }
- return da;
- }
- template <typename DA>
- inline DA _convert_array_from_variant(const Variant &p_variant) {
- switch (p_variant.get_type()) {
- case Variant::ARRAY: {
- return _convert_array<DA, Array>(p_variant.operator Array());
- }
- case Variant::PACKED_BYTE_ARRAY: {
- return _convert_array<DA, PackedByteArray>(p_variant.operator PackedByteArray());
- }
- case Variant::PACKED_INT32_ARRAY: {
- return _convert_array<DA, PackedInt32Array>(p_variant.operator PackedInt32Array());
- }
- case Variant::PACKED_INT64_ARRAY: {
- return _convert_array<DA, PackedInt64Array>(p_variant.operator PackedInt64Array());
- }
- case Variant::PACKED_FLOAT32_ARRAY: {
- return _convert_array<DA, PackedFloat32Array>(p_variant.operator PackedFloat32Array());
- }
- case Variant::PACKED_FLOAT64_ARRAY: {
- return _convert_array<DA, PackedFloat64Array>(p_variant.operator PackedFloat64Array());
- }
- case Variant::PACKED_STRING_ARRAY: {
- return _convert_array<DA, PackedStringArray>(p_variant.operator PackedStringArray());
- }
- case Variant::PACKED_VECTOR2_ARRAY: {
- return _convert_array<DA, PackedVector2Array>(p_variant.operator PackedVector2Array());
- }
- case Variant::PACKED_VECTOR3_ARRAY: {
- return _convert_array<DA, PackedVector3Array>(p_variant.operator PackedVector3Array());
- }
- case Variant::PACKED_COLOR_ARRAY: {
- return _convert_array<DA, PackedColorArray>(p_variant.operator PackedColorArray());
- }
- case Variant::PACKED_VECTOR4_ARRAY: {
- return _convert_array<DA, PackedVector4Array>(p_variant.operator PackedVector4Array());
- }
- default: {
- return DA();
- }
- }
- }
- Variant::operator Array() const {
- if (type == ARRAY) {
- return *reinterpret_cast<const Array *>(_data._mem);
- } else {
- return _convert_array_from_variant<Array>(*this);
- }
- }
- Variant::operator PackedByteArray() const {
- if (type == PACKED_BYTE_ARRAY) {
- return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedByteArray>(*this);
- }
- }
- Variant::operator PackedInt32Array() const {
- if (type == PACKED_INT32_ARRAY) {
- return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedInt32Array>(*this);
- }
- }
- Variant::operator PackedInt64Array() const {
- if (type == PACKED_INT64_ARRAY) {
- return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedInt64Array>(*this);
- }
- }
- Variant::operator PackedFloat32Array() const {
- if (type == PACKED_FLOAT32_ARRAY) {
- return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedFloat32Array>(*this);
- }
- }
- Variant::operator PackedFloat64Array() const {
- if (type == PACKED_FLOAT64_ARRAY) {
- return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedFloat64Array>(*this);
- }
- }
- Variant::operator PackedStringArray() const {
- if (type == PACKED_STRING_ARRAY) {
- return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedStringArray>(*this);
- }
- }
- Variant::operator PackedVector2Array() const {
- if (type == PACKED_VECTOR2_ARRAY) {
- return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedVector2Array>(*this);
- }
- }
- Variant::operator PackedVector3Array() const {
- if (type == PACKED_VECTOR3_ARRAY) {
- return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedVector3Array>(*this);
- }
- }
- Variant::operator PackedColorArray() const {
- if (type == PACKED_COLOR_ARRAY) {
- return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedColorArray>(*this);
- }
- }
- Variant::operator PackedVector4Array() const {
- if (type == PACKED_VECTOR4_ARRAY) {
- return static_cast<PackedArrayRef<Vector4> *>(_data.packed_array)->array;
- } else {
- return _convert_array_from_variant<PackedVector4Array>(*this);
- }
- }
- /* helpers */
- Variant::operator Vector<::RID>() const {
- Array va = operator Array();
- Vector<::RID> rids;
- rids.resize(va.size());
- for (int i = 0; i < rids.size(); i++) {
- rids.write[i] = va[i];
- }
- return rids;
- }
- Variant::operator Vector<Plane>() const {
- Array va = operator Array();
- Vector<Plane> planes;
- int va_size = va.size();
- if (va_size == 0) {
- return planes;
- }
- planes.resize(va_size);
- Plane *w = planes.ptrw();
- for (int i = 0; i < va_size; i++) {
- w[i] = va[i];
- }
- return planes;
- }
- Variant::operator Vector<Face3>() const {
- PackedVector3Array va = operator PackedVector3Array();
- Vector<Face3> faces;
- int va_size = va.size();
- if (va_size == 0) {
- return faces;
- }
- faces.resize(va_size / 3);
- Face3 *w = faces.ptrw();
- const Vector3 *r = va.ptr();
- for (int i = 0; i < va_size; i++) {
- w[i / 3].vertex[i % 3] = r[i];
- }
- return faces;
- }
- Variant::operator Vector<Variant>() const {
- Array va = operator Array();
- Vector<Variant> variants;
- int va_size = va.size();
- if (va_size == 0) {
- return variants;
- }
- variants.resize(va_size);
- Variant *w = variants.ptrw();
- for (int i = 0; i < va_size; i++) {
- w[i] = va[i];
- }
- return variants;
- }
- Variant::operator Vector<StringName>() const {
- PackedStringArray from = operator PackedStringArray();
- Vector<StringName> to;
- int len = from.size();
- to.resize(len);
- for (int i = 0; i < len; i++) {
- to.write[i] = from[i];
- }
- return to;
- }
- Variant::operator Side() const {
- return (Side) operator int();
- }
- Variant::operator Orientation() const {
- return (Orientation) operator int();
- }
- Variant::operator IPAddress() const {
- if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
- Vector<int> addr = operator Vector<int>();
- if (addr.size() == 4) {
- return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
- }
- }
- return IPAddress(operator String());
- }
- Variant::Variant(bool p_bool) :
- type(BOOL) {
- _data._bool = p_bool;
- }
- Variant::Variant(int64_t p_int64) :
- type(INT) {
- _data._int = p_int64;
- }
- Variant::Variant(int32_t p_int32) :
- type(INT) {
- _data._int = p_int32;
- }
- Variant::Variant(int16_t p_int16) :
- type(INT) {
- _data._int = p_int16;
- }
- Variant::Variant(int8_t p_int8) :
- type(INT) {
- _data._int = p_int8;
- }
- Variant::Variant(uint64_t p_uint64) :
- type(INT) {
- _data._int = p_uint64;
- }
- Variant::Variant(uint32_t p_uint32) :
- type(INT) {
- _data._int = p_uint32;
- }
- Variant::Variant(uint16_t p_uint16) :
- type(INT) {
- _data._int = p_uint16;
- }
- Variant::Variant(uint8_t p_uint8) :
- type(INT) {
- _data._int = p_uint8;
- }
- Variant::Variant(float p_float) :
- type(FLOAT) {
- _data._float = p_float;
- }
- Variant::Variant(double p_double) :
- type(FLOAT) {
- _data._float = p_double;
- }
- Variant::Variant(const ObjectID &p_id) :
- type(INT) {
- _data._int = p_id;
- }
- Variant::Variant(const StringName &p_string) :
- type(STRING_NAME) {
- memnew_placement(_data._mem, StringName(p_string));
- }
- Variant::Variant(const String &p_string) :
- type(STRING) {
- memnew_placement(_data._mem, String(p_string));
- }
- Variant::Variant(const char *const p_cstring) :
- type(STRING) {
- memnew_placement(_data._mem, String((const char *)p_cstring));
- }
- Variant::Variant(const char32_t *p_wstring) :
- type(STRING) {
- memnew_placement(_data._mem, String(p_wstring));
- }
- Variant::Variant(const Vector3 &p_vector3) :
- type(VECTOR3) {
- memnew_placement(_data._mem, Vector3(p_vector3));
- }
- Variant::Variant(const Vector3i &p_vector3i) :
- type(VECTOR3I) {
- memnew_placement(_data._mem, Vector3i(p_vector3i));
- }
- Variant::Variant(const Vector4 &p_vector4) :
- type(VECTOR4) {
- memnew_placement(_data._mem, Vector4(p_vector4));
- }
- Variant::Variant(const Vector4i &p_vector4i) :
- type(VECTOR4I) {
- memnew_placement(_data._mem, Vector4i(p_vector4i));
- }
- Variant::Variant(const Vector2 &p_vector2) :
- type(VECTOR2) {
- memnew_placement(_data._mem, Vector2(p_vector2));
- }
- Variant::Variant(const Vector2i &p_vector2i) :
- type(VECTOR2I) {
- memnew_placement(_data._mem, Vector2i(p_vector2i));
- }
- Variant::Variant(const Rect2 &p_rect2) :
- type(RECT2) {
- memnew_placement(_data._mem, Rect2(p_rect2));
- }
- Variant::Variant(const Rect2i &p_rect2i) :
- type(RECT2I) {
- memnew_placement(_data._mem, Rect2i(p_rect2i));
- }
- Variant::Variant(const Plane &p_plane) :
- type(PLANE) {
- memnew_placement(_data._mem, Plane(p_plane));
- }
- Variant::Variant(const ::AABB &p_aabb) :
- type(AABB) {
- _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
- memnew_placement(_data._aabb, ::AABB(p_aabb));
- }
- Variant::Variant(const Basis &p_matrix) :
- type(BASIS) {
- _data._basis = (Basis *)Pools::_bucket_medium.alloc();
- memnew_placement(_data._basis, Basis(p_matrix));
- }
- Variant::Variant(const Quaternion &p_quaternion) :
- type(QUATERNION) {
- memnew_placement(_data._mem, Quaternion(p_quaternion));
- }
- Variant::Variant(const Transform3D &p_transform) :
- type(TRANSFORM3D) {
- _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
- memnew_placement(_data._transform3d, Transform3D(p_transform));
- }
- Variant::Variant(const Projection &pp_projection) :
- type(PROJECTION) {
- _data._projection = (Projection *)Pools::_bucket_large.alloc();
- memnew_placement(_data._projection, Projection(pp_projection));
- }
- Variant::Variant(const Transform2D &p_transform) :
- type(TRANSFORM2D) {
- _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
- memnew_placement(_data._transform2d, Transform2D(p_transform));
- }
- Variant::Variant(const Color &p_color) :
- type(COLOR) {
- memnew_placement(_data._mem, Color(p_color));
- }
- Variant::Variant(const NodePath &p_node_path) :
- type(NODE_PATH) {
- memnew_placement(_data._mem, NodePath(p_node_path));
- }
- Variant::Variant(const ::RID &p_rid) :
- type(RID) {
- memnew_placement(_data._mem, ::RID(p_rid));
- }
- Variant::Variant(const Object *p_object) :
- type(OBJECT) {
- _get_obj() = ObjData();
- _get_obj().ref_pointer(const_cast<Object *>(p_object));
- }
- Variant::Variant(const Callable &p_callable) :
- type(CALLABLE) {
- memnew_placement(_data._mem, Callable(p_callable));
- }
- Variant::Variant(const Signal &p_callable) :
- type(SIGNAL) {
- memnew_placement(_data._mem, Signal(p_callable));
- }
- Variant::Variant(const Dictionary &p_dictionary) :
- type(DICTIONARY) {
- memnew_placement(_data._mem, Dictionary(p_dictionary));
- }
- Variant::Variant(const Array &p_array) :
- type(ARRAY) {
- memnew_placement(_data._mem, Array(p_array));
- }
- Variant::Variant(const PackedByteArray &p_byte_array) :
- type(PACKED_BYTE_ARRAY) {
- _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
- }
- Variant::Variant(const PackedInt32Array &p_int32_array) :
- type(PACKED_INT32_ARRAY) {
- _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
- }
- Variant::Variant(const PackedInt64Array &p_int64_array) :
- type(PACKED_INT64_ARRAY) {
- _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
- }
- Variant::Variant(const PackedFloat32Array &p_float32_array) :
- type(PACKED_FLOAT32_ARRAY) {
- _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
- }
- Variant::Variant(const PackedFloat64Array &p_float64_array) :
- type(PACKED_FLOAT64_ARRAY) {
- _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
- }
- Variant::Variant(const PackedStringArray &p_string_array) :
- type(PACKED_STRING_ARRAY) {
- _data.packed_array = PackedArrayRef<String>::create(p_string_array);
- }
- Variant::Variant(const PackedVector2Array &p_vector2_array) :
- type(PACKED_VECTOR2_ARRAY) {
- _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
- }
- Variant::Variant(const PackedVector3Array &p_vector3_array) :
- type(PACKED_VECTOR3_ARRAY) {
- _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
- }
- Variant::Variant(const PackedColorArray &p_color_array) :
- type(PACKED_COLOR_ARRAY) {
- _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
- }
- Variant::Variant(const PackedVector4Array &p_vector4_array) :
- type(PACKED_VECTOR4_ARRAY) {
- _data.packed_array = PackedArrayRef<Vector4>::create(p_vector4_array);
- }
- /* helpers */
- Variant::Variant(const Vector<::RID> &p_array) :
- type(ARRAY) {
- Array *rid_array = memnew_placement(_data._mem, Array);
- rid_array->resize(p_array.size());
- for (int i = 0; i < p_array.size(); i++) {
- rid_array->set(i, Variant(p_array[i]));
- }
- }
- Variant::Variant(const Vector<Plane> &p_array) :
- type(ARRAY) {
- Array *plane_array = memnew_placement(_data._mem, Array);
- plane_array->resize(p_array.size());
- for (int i = 0; i < p_array.size(); i++) {
- plane_array->operator[](i) = Variant(p_array[i]);
- }
- }
- Variant::Variant(const Vector<Face3> &p_face_array) {
- PackedVector3Array vertices;
- int face_count = p_face_array.size();
- vertices.resize(face_count * 3);
- if (face_count) {
- const Face3 *r = p_face_array.ptr();
- Vector3 *w = vertices.ptrw();
- for (int i = 0; i < face_count; i++) {
- for (int j = 0; j < 3; j++) {
- w[i * 3 + j] = r[i].vertex[j];
- }
- }
- }
- *this = vertices;
- }
- Variant::Variant(const Vector<Variant> &p_array) {
- Array arr;
- arr.resize(p_array.size());
- for (int i = 0; i < p_array.size(); i++) {
- arr[i] = p_array[i];
- }
- *this = arr;
- }
- Variant::Variant(const Vector<StringName> &p_array) {
- PackedStringArray v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- void Variant::operator=(const Variant &p_variant) {
- if (unlikely(this == &p_variant)) {
- return;
- }
- if (unlikely(type != p_variant.type)) {
- reference(p_variant);
- return;
- }
- switch (p_variant.type) {
- case NIL: {
- // none
- } break;
- // atomic types
- case BOOL: {
- _data._bool = p_variant._data._bool;
- } break;
- case INT: {
- _data._int = p_variant._data._int;
- } break;
- case FLOAT: {
- _data._float = p_variant._data._float;
- } break;
- case STRING: {
- *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
- } break;
- // math types
- case VECTOR2: {
- *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
- } break;
- case VECTOR2I: {
- *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
- } break;
- case RECT2: {
- *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
- } break;
- case RECT2I: {
- *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
- } break;
- case TRANSFORM2D: {
- *_data._transform2d = *(p_variant._data._transform2d);
- } break;
- case VECTOR3: {
- *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
- } break;
- case VECTOR3I: {
- *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
- } break;
- case VECTOR4: {
- *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
- } break;
- case VECTOR4I: {
- *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
- } break;
- case PLANE: {
- *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
- } break;
- case AABB: {
- *_data._aabb = *(p_variant._data._aabb);
- } break;
- case QUATERNION: {
- *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
- } break;
- case BASIS: {
- *_data._basis = *(p_variant._data._basis);
- } break;
- case TRANSFORM3D: {
- *_data._transform3d = *(p_variant._data._transform3d);
- } break;
- case PROJECTION: {
- *_data._projection = *(p_variant._data._projection);
- } break;
- // misc types
- case COLOR: {
- *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
- } break;
- case RID: {
- *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
- } break;
- case OBJECT: {
- _get_obj().ref(p_variant._get_obj());
- } break;
- case CALLABLE: {
- *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
- } break;
- case SIGNAL: {
- *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
- } break;
- case STRING_NAME: {
- *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
- } break;
- case NODE_PATH: {
- *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
- } break;
- case DICTIONARY: {
- *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
- } break;
- case ARRAY: {
- *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
- } break;
- // arrays
- case PACKED_BYTE_ARRAY: {
- _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_INT32_ARRAY: {
- _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_INT64_ARRAY: {
- _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_FLOAT32_ARRAY: {
- _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_FLOAT64_ARRAY: {
- _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_STRING_ARRAY: {
- _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_VECTOR2_ARRAY: {
- _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_VECTOR3_ARRAY: {
- _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_COLOR_ARRAY: {
- _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- case PACKED_VECTOR4_ARRAY: {
- _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
- } break;
- default: {
- }
- }
- }
- Variant::Variant(const IPAddress &p_address) :
- type(STRING) {
- memnew_placement(_data._mem, String(p_address));
- }
- Variant::Variant(const Variant &p_variant) {
- reference(p_variant);
- }
- uint32_t Variant::hash() const {
- return recursive_hash(0);
- }
- uint32_t Variant::recursive_hash(int recursion_count) const {
- switch (type) {
- case NIL: {
- return 0;
- } break;
- case BOOL: {
- return _data._bool ? 1 : 0;
- } break;
- case INT: {
- return hash_one_uint64((uint64_t)_data._int);
- } break;
- case FLOAT: {
- return hash_murmur3_one_double(_data._float);
- } break;
- case STRING: {
- return reinterpret_cast<const String *>(_data._mem)->hash();
- } break;
- // math types
- case VECTOR2: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
- } break;
- case VECTOR2I: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
- } break;
- case RECT2: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
- } break;
- case RECT2I: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
- } break;
- case TRANSFORM2D: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Transform2D &t = *_data._transform2d;
- h = hash_murmur3_one_real(t[0].x, h);
- h = hash_murmur3_one_real(t[0].y, h);
- h = hash_murmur3_one_real(t[1].x, h);
- h = hash_murmur3_one_real(t[1].y, h);
- h = hash_murmur3_one_real(t[2].x, h);
- h = hash_murmur3_one_real(t[2].y, h);
- return hash_fmix32(h);
- } break;
- case VECTOR3: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
- } break;
- case VECTOR3I: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
- } break;
- case VECTOR4: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
- } break;
- case VECTOR4I: {
- return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
- } break;
- case PLANE: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
- h = hash_murmur3_one_real(p.normal.x, h);
- h = hash_murmur3_one_real(p.normal.y, h);
- h = hash_murmur3_one_real(p.normal.z, h);
- h = hash_murmur3_one_real(p.d, h);
- return hash_fmix32(h);
- } break;
- case AABB: {
- return HashMapHasherDefault::hash(*_data._aabb);
- } break;
- case QUATERNION: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
- h = hash_murmur3_one_real(q.x, h);
- h = hash_murmur3_one_real(q.y, h);
- h = hash_murmur3_one_real(q.z, h);
- h = hash_murmur3_one_real(q.w, h);
- return hash_fmix32(h);
- } break;
- case BASIS: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Basis &b = *_data._basis;
- h = hash_murmur3_one_real(b[0].x, h);
- h = hash_murmur3_one_real(b[0].y, h);
- h = hash_murmur3_one_real(b[0].z, h);
- h = hash_murmur3_one_real(b[1].x, h);
- h = hash_murmur3_one_real(b[1].y, h);
- h = hash_murmur3_one_real(b[1].z, h);
- h = hash_murmur3_one_real(b[2].x, h);
- h = hash_murmur3_one_real(b[2].y, h);
- h = hash_murmur3_one_real(b[2].z, h);
- return hash_fmix32(h);
- } break;
- case TRANSFORM3D: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Transform3D &t = *_data._transform3d;
- h = hash_murmur3_one_real(t.basis[0].x, h);
- h = hash_murmur3_one_real(t.basis[0].y, h);
- h = hash_murmur3_one_real(t.basis[0].z, h);
- h = hash_murmur3_one_real(t.basis[1].x, h);
- h = hash_murmur3_one_real(t.basis[1].y, h);
- h = hash_murmur3_one_real(t.basis[1].z, h);
- h = hash_murmur3_one_real(t.basis[2].x, h);
- h = hash_murmur3_one_real(t.basis[2].y, h);
- h = hash_murmur3_one_real(t.basis[2].z, h);
- h = hash_murmur3_one_real(t.origin.x, h);
- h = hash_murmur3_one_real(t.origin.y, h);
- h = hash_murmur3_one_real(t.origin.z, h);
- return hash_fmix32(h);
- } break;
- case PROJECTION: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Projection &t = *_data._projection;
- h = hash_murmur3_one_real(t.columns[0].x, h);
- h = hash_murmur3_one_real(t.columns[0].y, h);
- h = hash_murmur3_one_real(t.columns[0].z, h);
- h = hash_murmur3_one_real(t.columns[0].w, h);
- h = hash_murmur3_one_real(t.columns[1].x, h);
- h = hash_murmur3_one_real(t.columns[1].y, h);
- h = hash_murmur3_one_real(t.columns[1].z, h);
- h = hash_murmur3_one_real(t.columns[1].w, h);
- h = hash_murmur3_one_real(t.columns[2].x, h);
- h = hash_murmur3_one_real(t.columns[2].y, h);
- h = hash_murmur3_one_real(t.columns[2].z, h);
- h = hash_murmur3_one_real(t.columns[2].w, h);
- h = hash_murmur3_one_real(t.columns[3].x, h);
- h = hash_murmur3_one_real(t.columns[3].y, h);
- h = hash_murmur3_one_real(t.columns[3].z, h);
- h = hash_murmur3_one_real(t.columns[3].w, h);
- return hash_fmix32(h);
- } break;
- // misc types
- case COLOR: {
- uint32_t h = HASH_MURMUR3_SEED;
- const Color &c = *reinterpret_cast<const Color *>(_data._mem);
- h = hash_murmur3_one_float(c.r, h);
- h = hash_murmur3_one_float(c.g, h);
- h = hash_murmur3_one_float(c.b, h);
- h = hash_murmur3_one_float(c.a, h);
- return hash_fmix32(h);
- } break;
- case RID: {
- return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
- } break;
- case OBJECT: {
- return hash_one_uint64(hash_make_uint64_t(_get_obj().obj));
- } break;
- case STRING_NAME: {
- return reinterpret_cast<const StringName *>(_data._mem)->hash();
- } break;
- case NODE_PATH: {
- return reinterpret_cast<const NodePath *>(_data._mem)->hash();
- } break;
- case DICTIONARY: {
- return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
- } break;
- case CALLABLE: {
- return reinterpret_cast<const Callable *>(_data._mem)->hash();
- } break;
- case SIGNAL: {
- const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
- uint32_t hash = s.get_name().hash();
- return hash_murmur3_one_64(s.get_object_id(), hash);
- } break;
- case ARRAY: {
- const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
- return arr.recursive_hash(recursion_count);
- } break;
- case PACKED_BYTE_ARRAY: {
- const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const uint8_t *r = arr.ptr();
- return hash_murmur3_buffer((uint8_t *)&r[0], len);
- } else {
- return hash_murmur3_one_64(0);
- }
- } break;
- case PACKED_INT32_ARRAY: {
- const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const int32_t *r = arr.ptr();
- return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
- } else {
- return hash_murmur3_one_64(0);
- }
- } break;
- case PACKED_INT64_ARRAY: {
- const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const int64_t *r = arr.ptr();
- return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
- } else {
- return hash_murmur3_one_64(0);
- }
- } break;
- case PACKED_FLOAT32_ARRAY: {
- const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const float *r = arr.ptr();
- uint32_t h = HASH_MURMUR3_SEED;
- for (int32_t i = 0; i < len; i++) {
- h = hash_murmur3_one_float(r[i], h);
- }
- return hash_fmix32(h);
- } else {
- return hash_murmur3_one_float(0.0);
- }
- } break;
- case PACKED_FLOAT64_ARRAY: {
- const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const double *r = arr.ptr();
- uint32_t h = HASH_MURMUR3_SEED;
- for (int32_t i = 0; i < len; i++) {
- h = hash_murmur3_one_double(r[i], h);
- }
- return hash_fmix32(h);
- } else {
- return hash_murmur3_one_double(0.0);
- }
- } break;
- case PACKED_STRING_ARRAY: {
- uint32_t hash = HASH_MURMUR3_SEED;
- const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const String *r = arr.ptr();
- for (int i = 0; i < len; i++) {
- hash = hash_murmur3_one_32(r[i].hash(), hash);
- }
- hash = hash_fmix32(hash);
- }
- return hash;
- } break;
- case PACKED_VECTOR2_ARRAY: {
- uint32_t hash = HASH_MURMUR3_SEED;
- const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const Vector2 *r = arr.ptr();
- for (int i = 0; i < len; i++) {
- hash = hash_murmur3_one_real(r[i].x, hash);
- hash = hash_murmur3_one_real(r[i].y, hash);
- }
- hash = hash_fmix32(hash);
- }
- return hash;
- } break;
- case PACKED_VECTOR3_ARRAY: {
- uint32_t hash = HASH_MURMUR3_SEED;
- const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const Vector3 *r = arr.ptr();
- for (int i = 0; i < len; i++) {
- hash = hash_murmur3_one_real(r[i].x, hash);
- hash = hash_murmur3_one_real(r[i].y, hash);
- hash = hash_murmur3_one_real(r[i].z, hash);
- }
- hash = hash_fmix32(hash);
- }
- return hash;
- } break;
- case PACKED_COLOR_ARRAY: {
- uint32_t hash = HASH_MURMUR3_SEED;
- const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const Color *r = arr.ptr();
- for (int i = 0; i < len; i++) {
- hash = hash_murmur3_one_float(r[i].r, hash);
- hash = hash_murmur3_one_float(r[i].g, hash);
- hash = hash_murmur3_one_float(r[i].b, hash);
- hash = hash_murmur3_one_float(r[i].a, hash);
- }
- hash = hash_fmix32(hash);
- }
- return hash;
- } break;
- case PACKED_VECTOR4_ARRAY: {
- uint32_t hash = HASH_MURMUR3_SEED;
- const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
- int len = arr.size();
- if (likely(len)) {
- const Vector4 *r = arr.ptr();
- for (int i = 0; i < len; i++) {
- hash = hash_murmur3_one_real(r[i].x, hash);
- hash = hash_murmur3_one_real(r[i].y, hash);
- hash = hash_murmur3_one_real(r[i].z, hash);
- hash = hash_murmur3_one_real(r[i].w, hash);
- }
- hash = hash_fmix32(hash);
- }
- return hash;
- } break;
- default: {
- }
- }
- return 0;
- }
- #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
- (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
- #define hash_compare_scalar(p_lhs, p_rhs) \
- (hash_compare_scalar_base(p_lhs, p_rhs, true))
- #define hash_compare_vector2(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
- hash_compare_scalar((p_lhs).y, (p_rhs).y))
- #define hash_compare_vector3(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
- hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
- hash_compare_scalar((p_lhs).z, (p_rhs).z))
- #define hash_compare_vector4(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
- hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
- hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
- hash_compare_scalar((p_lhs).w, (p_rhs).w))
- #define hash_compare_quaternion(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
- hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
- hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
- hash_compare_scalar((p_lhs).w, (p_rhs).w))
- #define hash_compare_color(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
- hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
- hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
- hash_compare_scalar((p_lhs).a, (p_rhs).a))
- #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
- const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
- const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
- \
- if (l.size() != r.size()) \
- return false; \
- \
- const p_type *lr = l.ptr(); \
- const p_type *rr = r.ptr(); \
- \
- for (int i = 0; i < l.size(); ++i) { \
- if (!p_compare_func((lr[i]), (rr[i]))) \
- return false; \
- } \
- \
- return true
- bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
- if (type != p_variant.type) {
- return false;
- }
- switch (type) {
- case INT: {
- return _data._int == p_variant._data._int;
- } break;
- case FLOAT: {
- return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
- } break;
- case STRING: {
- return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
- } break;
- case STRING_NAME: {
- return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
- } break;
- case VECTOR2: {
- const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
- const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
- return hash_compare_vector2(*l, *r);
- } break;
- case VECTOR2I: {
- const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
- const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
- return *l == *r;
- } break;
- case RECT2: {
- const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
- const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
- return hash_compare_vector2(l->position, r->position) &&
- hash_compare_vector2(l->size, r->size);
- } break;
- case RECT2I: {
- const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
- const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
- return *l == *r;
- } break;
- case TRANSFORM2D: {
- Transform2D *l = _data._transform2d;
- Transform2D *r = p_variant._data._transform2d;
- for (int i = 0; i < 3; i++) {
- if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
- return false;
- }
- }
- return true;
- } break;
- case VECTOR3: {
- const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
- const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
- return hash_compare_vector3(*l, *r);
- } break;
- case VECTOR3I: {
- const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
- const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
- return *l == *r;
- } break;
- case VECTOR4: {
- const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
- const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
- return hash_compare_vector4(*l, *r);
- } break;
- case VECTOR4I: {
- const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
- const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
- return *l == *r;
- } break;
- case PLANE: {
- const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
- const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
- return hash_compare_vector3(l->normal, r->normal) &&
- hash_compare_scalar(l->d, r->d);
- } break;
- case AABB: {
- const ::AABB *l = _data._aabb;
- const ::AABB *r = p_variant._data._aabb;
- return hash_compare_vector3(l->position, r->position) &&
- hash_compare_vector3(l->size, r->size);
- } break;
- case QUATERNION: {
- const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
- const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
- return hash_compare_quaternion(*l, *r);
- } break;
- case BASIS: {
- const Basis *l = _data._basis;
- const Basis *r = p_variant._data._basis;
- for (int i = 0; i < 3; i++) {
- if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
- return false;
- }
- }
- return true;
- } break;
- case TRANSFORM3D: {
- const Transform3D *l = _data._transform3d;
- const Transform3D *r = p_variant._data._transform3d;
- for (int i = 0; i < 3; i++) {
- if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
- return false;
- }
- }
- return hash_compare_vector3(l->origin, r->origin);
- } break;
- case PROJECTION: {
- const Projection *l = _data._projection;
- const Projection *r = p_variant._data._projection;
- for (int i = 0; i < 4; i++) {
- if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
- return false;
- }
- }
- return true;
- } break;
- case COLOR: {
- const Color *l = reinterpret_cast<const Color *>(_data._mem);
- const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
- return hash_compare_color(*l, *r);
- } break;
- case ARRAY: {
- const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
- const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
- if (!l.recursive_equal(r, recursion_count + 1)) {
- return false;
- }
- return true;
- } break;
- case DICTIONARY: {
- const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
- const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
- if (!l.recursive_equal(r, recursion_count + 1)) {
- return false;
- }
- return true;
- } break;
- // This is for floating point comparisons only.
- case PACKED_FLOAT32_ARRAY: {
- hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
- } break;
- case PACKED_FLOAT64_ARRAY: {
- hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
- } break;
- case PACKED_VECTOR2_ARRAY: {
- hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
- } break;
- case PACKED_VECTOR3_ARRAY: {
- hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
- } break;
- case PACKED_COLOR_ARRAY: {
- hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
- } break;
- case PACKED_VECTOR4_ARRAY: {
- hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
- } break;
- default:
- bool v;
- Variant r;
- evaluate(OP_EQUAL, *this, p_variant, r, v);
- return r;
- }
- }
- bool Variant::identity_compare(const Variant &p_variant) const {
- if (type != p_variant.type) {
- return false;
- }
- switch (type) {
- case OBJECT: {
- return _get_obj().id == p_variant._get_obj().id;
- } break;
- case DICTIONARY: {
- const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
- const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
- return l.id() == r.id();
- } break;
- case ARRAY: {
- const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
- const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
- return l.id() == r.id();
- } break;
- case PACKED_BYTE_ARRAY:
- case PACKED_INT32_ARRAY:
- case PACKED_INT64_ARRAY:
- case PACKED_FLOAT32_ARRAY:
- case PACKED_FLOAT64_ARRAY:
- case PACKED_STRING_ARRAY:
- case PACKED_VECTOR2_ARRAY:
- case PACKED_VECTOR3_ARRAY:
- case PACKED_COLOR_ARRAY:
- case PACKED_VECTOR4_ARRAY: {
- return _data.packed_array == p_variant._data.packed_array;
- } break;
- default: {
- return hash_compare(p_variant);
- }
- }
- }
- bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
- if (p_lhs.hash_compare(p_rhs)) {
- return true;
- }
- if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
- return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
- }
- if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
- return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
- }
- return false;
- }
- bool Variant::is_ref_counted() const {
- return type == OBJECT && _get_obj().id.is_ref_counted();
- }
- void Variant::static_assign(const Variant &p_variant) {
- }
- bool Variant::is_type_shared(Variant::Type p_type) {
- switch (p_type) {
- case OBJECT:
- case ARRAY:
- case DICTIONARY:
- return true;
- default: {
- }
- }
- return false;
- }
- bool Variant::is_shared() const {
- return is_type_shared(type);
- }
- bool Variant::is_read_only() const {
- switch (type) {
- case ARRAY:
- return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
- case DICTIONARY:
- return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
- default:
- return false;
- }
- }
- void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
- switch (error.error) {
- case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
- String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
- ERR_PRINT(err.utf8().get_data());
- } break;
- case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
- String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
- ERR_PRINT(err.utf8().get_data());
- } break;
- case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
- String err = "Too many arguments for method '" + p_method + "'";
- ERR_PRINT(err.utf8().get_data());
- } break;
- default: {
- }
- }
- }
- void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
- r_value = Variant();
- }
- String Variant::get_construct_string() const {
- String vars;
- VariantWriter::write_to_string(*this, vars);
- return vars;
- }
- String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
- return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
- }
- String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
- String err_text;
- if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
- int errorarg = ce.argument;
- if (p_argptrs) {
- 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));
- } else {
- err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
- }
- } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
- err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
- } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
- err_text = "Method expected " + itos(ce.expected) + " arguments, but called with " + itos(p_argcount);
- } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
- err_text = "Method not found";
- } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
- err_text = "Instance is null";
- } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
- err_text = "Method not const in const instance";
- } else if (ce.error == Callable::CallError::CALL_OK) {
- return "Call OK";
- }
- String base_text;
- if (p_base) {
- base_text = p_base->get_class();
- Ref<Resource> script = p_base->get_script();
- if (script.is_valid() && script->get_path().is_resource_file()) {
- base_text += "(" + script->get_path().get_file() + ")";
- }
- base_text += "::";
- }
- return "'" + base_text + String(p_method) + "': " + err_text;
- }
- String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
- Vector<Variant> binds;
- p_callable.get_bound_arguments_ref(binds);
- int args_unbound = p_callable.get_unbound_arguments_count();
- if (p_argcount - args_unbound < 0) {
- return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
- } else {
- Vector<const Variant *> argptrs;
- argptrs.resize(p_argcount - args_unbound + binds.size());
- for (int i = 0; i < p_argcount - args_unbound; i++) {
- argptrs.write[i] = p_argptrs[i];
- }
- for (int i = 0; i < binds.size(); i++) {
- argptrs.write[i + p_argcount - args_unbound] = &binds[i];
- }
- return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
- }
- }
- void Variant::register_types() {
- _register_variant_operators();
- _register_variant_methods();
- _register_variant_setters_getters();
- _register_variant_constructors();
- _register_variant_destructors();
- _register_variant_utility_functions();
- }
- void Variant::unregister_types() {
- _unregister_variant_operators();
- _unregister_variant_methods();
- _unregister_variant_setters_getters();
- _unregister_variant_destructors();
- _unregister_variant_utility_functions();
- }
|