Variant.h 26 KB

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