Variant.h 25 KB

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