Variant.h 51 KB

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