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