Variant.h 25 KB

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