Variant.h 51 KB

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