1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103 |
- /**************************************************************************/
- /* 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/core_string_names.h"
- #include "core/io/marshalls.h"
- #include "core/math/math_funcs.h"
- #include "core/object_rc.h"
- #include "core/print_string.h"
- #include "core/resource.h"
- #include "core/variant_parser.h"
- #include "scene/gui/control.h"
- #include "scene/main/node.h"
- String Variant::get_type_name(Variant::Type p_type) {
- switch (p_type) {
- case NIL: {
- return "Nil";
- } break;
- // atomic types
- case BOOL: {
- return "bool";
- } break;
- case INT: {
- return "int";
- } break;
- case REAL: {
- return "float";
- } break;
- case STRING: {
- return "String";
- } break;
- // math types
- case VECTOR2: {
- return "Vector2";
- } break;
- case RECT2: {
- return "Rect2";
- } break;
- case TRANSFORM2D: {
- return "Transform2D";
- } break;
- case VECTOR3: {
- return "Vector3";
- } break;
- case PLANE: {
- return "Plane";
- } break;
- /*
- case QUAT: {
- } break;*/
- case AABB: {
- return "AABB";
- } break;
- case QUAT: {
- return "Quat";
- } break;
- case BASIS: {
- return "Basis";
- } break;
- case TRANSFORM: {
- return "Transform";
- } break;
- // misc types
- case COLOR: {
- return "Color";
- } break;
- case _RID: {
- return "RID";
- } break;
- case OBJECT: {
- return "Object";
- } break;
- case NODE_PATH: {
- return "NodePath";
- } break;
- case DICTIONARY: {
- return "Dictionary";
- } break;
- case ARRAY: {
- return "Array";
- } break;
- // arrays
- case POOL_BYTE_ARRAY: {
- return "PoolByteArray";
- } break;
- case POOL_INT_ARRAY: {
- return "PoolIntArray";
- } break;
- case POOL_REAL_ARRAY: {
- return "PoolRealArray";
- } break;
- case POOL_STRING_ARRAY: {
- return "PoolStringArray";
- } break;
- case POOL_VECTOR2_ARRAY: {
- return "PoolVector2Array";
- } break;
- case POOL_VECTOR3_ARRAY: {
- return "PoolVector3Array";
- } break;
- case POOL_COLOR_ARRAY: {
- return "PoolColorArray";
- } break;
- 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 && p_type_from != 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,
- REAL,
- STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case INT: {
- static const Type valid[] = {
- BOOL,
- REAL,
- STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case REAL: {
- 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 TRANSFORM2D: {
- static const Type valid[] = {
- TRANSFORM,
- NIL
- };
- valid_types = valid;
- } break;
- case QUAT: {
- static const Type valid[] = {
- BASIS,
- NIL
- };
- valid_types = valid;
- } break;
- case BASIS: {
- static const Type valid[] = {
- QUAT,
- VECTOR3,
- NIL
- };
- valid_types = valid;
- } break;
- case TRANSFORM: {
- static const Type valid[] = {
- TRANSFORM2D,
- QUAT,
- BASIS,
- 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 NODE_PATH: {
- static const Type valid[] = {
- STRING,
- NIL
- };
- valid_types = valid;
- } break;
- case ARRAY: {
- static const Type valid[] = {
- POOL_BYTE_ARRAY,
- POOL_INT_ARRAY,
- POOL_STRING_ARRAY,
- POOL_REAL_ARRAY,
- POOL_COLOR_ARRAY,
- POOL_VECTOR2_ARRAY,
- POOL_VECTOR3_ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- // arrays
- case POOL_BYTE_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_INT_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_REAL_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_STRING_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_VECTOR2_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_VECTOR3_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_COLOR_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 && p_type_from != 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,
- REAL,
- //STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case INT: {
- static const Type valid[] = {
- BOOL,
- REAL,
- //STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case REAL: {
- static const Type valid[] = {
- BOOL,
- INT,
- //STRING,
- NIL,
- };
- valid_types = valid;
- } break;
- case STRING: {
- static const Type valid[] = {
- NODE_PATH,
- NIL
- };
- valid_types = valid;
- } break;
- case TRANSFORM2D: {
- static const Type valid[] = {
- TRANSFORM,
- NIL
- };
- valid_types = valid;
- } break;
- case QUAT: {
- static const Type valid[] = {
- BASIS,
- NIL
- };
- valid_types = valid;
- } break;
- case BASIS: {
- static const Type valid[] = {
- QUAT,
- VECTOR3,
- NIL
- };
- valid_types = valid;
- } break;
- case TRANSFORM: {
- static const Type valid[] = {
- TRANSFORM2D,
- QUAT,
- BASIS,
- 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 NODE_PATH: {
- static const Type valid[] = {
- STRING,
- NIL
- };
- valid_types = valid;
- } break;
- case ARRAY: {
- static const Type valid[] = {
- POOL_BYTE_ARRAY,
- POOL_INT_ARRAY,
- POOL_STRING_ARRAY,
- POOL_REAL_ARRAY,
- POOL_COLOR_ARRAY,
- POOL_VECTOR2_ARRAY,
- POOL_VECTOR3_ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- // arrays
- case POOL_BYTE_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_INT_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_REAL_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_STRING_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_VECTOR2_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_VECTOR3_ARRAY: {
- static const Type valid[] = {
- ARRAY,
- NIL
- };
- valid_types = valid;
- } break;
- case POOL_COLOR_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::deep_equal(const Variant &p_variant, int p_recursion_count) const {
- ERR_FAIL_COND_V_MSG(p_recursion_count > MAX_RECURSION, true, "Max recursion reached");
- // Containers must be handled with recursivity checks
- switch (type) {
- case Variant::Type::DICTIONARY: {
- if (p_variant.type != Variant::Type::DICTIONARY) {
- return false;
- }
- const Dictionary v1_as_d = Dictionary(*this);
- const Dictionary v2_as_d = Dictionary(p_variant);
- return v1_as_d.deep_equal(v2_as_d, p_recursion_count + 1);
- } break;
- case Variant::Type::ARRAY: {
- if (p_variant.type != Variant::Type::ARRAY) {
- return false;
- }
- const Array v1_as_a = Array(*this);
- const Array v2_as_a = Array(p_variant);
- return v1_as_a.deep_equal(v2_as_a, p_recursion_count + 1);
- } break;
- default: {
- return *this == p_variant;
- } break;
- }
- }
- bool Variant::operator==(const Variant &p_variant) const {
- if (type != p_variant.type) { //evaluation of operator== needs to be more strict
- return false;
- }
- bool v;
- Variant r;
- evaluate(OP_EQUAL, *this, p_variant, r, v);
- return r;
- }
- bool Variant::operator!=(const Variant &p_variant) const {
- 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;
- } break;
- // atomic types
- case BOOL: {
- return !(_data._bool);
- } break;
- case INT: {
- return _data._int == 0;
- } break;
- case REAL: {
- return _data._real == 0;
- } break;
- case STRING: {
- return *reinterpret_cast<const String *>(_data._mem) == String();
- } break;
- // math types
- case VECTOR2: {
- return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
- } break;
- case RECT2: {
- return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
- } break;
- case TRANSFORM2D: {
- return *_data._transform2d == Transform2D();
- } break;
- case VECTOR3: {
- return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
- } break;
- case PLANE: {
- return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
- } break;
- /*
- case QUAT: {
- } break;*/
- case AABB: {
- return *_data._aabb == ::AABB();
- } break;
- case QUAT: {
- return *reinterpret_cast<const Quat *>(_data._mem) == Quat();
- } break;
- case BASIS: {
- return *_data._basis == Basis();
- } break;
- case TRANSFORM: {
- return *_data._transform == Transform();
- } break;
- // misc types
- case COLOR: {
- return *reinterpret_cast<const Color *>(_data._mem) == Color();
- } break;
- case _RID: {
- return *reinterpret_cast<const RID *>(_data._mem) == RID();
- } break;
- case OBJECT: {
- return _UNSAFE_OBJ_PROXY_PTR(*this) == nullptr;
- } break;
- case NODE_PATH: {
- return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
- } break;
- case DICTIONARY: {
- return reinterpret_cast<const Dictionary *>(_data._mem)->empty();
- } break;
- case ARRAY: {
- return reinterpret_cast<const Array *>(_data._mem)->empty();
- } break;
- // arrays
- case POOL_BYTE_ARRAY: {
- return reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem)->size() == 0;
- } break;
- case POOL_INT_ARRAY: {
- return reinterpret_cast<const PoolVector<int> *>(_data._mem)->size() == 0;
- } break;
- case POOL_REAL_ARRAY: {
- return reinterpret_cast<const PoolVector<real_t> *>(_data._mem)->size() == 0;
- } break;
- case POOL_STRING_ARRAY: {
- return reinterpret_cast<const PoolVector<String> *>(_data._mem)->size() == 0;
- } break;
- case POOL_VECTOR2_ARRAY: {
- return reinterpret_cast<const PoolVector<Vector2> *>(_data._mem)->size() == 0;
- } break;
- case POOL_VECTOR3_ARRAY: {
- return reinterpret_cast<const PoolVector<Vector3> *>(_data._mem)->size() == 0;
- } break;
- case POOL_COLOR_ARRAY: {
- return reinterpret_cast<const PoolVector<Color> *>(_data._mem)->size() == 0;
- } break;
- default: {
- }
- }
- return false;
- }
- bool Variant::is_one() const {
- switch (type) {
- case NIL: {
- return true;
- } break;
- // atomic types
- case BOOL: {
- return _data._bool;
- } break;
- case INT: {
- return _data._int == 1;
- } break;
- case REAL: {
- return _data._real == 1;
- } break;
- case VECTOR2: {
- return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
- } break;
- case RECT2: {
- return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
- } break;
- case VECTOR3: {
- return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
- } break;
- case PLANE: {
- return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
- } break;
- case COLOR: {
- return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
- } break;
- default: {
- return !is_zero();
- }
- }
- return false;
- }
- ObjectID Variant::get_object_instance_id() const {
- if (unlikely(type != OBJECT)) {
- return 0;
- } else if (likely(_get_obj().rc)) {
- return _get_obj().rc->instance_id;
- } else if (likely(!_get_obj().ref.is_null())) {
- return _REF_OBJ_PTR(*this)->get_instance_id();
- } else {
- return 0;
- }
- }
- bool Variant::is_invalid_object() const {
- return type == OBJECT && _get_obj().rc && !_get_obj().rc->get_ptr();
- }
- void Variant::reference(const Variant &p_variant) {
- switch (type) {
- case NIL:
- case BOOL:
- case INT:
- case REAL:
- break;
- default:
- 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 REAL: {
- _data._real = p_variant._data._real;
- } 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 RECT2: {
- memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
- } break;
- case TRANSFORM2D: {
- _data._transform2d = memnew(Transform2D(*p_variant._data._transform2d));
- } break;
- case VECTOR3: {
- memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(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 = memnew(::AABB(*p_variant._data._aabb));
- } break;
- case QUAT: {
- memnew_placement(_data._mem, Quat(*reinterpret_cast<const Quat *>(p_variant._data._mem)));
- } break;
- case BASIS: {
- _data._basis = memnew(Basis(*p_variant._data._basis));
- } break;
- case TRANSFORM: {
- _data._transform = memnew(Transform(*p_variant._data._transform));
- } break;
- // misc 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(p_variant._get_obj()));
- if (likely(_get_obj().rc)) {
- _get_obj().rc->increment();
- }
- } 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 POOL_BYTE_ARRAY: {
- memnew_placement(_data._mem, PoolVector<uint8_t>(*reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem)));
- } break;
- case POOL_INT_ARRAY: {
- memnew_placement(_data._mem, PoolVector<int>(*reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem)));
- } break;
- case POOL_REAL_ARRAY: {
- memnew_placement(_data._mem, PoolVector<real_t>(*reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem)));
- } break;
- case POOL_STRING_ARRAY: {
- memnew_placement(_data._mem, PoolVector<String>(*reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem)));
- } break;
- case POOL_VECTOR2_ARRAY: {
- memnew_placement(_data._mem, PoolVector<Vector2>(*reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem)));
- } break;
- case POOL_VECTOR3_ARRAY: {
- memnew_placement(_data._mem, PoolVector<Vector3>(*reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem)));
- } break;
- case POOL_COLOR_ARRAY: {
- memnew_placement(_data._mem, PoolVector<Color>(*reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem)));
- } break;
- default: {
- }
- }
- }
- void Variant::zero() {
- switch (type) {
- case NIL:
- break;
- case BOOL:
- this->_data._bool = false;
- break;
- case INT:
- this->_data._int = 0;
- break;
- case REAL:
- this->_data._real = 0;
- break;
- case VECTOR2:
- *reinterpret_cast<Vector2 *>(this->_data._mem) = Vector2();
- break;
- case RECT2:
- *reinterpret_cast<Rect2 *>(this->_data._mem) = Rect2();
- break;
- case VECTOR3:
- *reinterpret_cast<Vector3 *>(this->_data._mem) = Vector3();
- break;
- case PLANE:
- *reinterpret_cast<Plane *>(this->_data._mem) = Plane();
- break;
- case QUAT:
- *reinterpret_cast<Quat *>(this->_data._mem) = Quat();
- break;
- case COLOR:
- *reinterpret_cast<Color *>(this->_data._mem) = Color();
- break;
- default:
- this->clear();
- break;
- }
- }
- void Variant::clear() {
- switch (type) {
- case STRING: {
- reinterpret_cast<String *>(_data._mem)->~String();
- } break;
- /*
- // no point, they don't allocate memory
- VECTOR3,
- PLANE,
- QUAT,
- COLOR,
- VECTOR2,
- RECT2
- */
- case TRANSFORM2D: {
- memdelete(_data._transform2d);
- } break;
- case AABB: {
- memdelete(_data._aabb);
- } break;
- case BASIS: {
- memdelete(_data._basis);
- } break;
- case TRANSFORM: {
- memdelete(_data._transform);
- } break;
- // misc types
- case NODE_PATH: {
- reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
- } break;
- case OBJECT: {
- if (likely(_get_obj().rc)) {
- if (unlikely(_get_obj().rc->decrement())) {
- memdelete(_get_obj().rc);
- }
- } else {
- _get_obj().ref.unref();
- }
- } break;
- case _RID: {
- // not much need probably
- reinterpret_cast<RID *>(_data._mem)->~RID();
- } break;
- case DICTIONARY: {
- reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
- } break;
- case ARRAY: {
- reinterpret_cast<Array *>(_data._mem)->~Array();
- } break;
- // arrays
- case POOL_BYTE_ARRAY: {
- reinterpret_cast<PoolVector<uint8_t> *>(_data._mem)->~PoolVector<uint8_t>();
- } break;
- case POOL_INT_ARRAY: {
- reinterpret_cast<PoolVector<int> *>(_data._mem)->~PoolVector<int>();
- } break;
- case POOL_REAL_ARRAY: {
- reinterpret_cast<PoolVector<real_t> *>(_data._mem)->~PoolVector<real_t>();
- } break;
- case POOL_STRING_ARRAY: {
- reinterpret_cast<PoolVector<String> *>(_data._mem)->~PoolVector<String>();
- } break;
- case POOL_VECTOR2_ARRAY: {
- reinterpret_cast<PoolVector<Vector2> *>(_data._mem)->~PoolVector<Vector2>();
- } break;
- case POOL_VECTOR3_ARRAY: {
- reinterpret_cast<PoolVector<Vector3> *>(_data._mem)->~PoolVector<Vector3>();
- } break;
- case POOL_COLOR_ARRAY: {
- reinterpret_cast<PoolVector<Color> *>(_data._mem)->~PoolVector<Color>();
- } break;
- default: {
- } /* not needed */
- }
- type = NIL;
- }
- Variant::operator signed int() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator unsigned int() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator int64_t() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int64();
- default: {
- return 0;
- }
- }
- }
- /*
- Variant::operator long unsigned int() const {
- switch( type ) {
- case NIL: return 0;
- case BOOL: return _data._bool ? 1 : 0;
- case INT: return _data._int;
- case REAL: return _data._real;
- case STRING: return operator String().to_int();
- default: {
- return 0;
- }
- }
- 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 REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- #ifdef NEED_LONG_INT
- Variant::operator signed long() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- return 0;
- };
- Variant::operator unsigned long() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- return 0;
- };
- #endif
- Variant::operator signed short() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator unsigned short() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator signed char() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator unsigned char() const {
- switch (type) {
- case NIL:
- return 0;
- case BOOL:
- return _data._bool ? 1 : 0;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return operator String().to_int();
- default: {
- return 0;
- }
- }
- }
- Variant::operator CharType() const {
- return operator unsigned int();
- }
- 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 REAL:
- return _data._real;
- case STRING:
- return operator String().to_double();
- 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 REAL:
- return _data._real;
- case STRING:
- return operator String().to_double();
- default: {
- return 0;
- }
- }
- }
- Variant::operator StringName() const {
- if (type == NODE_PATH) {
- return reinterpret_cast<const NodePath *>(_data._mem)->get_sname();
- }
- return StringName(operator String());
- }
- struct _VariantStrPair {
- String key;
- String value;
- bool operator<(const _VariantStrPair &p) const {
- return key < p.key;
- }
- };
- Variant::operator String() const {
- List<const void *> stack;
- return stringify(stack);
- }
- template <class T>
- String stringify_vector(const T &vec, List<const void *> &stack) {
- String str("[");
- for (int i = 0; i < vec.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str = str + Variant(vec[i]).stringify(stack);
- }
- str += "]";
- return str;
- }
- String Variant::stringify(List<const void *> &stack) const {
- switch (type) {
- case NIL:
- return "Null";
- case BOOL:
- return _data._bool ? "True" : "False";
- case INT:
- return itos(_data._int);
- case REAL:
- return rtos(_data._real);
- case STRING:
- return *reinterpret_cast<const String *>(_data._mem);
- case VECTOR2:
- return "(" + operator Vector2() + ")";
- case RECT2:
- return "(" + operator Rect2() + ")";
- case TRANSFORM2D: {
- Transform2D mat32 = operator Transform2D();
- return "(" + Variant(mat32.elements[0]).operator String() + ", " + Variant(mat32.elements[1]).operator String() + ", " + Variant(mat32.elements[2]).operator String() + ")";
- } break;
- case VECTOR3:
- return "(" + operator Vector3() + ")";
- case PLANE:
- return operator Plane();
- //case QUAT:
- case AABB:
- return operator ::AABB();
- case QUAT:
- return "(" + operator Quat() + ")";
- case BASIS: {
- Basis mat3 = operator Basis();
- String mtx("(");
- for (int i = 0; i < 3; i++) {
- if (i != 0) {
- mtx += ", ";
- }
- mtx += "(";
- for (int j = 0; j < 3; j++) {
- if (j != 0) {
- mtx += ", ";
- }
- mtx += Variant(mat3.elements[i][j]).operator String();
- }
- mtx += ")";
- }
- return mtx + ")";
- } break;
- case TRANSFORM:
- return operator Transform();
- case NODE_PATH:
- return operator NodePath();
- case COLOR:
- return String::num(operator Color().r) + "," + String::num(operator Color().g) + "," + String::num(operator Color().b) + "," + String::num(operator Color().a);
- case DICTIONARY: {
- const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
- if (stack.find(d.id())) {
- return "{...}";
- }
- stack.push_back(d.id());
- //const String *K=NULL;
- 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 = E->get().stringify(stack);
- sp.value = d[E->get()].stringify(stack);
- pairs.push_back(sp);
- }
- pairs.sort();
- for (int i = 0; i < pairs.size(); i++) {
- if (i > 0) {
- str += ", ";
- }
- str += pairs[i].key + ":" + pairs[i].value;
- }
- str += "}";
- stack.erase(d.id());
- return str;
- } break;
- case POOL_VECTOR2_ARRAY: {
- return stringify_vector(operator PoolVector<Vector2>(), stack);
- } break;
- case POOL_VECTOR3_ARRAY: {
- return stringify_vector(operator PoolVector<Vector3>(), stack);
- } break;
- case POOL_COLOR_ARRAY: {
- return stringify_vector(operator PoolVector<Color>(), stack);
- } break;
- case POOL_STRING_ARRAY: {
- return stringify_vector(operator PoolVector<String>(), stack);
- } break;
- case POOL_BYTE_ARRAY: {
- return stringify_vector(operator PoolVector<uint8_t>(), stack);
- } break;
- case POOL_INT_ARRAY: {
- return stringify_vector(operator PoolVector<int>(), stack);
- } break;
- case POOL_REAL_ARRAY: {
- return stringify_vector(operator PoolVector<real_t>(), stack);
- } break;
- case ARRAY: {
- Array arr = operator Array();
- if (stack.find(arr.id())) {
- return "[...]";
- }
- stack.push_back(arr.id());
- String str = stringify_vector(arr, stack);
- stack.erase(arr.id());
- return str;
- } break;
- case OBJECT: {
- Object *obj = _OBJ_PTR(*this);
- if (likely(obj)) {
- return obj->to_string();
- } else {
- if (_get_obj().rc) {
- return "[Deleted Object]";
- }
- return "[Object:null]";
- }
- } break;
- default: {
- return "[" + get_type_name(type) + "]";
- }
- }
- return "";
- }
- Variant::operator Vector2() const {
- 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 {
- return Vector2();
- }
- }
- Variant::operator Rect2() const {
- if (type == RECT2) {
- return *reinterpret_cast<const Rect2 *>(_data._mem);
- } else {
- return Rect2();
- }
- }
- Variant::operator Vector3() const {
- if (type == VECTOR3) {
- return *reinterpret_cast<const Vector3 *>(_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 {
- return Vector3();
- }
- }
- 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 == QUAT) {
- return *reinterpret_cast<const Quat *>(_data._mem);
- } else if (type == VECTOR3) {
- return Basis(*reinterpret_cast<const Vector3 *>(_data._mem));
- } else if (type == TRANSFORM) { // unexposed in Variant::can_convert?
- return _data._transform->basis;
- } else {
- return Basis();
- }
- }
- Variant::operator Quat() const {
- if (type == QUAT) {
- return *reinterpret_cast<const Quat *>(_data._mem);
- } else if (type == BASIS) {
- return *_data._basis;
- } else if (type == TRANSFORM) {
- return _data._transform->basis;
- } else {
- return Quat();
- }
- }
- Variant::operator Transform() const {
- if (type == TRANSFORM) {
- return *_data._transform;
- } else if (type == BASIS) {
- return Transform(*_data._basis, Vector3());
- } else if (type == QUAT) {
- return Transform(Basis(*reinterpret_cast<const Quat *>(_data._mem)), Vector3());
- } else if (type == TRANSFORM2D) {
- const Transform2D &t = *_data._transform2d;
- Transform m;
- m.basis.elements[0][0] = t.elements[0][0];
- m.basis.elements[1][0] = t.elements[0][1];
- m.basis.elements[0][1] = t.elements[1][0];
- m.basis.elements[1][1] = t.elements[1][1];
- m.origin[0] = t.elements[2][0];
- m.origin[1] = t.elements[2][1];
- return m;
- } else {
- return Transform();
- }
- }
- Variant::operator Transform2D() const {
- if (type == TRANSFORM2D) {
- return *_data._transform2d;
- } else if (type == TRANSFORM) {
- const Transform &t = *_data._transform;
- Transform2D m;
- m.elements[0][0] = t.basis.elements[0][0];
- m.elements[0][1] = t.basis.elements[1][0];
- m.elements[1][0] = t.basis.elements[0][1];
- m.elements[1][1] = t.basis.elements[1][1];
- m.elements[2][0] = t.origin[0];
- m.elements[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::html(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 RefPtr() const {
- if (type == OBJECT) {
- return _get_obj().ref;
- } else {
- return RefPtr();
- }
- }
- Variant::operator RID() const {
- if (type == _RID) {
- return *reinterpret_cast<const RID *>(_data._mem);
- } else if (type == OBJECT) {
- if (!_get_obj().ref.is_null()) {
- return _get_obj().ref.get_rid();
- } else {
- Object *obj = likely(_get_obj().rc) ? _get_obj().rc->get_ptr() : nullptr;
- if (unlikely(!obj)) {
- if (_get_obj().rc) {
- ERR_PRINT("Attempted get RID on a deleted object.");
- }
- return RID();
- }
- Variant::CallError ce;
- Variant ret = obj->call(CoreStringNames::get_singleton()->get_rid, nullptr, 0, ce);
- if (ce.error == Variant::CallError::CALL_OK && ret.get_type() == Variant::_RID) {
- return ret;
- } else {
- return RID();
- }
- }
- } else {
- return RID();
- }
- }
- Variant::operator Object *() const {
- if (type == OBJECT) {
- return _OBJ_PTR(*this);
- } else {
- return nullptr;
- }
- }
- Variant::operator Node *() const {
- if (type == OBJECT) {
- Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
- return Object::cast_to<Node>(obj);
- }
- return nullptr;
- }
- Variant::operator Control *() const {
- if (type == OBJECT) {
- Object *obj = _get_obj().rc ? _get_obj().rc->get_ptr() : nullptr;
- return Object::cast_to<Control>(obj);
- }
- return nullptr;
- }
- Variant::operator Dictionary() const {
- if (type == DICTIONARY) {
- return *reinterpret_cast<const Dictionary *>(_data._mem);
- } else {
- return Dictionary();
- }
- }
- template <class DA, class 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 <class 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::POOL_BYTE_ARRAY: {
- return _convert_array<DA, PoolVector<uint8_t>>(p_variant.operator PoolVector<uint8_t>());
- }
- case Variant::POOL_INT_ARRAY: {
- return _convert_array<DA, PoolVector<int>>(p_variant.operator PoolVector<int>());
- }
- case Variant::POOL_REAL_ARRAY: {
- return _convert_array<DA, PoolVector<real_t>>(p_variant.operator PoolVector<real_t>());
- }
- case Variant::POOL_STRING_ARRAY: {
- return _convert_array<DA, PoolVector<String>>(p_variant.operator PoolVector<String>());
- }
- case Variant::POOL_VECTOR2_ARRAY: {
- return _convert_array<DA, PoolVector<Vector2>>(p_variant.operator PoolVector<Vector2>());
- }
- case Variant::POOL_VECTOR3_ARRAY: {
- return _convert_array<DA, PoolVector<Vector3>>(p_variant.operator PoolVector<Vector3>());
- }
- case Variant::POOL_COLOR_ARRAY: {
- return _convert_array<DA, PoolVector<Color>>(p_variant.operator PoolVector<Color>());
- }
- 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 PoolVector<uint8_t>() const {
- if (type == POOL_BYTE_ARRAY) {
- return *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<uint8_t>>(*this);
- }
- }
- Variant::operator PoolVector<int>() const {
- if (type == POOL_INT_ARRAY) {
- return *reinterpret_cast<const PoolVector<int> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<int>>(*this);
- }
- }
- Variant::operator PoolVector<real_t>() const {
- if (type == POOL_REAL_ARRAY) {
- return *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<real_t>>(*this);
- }
- }
- Variant::operator PoolVector<String>() const {
- if (type == POOL_STRING_ARRAY) {
- return *reinterpret_cast<const PoolVector<String> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<String>>(*this);
- }
- }
- Variant::operator PoolVector<Vector3>() const {
- if (type == POOL_VECTOR3_ARRAY) {
- return *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<Vector3>>(*this);
- }
- }
- Variant::operator PoolVector<Vector2>() const {
- if (type == POOL_VECTOR2_ARRAY) {
- return *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<Vector2>>(*this);
- }
- }
- Variant::operator PoolVector<Color>() const {
- if (type == POOL_COLOR_ARRAY) {
- return *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
- } else {
- return _convert_array_from_variant<PoolVector<Color>>(*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<Vector2>() const {
- PoolVector<Vector2> from = operator PoolVector<Vector2>();
- Vector<Vector2> to;
- int len = from.size();
- if (len == 0) {
- return Vector<Vector2>();
- }
- to.resize(len);
- PoolVector<Vector2>::Read r = from.read();
- Vector2 *w = to.ptrw();
- for (int i = 0; i < len; i++) {
- w[i] = r[i];
- }
- return to;
- }
- Variant::operator PoolVector<Plane>() const {
- Array va = operator Array();
- PoolVector<Plane> planes;
- int va_size = va.size();
- if (va_size == 0) {
- return planes;
- }
- planes.resize(va_size);
- PoolVector<Plane>::Write w = planes.write();
- for (int i = 0; i < va_size; i++) {
- w[i] = va[i];
- }
- return planes;
- }
- Variant::operator PoolVector<Face3>() const {
- PoolVector<Vector3> va = operator PoolVector<Vector3>();
- PoolVector<Face3> faces;
- int va_size = va.size();
- if (va_size == 0) {
- return faces;
- }
- faces.resize(va_size / 3);
- PoolVector<Face3>::Write w = faces.write();
- PoolVector<Vector3>::Read r = va.read();
- for (int i = 0; i < va_size; i++) {
- w[i / 3].vertex[i % 3] = r[i];
- }
- return faces;
- }
- 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);
- for (int i = 0; i < va_size; i++) {
- planes.write[i] = va[i];
- }
- return planes;
- }
- Variant::operator Vector<Variant>() const {
- Array from = operator Array();
- Vector<Variant> to;
- int len = from.size();
- to.resize(len);
- for (int i = 0; i < len; i++) {
- to.write[i] = from[i];
- }
- return to;
- }
- Variant::operator Vector<uint8_t>() const {
- PoolVector<uint8_t> from = operator PoolVector<uint8_t>();
- Vector<uint8_t> to;
- int len = from.size();
- to.resize(len);
- for (int i = 0; i < len; i++) {
- to.write[i] = from[i];
- }
- return to;
- }
- Variant::operator Vector<int>() const {
- PoolVector<int> from = operator PoolVector<int>();
- Vector<int> to;
- int len = from.size();
- to.resize(len);
- for (int i = 0; i < len; i++) {
- to.write[i] = from[i];
- }
- return to;
- }
- Variant::operator Vector<real_t>() const {
- PoolVector<real_t> from = operator PoolVector<real_t>();
- Vector<real_t> to;
- int len = from.size();
- to.resize(len);
- for (int i = 0; i < len; i++) {
- to.write[i] = from[i];
- }
- return to;
- }
- Variant::operator Vector<String>() const {
- PoolVector<String> from = operator PoolVector<String>();
- Vector<String> to;
- int len = from.size();
- to.resize(len);
- for (int i = 0; i < len; i++) {
- to.write[i] = from[i];
- }
- return to;
- }
- Variant::operator Vector<StringName>() const {
- PoolVector<String> from = operator PoolVector<String>();
- 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 Vector<Vector3>() const {
- PoolVector<Vector3> from = operator PoolVector<Vector3>();
- Vector<Vector3> to;
- int len = from.size();
- if (len == 0) {
- return Vector<Vector3>();
- }
- to.resize(len);
- PoolVector<Vector3>::Read r = from.read();
- Vector3 *w = to.ptrw();
- for (int i = 0; i < len; i++) {
- w[i] = r[i];
- }
- return to;
- }
- Variant::operator Vector<Color>() const {
- PoolVector<Color> from = operator PoolVector<Color>();
- Vector<Color> to;
- int len = from.size();
- if (len == 0) {
- return Vector<Color>();
- }
- to.resize(len);
- PoolVector<Color>::Read r = from.read();
- Color *w = to.ptrw();
- for (int i = 0; i < len; i++) {
- w[i] = r[i];
- }
- return to;
- }
- Variant::operator Margin() const {
- return (Margin) operator int();
- }
- Variant::operator Orientation() const {
- return (Orientation) operator int();
- }
- Variant::operator IP_Address() const {
- if (type == POOL_REAL_ARRAY || type == POOL_INT_ARRAY || type == POOL_BYTE_ARRAY) {
- PoolVector<int> addr = operator PoolVector<int>();
- if (addr.size() == 4) {
- return IP_Address(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
- }
- }
- return IP_Address(operator String());
- }
- Variant::Variant(bool p_bool) {
- type = BOOL;
- _data._bool = p_bool;
- }
- /*
- Variant::Variant(long unsigned int p_long) {
- type=INT;
- _data._int=p_long;
- };
- */
- Variant::Variant(signed int p_int) {
- type = INT;
- _data._int = p_int;
- }
- Variant::Variant(unsigned int p_int) {
- type = INT;
- _data._int = p_int;
- }
- #ifdef NEED_LONG_INT
- Variant::Variant(signed long p_int) {
- type = INT;
- _data._int = p_int;
- }
- Variant::Variant(unsigned long p_int) {
- type = INT;
- _data._int = p_int;
- }
- #endif
- Variant::Variant(int64_t p_int) {
- type = INT;
- _data._int = p_int;
- }
- Variant::Variant(uint64_t p_int) {
- type = INT;
- _data._int = p_int;
- }
- Variant::Variant(signed short p_short) {
- type = INT;
- _data._int = p_short;
- }
- Variant::Variant(unsigned short p_short) {
- type = INT;
- _data._int = p_short;
- }
- Variant::Variant(signed char p_char) {
- type = INT;
- _data._int = p_char;
- }
- Variant::Variant(unsigned char p_char) {
- type = INT;
- _data._int = p_char;
- }
- Variant::Variant(float p_float) {
- type = REAL;
- _data._real = p_float;
- }
- Variant::Variant(double p_double) {
- type = REAL;
- _data._real = p_double;
- }
- Variant::Variant(const StringName &p_string) {
- type = STRING;
- memnew_placement(_data._mem, String(p_string.operator 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 CharType *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 Vector2 &p_vector2) {
- type = VECTOR2;
- memnew_placement(_data._mem, Vector2(p_vector2));
- }
- Variant::Variant(const Rect2 &p_rect2) {
- type = RECT2;
- memnew_placement(_data._mem, Rect2(p_rect2));
- }
- 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 = memnew(::AABB(p_aabb));
- }
- Variant::Variant(const Basis &p_matrix) {
- type = BASIS;
- _data._basis = memnew(Basis(p_matrix));
- }
- Variant::Variant(const Quat &p_quat) {
- type = QUAT;
- memnew_placement(_data._mem, Quat(p_quat));
- }
- Variant::Variant(const Transform &p_transform) {
- type = TRANSFORM;
- _data._transform = memnew(Transform(p_transform));
- }
- Variant::Variant(const Transform2D &p_transform) {
- type = TRANSFORM2D;
- _data._transform2d = memnew(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 RefPtr &p_resource) {
- type = OBJECT;
- memnew_placement(_data._mem, ObjData);
- _get_obj().rc = nullptr;
- _get_obj().ref = p_resource;
- }
- Variant::Variant(const RID &p_rid) {
- type = _RID;
- memnew_placement(_data._mem, RID(p_rid));
- }
- Variant::Variant(const Object *p_object) {
- type = OBJECT;
- Object *obj = const_cast<Object *>(p_object);
- memnew_placement(_data._mem, ObjData);
- Reference *ref = Object::cast_to<Reference>(obj);
- if (unlikely(ref)) {
- *reinterpret_cast<Ref<Reference> *>(_get_obj().ref.get_data()) = Ref<Reference>(ref);
- _get_obj().rc = nullptr;
- } else {
- _get_obj().rc = likely(obj) ? obj->_use_rc() : nullptr;
- }
- }
- 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 PoolVector<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<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<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<Vector2> &p_array) {
- type = NIL;
- PoolVector<Vector2> v;
- int len = p_array.size();
- if (len > 0) {
- v.resize(len);
- PoolVector<Vector2>::Write w = v.write();
- const Vector2 *r = p_array.ptr();
- for (int i = 0; i < len; i++) {
- w[i] = r[i];
- }
- }
- *this = v;
- }
- Variant::Variant(const PoolVector<uint8_t> &p_raw_array) {
- type = POOL_BYTE_ARRAY;
- memnew_placement(_data._mem, PoolVector<uint8_t>(p_raw_array));
- }
- Variant::Variant(const PoolVector<int> &p_int_array) {
- type = POOL_INT_ARRAY;
- memnew_placement(_data._mem, PoolVector<int>(p_int_array));
- }
- Variant::Variant(const PoolVector<real_t> &p_real_array) {
- type = POOL_REAL_ARRAY;
- memnew_placement(_data._mem, PoolVector<real_t>(p_real_array));
- }
- Variant::Variant(const PoolVector<String> &p_string_array) {
- type = POOL_STRING_ARRAY;
- memnew_placement(_data._mem, PoolVector<String>(p_string_array));
- }
- Variant::Variant(const PoolVector<Vector3> &p_vector3_array) {
- type = POOL_VECTOR3_ARRAY;
- memnew_placement(_data._mem, PoolVector<Vector3>(p_vector3_array));
- }
- Variant::Variant(const PoolVector<Vector2> &p_vector2_array) {
- type = POOL_VECTOR2_ARRAY;
- memnew_placement(_data._mem, PoolVector<Vector2>(p_vector2_array));
- }
- Variant::Variant(const PoolVector<Color> &p_color_array) {
- type = POOL_COLOR_ARRAY;
- memnew_placement(_data._mem, PoolVector<Color>(p_color_array));
- }
- Variant::Variant(const PoolVector<Face3> &p_face_array) {
- PoolVector<Vector3> vertices;
- int face_count = p_face_array.size();
- vertices.resize(face_count * 3);
- if (face_count) {
- PoolVector<Face3>::Read r = p_face_array.read();
- PoolVector<Vector3>::Write w = vertices.write();
- for (int i = 0; i < face_count; i++) {
- for (int j = 0; j < 3; j++) {
- w[i * 3 + j] = r[i].vertex[j];
- }
- }
- }
- type = NIL;
- *this = vertices;
- }
- /* helpers */
- Variant::Variant(const Vector<Variant> &p_array) {
- type = NIL;
- Array v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- Variant::Variant(const Vector<uint8_t> &p_array) {
- type = NIL;
- PoolVector<uint8_t> v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- Variant::Variant(const Vector<int> &p_array) {
- type = NIL;
- PoolVector<int> v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- Variant::Variant(const Vector<real_t> &p_array) {
- type = NIL;
- PoolVector<real_t> v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- Variant::Variant(const Vector<String> &p_array) {
- type = NIL;
- PoolVector<String> v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- Variant::Variant(const Vector<StringName> &p_array) {
- type = NIL;
- PoolVector<String> v;
- int len = p_array.size();
- v.resize(len);
- for (int i = 0; i < len; i++) {
- v.set(i, p_array[i]);
- }
- *this = v;
- }
- Variant::Variant(const Vector<Vector3> &p_array) {
- type = NIL;
- PoolVector<Vector3> v;
- int len = p_array.size();
- if (len > 0) {
- v.resize(len);
- PoolVector<Vector3>::Write w = v.write();
- const Vector3 *r = p_array.ptr();
- for (int i = 0; i < len; i++) {
- w[i] = r[i];
- }
- }
- *this = v;
- }
- Variant::Variant(const Vector<Color> &p_array) {
- type = NIL;
- PoolVector<Color> 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 REAL: {
- _data._real = p_variant._data._real;
- } 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 RECT2: {
- *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(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 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 QUAT: {
- *reinterpret_cast<Quat *>(_data._mem) = *reinterpret_cast<const Quat *>(p_variant._data._mem);
- } break;
- case BASIS: {
- *_data._basis = *(p_variant._data._basis);
- } break;
- case TRANSFORM: {
- *_data._transform = *(p_variant._data._transform);
- } 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: {
- if (likely(_get_obj().rc)) {
- if (unlikely(_get_obj().rc->decrement())) {
- memdelete(_get_obj().rc);
- }
- }
- *reinterpret_cast<ObjData *>(_data._mem) = p_variant._get_obj();
- if (likely(_get_obj().rc)) {
- _get_obj().rc->increment();
- }
- } 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 POOL_BYTE_ARRAY: {
- *reinterpret_cast<PoolVector<uint8_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<uint8_t> *>(p_variant._data._mem);
- } break;
- case POOL_INT_ARRAY: {
- *reinterpret_cast<PoolVector<int> *>(_data._mem) = *reinterpret_cast<const PoolVector<int> *>(p_variant._data._mem);
- } break;
- case POOL_REAL_ARRAY: {
- *reinterpret_cast<PoolVector<real_t> *>(_data._mem) = *reinterpret_cast<const PoolVector<real_t> *>(p_variant._data._mem);
- } break;
- case POOL_STRING_ARRAY: {
- *reinterpret_cast<PoolVector<String> *>(_data._mem) = *reinterpret_cast<const PoolVector<String> *>(p_variant._data._mem);
- } break;
- case POOL_VECTOR2_ARRAY: {
- *reinterpret_cast<PoolVector<Vector2> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector2> *>(p_variant._data._mem);
- } break;
- case POOL_VECTOR3_ARRAY: {
- *reinterpret_cast<PoolVector<Vector3> *>(_data._mem) = *reinterpret_cast<const PoolVector<Vector3> *>(p_variant._data._mem);
- } break;
- case POOL_COLOR_ARRAY: {
- *reinterpret_cast<PoolVector<Color> *>(_data._mem) = *reinterpret_cast<const PoolVector<Color> *>(p_variant._data._mem);
- } break;
- default: {
- }
- }
- }
- Variant::Variant(const IP_Address &p_address) {
- type = STRING;
- memnew_placement(_data._mem, String(p_address));
- }
- Variant::Variant(const Variant &p_variant) {
- type = NIL;
- reference(p_variant);
- }
- /*
- Variant::~Variant() {
- clear();
- }*/
- uint32_t Variant::hash() const {
- return recursive_hash(0);
- }
- uint32_t Variant::recursive_hash(int p_recursion_count) const {
- switch (type) {
- case NIL: {
- return 0;
- } break;
- case BOOL: {
- return _data._bool ? 1 : 0;
- } break;
- case INT: {
- return _data._int;
- } break;
- case REAL: {
- return hash_djb2_one_float(_data._real);
- } break;
- case STRING: {
- return reinterpret_cast<const String *>(_data._mem)->hash();
- } break;
- // math types
- case VECTOR2: {
- uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->x);
- return hash_djb2_one_float(reinterpret_cast<const Vector2 *>(_data._mem)->y, hash);
- } break;
- case RECT2: {
- uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.x);
- hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->position.y, hash);
- hash = hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.x, hash);
- return hash_djb2_one_float(reinterpret_cast<const Rect2 *>(_data._mem)->size.y, hash);
- } break;
- case TRANSFORM2D: {
- uint32_t hash = 5831;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 2; j++) {
- hash = hash_djb2_one_float(_data._transform2d->elements[i][j], hash);
- }
- }
- return hash;
- } break;
- case VECTOR3: {
- uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->x);
- hash = hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->y, hash);
- return hash_djb2_one_float(reinterpret_cast<const Vector3 *>(_data._mem)->z, hash);
- } break;
- case PLANE: {
- uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.x);
- hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.y, hash);
- hash = hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->normal.z, hash);
- return hash_djb2_one_float(reinterpret_cast<const Plane *>(_data._mem)->d, hash);
- } break;
- /*
- case QUAT: {
- } break;*/
- case AABB: {
- uint32_t hash = 5831;
- for (int i = 0; i < 3; i++) {
- hash = hash_djb2_one_float(_data._aabb->position[i], hash);
- hash = hash_djb2_one_float(_data._aabb->size[i], hash);
- }
- return hash;
- } break;
- case QUAT: {
- uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->x);
- hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->y, hash);
- hash = hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->z, hash);
- return hash_djb2_one_float(reinterpret_cast<const Quat *>(_data._mem)->w, hash);
- } break;
- case BASIS: {
- uint32_t hash = 5831;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- hash = hash_djb2_one_float(_data._basis->elements[i][j], hash);
- }
- }
- return hash;
- } break;
- case TRANSFORM: {
- uint32_t hash = 5831;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- hash = hash_djb2_one_float(_data._transform->basis.elements[i][j], hash);
- }
- hash = hash_djb2_one_float(_data._transform->origin[i], hash);
- }
- return hash;
- } break;
- // misc types
- case COLOR: {
- uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->r);
- hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->g, hash);
- hash = hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->b, hash);
- return hash_djb2_one_float(reinterpret_cast<const Color *>(_data._mem)->a, hash);
- } break;
- case _RID: {
- return hash_djb2_one_64(reinterpret_cast<const RID *>(_data._mem)->get_id());
- } break;
- case OBJECT: {
- return hash_djb2_one_64(make_uint64_t(_UNSAFE_OBJ_PROXY_PTR(*this)));
- } break;
- case NODE_PATH: {
- return reinterpret_cast<const NodePath *>(_data._mem)->hash();
- } break;
- case DICTIONARY: {
- return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(p_recursion_count);
- } break;
- case ARRAY: {
- return reinterpret_cast<const Array *>(_data._mem)->recursive_hash(p_recursion_count);
- } break;
- case POOL_BYTE_ARRAY: {
- const PoolVector<uint8_t> &arr = *reinterpret_cast<const PoolVector<uint8_t> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<uint8_t>::Read r = arr.read();
- return hash_djb2_buffer((uint8_t *)&r[0], len);
- } else {
- return hash_djb2_one_64(0);
- }
- } break;
- case POOL_INT_ARRAY: {
- const PoolVector<int> &arr = *reinterpret_cast<const PoolVector<int> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<int>::Read r = arr.read();
- return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(int));
- } else {
- return hash_djb2_one_64(0);
- }
- } break;
- case POOL_REAL_ARRAY: {
- const PoolVector<real_t> &arr = *reinterpret_cast<const PoolVector<real_t> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<real_t>::Read r = arr.read();
- return hash_djb2_buffer((uint8_t *)&r[0], len * sizeof(real_t));
- } else {
- return hash_djb2_one_float(0.0);
- }
- } break;
- case POOL_STRING_ARRAY: {
- uint32_t hash = 5831;
- const PoolVector<String> &arr = *reinterpret_cast<const PoolVector<String> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<String>::Read r = arr.read();
- for (int i = 0; i < len; i++) {
- hash = hash_djb2_one_32(r[i].hash(), hash);
- }
- }
- return hash;
- } break;
- case POOL_VECTOR2_ARRAY: {
- uint32_t hash = 5831;
- const PoolVector<Vector2> &arr = *reinterpret_cast<const PoolVector<Vector2> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<Vector2>::Read r = arr.read();
- for (int i = 0; i < len; i++) {
- hash = hash_djb2_one_float(r[i].x, hash);
- hash = hash_djb2_one_float(r[i].y, hash);
- }
- }
- return hash;
- } break;
- case POOL_VECTOR3_ARRAY: {
- uint32_t hash = 5831;
- const PoolVector<Vector3> &arr = *reinterpret_cast<const PoolVector<Vector3> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<Vector3>::Read r = arr.read();
- for (int i = 0; i < len; i++) {
- hash = hash_djb2_one_float(r[i].x, hash);
- hash = hash_djb2_one_float(r[i].y, hash);
- hash = hash_djb2_one_float(r[i].z, hash);
- }
- }
- return hash;
- } break;
- case POOL_COLOR_ARRAY: {
- uint32_t hash = 5831;
- const PoolVector<Color> &arr = *reinterpret_cast<const PoolVector<Color> *>(_data._mem);
- int len = arr.size();
- if (likely(len)) {
- PoolVector<Color>::Read r = arr.read();
- for (int i = 0; i < len; i++) {
- hash = hash_djb2_one_float(r[i].r, hash);
- hash = hash_djb2_one_float(r[i].g, hash);
- hash = hash_djb2_one_float(r[i].b, hash);
- hash = hash_djb2_one_float(r[i].a, hash);
- }
- }
- return hash;
- } break;
- default: {
- }
- }
- return 0;
- }
- #define hash_compare_scalar(p_lhs, p_rhs) \
- (((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
- #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_quat(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_pool_array(p_lhs, p_rhs, p_type, p_compare_func) \
- const PoolVector<p_type> &l = *reinterpret_cast<const PoolVector<p_type> *>(p_lhs); \
- const PoolVector<p_type> &r = *reinterpret_cast<const PoolVector<p_type> *>(p_rhs); \
- \
- if (l.size() != r.size()) \
- return false; \
- \
- PoolVector<p_type>::Read lr = l.read(); \
- PoolVector<p_type>::Read rr = r.read(); \
- \
- 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) const {
- if (type != p_variant.type) {
- return false;
- }
- switch (type) {
- case INT: {
- return _data._int == p_variant._data._int;
- } break;
- case REAL: {
- return hash_compare_scalar(_data._real, p_variant._data._real);
- } break;
- case STRING: {
- return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(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 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 TRANSFORM2D: {
- Transform2D *l = _data._transform2d;
- Transform2D *r = p_variant._data._transform2d;
- for (int i = 0; i < 3; i++) {
- if (!hash_compare_vector2(l->elements[i], r->elements[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 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 QUAT: {
- const Quat *l = reinterpret_cast<const Quat *>(_data._mem);
- const Quat *r = reinterpret_cast<const Quat *>(p_variant._data._mem);
- return hash_compare_quat(*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->elements[i], r->elements[i])) {
- return false;
- }
- }
- return true;
- } break;
- case TRANSFORM: {
- const Transform *l = _data._transform;
- const Transform *r = p_variant._data._transform;
- for (int i = 0; i < 3; i++) {
- if (!hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])) {
- return false;
- }
- }
- return hash_compare_vector3(l->origin, r->origin);
- } 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.size() != r.size()) {
- return false;
- }
- for (int i = 0; i < l.size(); ++i) {
- if (!l[i].hash_compare(r[i])) {
- return false;
- }
- }
- return true;
- } break;
- case POOL_REAL_ARRAY: {
- hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
- } break;
- case POOL_VECTOR2_ARRAY: {
- hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
- } break;
- case POOL_VECTOR3_ARRAY: {
- hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
- } break;
- case POOL_COLOR_ARRAY: {
- hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
- } break;
- default:
- bool v;
- Variant r;
- evaluate(OP_EQUAL, *this, p_variant, r, v);
- return r;
- }
- return false;
- }
- bool Variant::is_ref() const {
- return type == OBJECT && !_get_obj().ref.is_null();
- }
- Vector<Variant> varray() {
- return Vector<Variant>();
- }
- Vector<Variant> varray(const Variant &p_arg1) {
- Vector<Variant> v;
- v.push_back(p_arg1);
- return v;
- }
- Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2) {
- Vector<Variant> v;
- v.push_back(p_arg1);
- v.push_back(p_arg2);
- return v;
- }
- Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3) {
- Vector<Variant> v;
- v.push_back(p_arg1);
- v.push_back(p_arg2);
- v.push_back(p_arg3);
- return v;
- }
- Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4) {
- Vector<Variant> v;
- v.push_back(p_arg1);
- v.push_back(p_arg2);
- v.push_back(p_arg3);
- v.push_back(p_arg4);
- return v;
- }
- Vector<Variant> varray(const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5) {
- Vector<Variant> v;
- v.push_back(p_arg1);
- v.push_back(p_arg2);
- v.push_back(p_arg3);
- v.push_back(p_arg4);
- v.push_back(p_arg5);
- return v;
- }
- void Variant::static_assign(const Variant &p_variant) {
- }
- bool Variant::is_shared() const {
- switch (type) {
- case OBJECT:
- return true;
- case ARRAY:
- return true;
- case DICTIONARY:
- return true;
- default: {
- }
- }
- return false;
- }
- Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) {
- VARIANT_ARGPTRS;
- int argc = 0;
- for (int i = 0; i < VARIANT_ARG_MAX; i++) {
- if (argptr[i]->get_type() == Variant::NIL) {
- break;
- }
- argc++;
- }
- CallError error;
- Variant ret = call(p_method, argptr, argc, error);
- switch (error.error) {
- case CallError::CALL_ERROR_INVALID_ARGUMENT: {
- String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(error.expected) + "'.";
- ERR_PRINT(err.utf8().get_data());
- } break;
- case 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 CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
- String err = "Too many arguments for method '" + p_method + "'";
- ERR_PRINT(err.utf8().get_data());
- } break;
- default: {
- }
- }
- return ret;
- }
- 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(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Variant::CallError &ce) {
- String err_text;
- if (ce.error == Variant::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(ce.expected) + ".";
- } else {
- err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(ce.expected) + ".";
- }
- } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
- err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
- } else if (ce.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
- err_text = "Method expected " + itos(ce.argument) + " arguments, but called with " + itos(p_argcount) + ".";
- } else if (ce.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
- err_text = "Method not found.";
- } else if (ce.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
- err_text = "Instance is null";
- } else if (ce.error == Variant::CallError::CALL_OK) {
- return "Call OK";
- }
- String class_name = p_base->get_class();
- Ref<Script> script = p_base->get_script();
- if (script.is_valid() && script->get_path().is_resource_file()) {
- class_name += "(" + script->get_path().get_file() + ")";
- }
- return "'" + class_name + "::" + String(p_method) + "': " + err_text;
- }
- String vformat(const String &p_text, const Variant &p1, const Variant &p2, const Variant &p3, const Variant &p4, const Variant &p5) {
- Array args;
- if (p1.get_type() != Variant::NIL) {
- args.push_back(p1);
- if (p2.get_type() != Variant::NIL) {
- args.push_back(p2);
- if (p3.get_type() != Variant::NIL) {
- args.push_back(p3);
- if (p4.get_type() != Variant::NIL) {
- args.push_back(p4);
- if (p5.get_type() != Variant::NIL) {
- args.push_back(p5);
- }
- }
- }
- }
- }
- bool error = false;
- String fmt = p_text.sprintf(args, &error);
- ERR_FAIL_COND_V_MSG(error, String(), fmt);
- return fmt;
- }
|