Variant.h 25 KB

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