Variant.h 51 KB

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