Variant.h 24 KB

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