Variant.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/HashMap.h"
  24. #include "../Container/Ptr.h"
  25. #include "../Math/Color.h"
  26. #include "../Math/Matrix3.h"
  27. #include "../Math/Matrix3x4.h"
  28. #include "../Math/Rect.h"
  29. #include "../Math/StringHash.h"
  30. namespace Atomic
  31. {
  32. /// Variant's supported types.
  33. enum VariantType
  34. {
  35. VAR_NONE = 0,
  36. VAR_INT,
  37. VAR_BOOL,
  38. VAR_FLOAT,
  39. VAR_VECTOR2,
  40. VAR_VECTOR3,
  41. VAR_VECTOR4,
  42. VAR_QUATERNION,
  43. VAR_COLOR,
  44. VAR_STRING,
  45. VAR_BUFFER,
  46. VAR_VOIDPTR,
  47. VAR_RESOURCEREF,
  48. VAR_RESOURCEREFLIST,
  49. VAR_VARIANTVECTOR,
  50. VAR_VARIANTMAP,
  51. VAR_INTRECT,
  52. VAR_INTVECTOR2,
  53. VAR_PTR,
  54. VAR_MATRIX3,
  55. VAR_MATRIX3X4,
  56. VAR_MATRIX4,
  57. VAR_DOUBLE,
  58. MAX_VAR_TYPES
  59. };
  60. /// Union for the possible variant values. Also stores non-POD objects such as String which must not exceed 16 bytes in size.
  61. struct VariantValue
  62. {
  63. union
  64. {
  65. int int_;
  66. bool bool_;
  67. float float_;
  68. void* ptr_;
  69. };
  70. union
  71. {
  72. int int2_;
  73. float float2_;
  74. void* ptr2_;
  75. };
  76. union
  77. {
  78. int int3_;
  79. float float3_;
  80. void* ptr3_;
  81. };
  82. union
  83. {
  84. int int4_;
  85. float float4_;
  86. void* ptr4_;
  87. };
  88. };
  89. /// Typed resource reference.
  90. struct ATOMIC_API ResourceRef
  91. {
  92. /// Construct.
  93. ResourceRef()
  94. {
  95. }
  96. /// Construct with type only and empty id.
  97. ResourceRef(StringHash type) :
  98. type_(type)
  99. {
  100. }
  101. /// Construct with type and resource name.
  102. ResourceRef(StringHash type, const String& name) :
  103. type_(type),
  104. name_(name)
  105. {
  106. }
  107. // Construct from another ResourceRef.
  108. ResourceRef(const ResourceRef& rhs) :
  109. type_(rhs.type_),
  110. name_(rhs.name_)
  111. {
  112. }
  113. /// Object type.
  114. StringHash type_;
  115. /// Object name.
  116. String name_;
  117. /// Test for equality with another reference.
  118. bool operator ==(const ResourceRef& rhs) const { return type_ == rhs.type_ && name_ == rhs.name_; }
  119. /// Test for inequality with another reference.
  120. bool operator !=(const ResourceRef& rhs) const { return type_ != rhs.type_ || name_ != rhs.name_; }
  121. };
  122. /// %List of typed resource references.
  123. struct ATOMIC_API ResourceRefList
  124. {
  125. /// Construct.
  126. ResourceRefList()
  127. {
  128. }
  129. /// Construct with type only.
  130. ResourceRefList(StringHash type) :
  131. type_(type)
  132. {
  133. }
  134. /// Construct with type and id list.
  135. ResourceRefList(StringHash type, const Vector<String>& names) :
  136. type_(type),
  137. names_(names)
  138. {
  139. }
  140. /// Object type.
  141. StringHash type_;
  142. /// List of object names.
  143. Vector<String> names_;
  144. /// Test for equality with another reference list.
  145. bool operator ==(const ResourceRefList& rhs) const { return type_ == rhs.type_ && names_ == rhs.names_; }
  146. /// Test for inequality with another reference list.
  147. bool operator !=(const ResourceRefList& rhs) const { return type_ != rhs.type_ || names_ != rhs.names_; }
  148. };
  149. class Variant;
  150. /// Vector of variants.
  151. typedef Vector<Variant> VariantVector;
  152. /// Map of variants.
  153. typedef HashMap<StringHash, Variant> VariantMap;
  154. /// Variable that supports a fixed set of types.
  155. class ATOMIC_API Variant
  156. {
  157. public:
  158. /// Construct empty.
  159. Variant() :
  160. type_(VAR_NONE)
  161. {
  162. }
  163. /// Construct from integer.
  164. Variant(int value) :
  165. type_(VAR_NONE)
  166. {
  167. *this = value;
  168. }
  169. /// Construct from unsigned integer.
  170. Variant(unsigned value) :
  171. type_(VAR_NONE)
  172. {
  173. *this = (int)value;
  174. }
  175. /// Construct from a string hash (convert to integer).
  176. Variant(const StringHash& value) :
  177. type_(VAR_NONE)
  178. {
  179. *this = (int)value.Value();
  180. }
  181. /// Construct from a bool.
  182. Variant(bool value) :
  183. type_(VAR_NONE)
  184. {
  185. *this = value;
  186. }
  187. /// Construct from a float.
  188. Variant(float value) :
  189. type_(VAR_NONE)
  190. {
  191. *this = value;
  192. }
  193. /// Construct from a double.
  194. Variant(double value) :
  195. type_(VAR_NONE)
  196. {
  197. *this = value;
  198. }
  199. /// Construct from a Vector2.
  200. Variant(const Vector2& value) :
  201. type_(VAR_NONE)
  202. {
  203. *this = value;
  204. }
  205. /// Construct from a Vector3.
  206. Variant(const Vector3& value) :
  207. type_(VAR_NONE)
  208. {
  209. *this = value;
  210. }
  211. /// Construct from a Vector4.
  212. Variant(const Vector4& value) :
  213. type_(VAR_NONE)
  214. {
  215. *this = value;
  216. }
  217. /// Construct from a quaternion.
  218. Variant(const Quaternion& value) :
  219. type_(VAR_NONE)
  220. {
  221. *this = value;
  222. }
  223. /// Construct from a color.
  224. Variant(const Color& value) :
  225. type_(VAR_NONE)
  226. {
  227. *this = value;
  228. }
  229. /// Construct from a string.
  230. Variant(const String& value) :
  231. type_(VAR_NONE)
  232. {
  233. *this = value;
  234. }
  235. /// Construct from a C string.
  236. Variant(const char* value) :
  237. type_(VAR_NONE)
  238. {
  239. *this = value;
  240. }
  241. /// Construct from a buffer.
  242. Variant(const PODVector<unsigned char>& value) :
  243. type_(VAR_NONE)
  244. {
  245. *this = value;
  246. }
  247. /// Construct from a pointer.
  248. Variant(void* value) :
  249. type_(VAR_NONE)
  250. {
  251. *this = value;
  252. }
  253. /// Construct from a resource reference.
  254. Variant(const ResourceRef& value) :
  255. type_(VAR_NONE)
  256. {
  257. *this = value;
  258. }
  259. /// Construct from a resource reference list.
  260. Variant(const ResourceRefList& value) :
  261. type_(VAR_NONE)
  262. {
  263. *this = value;
  264. }
  265. /// Construct from a variant vector.
  266. Variant(const VariantVector& value) :
  267. type_(VAR_NONE)
  268. {
  269. *this = value;
  270. }
  271. /// Construct from a variant map.
  272. Variant(const VariantMap& value) :
  273. type_(VAR_NONE)
  274. {
  275. *this = value;
  276. }
  277. /// Construct from an integer rect.
  278. Variant(const IntRect& value) :
  279. type_(VAR_NONE)
  280. {
  281. *this = value;
  282. }
  283. /// Construct from an IntVector2.
  284. Variant(const IntVector2& value) :
  285. type_(VAR_NONE)
  286. {
  287. *this = value;
  288. }
  289. /// Construct from a RefCounted pointer. The object will be stored internally in a WeakPtr so that its expiration can be detected safely.
  290. Variant(RefCounted* value) :
  291. type_(VAR_NONE)
  292. {
  293. *this = value;
  294. }
  295. /// Construct from a Matrix3.
  296. Variant(const Matrix3& value) :
  297. type_(VAR_NONE)
  298. {
  299. *this = value;
  300. }
  301. /// Construct from a Matrix3x4.
  302. Variant(const Matrix3x4& value) :
  303. type_(VAR_NONE)
  304. {
  305. *this = value;
  306. }
  307. /// Construct from a Matrix4.
  308. Variant(const Matrix4& value) :
  309. type_(VAR_NONE)
  310. {
  311. *this = value;
  312. }
  313. /// Construct from type and value.
  314. Variant(const String& type, const String& value) :
  315. type_(VAR_NONE)
  316. {
  317. FromString(type, value);
  318. }
  319. /// Construct from type and value.
  320. Variant(VariantType type, const String& value) :
  321. type_(VAR_NONE)
  322. {
  323. FromString(type, value);
  324. }
  325. /// Construct from type and value.
  326. Variant(const char* type, const char* value) :
  327. type_(VAR_NONE)
  328. {
  329. FromString(type, value);
  330. }
  331. /// Construct from type and value.
  332. Variant(VariantType type, const char* value) :
  333. type_(VAR_NONE)
  334. {
  335. FromString(type, value);
  336. }
  337. /// Copy-construct from another variant.
  338. Variant(const Variant& value) :
  339. type_(VAR_NONE)
  340. {
  341. *this = value;
  342. }
  343. /// Destruct.
  344. ~Variant()
  345. {
  346. SetType(VAR_NONE);
  347. }
  348. /// Reset to empty.
  349. void Clear()
  350. {
  351. SetType(VAR_NONE);
  352. }
  353. /// Assign from another variant.
  354. Variant& operator =(const Variant& rhs);
  355. /// Assign from an integer.
  356. Variant& operator =(int rhs)
  357. {
  358. SetType(VAR_INT);
  359. value_.int_ = rhs;
  360. return *this;
  361. }
  362. /// Assign from an unsigned integer.
  363. Variant& operator =(unsigned rhs)
  364. {
  365. SetType(VAR_INT);
  366. value_.int_ = (int)rhs;
  367. return *this;
  368. }
  369. /// Assign from a StringHash (convert to integer.)
  370. Variant& operator =(const StringHash& rhs)
  371. {
  372. SetType(VAR_INT);
  373. value_.int_ = (int)rhs.Value();
  374. return *this;
  375. }
  376. /// Assign from a bool.
  377. Variant& operator =(bool rhs)
  378. {
  379. SetType(VAR_BOOL);
  380. value_.bool_ = rhs;
  381. return *this;
  382. }
  383. /// Assign from a float.
  384. Variant& operator =(float rhs)
  385. {
  386. SetType(VAR_FLOAT);
  387. value_.float_ = rhs;
  388. return *this;
  389. }
  390. /// Assign from a double.
  391. Variant& operator = (double rhs)
  392. {
  393. SetType(VAR_DOUBLE);
  394. *(reinterpret_cast<double*>(&value_)) = rhs;
  395. return *this;
  396. }
  397. /// Assign from a Vector2.
  398. Variant& operator =(const Vector2& rhs)
  399. {
  400. SetType(VAR_VECTOR2);
  401. *(reinterpret_cast<Vector2*>(&value_)) = rhs;
  402. return *this;
  403. }
  404. /// Assign from a Vector3.
  405. Variant& operator =(const Vector3& rhs)
  406. {
  407. SetType(VAR_VECTOR3);
  408. *(reinterpret_cast<Vector3*>(&value_)) = rhs;
  409. return *this;
  410. }
  411. /// Assign from a Vector4.
  412. Variant& operator =(const Vector4& rhs)
  413. {
  414. SetType(VAR_VECTOR4);
  415. *(reinterpret_cast<Vector4*>(&value_)) = rhs;
  416. return *this;
  417. }
  418. /// Assign from a quaternion.
  419. Variant& operator =(const Quaternion& rhs)
  420. {
  421. SetType(VAR_QUATERNION);
  422. *(reinterpret_cast<Quaternion*>(&value_)) = rhs;
  423. return *this;
  424. }
  425. /// Assign from a color.
  426. Variant& operator =(const Color& rhs)
  427. {
  428. SetType(VAR_COLOR);
  429. *(reinterpret_cast<Color*>(&value_)) = rhs;
  430. return *this;
  431. }
  432. /// Assign from a string.
  433. Variant& operator =(const String& rhs)
  434. {
  435. SetType(VAR_STRING);
  436. *(reinterpret_cast<String*>(&value_)) = rhs;
  437. return *this;
  438. }
  439. /// Assign from a C string.
  440. Variant& operator =(const char* rhs)
  441. {
  442. SetType(VAR_STRING);
  443. *(reinterpret_cast<String*>(&value_)) = String(rhs);
  444. return *this;
  445. }
  446. /// Assign from a buffer.
  447. Variant& operator =(const PODVector<unsigned char>& rhs)
  448. {
  449. SetType(VAR_BUFFER);
  450. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = rhs;
  451. return *this;
  452. }
  453. /// Assign from a void pointer.
  454. Variant& operator =(void* rhs)
  455. {
  456. SetType(VAR_VOIDPTR);
  457. value_.ptr_ = rhs;
  458. return *this;
  459. }
  460. /// Assign from a resource reference.
  461. Variant& operator =(const ResourceRef& rhs)
  462. {
  463. SetType(VAR_RESOURCEREF);
  464. *(reinterpret_cast<ResourceRef*>(&value_)) = rhs;
  465. return *this;
  466. }
  467. /// Assign from a resource reference list.
  468. Variant& operator =(const ResourceRefList& rhs)
  469. {
  470. SetType(VAR_RESOURCEREFLIST);
  471. *(reinterpret_cast<ResourceRefList*>(&value_)) = rhs;
  472. return *this;
  473. }
  474. /// Assign from a variant vector.
  475. Variant& operator =(const VariantVector& rhs)
  476. {
  477. SetType(VAR_VARIANTVECTOR);
  478. *(reinterpret_cast<VariantVector*>(&value_)) = rhs;
  479. return *this;
  480. }
  481. /// Assign from a variant map.
  482. Variant& operator =(const VariantMap& rhs)
  483. {
  484. SetType(VAR_VARIANTMAP);
  485. *(reinterpret_cast<VariantMap*>(&value_)) = rhs;
  486. return *this;
  487. }
  488. /// Assign from an integer rect.
  489. Variant& operator =(const IntRect& rhs)
  490. {
  491. SetType(VAR_INTRECT);
  492. *(reinterpret_cast<IntRect*>(&value_)) = rhs;
  493. return *this;
  494. }
  495. /// Assign from an IntVector2.
  496. Variant& operator =(const IntVector2& rhs)
  497. {
  498. SetType(VAR_INTVECTOR2);
  499. *(reinterpret_cast<IntVector2*>(&value_)) = rhs;
  500. return *this;
  501. }
  502. /// Assign from a RefCounted pointer. The object will be stored internally in a WeakPtr so that its expiration can be detected safely.
  503. Variant& operator =(RefCounted* rhs)
  504. {
  505. SetType(VAR_PTR);
  506. *(reinterpret_cast<WeakPtr<RefCounted>*>(&value_)) = rhs;
  507. return *this;
  508. }
  509. /// Assign from a Matrix3.
  510. Variant& operator =(const Matrix3& rhs)
  511. {
  512. SetType(VAR_MATRIX3);
  513. *(reinterpret_cast<Matrix3*>(value_.ptr_)) = rhs;
  514. return *this;
  515. }
  516. /// Assign from a Matrix3x4.
  517. Variant& operator =(const Matrix3x4& rhs)
  518. {
  519. SetType(VAR_MATRIX3X4);
  520. *(reinterpret_cast<Matrix3x4*>(value_.ptr_)) = rhs;
  521. return *this;
  522. }
  523. /// Assign from a Matrix4.
  524. Variant& operator =(const Matrix4& rhs)
  525. {
  526. SetType(VAR_MATRIX4);
  527. *(reinterpret_cast<Matrix4*>(value_.ptr_)) = rhs;
  528. return *this;
  529. }
  530. /// Test for equality with another variant.
  531. bool operator ==(const Variant& rhs) const;
  532. /// Test for equality with an integer. To return true, both the type and value must match.
  533. bool operator ==(int rhs) const { return type_ == VAR_INT ? value_.int_ == rhs : false; }
  534. /// Test for equality with an unsigned integer. To return true, both the type and value must match.
  535. bool operator ==(unsigned rhs) const { return type_ == VAR_INT ? value_.int_ == (int)rhs : false; }
  536. /// Test for equality with a bool. To return true, both the type and value must match.
  537. bool operator ==(bool rhs) const { return type_ == VAR_BOOL ? value_.bool_ == rhs : false; }
  538. /// Test for equality with a float. To return true, both the type and value must match.
  539. bool operator ==(float rhs) const { return type_ == VAR_FLOAT ? value_.float_ == rhs : false; }
  540. /// Test for equality with a double. To return true, both the type and value must match.
  541. bool operator ==(double rhs) const { return type_ == VAR_DOUBLE ? *(reinterpret_cast<const double*>(&value_)) == rhs : false; }
  542. /// Test for equality with a Vector2. To return true, both the type and value must match.
  543. bool operator ==(const Vector2& rhs) const
  544. {
  545. return type_ == VAR_VECTOR2 ? *(reinterpret_cast<const Vector2*>(&value_)) == rhs : false;
  546. }
  547. /// Test for equality with a Vector3. To return true, both the type and value must match.
  548. bool operator ==(const Vector3& rhs) const
  549. {
  550. return type_ == VAR_VECTOR3 ? *(reinterpret_cast<const Vector3*>(&value_)) == rhs : false;
  551. }
  552. /// Test for equality with a Vector4. To return true, both the type and value must match.
  553. bool operator ==(const Vector4& rhs) const
  554. {
  555. return type_ == VAR_VECTOR4 ? *(reinterpret_cast<const Vector4*>(&value_)) == rhs : false;
  556. }
  557. /// Test for equality with a quaternion. To return true, both the type and value must match.
  558. bool operator ==(const Quaternion& rhs) const
  559. {
  560. return type_ == VAR_QUATERNION ? *(reinterpret_cast<const Quaternion*>(&value_)) == rhs : false;
  561. }
  562. /// Test for equality with a color. To return true, both the type and value must match.
  563. bool operator ==(const Color& rhs) const
  564. {
  565. return type_ == VAR_COLOR ? *(reinterpret_cast<const Color*>(&value_)) == rhs : false;
  566. }
  567. /// Test for equality with a string. To return true, both the type and value must match.
  568. bool operator ==(const String& rhs) const
  569. {
  570. return type_ == VAR_STRING ? *(reinterpret_cast<const String*>(&value_)) == rhs : false;
  571. }
  572. /// Test for equality with a buffer. To return true, both the type and value must match.
  573. bool operator ==(const PODVector<unsigned char>& rhs) const
  574. {
  575. return type_ == VAR_BUFFER ? *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)) == rhs : false;
  576. }
  577. /// 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.
  578. bool operator ==(void* rhs) const
  579. {
  580. if (type_ == VAR_VOIDPTR)
  581. return value_.ptr_ == rhs;
  582. else if (type_ == VAR_PTR)
  583. return *(reinterpret_cast<const WeakPtr<RefCounted>*>(&value_)) == rhs;
  584. else
  585. return false;
  586. }
  587. /// Test for equality with a resource reference. To return true, both the type and value must match.
  588. bool operator ==(const ResourceRef& rhs) const
  589. {
  590. return type_ == VAR_RESOURCEREF ? *(reinterpret_cast<const ResourceRef*>(&value_)) == rhs : false;
  591. }
  592. /// Test for equality with a resource reference list. To return true, both the type and value must match.
  593. bool operator ==(const ResourceRefList& rhs) const
  594. {
  595. return type_ == VAR_RESOURCEREFLIST ? *(reinterpret_cast<const ResourceRefList*>(&value_)) == rhs : false;
  596. }
  597. /// Test for equality with a variant vector. To return true, both the type and value must match.
  598. bool operator ==(const VariantVector& rhs) const
  599. {
  600. return type_ == VAR_VARIANTVECTOR ? *(reinterpret_cast<const VariantVector*>(&value_)) == rhs : false;
  601. }
  602. /// Test for equality with a variant map. To return true, both the type and value must match.
  603. bool operator ==(const VariantMap& rhs) const
  604. {
  605. return type_ == VAR_VARIANTMAP ? *(reinterpret_cast<const VariantMap*>(&value_)) == rhs : false;
  606. }
  607. /// Test for equality with an integer rect. To return true, both the type and value must match.
  608. bool operator ==(const IntRect& rhs) const
  609. {
  610. return type_ == VAR_INTRECT ? *(reinterpret_cast<const IntRect*>(&value_)) == rhs : false;
  611. }
  612. /// Test for equality with an IntVector2. To return true, both the type and value must match.
  613. bool operator ==(const IntVector2& rhs) const
  614. {
  615. return type_ == VAR_INTVECTOR2 ? *(reinterpret_cast<const IntVector2*>(&value_)) == rhs : false;
  616. }
  617. /// Test for equality with a StringHash. To return true, both the type and value must match.
  618. bool operator ==(const StringHash& rhs) const { return type_ == VAR_INT ? (unsigned)value_.int_ == rhs.Value() : false; }
  619. /// 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.
  620. bool operator ==(RefCounted* rhs) const
  621. {
  622. if (type_ == VAR_PTR)
  623. return *(reinterpret_cast<const WeakPtr<RefCounted>*>(&value_)) == rhs;
  624. else if (type_ == VAR_VOIDPTR)
  625. return value_.ptr_ == rhs;
  626. else
  627. return false;
  628. }
  629. /// Test for equality with a Matrix3. To return true, both the type and value must match.
  630. bool operator ==(const Matrix3& rhs) const
  631. {
  632. return type_ == VAR_MATRIX3 ? *(reinterpret_cast<const Matrix3*>(value_.ptr_)) == rhs : false;
  633. }
  634. /// Test for equality with a Matrix3x4. To return true, both the type and value must match.
  635. bool operator ==(const Matrix3x4& rhs) const
  636. {
  637. return type_ == VAR_MATRIX3X4 ? *(reinterpret_cast<const Matrix3x4*>(value_.ptr_)) == rhs : false;
  638. }
  639. /// Test for equality with a Matrix4. To return true, both the type and value must match.
  640. bool operator ==(const Matrix4& rhs) const
  641. {
  642. return type_ == VAR_MATRIX4 ? *(reinterpret_cast<const Matrix4*>(value_.ptr_)) == rhs : false;
  643. }
  644. /// Test for inequality with another variant.
  645. bool operator !=(const Variant& rhs) const { return !(*this == rhs); }
  646. /// Test for inequality with an integer.
  647. bool operator !=(int rhs) const { return !(*this == rhs); }
  648. /// Test for inequality with an unsigned integer.
  649. bool operator !=(unsigned rhs) const { return !(*this == rhs); }
  650. /// Test for inequality with a bool.
  651. bool operator !=(bool rhs) const { return !(*this == rhs); }
  652. /// Test for inequality with a float.
  653. bool operator !=(float rhs) const { return !(*this == rhs); }
  654. /// Test for inequality with a double.
  655. bool operator !=(double rhs) const { return !(*this == rhs); }
  656. /// Test for inequality with a Vector2.
  657. bool operator !=(const Vector2& rhs) const { return !(*this == rhs); }
  658. /// Test for inequality with a Vector3.
  659. bool operator !=(const Vector3& rhs) const { return !(*this == rhs); }
  660. /// Test for inequality with an Vector4.
  661. bool operator !=(const Vector4& rhs) const { return !(*this == rhs); }
  662. /// Test for inequality with a Quaternion.
  663. bool operator !=(const Quaternion& rhs) const { return !(*this == rhs); }
  664. /// Test for inequality with a string.
  665. bool operator !=(const String& rhs) const { return !(*this == rhs); }
  666. /// Test for inequality with a buffer.
  667. bool operator !=(const PODVector<unsigned char>& rhs) const { return !(*this == rhs); }
  668. /// Test for inequality with a pointer.
  669. bool operator !=(void* rhs) const { return !(*this == rhs); }
  670. /// Test for inequality with a resource reference.
  671. bool operator !=(const ResourceRef& rhs) const { return !(*this == rhs); }
  672. /// Test for inequality with a resource reference list.
  673. bool operator !=(const ResourceRefList& rhs) const { return !(*this == rhs); }
  674. /// Test for inequality with a variant vector.
  675. bool operator !=(const VariantVector& rhs) const { return !(*this == rhs); }
  676. /// Test for inequality with a variant map.
  677. bool operator !=(const VariantMap& rhs) const { return !(*this == rhs); }
  678. /// Test for inequality with an integer rect.
  679. bool operator !=(const IntRect& rhs) const { return !(*this == rhs); }
  680. /// Test for inequality with an IntVector2.
  681. bool operator !=(const IntVector2& rhs) const { return !(*this == rhs); }
  682. /// Test for inequality with a StringHash.
  683. bool operator !=(const StringHash& rhs) const { return !(*this == rhs); }
  684. /// Test for inequality with a RefCounted pointer.
  685. bool operator !=(RefCounted* rhs) const { return !(*this == rhs); }
  686. /// Test for inequality with a Matrix3.
  687. bool operator !=(const Matrix3& rhs) const { return !(*this == rhs); }
  688. /// Test for inequality with a Matrix3x4.
  689. bool operator !=(const Matrix3x4& rhs) const { return !(*this == rhs); }
  690. /// Test for inequality with a Matrix4.
  691. bool operator !=(const Matrix4& rhs) const { return !(*this == rhs); }
  692. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  693. void FromString(const String& type, const String& value);
  694. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  695. void FromString(const char* type, const char* value);
  696. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  697. void FromString(VariantType type, const String& value);
  698. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  699. void FromString(VariantType type, const char* value);
  700. /// Set buffer type from a memory area.
  701. void SetBuffer(const void* data, unsigned size);
  702. /// Return int or zero on type mismatch.
  703. int GetInt() const { return type_ == VAR_INT ? value_.int_ : 0; }
  704. /// Return unsigned int or zero on type mismatch.
  705. unsigned GetUInt() const { return type_ == VAR_INT ? (unsigned)value_.int_ : 0; }
  706. /// Return StringHash or zero on type mismatch.
  707. StringHash GetStringHash() const { return StringHash(GetUInt()); }
  708. /// Return bool or false on type mismatch.
  709. bool GetBool() const { return type_ == VAR_BOOL ? value_.bool_ : false; }
  710. /// Return float or zero on type mismatch.
  711. float GetFloat() const { return type_ == VAR_FLOAT ? value_.float_ : 0.0f; }
  712. /// Return double or zero on type mismatch.
  713. double GetDouble() const { return type_ == VAR_DOUBLE ? *reinterpret_cast<const double*>(&value_) : 0.0; }
  714. /// Return Vector2 or zero on type mismatch.
  715. const Vector2& GetVector2() const { return type_ == VAR_VECTOR2 ? *reinterpret_cast<const Vector2*>(&value_) : Vector2::ZERO; }
  716. /// Return Vector3 or zero on type mismatch.
  717. const Vector3& GetVector3() const { return type_ == VAR_VECTOR3 ? *reinterpret_cast<const Vector3*>(&value_) : Vector3::ZERO; }
  718. /// Return Vector4 or zero on type mismatch.
  719. const Vector4& GetVector4() const { return type_ == VAR_VECTOR4 ? *reinterpret_cast<const Vector4*>(&value_) : Vector4::ZERO; }
  720. /// Return quaternion or identity on type mismatch.
  721. const Quaternion& GetQuaternion() const
  722. {
  723. return type_ == VAR_QUATERNION ? *reinterpret_cast<const Quaternion*>(&value_) : Quaternion::IDENTITY;
  724. }
  725. /// Return color or default on type mismatch.
  726. const Color& GetColor() const { return type_ == VAR_COLOR ? *reinterpret_cast<const Color*>(&value_) : Color::WHITE; }
  727. /// Return string or empty on type mismatch.
  728. const String& GetString() const { return type_ == VAR_STRING ? *reinterpret_cast<const String*>(&value_) : String::EMPTY; }
  729. /// Return buffer or empty on type mismatch.
  730. const PODVector<unsigned char>& GetBuffer() const
  731. {
  732. return type_ == VAR_BUFFER ? *reinterpret_cast<const PODVector<unsigned char>*>(&value_) : emptyBuffer;
  733. }
  734. /// Return void pointer or null on type mismatch. RefCounted pointer will be converted.
  735. void* GetVoidPtr() const
  736. {
  737. if (type_ == VAR_VOIDPTR)
  738. return value_.ptr_;
  739. else if (type_ == VAR_PTR)
  740. return *reinterpret_cast<const WeakPtr<RefCounted>*>(&value_);
  741. else
  742. return 0;
  743. }
  744. /// Return a resource reference or empty on type mismatch.
  745. const ResourceRef& GetResourceRef() const
  746. {
  747. return type_ == VAR_RESOURCEREF ? *reinterpret_cast<const ResourceRef*>(&value_) : emptyResourceRef;
  748. }
  749. /// Return a resource reference list or empty on type mismatch.
  750. const ResourceRefList& GetResourceRefList() const
  751. {
  752. return type_ == VAR_RESOURCEREFLIST ? *reinterpret_cast<const ResourceRefList*>(&value_) : emptyResourceRefList;
  753. }
  754. /// Return a variant vector or empty on type mismatch.
  755. const VariantVector& GetVariantVector() const
  756. {
  757. return type_ == VAR_VARIANTVECTOR ? *reinterpret_cast<const VariantVector*>(&value_) : emptyVariantVector;
  758. }
  759. /// Return a variant map or empty on type mismatch.
  760. const VariantMap& GetVariantMap() const
  761. {
  762. return type_ == VAR_VARIANTMAP ? *reinterpret_cast<const VariantMap*>(&value_) : emptyVariantMap;
  763. }
  764. /// Return an integer rect or empty on type mismatch.
  765. const IntRect& GetIntRect() const { return type_ == VAR_INTRECT ? *reinterpret_cast<const IntRect*>(&value_) : IntRect::ZERO; }
  766. /// Return an IntVector2 or empty on type mismatch.
  767. const IntVector2& GetIntVector2() const
  768. {
  769. return type_ == VAR_INTVECTOR2 ? *reinterpret_cast<const IntVector2*>(&value_) : IntVector2::ZERO;
  770. }
  771. /// 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.
  772. RefCounted* GetPtr() const
  773. {
  774. return type_ == VAR_PTR ? *reinterpret_cast<const WeakPtr<RefCounted>*>(&value_) : (RefCounted*)0;
  775. }
  776. /// Return a Matrix3 or identity on type mismatch.
  777. const Matrix3& GetMatrix3() const
  778. {
  779. return type_ == VAR_MATRIX3 ? *(reinterpret_cast<const Matrix3*>(value_.ptr_)) : Matrix3::IDENTITY;
  780. }
  781. /// Return a Matrix3x4 or identity on type mismatch.
  782. const Matrix3x4& GetMatrix3x4() const
  783. {
  784. return type_ == VAR_MATRIX3X4 ? *(reinterpret_cast<const Matrix3x4*>(value_.ptr_)) : Matrix3x4::IDENTITY;
  785. }
  786. /// Return a Matrix4 or identity on type mismatch.
  787. const Matrix4& GetMatrix4() const
  788. {
  789. return type_ == VAR_MATRIX4 ? *(reinterpret_cast<const Matrix4*>(value_.ptr_)) : Matrix4::IDENTITY;
  790. }
  791. /// Return value's type.
  792. VariantType GetType() const { return type_; }
  793. /// Return value's type name.
  794. String GetTypeName() const;
  795. /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
  796. String ToString() const;
  797. /// Return true when the variant value is considered zero according to its actual type.
  798. bool IsZero() const;
  799. /// Return true when the variant is empty (i.e. not initialized yet).
  800. bool IsEmpty() const { return type_ == VAR_NONE; }
  801. /// Return the value, template version.
  802. template <class T> T Get() const;
  803. /// Return a pointer to a modifiable buffer or null on type mismatch.
  804. PODVector<unsigned char>* GetBufferPtr()
  805. {
  806. return type_ == VAR_BUFFER ? reinterpret_cast<PODVector<unsigned char>*>(&value_) : 0;
  807. }
  808. /// Return a pointer to a modifiable variant vector or null on type mismatch.
  809. VariantVector* GetVariantVectorPtr() { return type_ == VAR_VARIANTVECTOR ? reinterpret_cast<VariantVector*>(&value_) : 0; }
  810. /// Return a pointer to a modifiable variant map or null on type mismatch.
  811. VariantMap* GetVariantMapPtr() { return type_ == VAR_VARIANTMAP ? reinterpret_cast<VariantMap*>(&value_) : 0; }
  812. /// Return name for variant type.
  813. static String GetTypeName(VariantType type);
  814. /// Return variant type from type name.
  815. static VariantType GetTypeFromName(const String& typeName);
  816. /// Return variant type from type name.
  817. static VariantType GetTypeFromName(const char* typeName);
  818. /// Empty variant.
  819. static const Variant EMPTY;
  820. /// Empty buffer.
  821. static const PODVector<unsigned char> emptyBuffer;
  822. /// Empty resource reference.
  823. static const ResourceRef emptyResourceRef;
  824. /// Empty resource reference list.
  825. static const ResourceRefList emptyResourceRefList;
  826. /// Empty variant map.
  827. static const VariantMap emptyVariantMap;
  828. /// Empty variant vector.
  829. static const VariantVector emptyVariantVector;
  830. private:
  831. /// Set new type and allocate/deallocate memory as necessary.
  832. void SetType(VariantType newType);
  833. /// Variant type.
  834. VariantType type_;
  835. /// Variant value.
  836. VariantValue value_;
  837. };
  838. /// Return variant type from type.
  839. template <typename T> VariantType GetVariantType();
  840. /// Return variant type from concrete types.
  841. template <> inline VariantType GetVariantType<int>() { return VAR_INT; }
  842. template <> inline VariantType GetVariantType<unsigned>() { return VAR_INT; }
  843. template <> inline VariantType GetVariantType<bool>() { return VAR_BOOL; }
  844. template <> inline VariantType GetVariantType<float>() { return VAR_FLOAT; }
  845. template <> inline VariantType GetVariantType<double>() { return VAR_DOUBLE; }
  846. template <> inline VariantType GetVariantType<Vector2>() { return VAR_VECTOR2; }
  847. template <> inline VariantType GetVariantType<Vector3>() { return VAR_VECTOR3; }
  848. template <> inline VariantType GetVariantType<Vector4>() { return VAR_VECTOR4; }
  849. template <> inline VariantType GetVariantType<Quaternion>() { return VAR_QUATERNION; }
  850. template <> inline VariantType GetVariantType<Color>() { return VAR_COLOR; }
  851. template <> inline VariantType GetVariantType<String>() { return VAR_STRING; }
  852. template <> inline VariantType GetVariantType<StringHash>() { return VAR_INT; }
  853. template <> inline VariantType GetVariantType<PODVector<unsigned char> >() { return VAR_BUFFER; }
  854. template <> inline VariantType GetVariantType<ResourceRef>() { return VAR_RESOURCEREF; }
  855. template <> inline VariantType GetVariantType<ResourceRefList>() { return VAR_RESOURCEREFLIST; }
  856. template <> inline VariantType GetVariantType<VariantVector>() { return VAR_VARIANTVECTOR; }
  857. template <> inline VariantType GetVariantType<VariantMap>() { return VAR_VARIANTMAP; }
  858. template <> inline VariantType GetVariantType<IntRect>() { return VAR_INTRECT; }
  859. template <> inline VariantType GetVariantType<IntVector2>() { return VAR_INTVECTOR2; }
  860. template <> inline VariantType GetVariantType<Matrix3>() { return VAR_MATRIX3; }
  861. template <> inline VariantType GetVariantType<Matrix3x4>() { return VAR_MATRIX3X4; }
  862. template <> inline VariantType GetVariantType<Matrix4>() { return VAR_MATRIX4; }
  863. }