Variant.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/HashMap.h"
  24. #include "../Container/Ptr.h"
  25. #include "../Math/Color.h"
  26. #include "../Math/Matrix3.h"
  27. #include "../Math/Matrix3x4.h"
  28. #include "../Math/Rect.h"
  29. #include "../Math/StringHash.h"
  30. #include <typeinfo>
  31. namespace Urho3D
  32. {
  33. /// Variant's supported types.
  34. enum VariantType
  35. {
  36. VAR_NONE = 0,
  37. VAR_INT,
  38. VAR_BOOL,
  39. VAR_FLOAT,
  40. VAR_VECTOR2,
  41. VAR_VECTOR3,
  42. VAR_VECTOR4,
  43. VAR_QUATERNION,
  44. VAR_COLOR,
  45. VAR_STRING,
  46. VAR_BUFFER,
  47. VAR_VOIDPTR,
  48. VAR_RESOURCEREF,
  49. VAR_RESOURCEREFLIST,
  50. VAR_VARIANTVECTOR,
  51. VAR_VARIANTMAP,
  52. VAR_INTRECT,
  53. VAR_INTVECTOR2,
  54. VAR_PTR,
  55. VAR_MATRIX3,
  56. VAR_MATRIX3X4,
  57. VAR_MATRIX4,
  58. VAR_DOUBLE,
  59. VAR_STRINGVECTOR,
  60. VAR_RECT,
  61. VAR_INTVECTOR3,
  62. VAR_INT64,
  63. // Add new types here
  64. VAR_CUSTOM_HEAP,
  65. VAR_CUSTOM_STACK,
  66. MAX_VAR_TYPES
  67. };
  68. class Variant;
  69. class VectorBuffer;
  70. /// Vector of variants.
  71. using VariantVector = Vector<Variant>;
  72. /// Vector of strings.
  73. using StringVector = Vector<String>;
  74. /// Map of variants.
  75. using VariantMap = HashMap<StringHash, Variant>;
  76. /// Typed resource reference.
  77. struct URHO3D_API ResourceRef
  78. {
  79. /// Construct.
  80. ResourceRef()
  81. {
  82. }
  83. /// Construct with type only and empty id.
  84. ResourceRef(StringHash type) :
  85. type_(type)
  86. {
  87. }
  88. /// Construct with type and resource name.
  89. ResourceRef(StringHash type, const String& name) :
  90. type_(type),
  91. name_(name)
  92. {
  93. }
  94. /// Construct with type and resource name.
  95. ResourceRef(const String& type, const String& name) :
  96. type_(type),
  97. name_(name)
  98. {
  99. }
  100. /// Construct with type and resource name.
  101. ResourceRef(const char* type, const char* name) :
  102. type_(type),
  103. name_(name)
  104. {
  105. }
  106. /// Construct from another ResourceRef.
  107. ResourceRef(const ResourceRef& rhs) :
  108. type_(rhs.type_),
  109. name_(rhs.name_)
  110. {
  111. }
  112. /// Object type.
  113. StringHash type_;
  114. /// Object name.
  115. String name_;
  116. /// Test for equality with another reference.
  117. bool operator ==(const ResourceRef& rhs) const { return type_ == rhs.type_ && name_ == rhs.name_; }
  118. /// Test for inequality with another reference.
  119. bool operator !=(const ResourceRef& rhs) const { return type_ != rhs.type_ || name_ != rhs.name_; }
  120. };
  121. /// %List of typed resource references.
  122. struct URHO3D_API ResourceRefList
  123. {
  124. /// Construct.
  125. ResourceRefList()
  126. {
  127. }
  128. /// Construct with type only.
  129. ResourceRefList(StringHash type) :
  130. type_(type)
  131. {
  132. }
  133. /// Construct with type and id list.
  134. ResourceRefList(StringHash type, const StringVector& names) :
  135. type_(type),
  136. names_(names)
  137. {
  138. }
  139. /// Object type.
  140. StringHash type_;
  141. /// List of object names.
  142. StringVector names_;
  143. /// Test for equality with another reference list.
  144. bool operator ==(const ResourceRefList& rhs) const { return type_ == rhs.type_ && names_ == rhs.names_; }
  145. /// Test for inequality with another reference list.
  146. bool operator !=(const ResourceRefList& rhs) const { return type_ != rhs.type_ || names_ != rhs.names_; }
  147. };
  148. /// Custom variant value. This type is not abstract to store it in the VariantValue by value.
  149. class CustomVariantValue
  150. {
  151. // GetValuePtr expects that CustomVariantValue is always convertible to CustomVariantValueImpl<T>.
  152. template <class T> friend class CustomVariantValueImpl;
  153. private:
  154. /// Construct from type info.
  155. CustomVariantValue(const std::type_info& typeInfo) : typeInfo_(typeInfo) {}
  156. public:
  157. /// Construct empty.
  158. CustomVariantValue() : typeInfo_(typeid(void)) { }
  159. /// Destruct.
  160. virtual ~CustomVariantValue() { }
  161. /// Get the type info.
  162. const std::type_info& GetTypeInfo() const { return typeInfo_; }
  163. /// Return whether the specified type is stored.
  164. template <class T> bool IsType() const { return GetTypeInfo() == typeid(T); }
  165. /// Return pointer to value of the specified type. Return null pointer if type does not match.
  166. template <class T> T* GetValuePtr();
  167. /// Return const pointer to value of the specified type. Return null pointer if type does not match.
  168. template <class T> const T* GetValuePtr() const;
  169. /// Assign value.
  170. virtual bool Assign(const CustomVariantValue& rhs) { return false; }
  171. /// Clone.
  172. virtual CustomVariantValue* Clone() const { return nullptr; }
  173. /// Placement clone.
  174. virtual void Clone(void* dest) const { }
  175. /// Get size.
  176. virtual unsigned GetSize() const { return sizeof(CustomVariantValue); }
  177. /// Compare to another custom value.
  178. virtual bool Compare(const CustomVariantValue& rhs) const { (void)rhs; return false; }
  179. /// Compare to zero.
  180. virtual bool IsZero() const { return false; }
  181. /// Convert custom value to string.
  182. virtual String ToString() const { return String::EMPTY; }
  183. private:
  184. /// Type info.
  185. const std::type_info& typeInfo_;
  186. };
  187. /// Custom variant value type traits. Specialize the template to implement custom type behavior.
  188. template <class T> struct CustomVariantValueTraits
  189. {
  190. /// Compare types.
  191. static bool Compare(const T& lhs, const T& rhs) { (void)lhs, rhs; return false; }
  192. /// Check whether the value is zero.
  193. static bool IsZero(const T& value) { (void)value; return false; }
  194. /// Convert type to string.
  195. static String ToString(const T& value) { (void)value; return String::EMPTY; }
  196. };
  197. /// Custom variant value implementation.
  198. template <class T> class CustomVariantValueImpl final : public CustomVariantValue
  199. {
  200. public:
  201. /// This class name.
  202. using ClassName = CustomVariantValueImpl<T>;
  203. /// Type traits.
  204. using Traits = CustomVariantValueTraits<T>;
  205. /// Construct from value.
  206. CustomVariantValueImpl(const T& value) : CustomVariantValue(typeid(T)), value_(value) {}
  207. /// Set value.
  208. void SetValue(const T& value) { value_ = value; }
  209. /// Get value.
  210. T& GetValue() { return value_; }
  211. /// Get const value.
  212. const T& GetValue() const { return value_; }
  213. /// Assign value.
  214. virtual bool Assign(const CustomVariantValue& rhs) override
  215. {
  216. if (const T* rhsValue = rhs.GetValuePtr<T>())
  217. {
  218. SetValue(*rhsValue);
  219. return true;
  220. }
  221. return false;
  222. }
  223. /// Clone.
  224. virtual CustomVariantValue* Clone() const override { return new ClassName(value_); }
  225. /// Placement clone.
  226. virtual void Clone(void* dest) const override { new (dest) ClassName(value_); }
  227. /// Get size.
  228. virtual unsigned GetSize() const override { return sizeof(ClassName); }
  229. /// Compare to another custom value.
  230. virtual bool Compare(const CustomVariantValue& rhs) const override
  231. {
  232. const T* rhsValue = rhs.GetValuePtr<T>();
  233. return rhsValue && Traits::Compare(value_, *rhsValue);
  234. }
  235. /// Compare to zero.
  236. virtual bool IsZero() const override { return Traits::IsZero(value_);}
  237. /// Convert custom value to string.
  238. virtual String ToString() const override { return Traits::ToString(value_); }
  239. private:
  240. /// Value.
  241. T value_;
  242. };
  243. /// Make custom variant value.
  244. template <typename T> CustomVariantValueImpl<T> MakeCustomValue(const T& value) { return CustomVariantValueImpl<T>(value); }
  245. /// Size of variant value. 16 bytes on 32-bit platform, 32 bytes on 64-bit platform.
  246. static const unsigned VARIANT_VALUE_SIZE = sizeof(void*) * 4;
  247. /// Union for the possible variant values. Objects exceeding the VARIANT_VALUE_SIZE are allocated on the heap.
  248. union VariantValue
  249. {
  250. unsigned char storage_[VARIANT_VALUE_SIZE];
  251. int int_;
  252. bool bool_;
  253. float float_;
  254. Vector2 vector2_;
  255. Vector3 vector3_;
  256. Vector4 vector4_;
  257. Quaternion quaternion_;
  258. Color color_;
  259. String string_;
  260. PODVector<unsigned char> buffer_;
  261. void* voidPtr_;
  262. ResourceRef resourceRef_;
  263. ResourceRefList resourceRefList_;
  264. VariantVector variantVector_;
  265. VariantMap variantMap_;
  266. IntRect intRect_;
  267. IntVector2 intVector2_;
  268. WeakPtr<RefCounted> weakPtr_;
  269. Matrix3* matrix3_;
  270. Matrix3x4* matrix3x4_;
  271. Matrix4* matrix4_;
  272. double double_;
  273. StringVector stringVector_;
  274. Rect rect_;
  275. IntVector3 intVector3_;
  276. long long int64_;
  277. CustomVariantValue* customValueHeap_;
  278. CustomVariantValue customValueStack_;
  279. /// Construct uninitialized.
  280. VariantValue() { }
  281. /// Non-copyable.
  282. VariantValue(const VariantValue& value) = delete;
  283. /// Destruct.
  284. ~VariantValue() { }
  285. };
  286. static_assert(sizeof(VariantValue) == VARIANT_VALUE_SIZE, "Unexpected size of VariantValue");
  287. /// Variable that supports a fixed set of types.
  288. class URHO3D_API Variant
  289. {
  290. public:
  291. /// Construct empty.
  292. Variant() { }
  293. /// Construct from integer.
  294. Variant(int value)
  295. {
  296. *this = value;
  297. }
  298. /// Construct from 64 bit integer.
  299. Variant(long long value)
  300. {
  301. *this = value;
  302. }
  303. /// Construct from unsigned integer.
  304. Variant(unsigned value)
  305. {
  306. *this = (int)value;
  307. }
  308. /// Construct from unsigned integer.
  309. Variant(unsigned long long value)
  310. {
  311. *this = (long long)value;
  312. }
  313. /// Construct from a string hash (convert to integer).
  314. Variant(const StringHash& value)
  315. {
  316. *this = (int)value.Value();
  317. }
  318. /// Construct from a bool.
  319. Variant(bool value)
  320. {
  321. *this = value;
  322. }
  323. /// Construct from a float.
  324. Variant(float value)
  325. {
  326. *this = value;
  327. }
  328. /// Construct from a double.
  329. Variant(double value)
  330. {
  331. *this = value;
  332. }
  333. /// Construct from a Vector2.
  334. Variant(const Vector2& value)
  335. {
  336. *this = value;
  337. }
  338. /// Construct from a Vector3.
  339. Variant(const Vector3& value)
  340. {
  341. *this = value;
  342. }
  343. /// Construct from a Vector4.
  344. Variant(const Vector4& value)
  345. {
  346. *this = value;
  347. }
  348. /// Construct from a quaternion.
  349. Variant(const Quaternion& value)
  350. {
  351. *this = value;
  352. }
  353. /// Construct from a color.
  354. Variant(const Color& value)
  355. {
  356. *this = value;
  357. }
  358. /// Construct from a string.
  359. Variant(const String& value)
  360. {
  361. *this = value;
  362. }
  363. /// Construct from a C string.
  364. Variant(const char* value)
  365. {
  366. *this = value;
  367. }
  368. /// Construct from a buffer.
  369. Variant(const PODVector<unsigned char>& value)
  370. {
  371. *this = value;
  372. }
  373. /// Construct from a %VectorBuffer and store as a buffer.
  374. Variant(const VectorBuffer& value)
  375. {
  376. *this = value;
  377. }
  378. /// Construct from a pointer.
  379. Variant(void* value)
  380. {
  381. *this = value;
  382. }
  383. /// Construct from a resource reference.
  384. Variant(const ResourceRef& value)
  385. {
  386. *this = value;
  387. }
  388. /// Construct from a resource reference list.
  389. Variant(const ResourceRefList& value)
  390. {
  391. *this = value;
  392. }
  393. /// Construct from a variant vector.
  394. Variant(const VariantVector& value)
  395. {
  396. *this = value;
  397. }
  398. /// Construct from a variant map.
  399. Variant(const VariantMap& value)
  400. {
  401. *this = value;
  402. }
  403. /// Construct from a string vector.
  404. Variant(const StringVector& value)
  405. {
  406. *this = value;
  407. }
  408. /// Construct from a rect.
  409. Variant(const Rect& value)
  410. {
  411. *this = value;
  412. }
  413. /// Construct from an integer rect.
  414. Variant(const IntRect& value)
  415. {
  416. *this = value;
  417. }
  418. /// Construct from an IntVector2.
  419. Variant(const IntVector2& value)
  420. {
  421. *this = value;
  422. }
  423. /// Construct from an IntVector3.
  424. Variant(const IntVector3& value)
  425. {
  426. *this = value;
  427. }
  428. /// Construct from a RefCounted pointer. The object will be stored internally in a WeakPtr so that its expiration can be detected safely.
  429. Variant(RefCounted* value)
  430. {
  431. *this = value;
  432. }
  433. /// Construct from a Matrix3.
  434. Variant(const Matrix3& value)
  435. {
  436. *this = value;
  437. }
  438. /// Construct from a Matrix3x4.
  439. Variant(const Matrix3x4& value)
  440. {
  441. *this = value;
  442. }
  443. /// Construct from a Matrix4.
  444. Variant(const Matrix4& value)
  445. {
  446. *this = value;
  447. }
  448. /// Construct from custom value.
  449. template <class T>
  450. Variant(const CustomVariantValueImpl<T>& value)
  451. {
  452. *this = value;
  453. }
  454. /// Construct from type and value.
  455. Variant(const String& type, const String& value)
  456. {
  457. FromString(type, value);
  458. }
  459. /// Construct from type and value.
  460. Variant(VariantType type, const String& value)
  461. {
  462. FromString(type, value);
  463. }
  464. /// Construct from type and value.
  465. Variant(const char* type, const char* value)
  466. {
  467. FromString(type, value);
  468. }
  469. /// Construct from type and value.
  470. Variant(VariantType type, const char* value)
  471. {
  472. FromString(type, value);
  473. }
  474. /// Copy-construct from another variant.
  475. Variant(const Variant& value)
  476. {
  477. *this = value;
  478. }
  479. /// Destruct.
  480. ~Variant()
  481. {
  482. SetType(VAR_NONE);
  483. }
  484. /// Reset to empty.
  485. void Clear()
  486. {
  487. SetType(VAR_NONE);
  488. }
  489. /// Assign from another variant.
  490. Variant& operator =(const Variant& rhs);
  491. /// Assign from an integer.
  492. Variant& operator =(int rhs)
  493. {
  494. SetType(VAR_INT);
  495. value_.int_ = rhs;
  496. return *this;
  497. }
  498. /// Assign from 64 bit integer.
  499. Variant& operator =(long long rhs)
  500. {
  501. SetType(VAR_INT64);
  502. value_.int64_ = rhs;
  503. return *this;
  504. }
  505. /// Assign from unsigned 64 bit integer.
  506. Variant& operator =(unsigned long long rhs)
  507. {
  508. SetType(VAR_INT64);
  509. value_.int64_ = static_cast<long long>(rhs);
  510. return *this;
  511. }
  512. /// Assign from an unsigned integer.
  513. Variant& operator =(unsigned rhs)
  514. {
  515. SetType(VAR_INT);
  516. value_.int_ = (int)rhs;
  517. return *this;
  518. }
  519. /// Assign from a StringHash (convert to integer.)
  520. Variant& operator =(const StringHash& rhs)
  521. {
  522. SetType(VAR_INT);
  523. value_.int_ = (int)rhs.Value();
  524. return *this;
  525. }
  526. /// Assign from a bool.
  527. Variant& operator =(bool rhs)
  528. {
  529. SetType(VAR_BOOL);
  530. value_.bool_ = rhs;
  531. return *this;
  532. }
  533. /// Assign from a float.
  534. Variant& operator =(float rhs)
  535. {
  536. SetType(VAR_FLOAT);
  537. value_.float_ = rhs;
  538. return *this;
  539. }
  540. /// Assign from a double.
  541. Variant& operator =(double rhs)
  542. {
  543. SetType(VAR_DOUBLE);
  544. value_.double_ = rhs;
  545. return *this;
  546. }
  547. /// Assign from a Vector2.
  548. Variant& operator =(const Vector2& rhs)
  549. {
  550. SetType(VAR_VECTOR2);
  551. value_.vector2_ = rhs;
  552. return *this;
  553. }
  554. /// Assign from a Vector3.
  555. Variant& operator =(const Vector3& rhs)
  556. {
  557. SetType(VAR_VECTOR3);
  558. value_.vector3_ = rhs;
  559. return *this;
  560. }
  561. /// Assign from a Vector4.
  562. Variant& operator =(const Vector4& rhs)
  563. {
  564. SetType(VAR_VECTOR4);
  565. value_.vector4_ = rhs;
  566. return *this;
  567. }
  568. /// Assign from a quaternion.
  569. Variant& operator =(const Quaternion& rhs)
  570. {
  571. SetType(VAR_QUATERNION);
  572. value_.quaternion_ = rhs;
  573. return *this;
  574. }
  575. /// Assign from a color.
  576. Variant& operator =(const Color& rhs)
  577. {
  578. SetType(VAR_COLOR);
  579. value_.color_ = rhs;
  580. return *this;
  581. }
  582. /// Assign from a string.
  583. Variant& operator =(const String& rhs)
  584. {
  585. SetType(VAR_STRING);
  586. value_.string_ = rhs;
  587. return *this;
  588. }
  589. /// Assign from a C string.
  590. Variant& operator =(const char* rhs)
  591. {
  592. SetType(VAR_STRING);
  593. value_.string_ = rhs;
  594. return *this;
  595. }
  596. /// Assign from a buffer.
  597. Variant& operator =(const PODVector<unsigned char>& rhs)
  598. {
  599. SetType(VAR_BUFFER);
  600. value_.buffer_ = rhs;
  601. return *this;
  602. }
  603. /// Assign from a %VectorBuffer and store as a buffer.
  604. Variant& operator =(const VectorBuffer& rhs);
  605. /// Assign from a void pointer.
  606. Variant& operator =(void* rhs)
  607. {
  608. SetType(VAR_VOIDPTR);
  609. value_.voidPtr_ = rhs;
  610. return *this;
  611. }
  612. /// Assign from a resource reference.
  613. Variant& operator =(const ResourceRef& rhs)
  614. {
  615. SetType(VAR_RESOURCEREF);
  616. value_.resourceRef_ = rhs;
  617. return *this;
  618. }
  619. /// Assign from a resource reference list.
  620. Variant& operator =(const ResourceRefList& rhs)
  621. {
  622. SetType(VAR_RESOURCEREFLIST);
  623. value_.resourceRefList_ = rhs;
  624. return *this;
  625. }
  626. /// Assign from a variant vector.
  627. Variant& operator =(const VariantVector& rhs)
  628. {
  629. SetType(VAR_VARIANTVECTOR);
  630. value_.variantVector_ = rhs;
  631. return *this;
  632. }
  633. /// Assign from a string vector.
  634. Variant& operator =(const StringVector& rhs)
  635. {
  636. SetType(VAR_STRINGVECTOR);
  637. value_.stringVector_ = rhs;
  638. return *this;
  639. }
  640. /// Assign from a variant map.
  641. Variant& operator =(const VariantMap& rhs)
  642. {
  643. SetType(VAR_VARIANTMAP);
  644. value_.variantMap_ = rhs;
  645. return *this;
  646. }
  647. /// Assign from a rect.
  648. Variant& operator =(const Rect& rhs)
  649. {
  650. SetType(VAR_RECT);
  651. value_.rect_ = rhs;
  652. return *this;
  653. }
  654. /// Assign from an integer rect.
  655. Variant& operator =(const IntRect& rhs)
  656. {
  657. SetType(VAR_INTRECT);
  658. value_.intRect_ = rhs;
  659. return *this;
  660. }
  661. /// Assign from an IntVector2.
  662. Variant& operator =(const IntVector2& rhs)
  663. {
  664. SetType(VAR_INTVECTOR2);
  665. value_.intVector2_ = rhs;
  666. return *this;
  667. }
  668. /// Assign from an IntVector3.
  669. Variant& operator =(const IntVector3& rhs)
  670. {
  671. SetType(VAR_INTVECTOR3);
  672. value_.intVector3_ = rhs;
  673. return *this;
  674. }
  675. /// Assign from a RefCounted pointer. The object will be stored internally in a WeakPtr so that its expiration can be detected safely.
  676. Variant& operator =(RefCounted* rhs)
  677. {
  678. SetType(VAR_PTR);
  679. value_.weakPtr_ = rhs;
  680. return *this;
  681. }
  682. /// Assign from a Matrix3.
  683. Variant& operator =(const Matrix3& rhs)
  684. {
  685. SetType(VAR_MATRIX3);
  686. *value_.matrix3_ = rhs;
  687. return *this;
  688. }
  689. /// Assign from a Matrix3x4.
  690. Variant& operator =(const Matrix3x4& rhs)
  691. {
  692. SetType(VAR_MATRIX3X4);
  693. *value_.matrix3x4_ = rhs;
  694. return *this;
  695. }
  696. /// Assign from a Matrix4.
  697. Variant& operator =(const Matrix4& rhs)
  698. {
  699. SetType(VAR_MATRIX4);
  700. *value_.matrix4_ = rhs;
  701. return *this;
  702. }
  703. /// Assign from custom value.
  704. template <class T>
  705. Variant& operator =(const CustomVariantValueImpl<T>& value)
  706. {
  707. SetCustomVariantValue(value);
  708. return *this;
  709. }
  710. /// Test for equality with another variant.
  711. bool operator ==(const Variant& rhs) const;
  712. /// Test for equality with an integer. To return true, both the type and value must match.
  713. bool operator ==(int rhs) const { return type_ == VAR_INT ? value_.int_ == rhs : false; }
  714. /// Test for equality with an unsigned 64 bit integer. To return true, both the type and value must match.
  715. bool operator ==(unsigned rhs) const { return type_ == VAR_INT ? value_.int_ == static_cast<int>(rhs) : false; }
  716. /// Test for equality with an 64 bit integer. To return true, both the type and value must match.
  717. bool operator ==(long long rhs) const { return type_ == VAR_INT64 ? value_.int64_ == rhs : false; }
  718. /// Test for equality with an unsigned integer. To return true, both the type and value must match.
  719. bool operator ==(unsigned long long rhs) const { return type_ == VAR_INT64 ? value_.int64_ == static_cast<long long>(rhs) : false; }
  720. /// Test for equality with a bool. To return true, both the type and value must match.
  721. bool operator ==(bool rhs) const { return type_ == VAR_BOOL ? value_.bool_ == rhs : false; }
  722. /// Test for equality with a float. To return true, both the type and value must match.
  723. bool operator ==(float rhs) const { return type_ == VAR_FLOAT ? value_.float_ == rhs : false; }
  724. /// Test for equality with a double. To return true, both the type and value must match.
  725. bool operator ==(double rhs) const { return type_ == VAR_DOUBLE ? value_.double_ == rhs : false; }
  726. /// Test for equality with a Vector2. To return true, both the type and value must match.
  727. bool operator ==(const Vector2& rhs) const
  728. {
  729. return type_ == VAR_VECTOR2 ? value_.vector2_ == rhs : false;
  730. }
  731. /// Test for equality with a Vector3. To return true, both the type and value must match.
  732. bool operator ==(const Vector3& rhs) const
  733. {
  734. return type_ == VAR_VECTOR3 ? value_.vector3_ == rhs : false;
  735. }
  736. /// Test for equality with a Vector4. To return true, both the type and value must match.
  737. bool operator ==(const Vector4& rhs) const
  738. {
  739. return type_ == VAR_VECTOR4 ? value_.vector4_ == rhs : false;
  740. }
  741. /// Test for equality with a quaternion. To return true, both the type and value must match.
  742. bool operator ==(const Quaternion& rhs) const
  743. {
  744. return type_ == VAR_QUATERNION ? value_.quaternion_ == rhs : false;
  745. }
  746. /// Test for equality with a color. To return true, both the type and value must match.
  747. bool operator ==(const Color& rhs) const
  748. {
  749. return type_ == VAR_COLOR ? value_.color_ == rhs : false;
  750. }
  751. /// Test for equality with a string. To return true, both the type and value must match.
  752. bool operator ==(const String& rhs) const
  753. {
  754. return type_ == VAR_STRING ? value_.string_ == rhs : false;
  755. }
  756. /// Test for equality with a buffer. To return true, both the type and value must match.
  757. bool operator ==(const PODVector<unsigned char>& rhs) const;
  758. /// Test for equality with a %VectorBuffer. To return true, both the type and value must match.
  759. bool operator ==(const VectorBuffer& rhs) const;
  760. /// Test for equality with a void pointer. To return true, both the type and value must match, with the exception that a RefCounted pointer is also allowed.
  761. bool operator ==(void* rhs) const
  762. {
  763. if (type_ == VAR_VOIDPTR)
  764. return value_.voidPtr_ == rhs;
  765. else if (type_ == VAR_PTR)
  766. return value_.weakPtr_ == rhs;
  767. else
  768. return false;
  769. }
  770. /// Test for equality with a resource reference. To return true, both the type and value must match.
  771. bool operator ==(const ResourceRef& rhs) const
  772. {
  773. return type_ == VAR_RESOURCEREF ? value_.resourceRef_ == rhs : false;
  774. }
  775. /// Test for equality with a resource reference list. To return true, both the type and value must match.
  776. bool operator ==(const ResourceRefList& rhs) const
  777. {
  778. return type_ == VAR_RESOURCEREFLIST ? value_.resourceRefList_ == rhs : false;
  779. }
  780. /// Test for equality with a variant vector. To return true, both the type and value must match.
  781. bool operator ==(const VariantVector& rhs) const
  782. {
  783. return type_ == VAR_VARIANTVECTOR ? value_.variantVector_ == rhs : false;
  784. }
  785. /// Test for equality with a string vector. To return true, both the type and value must match.
  786. bool operator ==(const StringVector& rhs) const
  787. {
  788. return type_ == VAR_STRINGVECTOR ? value_.stringVector_ == rhs : false;
  789. }
  790. /// Test for equality with a variant map. To return true, both the type and value must match.
  791. bool operator ==(const VariantMap& rhs) const
  792. {
  793. return type_ == VAR_VARIANTMAP ? value_.variantMap_ == rhs : false;
  794. }
  795. /// Test for equality with a rect. To return true, both the type and value must match.
  796. bool operator ==(const Rect& rhs) const
  797. {
  798. return type_ == VAR_RECT ? value_.rect_ == rhs : false;
  799. }
  800. /// Test for equality with an integer rect. To return true, both the type and value must match.
  801. bool operator ==(const IntRect& rhs) const
  802. {
  803. return type_ == VAR_INTRECT ? value_.intRect_ == rhs : false;
  804. }
  805. /// Test for equality with an IntVector2. To return true, both the type and value must match.
  806. bool operator ==(const IntVector2& rhs) const
  807. {
  808. return type_ == VAR_INTVECTOR2 ? value_.intVector2_ == rhs : false;
  809. }
  810. /// Test for equality with an IntVector3. To return true, both the type and value must match.
  811. bool operator ==(const IntVector3& rhs) const
  812. {
  813. return type_ == VAR_INTVECTOR3 ? value_.intVector3_ == rhs : false;
  814. }
  815. /// Test for equality with a StringHash. To return true, both the type and value must match.
  816. bool operator ==(const StringHash& rhs) const { return type_ == VAR_INT ? static_cast<unsigned>(value_.int_) == rhs.Value() : false; }
  817. /// Test for equality with a RefCounted pointer. To return true, both the type and value must match, with the exception that void pointer is also allowed.
  818. bool operator ==(RefCounted* rhs) const
  819. {
  820. if (type_ == VAR_PTR)
  821. return value_.weakPtr_ == rhs;
  822. else if (type_ == VAR_VOIDPTR)
  823. return value_.voidPtr_ == rhs;
  824. else
  825. return false;
  826. }
  827. /// Test for equality with a Matrix3. To return true, both the type and value must match.
  828. bool operator ==(const Matrix3& rhs) const
  829. {
  830. return type_ == VAR_MATRIX3 ? *value_.matrix3_ == rhs : false;
  831. }
  832. /// Test for equality with a Matrix3x4. To return true, both the type and value must match.
  833. bool operator ==(const Matrix3x4& rhs) const
  834. {
  835. return type_ == VAR_MATRIX3X4 ? *value_.matrix3x4_ == rhs : false;
  836. }
  837. /// Test for equality with a Matrix4. To return true, both the type and value must match.
  838. bool operator ==(const Matrix4& rhs) const
  839. {
  840. return type_ == VAR_MATRIX4 ? *value_.matrix4_ == rhs : false;
  841. }
  842. /// Test for inequality with another variant.
  843. bool operator !=(const Variant& rhs) const { return !(*this == rhs); }
  844. /// Test for inequality with an integer.
  845. bool operator !=(int rhs) const { return !(*this == rhs); }
  846. /// Test for inequality with an unsigned integer.
  847. bool operator !=(unsigned rhs) const { return !(*this == rhs); }
  848. /// Test for inequality with an 64 bit integer.
  849. bool operator !=(long long rhs) const { return !(*this == rhs); }
  850. /// Test for inequality with an unsigned 64 bit integer.
  851. bool operator !=(unsigned long long rhs) const { return !(*this == rhs); }
  852. /// Test for inequality with a bool.
  853. bool operator !=(bool rhs) const { return !(*this == rhs); }
  854. /// Test for inequality with a float.
  855. bool operator !=(float rhs) const { return !(*this == rhs); }
  856. /// Test for inequality with a double.
  857. bool operator !=(double rhs) const { return !(*this == rhs); }
  858. /// Test for inequality with a Vector2.
  859. bool operator !=(const Vector2& rhs) const { return !(*this == rhs); }
  860. /// Test for inequality with a Vector3.
  861. bool operator !=(const Vector3& rhs) const { return !(*this == rhs); }
  862. /// Test for inequality with an Vector4.
  863. bool operator !=(const Vector4& rhs) const { return !(*this == rhs); }
  864. /// Test for inequality with a Quaternion.
  865. bool operator !=(const Quaternion& rhs) const { return !(*this == rhs); }
  866. /// Test for inequality with a string.
  867. bool operator !=(const String& rhs) const { return !(*this == rhs); }
  868. /// Test for inequality with a buffer.
  869. bool operator !=(const PODVector<unsigned char>& rhs) const { return !(*this == rhs); }
  870. /// Test for inequality with a %VectorBuffer.
  871. bool operator !=(const VectorBuffer& rhs) const { return !(*this == rhs); }
  872. /// Test for inequality with a pointer.
  873. bool operator !=(void* rhs) const { return !(*this == rhs); }
  874. /// Test for inequality with a resource reference.
  875. bool operator !=(const ResourceRef& rhs) const { return !(*this == rhs); }
  876. /// Test for inequality with a resource reference list.
  877. bool operator !=(const ResourceRefList& rhs) const { return !(*this == rhs); }
  878. /// Test for inequality with a variant vector.
  879. bool operator !=(const VariantVector& rhs) const { return !(*this == rhs); }
  880. /// Test for inequality with a string vector.
  881. bool operator !=(const StringVector& rhs) const { return !(*this == rhs); }
  882. /// Test for inequality with a variant map.
  883. bool operator !=(const VariantMap& rhs) const { return !(*this == rhs); }
  884. /// Test for inequality with a rect.
  885. bool operator !=(const Rect& rhs) const { return !(*this == rhs); }
  886. /// Test for inequality with an integer rect.
  887. bool operator !=(const IntRect& rhs) const { return !(*this == rhs); }
  888. /// Test for inequality with an IntVector2.
  889. bool operator !=(const IntVector2& rhs) const { return !(*this == rhs); }
  890. /// Test for inequality with an IntVector3.
  891. bool operator !=(const IntVector3& rhs) const { return !(*this == rhs); }
  892. /// Test for inequality with a StringHash.
  893. bool operator !=(const StringHash& rhs) const { return !(*this == rhs); }
  894. /// Test for inequality with a RefCounted pointer.
  895. bool operator !=(RefCounted* rhs) const { return !(*this == rhs); }
  896. /// Test for inequality with a Matrix3.
  897. bool operator !=(const Matrix3& rhs) const { return !(*this == rhs); }
  898. /// Test for inequality with a Matrix3x4.
  899. bool operator !=(const Matrix3x4& rhs) const { return !(*this == rhs); }
  900. /// Test for inequality with a Matrix4.
  901. bool operator !=(const Matrix4& rhs) const { return !(*this == rhs); }
  902. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  903. void FromString(const String& type, const String& value);
  904. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  905. void FromString(const char* type, const char* value);
  906. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  907. void FromString(VariantType type, const String& value);
  908. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  909. void FromString(VariantType type, const char* value);
  910. /// Set buffer type from a memory area.
  911. void SetBuffer(const void* data, unsigned size);
  912. /// Set custom value.
  913. void SetCustomVariantValue(const CustomVariantValue& value);
  914. /// Set custom value.
  915. template <class T> void SetCustom(const T& value) { SetCustomVariantValue(MakeCustomValue<T>(value)); }
  916. /// Return int or zero on type mismatch. Floats and doubles are converted.
  917. int GetInt() const
  918. {
  919. if (type_ == VAR_INT)
  920. return value_.int_;
  921. else if (type_ == VAR_FLOAT)
  922. return static_cast<int>(value_.float_);
  923. else if (type_ == VAR_DOUBLE)
  924. return static_cast<int>(value_.double_);
  925. else
  926. return 0;
  927. }
  928. /// Return 64 bit int or zero on type mismatch. Floats and doubles are converted.
  929. long long GetInt64() const
  930. {
  931. if (type_ == VAR_INT64)
  932. return value_.int64_;
  933. else if (type_ == VAR_INT)
  934. return value_.int_;
  935. else if (type_ == VAR_FLOAT)
  936. return static_cast<long long>(value_.float_);
  937. else if (type_ == VAR_DOUBLE)
  938. return static_cast<long long>(value_.double_);
  939. else
  940. return 0;
  941. }
  942. /// Return unsigned 64 bit int or zero on type mismatch. Floats and doubles are converted.
  943. unsigned long long GetUInt64() const
  944. {
  945. if (type_ == VAR_INT64)
  946. return static_cast<unsigned long long>(value_.int64_);
  947. else if (type_ == VAR_INT)
  948. return static_cast<unsigned long long>(value_.int_);
  949. else if (type_ == VAR_FLOAT)
  950. return static_cast<unsigned long long>(value_.float_);
  951. else if (type_ == VAR_DOUBLE)
  952. return static_cast<unsigned long long>(value_.double_);
  953. else
  954. return 0;
  955. }
  956. /// Return unsigned int or zero on type mismatch. Floats and doubles are converted.
  957. unsigned GetUInt() const
  958. {
  959. if (type_ == VAR_INT)
  960. return static_cast<unsigned>(value_.int_);
  961. else if (type_ == VAR_FLOAT)
  962. return static_cast<unsigned>(value_.float_);
  963. else if (type_ == VAR_DOUBLE)
  964. return static_cast<unsigned>(value_.double_);
  965. else
  966. return 0;
  967. }
  968. /// Return StringHash or zero on type mismatch.
  969. StringHash GetStringHash() const { return StringHash(GetUInt()); }
  970. /// Return bool or false on type mismatch.
  971. bool GetBool() const { return type_ == VAR_BOOL ? value_.bool_ : false; }
  972. /// Return float or zero on type mismatch. Ints and doubles are converted.
  973. float GetFloat() const
  974. {
  975. if (type_ == VAR_FLOAT)
  976. return value_.float_;
  977. else if (type_ == VAR_DOUBLE)
  978. return static_cast<float>(value_.double_);
  979. else if (type_ == VAR_INT)
  980. return static_cast<float>(value_.int_);
  981. else if (type_ == VAR_INT64)
  982. return static_cast<float>(value_.int64_);
  983. else
  984. return 0.0f;
  985. }
  986. /// Return double or zero on type mismatch. Ints and floats are converted.
  987. double GetDouble() const
  988. {
  989. if (type_ == VAR_DOUBLE)
  990. return value_.double_;
  991. else if (type_ == VAR_FLOAT)
  992. return value_.float_;
  993. else if (type_ == VAR_INT)
  994. return static_cast<double>(value_.int_);
  995. else if (type_ == VAR_INT64)
  996. return static_cast<double>(value_.int64_);
  997. else
  998. return 0.0;
  999. }
  1000. /// Return Vector2 or zero on type mismatch.
  1001. const Vector2& GetVector2() const { return type_ == VAR_VECTOR2 ? value_.vector2_ : Vector2::ZERO; }
  1002. /// Return Vector3 or zero on type mismatch.
  1003. const Vector3& GetVector3() const { return type_ == VAR_VECTOR3 ? value_.vector3_ : Vector3::ZERO; }
  1004. /// Return Vector4 or zero on type mismatch.
  1005. const Vector4& GetVector4() const { return type_ == VAR_VECTOR4 ? value_.vector4_ : Vector4::ZERO; }
  1006. /// Return quaternion or identity on type mismatch.
  1007. const Quaternion& GetQuaternion() const
  1008. {
  1009. return type_ == VAR_QUATERNION ? value_.quaternion_ : Quaternion::IDENTITY;
  1010. }
  1011. /// Return color or default on type mismatch. Vector4 is aliased to Color if necessary.
  1012. const Color& GetColor() const { return (type_ == VAR_COLOR || type_ == VAR_VECTOR4) ? value_.color_ : Color::WHITE; }
  1013. /// Return string or empty on type mismatch.
  1014. const String& GetString() const { return type_ == VAR_STRING ? value_.string_ : String::EMPTY; }
  1015. /// Return buffer or empty on type mismatch.
  1016. const PODVector<unsigned char>& GetBuffer() const
  1017. {
  1018. return type_ == VAR_BUFFER ? value_.buffer_ : emptyBuffer;
  1019. }
  1020. /// Return %VectorBuffer containing the buffer or empty on type mismatch.
  1021. VectorBuffer GetVectorBuffer() const;
  1022. /// Return void pointer or null on type mismatch. RefCounted pointer will be converted.
  1023. void* GetVoidPtr() const
  1024. {
  1025. if (type_ == VAR_VOIDPTR)
  1026. return value_.voidPtr_;
  1027. else if (type_ == VAR_PTR)
  1028. return value_.weakPtr_;
  1029. else
  1030. return nullptr;
  1031. }
  1032. /// Return a resource reference or empty on type mismatch.
  1033. const ResourceRef& GetResourceRef() const
  1034. {
  1035. return type_ == VAR_RESOURCEREF ? value_.resourceRef_ : emptyResourceRef;
  1036. }
  1037. /// Return a resource reference list or empty on type mismatch.
  1038. const ResourceRefList& GetResourceRefList() const
  1039. {
  1040. return type_ == VAR_RESOURCEREFLIST ? value_.resourceRefList_ : emptyResourceRefList;
  1041. }
  1042. /// Return a variant vector or empty on type mismatch.
  1043. const VariantVector& GetVariantVector() const
  1044. {
  1045. return type_ == VAR_VARIANTVECTOR ? value_.variantVector_ : emptyVariantVector;
  1046. }
  1047. /// Return a string vector or empty on type mismatch.
  1048. const StringVector& GetStringVector() const
  1049. {
  1050. return type_ == VAR_STRINGVECTOR ? value_.stringVector_ : emptyStringVector;
  1051. }
  1052. /// Return a variant map or empty on type mismatch.
  1053. const VariantMap& GetVariantMap() const
  1054. {
  1055. return type_ == VAR_VARIANTMAP ? value_.variantMap_ : emptyVariantMap;
  1056. }
  1057. /// Return a rect or empty on type mismatch.
  1058. const Rect& GetRect() const { return type_ == VAR_RECT ? value_.rect_ : Rect::ZERO; }
  1059. /// Return an integer rect or empty on type mismatch.
  1060. const IntRect& GetIntRect() const { return type_ == VAR_INTRECT ? value_.intRect_ : IntRect::ZERO; }
  1061. /// Return an IntVector2 or empty on type mismatch.
  1062. const IntVector2& GetIntVector2() const
  1063. {
  1064. return type_ == VAR_INTVECTOR2 ? value_.intVector2_ : IntVector2::ZERO;
  1065. }
  1066. /// Return an IntVector3 or empty on type mismatch.
  1067. const IntVector3& GetIntVector3() const
  1068. {
  1069. return type_ == VAR_INTVECTOR3 ? value_.intVector3_ : IntVector3::ZERO;
  1070. }
  1071. /// Return a RefCounted pointer or null on type mismatch. Will return null if holding a void pointer, as it can not be safely verified that the object is a RefCounted.
  1072. RefCounted* GetPtr() const
  1073. {
  1074. return type_ == VAR_PTR ? value_.weakPtr_ : nullptr;
  1075. }
  1076. /// Return a Matrix3 or identity on type mismatch.
  1077. const Matrix3& GetMatrix3() const
  1078. {
  1079. return type_ == VAR_MATRIX3 ? *value_.matrix3_ : Matrix3::IDENTITY;
  1080. }
  1081. /// Return a Matrix3x4 or identity on type mismatch.
  1082. const Matrix3x4& GetMatrix3x4() const
  1083. {
  1084. return type_ == VAR_MATRIX3X4 ? *value_.matrix3x4_ : Matrix3x4::IDENTITY;
  1085. }
  1086. /// Return a Matrix4 or identity on type mismatch.
  1087. const Matrix4& GetMatrix4() const
  1088. {
  1089. return type_ == VAR_MATRIX4 ? *value_.matrix4_ : Matrix4::IDENTITY;
  1090. }
  1091. /// Return pointer to custom variant value.
  1092. CustomVariantValue* GetCustomVariantValuePtr()
  1093. {
  1094. return const_cast<CustomVariantValue*>(const_cast<const Variant*>(this)->GetCustomVariantValuePtr());
  1095. }
  1096. /// Return const pointer to custom variant value.
  1097. const CustomVariantValue* GetCustomVariantValuePtr() const
  1098. {
  1099. if (type_ == VAR_CUSTOM_HEAP)
  1100. return value_.customValueHeap_;
  1101. else if (type_ == VAR_CUSTOM_STACK)
  1102. return &value_.customValueStack_;
  1103. else
  1104. return nullptr;
  1105. }
  1106. /// Return custom variant value or default-constructed on type mismatch.
  1107. template <class T> T GetCustom() const
  1108. {
  1109. if (const CustomVariantValue* value = GetCustomVariantValuePtr())
  1110. {
  1111. if (value->IsType<T>())
  1112. return *value->GetValuePtr<T>();
  1113. }
  1114. return T();
  1115. }
  1116. /// Return true if specified custom type is stored in the variant.
  1117. template <class T> bool IsCustomType() const
  1118. {
  1119. if (const CustomVariantValue* custom = GetCustomVariantValuePtr())
  1120. return custom->IsType<T>();
  1121. else
  1122. return false;
  1123. }
  1124. /// Return value's type.
  1125. VariantType GetType() const { return type_; }
  1126. /// Return value's type name.
  1127. String GetTypeName() const;
  1128. /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
  1129. String ToString() const;
  1130. /// Return true when the variant value is considered zero according to its actual type.
  1131. bool IsZero() const;
  1132. /// Return true when the variant is empty (i.e. not initialized yet).
  1133. bool IsEmpty() const { return type_ == VAR_NONE; }
  1134. /// Return true when the variant stores custom type.
  1135. bool IsCustom() const { return type_ == VAR_CUSTOM_HEAP || type_ == VAR_CUSTOM_STACK; }
  1136. /// Return the value, template version.
  1137. template <class T> T Get() const;
  1138. /// Return a pointer to a modifiable buffer or null on type mismatch.
  1139. PODVector<unsigned char>* GetBufferPtr()
  1140. {
  1141. return type_ == VAR_BUFFER ? &value_.buffer_ : nullptr;
  1142. }
  1143. /// Return a pointer to a modifiable variant vector or null on type mismatch.
  1144. VariantVector* GetVariantVectorPtr() { return type_ == VAR_VARIANTVECTOR ? &value_.variantVector_ : nullptr; }
  1145. /// Return a pointer to a modifiable string vector or null on type mismatch.
  1146. StringVector* GetStringVectorPtr() { return type_ == VAR_STRINGVECTOR ? &value_.stringVector_ : nullptr; }
  1147. /// Return a pointer to a modifiable variant map or null on type mismatch.
  1148. VariantMap* GetVariantMapPtr() { return type_ == VAR_VARIANTMAP ? &value_.variantMap_ : nullptr; }
  1149. /// Return a pointer to a modifiable custom variant value or null on type mismatch.
  1150. template <class T> T* GetCustomPtr()
  1151. {
  1152. if (const CustomVariantValue* value = GetCustomVariantValuePtr())
  1153. {
  1154. if (value->IsType<T>())
  1155. return value->GetValuePtr<T>();
  1156. }
  1157. return nullptr;
  1158. }
  1159. /// Return name for variant type.
  1160. static String GetTypeName(VariantType type);
  1161. /// Return variant type from type name.
  1162. static VariantType GetTypeFromName(const String& typeName);
  1163. /// Return variant type from type name.
  1164. static VariantType GetTypeFromName(const char* typeName);
  1165. /// Empty variant.
  1166. static const Variant EMPTY;
  1167. /// Empty buffer.
  1168. static const PODVector<unsigned char> emptyBuffer;
  1169. /// Empty resource reference.
  1170. static const ResourceRef emptyResourceRef;
  1171. /// Empty resource reference list.
  1172. static const ResourceRefList emptyResourceRefList;
  1173. /// Empty variant map.
  1174. static const VariantMap emptyVariantMap;
  1175. /// Empty variant vector.
  1176. static const VariantVector emptyVariantVector;
  1177. /// Empty string vector.
  1178. static const StringVector emptyStringVector;
  1179. private:
  1180. /// Set new type and allocate/deallocate memory as necessary.
  1181. void SetType(VariantType newType);
  1182. /// Variant type.
  1183. VariantType type_ = VAR_NONE;
  1184. /// Variant value.
  1185. VariantValue value_;
  1186. };
  1187. /// Return variant type from type.
  1188. template <typename T> VariantType GetVariantType();
  1189. // Return variant type from concrete types
  1190. template <> inline VariantType GetVariantType<int>() { return VAR_INT; }
  1191. template <> inline VariantType GetVariantType<unsigned>() { return VAR_INT; }
  1192. template <> inline VariantType GetVariantType<long long>() { return VAR_INT64; }
  1193. template <> inline VariantType GetVariantType<unsigned long long>() { return VAR_INT64; }
  1194. template <> inline VariantType GetVariantType<bool>() { return VAR_BOOL; }
  1195. template <> inline VariantType GetVariantType<float>() { return VAR_FLOAT; }
  1196. template <> inline VariantType GetVariantType<double>() { return VAR_DOUBLE; }
  1197. template <> inline VariantType GetVariantType<Vector2>() { return VAR_VECTOR2; }
  1198. template <> inline VariantType GetVariantType<Vector3>() { return VAR_VECTOR3; }
  1199. template <> inline VariantType GetVariantType<Vector4>() { return VAR_VECTOR4; }
  1200. template <> inline VariantType GetVariantType<Quaternion>() { return VAR_QUATERNION; }
  1201. template <> inline VariantType GetVariantType<Color>() { return VAR_COLOR; }
  1202. template <> inline VariantType GetVariantType<String>() { return VAR_STRING; }
  1203. template <> inline VariantType GetVariantType<StringHash>() { return VAR_INT; }
  1204. template <> inline VariantType GetVariantType<PODVector<unsigned char> >() { return VAR_BUFFER; }
  1205. template <> inline VariantType GetVariantType<ResourceRef>() { return VAR_RESOURCEREF; }
  1206. template <> inline VariantType GetVariantType<ResourceRefList>() { return VAR_RESOURCEREFLIST; }
  1207. template <> inline VariantType GetVariantType<VariantVector>() { return VAR_VARIANTVECTOR; }
  1208. template <> inline VariantType GetVariantType<StringVector>() { return VAR_STRINGVECTOR; }
  1209. template <> inline VariantType GetVariantType<VariantMap>() { return VAR_VARIANTMAP; }
  1210. template <> inline VariantType GetVariantType<Rect>() { return VAR_RECT; }
  1211. template <> inline VariantType GetVariantType<IntRect>() { return VAR_INTRECT; }
  1212. template <> inline VariantType GetVariantType<IntVector2>() { return VAR_INTVECTOR2; }
  1213. template <> inline VariantType GetVariantType<IntVector3>() { return VAR_INTVECTOR3; }
  1214. template <> inline VariantType GetVariantType<Matrix3>() { return VAR_MATRIX3; }
  1215. template <> inline VariantType GetVariantType<Matrix3x4>() { return VAR_MATRIX3X4; }
  1216. template <> inline VariantType GetVariantType<Matrix4>() { return VAR_MATRIX4; }
  1217. // Specializations of Variant::Get<T>
  1218. template <> URHO3D_API int Variant::Get<int>() const;
  1219. template <> URHO3D_API unsigned Variant::Get<unsigned>() const;
  1220. template <> URHO3D_API long long Variant::Get<long long>() const;
  1221. template <> URHO3D_API unsigned long long Variant::Get<unsigned long long>() const;
  1222. template <> URHO3D_API StringHash Variant::Get<StringHash>() const;
  1223. template <> URHO3D_API bool Variant::Get<bool>() const;
  1224. template <> URHO3D_API float Variant::Get<float>() const;
  1225. template <> URHO3D_API double Variant::Get<double>() const;
  1226. template <> URHO3D_API const Vector2& Variant::Get<const Vector2&>() const;
  1227. template <> URHO3D_API const Vector3& Variant::Get<const Vector3&>() const;
  1228. template <> URHO3D_API const Vector4& Variant::Get<const Vector4&>() const;
  1229. template <> URHO3D_API const Quaternion& Variant::Get<const Quaternion&>() const;
  1230. template <> URHO3D_API const Color& Variant::Get<const Color&>() const;
  1231. template <> URHO3D_API const String& Variant::Get<const String&>() const;
  1232. template <> URHO3D_API const Rect& Variant::Get<const Rect&>() const;
  1233. template <> URHO3D_API const IntRect& Variant::Get<const IntRect&>() const;
  1234. template <> URHO3D_API const IntVector2& Variant::Get<const IntVector2&>() const;
  1235. template <> URHO3D_API const IntVector3& Variant::Get<const IntVector3&>() const;
  1236. template <> URHO3D_API const PODVector<unsigned char>& Variant::Get<const PODVector<unsigned char>&>() const;
  1237. template <> URHO3D_API void* Variant::Get<void*>() const;
  1238. template <> URHO3D_API RefCounted* Variant::Get<RefCounted*>() const;
  1239. template <> URHO3D_API const Matrix3& Variant::Get<const Matrix3&>() const;
  1240. template <> URHO3D_API const Matrix3x4& Variant::Get<const Matrix3x4&>() const;
  1241. template <> URHO3D_API const Matrix4& Variant::Get<const Matrix4&>() const;
  1242. template <> URHO3D_API ResourceRef Variant::Get<ResourceRef>() const;
  1243. template <> URHO3D_API ResourceRefList Variant::Get<ResourceRefList>() const;
  1244. template <> URHO3D_API VariantVector Variant::Get<VariantVector>() const;
  1245. template <> URHO3D_API StringVector Variant::Get<StringVector>() const;
  1246. template <> URHO3D_API VariantMap Variant::Get<VariantMap>() const;
  1247. template <> URHO3D_API Vector2 Variant::Get<Vector2>() const;
  1248. template <> URHO3D_API Vector3 Variant::Get<Vector3>() const;
  1249. template <> URHO3D_API Vector4 Variant::Get<Vector4>() const;
  1250. template <> URHO3D_API Quaternion Variant::Get<Quaternion>() const;
  1251. template <> URHO3D_API Color Variant::Get<Color>() const;
  1252. template <> URHO3D_API String Variant::Get<String>() const;
  1253. template <> URHO3D_API Rect Variant::Get<Rect>() const;
  1254. template <> URHO3D_API IntRect Variant::Get<IntRect>() const;
  1255. template <> URHO3D_API IntVector2 Variant::Get<IntVector2>() const;
  1256. template <> URHO3D_API IntVector3 Variant::Get<IntVector3>() const;
  1257. template <> URHO3D_API PODVector<unsigned char> Variant::Get<PODVector<unsigned char> >() const;
  1258. template <> URHO3D_API Matrix3 Variant::Get<Matrix3>() const;
  1259. template <> URHO3D_API Matrix3x4 Variant::Get<Matrix3x4>() const;
  1260. template <> URHO3D_API Matrix4 Variant::Get<Matrix4>() const;
  1261. // Implementations
  1262. template <class T> T* CustomVariantValue::GetValuePtr()
  1263. {
  1264. if (IsType<T>())
  1265. {
  1266. auto impl = static_cast<CustomVariantValueImpl<T>*>(this);
  1267. return &impl->GetValue();
  1268. }
  1269. return nullptr;
  1270. }
  1271. template <class T> const T* CustomVariantValue::GetValuePtr() const
  1272. {
  1273. if (IsType<T>())
  1274. {
  1275. auto impl = static_cast<const CustomVariantValueImpl<T>*>(this);
  1276. return &impl->GetValue();
  1277. }
  1278. return nullptr;
  1279. }
  1280. }