variant.cpp 48 KB

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