Variant.h 25 KB

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