variant.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "variant.h"
  30. #include "resource.h"
  31. #include "print_string.h"
  32. #include "scene/main/node.h"
  33. #include "scene/gui/control.h"
  34. #include "io/marshalls.h"
  35. String Variant::get_type_name(Variant::Type p_type) {
  36. switch( p_type ) {
  37. case NIL: {
  38. return "Nil";
  39. } break;
  40. // atomic types
  41. case BOOL: {
  42. return "bool";
  43. } break;
  44. case INT: {
  45. return "int";
  46. } break;
  47. case REAL: {
  48. return "real";
  49. } break;
  50. case STRING: {
  51. return "String";
  52. } break;
  53. // math types
  54. case VECTOR2: {
  55. return "Vector2";
  56. } break;
  57. case RECT2: {
  58. return "Rect2";
  59. } break;
  60. case MATRIX32: {
  61. return "Matrix32";
  62. } break;
  63. case VECTOR3: {
  64. return "Vector3";
  65. } break;
  66. case PLANE: {
  67. return "Plane";
  68. } break;
  69. /*
  70. case QUAT: {
  71. } break;*/
  72. case _AABB: {
  73. return "AABB";
  74. } break;
  75. case QUAT: {
  76. return "Quat";
  77. } break;
  78. case MATRIX3: {
  79. return "Matrix3";
  80. } break;
  81. case TRANSFORM: {
  82. return "Transform";
  83. } break;
  84. // misc types
  85. case COLOR: {
  86. return "Color";
  87. } break;
  88. case IMAGE: {
  89. return "Image";
  90. } break;
  91. case _RID: {
  92. return "RID";
  93. } break;
  94. case OBJECT: {
  95. return "Object";
  96. } break;
  97. case NODE_PATH: {
  98. return "NodePath";
  99. } break;
  100. case INPUT_EVENT: {
  101. return "InputEvent";
  102. } break;
  103. case DICTIONARY: {
  104. return "Dictionary";
  105. } break;
  106. case ARRAY: {
  107. return "Array";
  108. } break;
  109. // arrays
  110. case RAW_ARRAY: {
  111. return "RawArray";
  112. } break;
  113. case INT_ARRAY: {
  114. return "IntArray";
  115. } break;
  116. case REAL_ARRAY: {
  117. return "RealArray";
  118. } break;
  119. case STRING_ARRAY: {
  120. return "StringArray";
  121. } break;
  122. case VECTOR2_ARRAY: {
  123. return "Vector2Array";
  124. } break;
  125. case VECTOR3_ARRAY: {
  126. return "Vector3Array";
  127. } break;
  128. case COLOR_ARRAY: {
  129. return "ColorArray";
  130. } break;
  131. default: {}
  132. }
  133. return "";
  134. }
  135. bool Variant::can_convert(Variant::Type p_type_from,Variant::Type p_type_to) {
  136. if (p_type_from==p_type_to)
  137. return true;
  138. if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything
  139. return true;
  140. if (p_type_from == NIL) {
  141. return (p_type_to == OBJECT);
  142. };
  143. const Type *valid_types=NULL;
  144. const Type *invalid_types=NULL;
  145. switch(p_type_to) {
  146. case BOOL: {
  147. static const Type valid[]={
  148. INT,
  149. REAL,
  150. STRING,
  151. NIL,
  152. };
  153. valid_types=valid;
  154. } break;
  155. case INT: {
  156. static const Type valid[]={
  157. BOOL,
  158. REAL,
  159. STRING,
  160. NIL,
  161. };
  162. valid_types=valid;
  163. } break;
  164. case REAL: {
  165. static const Type valid[]={
  166. BOOL,
  167. INT,
  168. STRING,
  169. NIL,
  170. };
  171. valid_types=valid;
  172. } break;
  173. case STRING: {
  174. static const Type invalid[]={
  175. OBJECT,
  176. IMAGE,
  177. NIL
  178. };
  179. invalid_types=invalid;
  180. } break;
  181. case MATRIX32: {
  182. static const Type invalid[]={
  183. TRANSFORM,
  184. NIL
  185. };
  186. invalid_types=invalid;
  187. } break;
  188. case QUAT: {
  189. static const Type valid[]={
  190. MATRIX3,
  191. NIL
  192. };
  193. valid_types=valid;
  194. } break;
  195. case MATRIX3: {
  196. static const Type valid[]={
  197. QUAT,
  198. NIL
  199. };
  200. valid_types=valid;
  201. } break;
  202. case TRANSFORM: {
  203. static const Type valid[]={
  204. MATRIX32,
  205. QUAT,
  206. MATRIX3,
  207. NIL
  208. };
  209. valid_types=valid;
  210. } break;
  211. case _RID: {
  212. static const Type valid[]={
  213. OBJECT,
  214. NIL
  215. };
  216. valid_types=valid;
  217. } break;
  218. case OBJECT: {
  219. static const Type valid[]={
  220. NIL
  221. };
  222. valid_types=valid;
  223. } break;
  224. case NODE_PATH: {
  225. static const Type valid[]={
  226. STRING,
  227. NIL
  228. };
  229. valid_types=valid;
  230. } break;
  231. case ARRAY: {
  232. static const Type valid[]={
  233. RAW_ARRAY,
  234. INT_ARRAY,
  235. STRING_ARRAY,
  236. REAL_ARRAY,
  237. COLOR_ARRAY,
  238. VECTOR2_ARRAY,
  239. VECTOR3_ARRAY,
  240. NIL
  241. };
  242. valid_types=valid;
  243. } break;
  244. // arrays
  245. case RAW_ARRAY: {
  246. static const Type valid[]={
  247. ARRAY,
  248. NIL
  249. };
  250. valid_types=valid;
  251. } break;
  252. case INT_ARRAY: {
  253. static const Type valid[]={
  254. ARRAY,
  255. NIL
  256. };
  257. valid_types=valid;
  258. } break;
  259. case REAL_ARRAY: {
  260. static const Type valid[]={
  261. ARRAY,
  262. NIL
  263. };
  264. valid_types=valid;
  265. } break;
  266. case STRING_ARRAY: {
  267. static const Type valid[]={
  268. ARRAY,
  269. NIL
  270. };
  271. valid_types=valid;
  272. } break;
  273. case VECTOR2_ARRAY: {
  274. static const Type valid[]={
  275. ARRAY,
  276. NIL
  277. };
  278. valid_types=valid;
  279. } break;
  280. case VECTOR3_ARRAY: {
  281. static const Type valid[]={
  282. ARRAY,
  283. NIL
  284. };
  285. valid_types=valid;
  286. } break;
  287. case COLOR_ARRAY: {
  288. static const Type valid[]={
  289. ARRAY,
  290. NIL
  291. };
  292. valid_types=valid;
  293. } break;
  294. default: {}
  295. }
  296. if (valid_types) {
  297. int i=0;
  298. while(valid_types[i]!=NIL) {
  299. if (p_type_from==valid_types[i])
  300. return true;
  301. i++;
  302. }
  303. } else if (invalid_types) {
  304. int i=0;
  305. while(invalid_types[i]!=NIL) {
  306. if (p_type_from==invalid_types[i])
  307. return false;
  308. i++;
  309. }
  310. }
  311. return false;
  312. }
  313. bool Variant::operator==(const Variant& p_variant) const {
  314. if (type!=p_variant.type) //evaluation of operator== needs to be more strict
  315. return false;
  316. bool v;
  317. Variant r;
  318. evaluate(OP_EQUAL,*this,p_variant,r,v);
  319. return r;
  320. }
  321. bool Variant::is_zero() const {
  322. switch( type ) {
  323. case NIL: {
  324. return true;
  325. } break;
  326. // atomic types
  327. case BOOL: {
  328. return _data._bool==false;
  329. } break;
  330. case INT: {
  331. return _data._int==0;
  332. } break;
  333. case REAL: {
  334. return _data._real==0;
  335. } break;
  336. case STRING: {
  337. return *reinterpret_cast<const String*>(_data._mem)==String();
  338. } break;
  339. // math types
  340. case VECTOR2: {
  341. return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2();
  342. } break;
  343. case RECT2: {
  344. return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2();
  345. } break;
  346. case MATRIX32: {
  347. return *_data._matrix32==Matrix32();
  348. } break;
  349. case VECTOR3: {
  350. return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3();
  351. } break;
  352. case PLANE: {
  353. return *reinterpret_cast<const Plane*>(_data._mem)==Plane();
  354. } break;
  355. /*
  356. case QUAT: {
  357. } break;*/
  358. case _AABB: {
  359. return *_data._aabb==AABB();
  360. } break;
  361. case QUAT: {
  362. *reinterpret_cast<const Quat*>(_data._mem)==Quat();
  363. } break;
  364. case MATRIX3: {
  365. return *_data._matrix3==Matrix3();
  366. } break;
  367. case TRANSFORM: {
  368. return *_data._transform == Transform();
  369. } break;
  370. // misc types
  371. case COLOR: {
  372. return *reinterpret_cast<const Color*>(_data._mem)==Color();
  373. } break;
  374. case IMAGE: {
  375. return _data._image->empty();
  376. } break;
  377. case _RID: {
  378. return *reinterpret_cast<const RID*>(_data._mem)==RID();
  379. } break;
  380. case OBJECT: {
  381. return _get_obj().obj==NULL;
  382. } break;
  383. case NODE_PATH: {
  384. return reinterpret_cast<const NodePath*>(_data._mem)->is_empty();
  385. } break;
  386. case INPUT_EVENT: {
  387. return _data._input_event->type==InputEvent::NONE;
  388. } break;
  389. case DICTIONARY: {
  390. return reinterpret_cast<const Dictionary*>(_data._mem)->empty();
  391. } break;
  392. case ARRAY: {
  393. return reinterpret_cast<const Array*>(_data._mem)->empty();
  394. } break;
  395. // arrays
  396. case RAW_ARRAY: {
  397. return reinterpret_cast<const DVector<uint8_t>*>(_data._mem)->size()==0;
  398. } break;
  399. case INT_ARRAY: {
  400. return reinterpret_cast<const DVector<int>*>(_data._mem)->size()==0;
  401. } break;
  402. case REAL_ARRAY: {
  403. return reinterpret_cast<const DVector<real_t>*>(_data._mem)->size()==0;
  404. } break;
  405. case STRING_ARRAY: {
  406. return reinterpret_cast<const DVector<String>*>(_data._mem)->size()==0;
  407. } break;
  408. case VECTOR2_ARRAY: {
  409. return reinterpret_cast<const DVector<Vector2>*>(_data._mem)->size()==0;
  410. } break;
  411. case VECTOR3_ARRAY: {
  412. return reinterpret_cast<const DVector<Vector3>*>(_data._mem)->size()==0;
  413. } break;
  414. case COLOR_ARRAY: {
  415. return reinterpret_cast<const DVector<Color>*>(_data._mem)->size()==0;
  416. } break;
  417. default: {}
  418. }
  419. return false;
  420. }
  421. void Variant::reference(const Variant& p_variant) {
  422. if (this == &p_variant)
  423. return;
  424. clear();
  425. type=p_variant.type;
  426. switch( p_variant.type ) {
  427. case NIL: {
  428. // none
  429. } break;
  430. // atomic types
  431. case BOOL: {
  432. _data._bool=p_variant._data._bool;
  433. } break;
  434. case INT: {
  435. _data._int=p_variant._data._int;
  436. } break;
  437. case REAL: {
  438. _data._real=p_variant._data._real;
  439. } break;
  440. case STRING: {
  441. memnew_placement( _data._mem, String( *reinterpret_cast<const String*>(p_variant._data._mem) ) );
  442. } break;
  443. // math types
  444. case VECTOR2: {
  445. memnew_placement( _data._mem, Vector2( *reinterpret_cast<const Vector2*>(p_variant._data._mem) ) );
  446. } break;
  447. case RECT2: {
  448. memnew_placement( _data._mem, Rect2( *reinterpret_cast<const Rect2*>(p_variant._data._mem) ) );
  449. } break;
  450. case MATRIX32: {
  451. _data._matrix32 = memnew( Matrix32( *p_variant._data._matrix32 ) );
  452. } break;
  453. case VECTOR3: {
  454. memnew_placement( _data._mem, Vector3( *reinterpret_cast<const Vector3*>(p_variant._data._mem) ) );
  455. } break;
  456. case PLANE: {
  457. memnew_placement( _data._mem, Plane( *reinterpret_cast<const Plane*>(p_variant._data._mem) ) );
  458. } break;
  459. /*
  460. case QUAT: {
  461. } break;*/
  462. case _AABB: {
  463. _data._aabb = memnew( AABB( *p_variant._data._aabb ) );
  464. } break;
  465. case QUAT: {
  466. memnew_placement( _data._mem, Quat( *reinterpret_cast<const Quat*>(p_variant._data._mem) ) );
  467. } break;
  468. case MATRIX3: {
  469. _data._matrix3 = memnew( Matrix3( *p_variant._data._matrix3 ) );
  470. } break;
  471. case TRANSFORM: {
  472. _data._transform = memnew( Transform( *p_variant._data._transform ) );
  473. } break;
  474. // misc types
  475. case COLOR: {
  476. memnew_placement( _data._mem, Color( *reinterpret_cast<const Color*>(p_variant._data._mem) ) );
  477. } break;
  478. case IMAGE: {
  479. _data._image = memnew( Image( *p_variant._data._image ) );
  480. } break;
  481. case _RID: {
  482. memnew_placement( _data._mem, RID( *reinterpret_cast<const RID*>(p_variant._data._mem) ) );
  483. } break;
  484. case OBJECT: {
  485. memnew_placement( _data._mem, ObjData( p_variant._get_obj() ) );
  486. } break;
  487. case NODE_PATH: {
  488. memnew_placement( _data._mem, NodePath( *reinterpret_cast<const NodePath*>(p_variant._data._mem) ) );
  489. } break;
  490. case INPUT_EVENT: {
  491. _data._input_event= memnew( InputEvent( *p_variant._data._input_event ) );
  492. } break;
  493. case DICTIONARY: {
  494. memnew_placement( _data._mem, Dictionary( *reinterpret_cast<const Dictionary*>(p_variant._data._mem) ) );
  495. } break;
  496. case ARRAY: {
  497. memnew_placement( _data._mem, Array ( *reinterpret_cast<const Array*>(p_variant._data._mem) ) );
  498. } break;
  499. // arrays
  500. case RAW_ARRAY: {
  501. memnew_placement( _data._mem, DVector<uint8_t> ( *reinterpret_cast<const DVector<uint8_t>*>(p_variant._data._mem) ) );
  502. } break;
  503. case INT_ARRAY: {
  504. memnew_placement( _data._mem, DVector<int> ( *reinterpret_cast<const DVector<int>*>(p_variant._data._mem) ) );
  505. } break;
  506. case REAL_ARRAY: {
  507. memnew_placement( _data._mem, DVector<real_t> ( *reinterpret_cast<const DVector<real_t>*>(p_variant._data._mem) ) );
  508. } break;
  509. case STRING_ARRAY: {
  510. memnew_placement( _data._mem, DVector<String> ( *reinterpret_cast<const DVector<String>*>(p_variant._data._mem) ) );
  511. } break;
  512. case VECTOR2_ARRAY: {
  513. memnew_placement( _data._mem, DVector<Vector2> ( *reinterpret_cast<const DVector<Vector2>*>(p_variant._data._mem) ) );
  514. } break;
  515. case VECTOR3_ARRAY: {
  516. memnew_placement( _data._mem, DVector<Vector3> ( *reinterpret_cast<const DVector<Vector3>*>(p_variant._data._mem) ) );
  517. } break;
  518. case COLOR_ARRAY: {
  519. memnew_placement( _data._mem, DVector<Color> ( *reinterpret_cast<const DVector<Color>*>(p_variant._data._mem) ) );
  520. } break;
  521. default: {}
  522. }
  523. }
  524. void Variant::clear() {
  525. switch(type) {
  526. case STRING: {
  527. reinterpret_cast<String*>(_data._mem)->~String();
  528. } break;
  529. /*
  530. // no point, they don't allocate memory
  531. VECTOR3,
  532. PLANE,
  533. QUAT,
  534. COLOR,
  535. VECTOR2,
  536. RECT2
  537. */
  538. case MATRIX32: {
  539. memdelete( _data._matrix32 );
  540. } break;
  541. case _AABB: {
  542. memdelete( _data._aabb );
  543. } break;
  544. case MATRIX3: {
  545. memdelete( _data._matrix3 );
  546. } break;
  547. case TRANSFORM: {
  548. memdelete( _data._transform );
  549. } break;
  550. // misc types
  551. case IMAGE: {
  552. memdelete( _data._image );
  553. } break;
  554. case NODE_PATH: {
  555. reinterpret_cast<NodePath*>(_data._mem)->~NodePath();
  556. } break;
  557. case OBJECT: {
  558. _get_obj().obj=NULL;
  559. _get_obj().ref.unref();
  560. } break;
  561. case _RID: {
  562. // not much need probably
  563. reinterpret_cast<RID*>(_data._mem)->~RID();
  564. } break;
  565. case DICTIONARY: {
  566. reinterpret_cast<Dictionary*>(_data._mem)->~Dictionary();
  567. } break;
  568. case ARRAY: {
  569. reinterpret_cast<Array*>(_data._mem)->~Array();
  570. } break;
  571. case INPUT_EVENT: {
  572. memdelete( _data._input_event );
  573. } break;
  574. // arrays
  575. case RAW_ARRAY: {
  576. reinterpret_cast< DVector<uint8_t>* >(_data._mem)->~DVector<uint8_t>();
  577. } break;
  578. case INT_ARRAY: {
  579. reinterpret_cast< DVector<int>* >(_data._mem)->~DVector<int>();
  580. } break;
  581. case REAL_ARRAY: {
  582. reinterpret_cast< DVector<real_t>* >(_data._mem)->~DVector<real_t>();
  583. } break;
  584. case STRING_ARRAY: {
  585. reinterpret_cast< DVector<String>* >(_data._mem)->~DVector<String>();
  586. } break;
  587. case VECTOR2_ARRAY: {
  588. reinterpret_cast< DVector<Vector2>* >(_data._mem)->~DVector<Vector2>();
  589. } break;
  590. case VECTOR3_ARRAY: {
  591. reinterpret_cast< DVector<Vector3>* >(_data._mem)->~DVector<Vector3>();
  592. } break;
  593. case COLOR_ARRAY: {
  594. reinterpret_cast< DVector<Color>* >(_data._mem)->~DVector<Color>();
  595. } break;
  596. default: {} /* not needed */
  597. }
  598. type=NIL;
  599. }
  600. Variant::operator signed int() const {
  601. switch( type ) {
  602. case NIL: return 0;
  603. case BOOL: return _data._bool ? 1 : 0;
  604. case INT: return _data._int;
  605. case REAL: return _data._real;
  606. case STRING: return operator String().to_int();
  607. default: {
  608. return 0;
  609. }
  610. }
  611. return 0;
  612. }
  613. Variant::operator unsigned int() const {
  614. switch( type ) {
  615. case NIL: return 0;
  616. case BOOL: return _data._bool ? 1 : 0;
  617. case INT: return _data._int;
  618. case REAL: return _data._real;
  619. case STRING: return operator String().to_int();
  620. default: {
  621. return 0;
  622. }
  623. }
  624. return 0;
  625. }
  626. Variant::operator int64_t() const {
  627. switch( type ) {
  628. case NIL: return 0;
  629. case BOOL: return _data._bool ? 1 : 0;
  630. case INT: return _data._int;
  631. case REAL: return _data._real;
  632. case STRING: return operator String().to_int();
  633. default: {
  634. return 0;
  635. }
  636. }
  637. return 0;
  638. }
  639. /*
  640. Variant::operator long unsigned int() const {
  641. switch( type ) {
  642. case NIL: return 0;
  643. case BOOL: return _data._bool ? 1 : 0;
  644. case INT: return _data._int;
  645. case REAL: return _data._real;
  646. case STRING: return operator String().to_int();
  647. default: {
  648. return 0;
  649. }
  650. }
  651. return 0;
  652. };
  653. */
  654. Variant::operator uint64_t() const {
  655. switch( type ) {
  656. case NIL: return 0;
  657. case BOOL: return _data._bool ? 1 : 0;
  658. case INT: return _data._int;
  659. case REAL: return _data._real;
  660. case STRING: return operator String().to_int();
  661. default: {
  662. return 0;
  663. }
  664. }
  665. return 0;
  666. }
  667. #ifdef NEED_LONG_INT
  668. Variant::operator signed long() const {
  669. switch( type ) {
  670. case NIL: return 0;
  671. case BOOL: return _data._bool ? 1 : 0;
  672. case INT: return _data._int;
  673. case REAL: return _data._real;
  674. case STRING: return operator String().to_int();
  675. default: {
  676. return 0;
  677. }
  678. }
  679. return 0;
  680. };
  681. Variant::operator unsigned long() const {
  682. switch( type ) {
  683. case NIL: return 0;
  684. case BOOL: return _data._bool ? 1 : 0;
  685. case INT: return _data._int;
  686. case REAL: return _data._real;
  687. case STRING: return operator String().to_int();
  688. default: {
  689. return 0;
  690. }
  691. }
  692. return 0;
  693. };
  694. #endif
  695. Variant::operator signed short() const {
  696. switch( type ) {
  697. case NIL: return 0;
  698. case BOOL: return _data._bool ? 1 : 0;
  699. case INT: return _data._int;
  700. case REAL: return _data._real;
  701. case STRING: return operator String().to_int();
  702. default: {
  703. return 0;
  704. }
  705. }
  706. return 0;
  707. }
  708. Variant::operator unsigned short() const {
  709. switch( type ) {
  710. case NIL: return 0;
  711. case BOOL: return _data._bool ? 1 : 0;
  712. case INT: return _data._int;
  713. case REAL: return _data._real;
  714. case STRING: return operator String().to_int();
  715. default: {
  716. return 0;
  717. }
  718. }
  719. return 0;
  720. }
  721. Variant::operator signed char() const {
  722. switch( type ) {
  723. case NIL: return 0;
  724. case BOOL: return _data._bool ? 1 : 0;
  725. case INT: return _data._int;
  726. case REAL: return _data._real;
  727. case STRING: return operator String().to_int();
  728. default: {
  729. return 0;
  730. }
  731. }
  732. return 0;
  733. }
  734. Variant::operator unsigned char() const {
  735. switch( type ) {
  736. case NIL: return 0;
  737. case BOOL: return _data._bool ? 1 : 0;
  738. case INT: return _data._int;
  739. case REAL: return _data._real;
  740. case STRING: return operator String().to_int();
  741. default: {
  742. return 0;
  743. }
  744. }
  745. return 0;
  746. }
  747. #ifndef CHARTYPE_16BITS
  748. Variant::operator CharType() const {
  749. return operator unsigned int();
  750. }
  751. #endif
  752. Variant::operator float() const {
  753. switch( type ) {
  754. case NIL: return 0;
  755. case BOOL: return _data._bool ? 1.0 : 0.0;
  756. case INT: return (float)_data._int;
  757. case REAL: return _data._real;
  758. case STRING: return operator String().to_double();
  759. default: {
  760. return 0;
  761. }
  762. }
  763. return 0;
  764. }
  765. Variant::operator double() const {
  766. switch( type ) {
  767. case NIL: return 0;
  768. case BOOL: return _data._bool ? 1.0 : 0.0;
  769. case INT: return (float)_data._int;
  770. case REAL: return _data._real;
  771. case STRING: return operator String().to_double();
  772. default: {
  773. return 0;
  774. }
  775. }
  776. return true;
  777. }
  778. Variant::operator StringName() const {
  779. if (type==NODE_PATH) {
  780. return reinterpret_cast<const NodePath*>(_data._mem)->get_sname();
  781. }
  782. return StringName(operator String());
  783. }
  784. struct _VariantStrPair {
  785. String key;
  786. String value;
  787. bool operator<(const _VariantStrPair& p) const {
  788. return key < p.key;
  789. }
  790. };
  791. Variant::operator String() const {
  792. switch( type ) {
  793. case NIL: return "";
  794. case BOOL: return _data._bool ? "True" : "False";
  795. case INT: return String::num(_data._int);
  796. case REAL: return String::num(_data._real);
  797. case STRING: return *reinterpret_cast<const String*>(_data._mem);
  798. case VECTOR2: return operator Vector2();
  799. case RECT2: return operator Rect2();
  800. case MATRIX32: return operator Matrix32();
  801. case VECTOR3: return operator Vector3();
  802. case PLANE: return operator Plane();
  803. //case QUAT:
  804. case _AABB: return operator AABB();
  805. case QUAT: return operator Quat();
  806. case MATRIX3: return operator Matrix3();
  807. case TRANSFORM: return operator Transform();
  808. case NODE_PATH: return operator NodePath();
  809. case INPUT_EVENT: return operator InputEvent();
  810. case COLOR: return String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a) ;
  811. case DICTIONARY: {
  812. const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
  813. //const String *K=NULL;
  814. String str;
  815. List<Variant> keys;
  816. d.get_key_list(&keys);
  817. Vector<_VariantStrPair> pairs;
  818. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  819. _VariantStrPair sp;
  820. sp.key=String(E->get());
  821. sp.value=d[E->get()];
  822. pairs.push_back(sp);
  823. }
  824. pairs.sort();
  825. for(int i=0;i<pairs.size();i++) {
  826. if (i>0)
  827. str+=", ";
  828. str+="("+pairs[i].key+":"+pairs[i].value+")";
  829. }
  830. return str;
  831. } break;
  832. case VECTOR3_ARRAY: {
  833. DVector<Vector3> vec = operator DVector<Vector3>();
  834. String str;
  835. for(int i=0;i<vec.size();i++) {
  836. if (i>0)
  837. str+=", ";
  838. str=str+Variant( vec[i] );
  839. }
  840. return str;
  841. } break;
  842. case STRING_ARRAY: {
  843. DVector<String> vec = operator DVector<String>();
  844. String str;
  845. for(int i=0;i<vec.size();i++) {
  846. if (i>0)
  847. str+=", ";
  848. str=str+vec[i];
  849. }
  850. return str;
  851. } break;
  852. case INT_ARRAY: {
  853. DVector<int> vec = operator DVector<int>();
  854. String str;
  855. for(int i=0;i<vec.size();i++) {
  856. if (i>0)
  857. str+=", ";
  858. str=str+itos(vec[i]);
  859. }
  860. return str;
  861. } break;
  862. case REAL_ARRAY: {
  863. DVector<real_t> vec = operator DVector<real_t>();
  864. String str;
  865. for(int i=0;i<vec.size();i++) {
  866. if (i>0)
  867. str+=", ";
  868. str=str+rtos(vec[i]);
  869. }
  870. return str;
  871. } break;
  872. case ARRAY: {
  873. Array arr = operator Array();
  874. String str;
  875. for (int i=0; i<arr.size(); i++) {
  876. if (i)
  877. str+=", ";
  878. str += String(arr[i]);
  879. };
  880. return str;
  881. } break;
  882. case OBJECT: {
  883. if (_get_obj().obj)
  884. return "["+_get_obj().obj->get_type()+":"+itos(_get_obj().obj->get_instance_ID())+"]";
  885. else
  886. return "[Object:null]";
  887. } break;
  888. default: {
  889. return "["+get_type_name(type)+"]";
  890. }
  891. }
  892. return "";
  893. }
  894. Variant::operator Vector2() const {
  895. if (type==VECTOR2)
  896. return *reinterpret_cast<const Vector2*>(_data._mem);
  897. else if (type==VECTOR3)
  898. return Vector2(reinterpret_cast<const Vector3*>(_data._mem)->x,reinterpret_cast<const Vector3*>(_data._mem)->y);
  899. else
  900. return Vector2();
  901. }
  902. Variant::operator Rect2() const {
  903. if (type==RECT2)
  904. return *reinterpret_cast<const Rect2*>(_data._mem);
  905. else
  906. return Rect2();
  907. }
  908. Variant::operator Vector3() const {
  909. if (type==VECTOR3)
  910. return *reinterpret_cast<const Vector3*>(_data._mem);
  911. else
  912. return Vector3();
  913. }
  914. Variant::operator Plane() const {
  915. if (type==PLANE)
  916. return *reinterpret_cast<const Plane*>(_data._mem);
  917. else
  918. return Plane();
  919. }
  920. Variant::operator AABB() const {
  921. if (type==_AABB)
  922. return *_data._aabb;
  923. else
  924. return AABB();
  925. }
  926. Variant::operator Matrix3() const {
  927. if (type==MATRIX3)
  928. return *_data._matrix3;
  929. else
  930. return Matrix3();
  931. }
  932. Variant::operator Quat() const {
  933. if (type==QUAT)
  934. return *reinterpret_cast<const Quat*>(_data._mem);
  935. else
  936. return Quat();
  937. }
  938. Variant::operator Transform() const {
  939. if (type==TRANSFORM)
  940. return *_data._transform;
  941. else if (type==MATRIX3)
  942. return Transform(*_data._matrix3,Vector3());
  943. else
  944. return Transform();
  945. }
  946. Variant::operator Matrix32() const {
  947. if (type==MATRIX32) {
  948. return *_data._matrix32;
  949. } else if (type==TRANSFORM) {
  950. const Transform& t = *_data._transform;;
  951. Matrix32 m;
  952. m.elements[0][0]=t.basis.elements[0][0];
  953. m.elements[0][1]=t.basis.elements[1][0];
  954. m.elements[1][0]=t.basis.elements[0][1];
  955. m.elements[1][1]=t.basis.elements[1][1];
  956. m.elements[2][0]=t.origin[0];
  957. m.elements[2][1]=t.origin[1];
  958. return m;
  959. } else
  960. return Matrix32();
  961. }
  962. Variant::operator Color() const {
  963. if (type==COLOR)
  964. return *reinterpret_cast<const Color*>(_data._mem);
  965. else
  966. return Color();
  967. }
  968. Variant::operator Image() const {
  969. if (type==IMAGE)
  970. return *_data._image;
  971. else
  972. return Image();
  973. }
  974. Variant::operator NodePath() const {
  975. if (type==NODE_PATH)
  976. return *reinterpret_cast<const NodePath*>(_data._mem);
  977. else if (type==STRING)
  978. return NodePath(operator String());
  979. else
  980. return NodePath();
  981. }
  982. Variant::operator RefPtr() const {
  983. if (type==OBJECT)
  984. return _get_obj().ref;
  985. else
  986. return RefPtr();
  987. }
  988. Variant::operator RID() const {
  989. if (type==_RID)
  990. return *reinterpret_cast<const RID*>(_data._mem);
  991. else if (type==OBJECT && !_get_obj().ref.is_null()) {
  992. return _get_obj().ref.get_rid();
  993. } else
  994. return RID();
  995. }
  996. Variant::operator Object*() const {
  997. if (type==OBJECT)
  998. return _get_obj().obj;
  999. else
  1000. return NULL;
  1001. }
  1002. Variant::operator Node*() const {
  1003. if (type==OBJECT)
  1004. return _get_obj().obj?_get_obj().obj->cast_to<Node>():NULL;
  1005. else
  1006. return NULL;
  1007. }
  1008. Variant::operator Control*() const {
  1009. if (type==OBJECT)
  1010. return _get_obj().obj?_get_obj().obj->cast_to<Control>():NULL;
  1011. else
  1012. return NULL;
  1013. }
  1014. Variant::operator InputEvent() const {
  1015. if (type==INPUT_EVENT)
  1016. return *reinterpret_cast<const InputEvent*>(_data._input_event);
  1017. else
  1018. return InputEvent();
  1019. }
  1020. Variant::operator Dictionary() const {
  1021. if (type==DICTIONARY)
  1022. return *reinterpret_cast<const Dictionary*>(_data._mem);
  1023. else
  1024. return Dictionary();
  1025. }
  1026. template<class DA,class SA>
  1027. inline DA _convert_array(const SA& p_array) {
  1028. DA da;
  1029. da.resize(p_array.size());
  1030. for(int i=0;i<p_array.size();i++) {
  1031. da.set( i, Variant(p_array.get(i)) );
  1032. }
  1033. return da;
  1034. }
  1035. template<class DA>
  1036. inline DA _convert_array_from_variant(const Variant& p_variant) {
  1037. switch(p_variant.get_type()) {
  1038. case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); }
  1039. case Variant::RAW_ARRAY: { return _convert_array<DA,DVector<uint8_t> >( p_variant.operator DVector<uint8_t> () ); }
  1040. case Variant::INT_ARRAY: { return _convert_array<DA,DVector<int> >( p_variant.operator DVector<int> () ); }
  1041. case Variant::REAL_ARRAY: { return _convert_array<DA,DVector<real_t> >( p_variant.operator DVector<real_t> () ); }
  1042. case Variant::STRING_ARRAY: { return _convert_array<DA,DVector<String> >( p_variant.operator DVector<String> () ); }
  1043. case Variant::VECTOR2_ARRAY: { return _convert_array<DA,DVector<Vector2> >( p_variant.operator DVector<Vector2> () ); }
  1044. case Variant::VECTOR3_ARRAY: { return _convert_array<DA,DVector<Vector3> >( p_variant.operator DVector<Vector3> () ); }
  1045. case Variant::COLOR_ARRAY: { return _convert_array<DA,DVector<Color> >( p_variant.operator DVector<Color>() ); }
  1046. default: { return DA(); }
  1047. }
  1048. return DA();
  1049. }
  1050. Variant::operator Array() const {
  1051. if (type==ARRAY)
  1052. return *reinterpret_cast<const Array*>(_data._mem);
  1053. else
  1054. return _convert_array_from_variant<Array >(*this);
  1055. }
  1056. Variant::operator DVector<uint8_t>() const {
  1057. if (type==RAW_ARRAY)
  1058. return *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1059. else
  1060. return _convert_array_from_variant<DVector<uint8_t> >(*this);
  1061. }
  1062. Variant::operator DVector<int>() const {
  1063. if (type==INT_ARRAY)
  1064. return *reinterpret_cast<const DVector<int>* >(_data._mem);
  1065. else
  1066. return _convert_array_from_variant<DVector<int> >(*this);
  1067. }
  1068. Variant::operator DVector<real_t>() const {
  1069. if (type==REAL_ARRAY)
  1070. return *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1071. else
  1072. return _convert_array_from_variant<DVector<real_t> >(*this);
  1073. }
  1074. Variant::operator DVector<String>() const {
  1075. if (type==STRING_ARRAY)
  1076. return *reinterpret_cast<const DVector<String>* >(_data._mem);
  1077. else
  1078. return _convert_array_from_variant<DVector<String> >(*this);
  1079. }
  1080. Variant::operator DVector<Vector3>() const {
  1081. if (type==VECTOR3_ARRAY)
  1082. return *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1083. else
  1084. return _convert_array_from_variant<DVector<Vector3> >(*this);
  1085. }
  1086. Variant::operator DVector<Vector2>() const {
  1087. if (type==VECTOR2_ARRAY)
  1088. return *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1089. else
  1090. return _convert_array_from_variant<DVector<Vector2> >(*this);
  1091. }
  1092. Variant::operator DVector<Color>() const {
  1093. if (type==COLOR_ARRAY)
  1094. return *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1095. else
  1096. return _convert_array_from_variant<DVector<Color> >(*this);
  1097. }
  1098. /* helpers */
  1099. Variant::operator Vector<RID>() const {
  1100. Array va= operator Array();
  1101. Vector<RID> rids;
  1102. rids.resize(va.size());
  1103. for(int i=0;i<rids.size();i++)
  1104. rids[i]=va[i];
  1105. return rids;
  1106. }
  1107. Variant::operator Vector<Vector2>() const {
  1108. DVector<Vector2> from=operator DVector<Vector2>();
  1109. Vector<Vector2> to;
  1110. int len=from.size();
  1111. if (len==0)
  1112. return Vector<Vector2>();
  1113. to.resize(len);
  1114. DVector<Vector2>::Read r = from.read();
  1115. Vector2 *w = &to[0];
  1116. for (int i=0;i<len;i++) {
  1117. w[i]=r[i];
  1118. }
  1119. return to;
  1120. }
  1121. Variant::operator DVector<Plane>() const {
  1122. Array va= operator Array();
  1123. DVector<Plane> planes;
  1124. int va_size=va.size();
  1125. if (va_size==0)
  1126. return planes;
  1127. planes.resize(va_size);
  1128. DVector<Plane>::Write w = planes.write();
  1129. for(int i=0;i<va_size;i++)
  1130. w[i]=va[i];
  1131. return planes;
  1132. }
  1133. Variant::operator DVector<Face3>() const {
  1134. DVector<Vector3> va= operator DVector<Vector3>();
  1135. DVector<Face3> faces;
  1136. int va_size=va.size();
  1137. if (va_size==0)
  1138. return faces;
  1139. faces.resize(va_size/3);
  1140. DVector<Face3>::Write w = faces.write();
  1141. DVector<Vector3>::Read r = va.read();
  1142. for(int i=0;i<va_size;i++)
  1143. w[i/3].vertex[i%3]=r[i];
  1144. return faces;
  1145. }
  1146. Variant::operator Vector<Plane>() const {
  1147. Array va= operator Array();
  1148. Vector<Plane> planes;
  1149. int va_size=va.size();
  1150. if (va_size==0)
  1151. return planes;
  1152. planes.resize(va_size);
  1153. for(int i=0;i<va_size;i++)
  1154. planes[i]=va[i];
  1155. return planes;
  1156. }
  1157. Variant::operator Vector<Variant>() const {
  1158. Array from=operator Array();
  1159. Vector<Variant> to;
  1160. int len=from.size();
  1161. to.resize(len);
  1162. for (int i=0;i<len;i++) {
  1163. to[i]=from[i];
  1164. }
  1165. return to;
  1166. }
  1167. Variant::operator Vector<uint8_t>() const {
  1168. DVector<uint8_t> from=operator DVector<uint8_t>();
  1169. Vector<uint8_t> to;
  1170. int len=from.size();
  1171. to.resize(len);
  1172. for (int i=0;i<len;i++) {
  1173. to[i]=from[i];
  1174. }
  1175. return to;
  1176. }
  1177. Variant::operator Vector<int>() const {
  1178. DVector<int> from=operator DVector<int>();
  1179. Vector<int> to;
  1180. int len=from.size();
  1181. to.resize(len);
  1182. for (int i=0;i<len;i++) {
  1183. to[i]=from[i];
  1184. }
  1185. return to;
  1186. }
  1187. Variant::operator Vector<real_t>() const {
  1188. DVector<real_t> from=operator DVector<real_t>();
  1189. Vector<real_t> to;
  1190. int len=from.size();
  1191. to.resize(len);
  1192. for (int i=0;i<len;i++) {
  1193. to[i]=from[i];
  1194. }
  1195. return to;
  1196. }
  1197. Variant::operator Vector<String>() const {
  1198. DVector<String> from=operator DVector<String>();
  1199. Vector<String> to;
  1200. int len=from.size();
  1201. to.resize(len);
  1202. for (int i=0;i<len;i++) {
  1203. to[i]=from[i];
  1204. }
  1205. return to;
  1206. }
  1207. Variant::operator Vector<Vector3>() const {
  1208. DVector<Vector3> from=operator DVector<Vector3>();
  1209. Vector<Vector3> to;
  1210. int len=from.size();
  1211. if (len==0)
  1212. return Vector<Vector3>();
  1213. to.resize(len);
  1214. DVector<Vector3>::Read r = from.read();
  1215. Vector3 *w = &to[0];
  1216. for (int i=0;i<len;i++) {
  1217. w[i]=r[i];
  1218. }
  1219. return to;
  1220. }
  1221. Variant::operator Vector<Color>() const {
  1222. DVector<Color> from=operator DVector<Color>();
  1223. Vector<Color> to;
  1224. int len=from.size();
  1225. if (len==0)
  1226. return Vector<Color>();
  1227. to.resize(len);
  1228. DVector<Color>::Read r = from.read();
  1229. Color *w = &to[0];
  1230. for (int i=0;i<len;i++) {
  1231. w[i]=r[i];
  1232. }
  1233. return to;
  1234. }
  1235. Variant::operator Margin() const {
  1236. return (Margin)operator int();
  1237. }
  1238. Variant::operator Orientation() const {
  1239. return (Orientation)operator int();
  1240. }
  1241. Variant::operator IP_Address() const {
  1242. if (type==REAL_ARRAY || type==INT_ARRAY || type==RAW_ARRAY) {
  1243. DVector<int> addr=operator DVector<int>();
  1244. if (addr.size()==4) {
  1245. return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3));
  1246. }
  1247. }
  1248. return IP_Address( operator String() );
  1249. }
  1250. Variant::Variant(bool p_bool) {
  1251. type=BOOL;
  1252. _data._bool=p_bool;
  1253. }
  1254. /*
  1255. Variant::Variant(long unsigned int p_long) {
  1256. type=INT;
  1257. _data._int=p_long;
  1258. };
  1259. */
  1260. Variant::Variant(signed int p_int) {
  1261. type=INT;
  1262. _data._int=p_int;
  1263. }
  1264. Variant::Variant(unsigned int p_int) {
  1265. type=INT;
  1266. _data._int=p_int;
  1267. }
  1268. #ifdef NEED_LONG_INT
  1269. Variant::Variant(signed long p_int) {
  1270. type=INT;
  1271. _data._int=p_int;
  1272. }
  1273. Variant::Variant(unsigned long p_int) {
  1274. type=INT;
  1275. _data._int=p_int;
  1276. }
  1277. #endif
  1278. Variant::Variant(int64_t p_int) {
  1279. type=INT;
  1280. _data._int=p_int;
  1281. }
  1282. Variant::Variant(uint64_t p_int) {
  1283. type=INT;
  1284. _data._int=p_int;
  1285. }
  1286. Variant::Variant(signed short p_short) {
  1287. type=INT;
  1288. _data._int=p_short;
  1289. }
  1290. Variant::Variant(unsigned short p_short) {
  1291. type=INT;
  1292. _data._int=p_short;
  1293. }
  1294. Variant::Variant(signed char p_char) {
  1295. type=INT;
  1296. _data._int=p_char;
  1297. }
  1298. Variant::Variant(unsigned char p_char) {
  1299. type=INT;
  1300. _data._int=p_char;
  1301. }
  1302. Variant::Variant(float p_float) {
  1303. type=REAL;
  1304. _data._real=p_float;
  1305. }
  1306. Variant::Variant(double p_double) {
  1307. type=REAL;
  1308. _data._real=p_double;
  1309. }
  1310. Variant::Variant(const StringName& p_string) {
  1311. type=STRING;
  1312. memnew_placement( _data._mem, String( p_string.operator String() ) );
  1313. }
  1314. Variant::Variant(const String& p_string) {
  1315. type=STRING;
  1316. memnew_placement( _data._mem, String( p_string ) );
  1317. }
  1318. Variant::Variant(const char * const p_cstring) {
  1319. type=STRING;
  1320. memnew_placement( _data._mem, String( (const char*)p_cstring ) );
  1321. }
  1322. Variant::Variant(const CharType * p_wstring) {
  1323. type=STRING;
  1324. memnew_placement( _data._mem, String( p_wstring ) );
  1325. }
  1326. Variant::Variant(const Vector3& p_vector3) {
  1327. type=VECTOR3;
  1328. memnew_placement( _data._mem, Vector3( p_vector3 ) );
  1329. }
  1330. Variant::Variant(const Vector2& p_vector2) {
  1331. type=VECTOR2;
  1332. memnew_placement( _data._mem, Vector2( p_vector2 ) );
  1333. }
  1334. Variant::Variant(const Rect2& p_rect2) {
  1335. type=RECT2;
  1336. memnew_placement( _data._mem, Rect2( p_rect2 ) );
  1337. }
  1338. Variant::Variant(const Plane& p_plane) {
  1339. type=PLANE;
  1340. memnew_placement( _data._mem, Plane( p_plane ) );
  1341. }
  1342. Variant::Variant(const AABB& p_aabb) {
  1343. type=_AABB;
  1344. _data._aabb = memnew( AABB( p_aabb ) );
  1345. }
  1346. Variant::Variant(const Matrix3& p_matrix) {
  1347. type=MATRIX3;
  1348. _data._matrix3= memnew( Matrix3( p_matrix ) );
  1349. }
  1350. Variant::Variant(const Quat& p_quat) {
  1351. type=QUAT;
  1352. memnew_placement( _data._mem, Quat( p_quat ) );
  1353. }
  1354. Variant::Variant(const Transform& p_transform) {
  1355. type=TRANSFORM;
  1356. _data._transform = memnew( Transform( p_transform ) );
  1357. }
  1358. Variant::Variant(const Matrix32& p_transform) {
  1359. type=MATRIX32;
  1360. _data._matrix32 = memnew( Matrix32( p_transform ) );
  1361. }
  1362. Variant::Variant(const Color& p_color) {
  1363. type=COLOR;
  1364. memnew_placement( _data._mem, Color(p_color) );
  1365. }
  1366. Variant::Variant(const Image& p_image) {
  1367. type=IMAGE;
  1368. _data._image=memnew( Image(p_image) );
  1369. }
  1370. Variant::Variant(const NodePath& p_node_path) {
  1371. type=NODE_PATH;
  1372. memnew_placement( _data._mem, NodePath(p_node_path) );
  1373. }
  1374. Variant::Variant(const InputEvent& p_input_event) {
  1375. type=INPUT_EVENT;
  1376. _data._input_event = memnew( InputEvent(p_input_event) );
  1377. }
  1378. Variant::Variant(const RefPtr& p_resource) {
  1379. type=OBJECT;
  1380. memnew_placement( _data._mem, ObjData );
  1381. REF ref = p_resource;
  1382. _get_obj().obj=ref.ptr();
  1383. _get_obj().ref=p_resource;
  1384. }
  1385. Variant::Variant(const RID& p_rid) {
  1386. type=_RID;
  1387. memnew_placement( _data._mem, RID(p_rid) );
  1388. }
  1389. Variant::Variant(const Object* p_object) {
  1390. type=OBJECT;
  1391. memnew_placement( _data._mem, ObjData );
  1392. _get_obj().obj=const_cast<Object*>(p_object);
  1393. }
  1394. Variant::Variant(const Dictionary& p_dictionary) {
  1395. type=DICTIONARY;
  1396. memnew_placement( _data._mem, (Dictionary)( p_dictionary) );
  1397. }
  1398. Variant::Variant(const Array& p_array) {
  1399. type=ARRAY;
  1400. memnew_placement( _data._mem, Array(p_array) );
  1401. }
  1402. Variant::Variant(const DVector<Plane>& p_array) {
  1403. type=ARRAY;
  1404. Array *plane_array=memnew_placement( _data._mem, Array );
  1405. plane_array->resize( p_array.size() );
  1406. for (int i=0;i<p_array.size();i++) {
  1407. plane_array->operator [](i)=Variant(p_array[i]);
  1408. }
  1409. }
  1410. Variant::Variant(const Vector<Plane>& p_array) {
  1411. type=ARRAY;
  1412. Array *plane_array=memnew_placement( _data._mem, Array );
  1413. plane_array->resize( p_array.size() );
  1414. for (int i=0;i<p_array.size();i++) {
  1415. plane_array->operator [](i)=Variant(p_array[i]);
  1416. }
  1417. }
  1418. Variant::Variant(const Vector<RID>& p_array) {
  1419. type=ARRAY;
  1420. Array *rid_array=memnew_placement( _data._mem, Array );
  1421. rid_array->resize( p_array.size() );
  1422. for (int i=0;i<p_array.size();i++) {
  1423. rid_array->set(i,Variant(p_array[i]));
  1424. }
  1425. }
  1426. Variant::Variant(const Vector<Vector2>& p_array) {
  1427. type=NIL;
  1428. DVector<Vector2> v;
  1429. int len=p_array.size();
  1430. if (len>0) {
  1431. v.resize(len);
  1432. DVector<Vector2>::Write w = v.write();
  1433. const Vector2 *r = p_array.ptr();
  1434. for (int i=0;i<len;i++)
  1435. w[i]=r[i];
  1436. }
  1437. *this=v;
  1438. }
  1439. Variant::Variant(const DVector<uint8_t>& p_raw_array) {
  1440. type=RAW_ARRAY;
  1441. memnew_placement( _data._mem, DVector<uint8_t>(p_raw_array) );
  1442. }
  1443. Variant::Variant(const DVector<int>& p_int_array) {
  1444. type=INT_ARRAY;
  1445. memnew_placement( _data._mem, DVector<int>(p_int_array) );
  1446. }
  1447. Variant::Variant(const DVector<real_t>& p_real_array) {
  1448. type=REAL_ARRAY;
  1449. memnew_placement( _data._mem, DVector<real_t>(p_real_array) );
  1450. }
  1451. Variant::Variant(const DVector<String>& p_string_array) {
  1452. type=STRING_ARRAY;
  1453. memnew_placement( _data._mem, DVector<String>(p_string_array) );
  1454. }
  1455. Variant::Variant(const DVector<Vector3>& p_vector3_array) {
  1456. type=VECTOR3_ARRAY;
  1457. memnew_placement( _data._mem, DVector<Vector3>(p_vector3_array) );
  1458. }
  1459. Variant::Variant(const DVector<Vector2>& p_vector2_array) {
  1460. type=VECTOR2_ARRAY;
  1461. memnew_placement( _data._mem, DVector<Vector2>(p_vector2_array) );
  1462. }
  1463. Variant::Variant(const DVector<Color>& p_color_array) {
  1464. type=COLOR_ARRAY;
  1465. memnew_placement( _data._mem, DVector<Color>(p_color_array) );
  1466. }
  1467. Variant::Variant(const DVector<Face3>& p_face_array) {
  1468. DVector<Vector3> vertices;
  1469. int face_count=p_face_array.size();
  1470. vertices.resize(face_count*3);
  1471. if (face_count) {
  1472. DVector<Face3>::Read r = p_face_array.read();
  1473. DVector<Vector3>::Write w = vertices.write();
  1474. for(int i=0;i<face_count;i++) {
  1475. for(int j=0;j<3;j++)
  1476. w[i*3+j]=r[i].vertex[j];
  1477. }
  1478. r=DVector<Face3>::Read();
  1479. w=DVector<Vector3>::Write();
  1480. }
  1481. type = NIL;
  1482. *this = vertices;
  1483. }
  1484. /* helpers */
  1485. Variant::Variant(const Vector<Variant>& p_array) {
  1486. type=NIL;
  1487. Array v;
  1488. int len=p_array.size();
  1489. v.resize(len);
  1490. for (int i=0;i<len;i++)
  1491. v.set(i,p_array[i]);
  1492. *this=v;
  1493. }
  1494. Variant::Variant(const Vector<uint8_t>& p_array) {
  1495. type=NIL;
  1496. DVector<uint8_t> v;
  1497. int len=p_array.size();
  1498. v.resize(len);
  1499. for (int i=0;i<len;i++)
  1500. v.set(i,p_array[i]);
  1501. *this=v;
  1502. }
  1503. Variant::Variant(const Vector<int>& p_array) {
  1504. type=NIL;
  1505. DVector<int> v;
  1506. int len=p_array.size();
  1507. v.resize(len);
  1508. for (int i=0;i<len;i++)
  1509. v.set(i,p_array[i]);
  1510. *this=v;
  1511. }
  1512. Variant::Variant(const Vector<real_t>& p_array) {
  1513. type=NIL;
  1514. DVector<real_t> v;
  1515. int len=p_array.size();
  1516. v.resize(len);
  1517. for (int i=0;i<len;i++)
  1518. v.set(i,p_array[i]);
  1519. *this=v;
  1520. }
  1521. Variant::Variant(const Vector<String>& p_array) {
  1522. type=NIL;
  1523. DVector<String> v;
  1524. int len=p_array.size();
  1525. v.resize(len);
  1526. for (int i=0;i<len;i++)
  1527. v.set(i,p_array[i]);
  1528. *this=v;
  1529. }
  1530. Variant::Variant(const Vector<Vector3>& p_array) {
  1531. type=NIL;
  1532. DVector<Vector3> v;
  1533. int len=p_array.size();
  1534. if (len>0) {
  1535. v.resize(len);
  1536. DVector<Vector3>::Write w = v.write();
  1537. const Vector3 *r = p_array.ptr();
  1538. for (int i=0;i<len;i++)
  1539. w[i]=r[i];
  1540. }
  1541. *this=v;
  1542. }
  1543. Variant::Variant(const Vector<Color>& p_array) {
  1544. type=NIL;
  1545. DVector<Color> v;
  1546. int len=p_array.size();
  1547. v.resize(len);
  1548. for (int i=0;i<len;i++)
  1549. v.set(i,p_array[i]);
  1550. *this=v;
  1551. }
  1552. void Variant::operator=(const Variant& p_variant) {
  1553. reference(p_variant);
  1554. }
  1555. Variant::Variant(const IP_Address& p_address) {
  1556. type=STRING;
  1557. memnew_placement( _data._mem, String( p_address ) );
  1558. }
  1559. Variant::Variant(const Variant& p_variant) {
  1560. type=NIL;
  1561. reference(p_variant);
  1562. }
  1563. /*
  1564. Variant::~Variant() {
  1565. clear();
  1566. }*/
  1567. uint32_t Variant::hash() const {
  1568. switch( type ) {
  1569. case NIL: {
  1570. return 0;
  1571. } break;
  1572. case BOOL: {
  1573. return _data._bool?1:0;
  1574. } break;
  1575. case INT: {
  1576. return _data._int;
  1577. } break;
  1578. case REAL: {
  1579. MarshallFloat mf;
  1580. mf.f=_data._real;
  1581. return mf.i;
  1582. } break;
  1583. case STRING: {
  1584. return reinterpret_cast<const String*>(_data._mem)->hash();
  1585. } break;
  1586. // math types
  1587. case VECTOR2: {
  1588. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->x);
  1589. return hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->y,hash);
  1590. } break;
  1591. case RECT2: {
  1592. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.x);
  1593. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.y,hash);
  1594. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.x,hash);
  1595. return hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.y,hash);
  1596. } break;
  1597. case MATRIX32: {
  1598. uint32_t hash = 5831;
  1599. for(int i=0;i<3;i++) {
  1600. for(int j=0;j<2;j++) {
  1601. hash = hash_djb2_one_float(_data._matrix32->elements[i][j],hash);
  1602. }
  1603. }
  1604. return hash;
  1605. } break;
  1606. case VECTOR3: {
  1607. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->x);
  1608. hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->y,hash);
  1609. return hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->z,hash);
  1610. } break;
  1611. case PLANE: {
  1612. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.x);
  1613. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.y,hash);
  1614. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.z,hash);
  1615. return hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->d,hash);
  1616. } break;
  1617. /*
  1618. case QUAT: {
  1619. } break;*/
  1620. case _AABB: {
  1621. uint32_t hash = 5831;
  1622. for(int i=0;i<3;i++) {
  1623. hash = hash_djb2_one_float(_data._aabb->pos[i],hash);
  1624. hash = hash_djb2_one_float(_data._aabb->size[i],hash);
  1625. }
  1626. return hash;
  1627. } break;
  1628. case QUAT: {
  1629. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->x);
  1630. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->y,hash);
  1631. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->z,hash);
  1632. return hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->w,hash);
  1633. } break;
  1634. case MATRIX3: {
  1635. uint32_t hash = 5831;
  1636. for(int i=0;i<3;i++) {
  1637. for(int j=0;j<3;j++) {
  1638. hash = hash_djb2_one_float(_data._matrix3->elements[i][j],hash);
  1639. }
  1640. }
  1641. return hash;
  1642. } break;
  1643. case TRANSFORM: {
  1644. uint32_t hash = 5831;
  1645. for(int i=0;i<3;i++) {
  1646. for(int j=0;j<3;j++) {
  1647. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j],hash);
  1648. }
  1649. hash = hash_djb2_one_float(_data._transform->origin[i],hash);
  1650. }
  1651. return hash;
  1652. } break;
  1653. // misc types
  1654. case COLOR: {
  1655. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->r);
  1656. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->g,hash);
  1657. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->b,hash);
  1658. return hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->a,hash);
  1659. } break;
  1660. case IMAGE: {
  1661. return 0;
  1662. } break;
  1663. case _RID: {
  1664. return hash_djb2_one_64(reinterpret_cast<const RID*>(_data._mem)->get_id());
  1665. } break;
  1666. case OBJECT: {
  1667. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  1668. } break;
  1669. case NODE_PATH: {
  1670. return reinterpret_cast<const NodePath*>(_data._mem)->hash();
  1671. } break;
  1672. case INPUT_EVENT: {
  1673. return hash_djb2_buffer((uint8_t*)_data._input_event,sizeof(InputEvent));
  1674. } break;
  1675. case DICTIONARY: {
  1676. return reinterpret_cast<const Dictionary*>(_data._mem)->hash();
  1677. } break;
  1678. case ARRAY: {
  1679. const Array& arr = *reinterpret_cast<const Array* >(_data._mem);
  1680. return arr.hash();
  1681. } break;
  1682. case RAW_ARRAY: {
  1683. const DVector<uint8_t>& arr = *reinterpret_cast<const DVector<uint8_t>* >(_data._mem);
  1684. int len = arr.size();
  1685. DVector<uint8_t>::Read r = arr.read();
  1686. return hash_djb2_buffer((uint8_t*)&r[0],len);
  1687. } break;
  1688. case INT_ARRAY: {
  1689. const DVector<int>& arr = *reinterpret_cast<const DVector<int>* >(_data._mem);
  1690. int len = arr.size();
  1691. DVector<int>::Read r = arr.read();
  1692. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int));
  1693. } break;
  1694. case REAL_ARRAY: {
  1695. const DVector<real_t>& arr = *reinterpret_cast<const DVector<real_t>* >(_data._mem);
  1696. int len = arr.size();
  1697. DVector<real_t>::Read r = arr.read();
  1698. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t));
  1699. } break;
  1700. case STRING_ARRAY: {
  1701. uint32_t hash=5831;
  1702. const DVector<String>& arr = *reinterpret_cast<const DVector<String>* >(_data._mem);
  1703. int len = arr.size();
  1704. DVector<String>::Read r = arr.read();
  1705. for(int i=0;i<len;i++) {
  1706. hash = hash_djb2_one_32(r[i].hash(),hash);
  1707. }
  1708. return hash;
  1709. } break;
  1710. case VECTOR2_ARRAY: {
  1711. uint32_t hash=5831;
  1712. const DVector<Vector2>& arr = *reinterpret_cast<const DVector<Vector2>* >(_data._mem);
  1713. int len = arr.size();
  1714. DVector<Vector2>::Read r = arr.read();
  1715. for(int i=0;i<len;i++) {
  1716. hash = hash_djb2_one_float(r[i].x,hash);
  1717. hash = hash_djb2_one_float(r[i].y,hash);
  1718. }
  1719. return hash;
  1720. } break;
  1721. case VECTOR3_ARRAY: {
  1722. uint32_t hash=5831;
  1723. const DVector<Vector3>& arr = *reinterpret_cast<const DVector<Vector3>* >(_data._mem);
  1724. int len = arr.size();
  1725. DVector<Vector3>::Read r = arr.read();
  1726. for(int i=0;i<len;i++) {
  1727. hash = hash_djb2_one_float(r[i].x,hash);
  1728. hash = hash_djb2_one_float(r[i].y,hash);
  1729. hash = hash_djb2_one_float(r[i].z,hash);
  1730. }
  1731. return hash;
  1732. } break;
  1733. case COLOR_ARRAY: {
  1734. uint32_t hash=5831;
  1735. const DVector<Color>& arr = *reinterpret_cast<const DVector<Color>* >(_data._mem);
  1736. int len = arr.size();
  1737. DVector<Color>::Read r = arr.read();
  1738. for(int i=0;i<len;i++) {
  1739. hash = hash_djb2_one_float(r[i].r,hash);
  1740. hash = hash_djb2_one_float(r[i].g,hash);
  1741. hash = hash_djb2_one_float(r[i].b,hash);
  1742. hash = hash_djb2_one_float(r[i].a,hash);
  1743. }
  1744. return hash;
  1745. } break;
  1746. default: {}
  1747. }
  1748. return 0;
  1749. }
  1750. bool Variant::is_ref() const {
  1751. return type==OBJECT && !_get_obj().ref.is_null();
  1752. }
  1753. Vector<Variant> varray() {
  1754. return Vector<Variant>();
  1755. }
  1756. Vector<Variant> varray(const Variant& p_arg1) {
  1757. Vector<Variant> v;
  1758. v.push_back(p_arg1);
  1759. return v;
  1760. }
  1761. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2) {
  1762. Vector<Variant> v;
  1763. v.push_back(p_arg1);
  1764. v.push_back(p_arg2);
  1765. return v;
  1766. }
  1767. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3) {
  1768. Vector<Variant> v;
  1769. v.push_back(p_arg1);
  1770. v.push_back(p_arg2);
  1771. v.push_back(p_arg3);
  1772. return v;
  1773. }
  1774. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4) {
  1775. Vector<Variant> v;
  1776. v.push_back(p_arg1);
  1777. v.push_back(p_arg2);
  1778. v.push_back(p_arg3);
  1779. v.push_back(p_arg4);
  1780. return v;
  1781. }
  1782. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5) {
  1783. Vector<Variant> v;
  1784. v.push_back(p_arg1);
  1785. v.push_back(p_arg2);
  1786. v.push_back(p_arg3);
  1787. v.push_back(p_arg4);
  1788. v.push_back(p_arg5);
  1789. return v;
  1790. }
  1791. void Variant::static_assign(const Variant& p_variant) {
  1792. }
  1793. bool Variant::is_shared() const {
  1794. switch(type) {
  1795. case OBJECT: return true;
  1796. case ARRAY: return reinterpret_cast<const Array*>(_data._mem)->is_shared();
  1797. case DICTIONARY: return reinterpret_cast<const Dictionary*>(_data._mem)->is_shared();
  1798. default: {}
  1799. }
  1800. return false;
  1801. }
  1802. Variant Variant::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
  1803. VARIANT_ARGPTRS;
  1804. int argc=0;
  1805. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  1806. if (argptr[i]->get_type()==Variant::NIL)
  1807. break;
  1808. argc++;
  1809. }
  1810. CallError error;
  1811. Variant ret = call(p_method,argptr,argc,error);
  1812. switch(error.error) {
  1813. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1814. String err = "Invalid type for argument #"+itos(error.argument)+", expected '"+Variant::get_type_name(error.expected)+"'.";
  1815. ERR_PRINT(err.utf8().get_data());
  1816. } break;
  1817. case CallError::CALL_ERROR_INVALID_METHOD: {
  1818. String err = "Invalid method '"+p_method+"' for type '"+Variant::get_type_name(type)+"'.";
  1819. ERR_PRINT(err.utf8().get_data());
  1820. } break;
  1821. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1822. String err = "Too many arguments for method '"+p_method+"'";
  1823. ERR_PRINT(err.utf8().get_data());
  1824. } break;
  1825. default: {}
  1826. }
  1827. return ret;
  1828. }