Variant.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 "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. /// Construct from type and value.
  264. Variant(const char* type, const char* value) :
  265. type_(VAR_NONE)
  266. {
  267. FromString(type, value);
  268. }
  269. /// Construct from type and value.
  270. Variant(VariantType type, const char* value) :
  271. type_(VAR_NONE)
  272. {
  273. FromString(type, value);
  274. }
  275. /// Copy-construct from another variant.
  276. Variant(const Variant& value) :
  277. type_(VAR_NONE)
  278. {
  279. *this = value;
  280. }
  281. /// Destruct.
  282. ~Variant()
  283. {
  284. SetType(VAR_NONE);
  285. }
  286. /// Reset to empty.
  287. void Clear()
  288. {
  289. SetType(VAR_NONE);
  290. }
  291. /// Assign from another variant.
  292. Variant& operator = (const Variant& rhs);
  293. /// Assign from an integer.
  294. Variant& operator = (int rhs)
  295. {
  296. SetType(VAR_INT);
  297. value_.int_ = rhs;
  298. return *this;
  299. }
  300. /// Assign from an unsigned integer.
  301. Variant& operator = (unsigned rhs)
  302. {
  303. SetType(VAR_INT);
  304. value_.int_ = (int)rhs;
  305. return *this;
  306. }
  307. /// Assign from a StringHash (convert to integer.)
  308. Variant& operator = (const StringHash& rhs)
  309. {
  310. SetType(VAR_INT);
  311. value_.int_ = (int)rhs.GetValue();
  312. return *this;
  313. }
  314. /// Assign from a ShortStringHash (convert to integer)
  315. Variant& operator = (const ShortStringHash& rhs)
  316. {
  317. SetType(VAR_INT);
  318. value_.int_ = (int)rhs.GetValue();
  319. return *this;
  320. }
  321. /// Assign from a bool.
  322. Variant& operator = (bool rhs)
  323. {
  324. SetType(VAR_BOOL);
  325. value_.bool_ = rhs;
  326. return *this;
  327. }
  328. /// Assign from a float.
  329. Variant& operator = (float rhs)
  330. {
  331. SetType(VAR_FLOAT);
  332. value_.float_ = rhs;
  333. return *this;
  334. }
  335. /// Assign from a Vector2.
  336. Variant& operator = (const Vector2& rhs)
  337. {
  338. SetType(VAR_VECTOR2);
  339. *(reinterpret_cast<Vector2*>(&value_)) = rhs;
  340. return *this;
  341. }
  342. /// Assign from a Vector3.
  343. Variant& operator = (const Vector3& rhs)
  344. {
  345. SetType(VAR_VECTOR3);
  346. *(reinterpret_cast<Vector3*>(&value_)) = rhs;
  347. return *this;
  348. }
  349. /// Assign from a Vector4.
  350. Variant& operator = (const Vector4& rhs)
  351. {
  352. SetType(VAR_VECTOR4);
  353. *(reinterpret_cast<Vector4*>(&value_)) = rhs;
  354. return *this;
  355. }
  356. /// Assign from a quaternion.
  357. Variant& operator = (const Quaternion& rhs)
  358. {
  359. SetType(VAR_QUATERNION);
  360. *(reinterpret_cast<Quaternion*>(&value_)) = rhs;
  361. return *this;
  362. }
  363. /// Assign from a color.
  364. Variant& operator = (const Color& rhs)
  365. {
  366. SetType(VAR_COLOR);
  367. *(reinterpret_cast<Color*>(&value_)) = rhs;
  368. return *this;
  369. }
  370. /// Assign from a string.
  371. Variant& operator = (const String& rhs)
  372. {
  373. SetType(VAR_STRING);
  374. *(reinterpret_cast<String*>(&value_)) = rhs;
  375. return *this;
  376. }
  377. /// Assign from a C string.
  378. Variant& operator = (const char* rhs)
  379. {
  380. SetType(VAR_STRING);
  381. *(reinterpret_cast<String*>(&value_)) = String(rhs);
  382. return *this;
  383. }
  384. /// Assign from a buffer.
  385. Variant& operator = (const PODVector<unsigned char>& rhs)
  386. {
  387. SetType(VAR_BUFFER);
  388. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = rhs;
  389. return *this;
  390. }
  391. /// Assign from a pointer.
  392. Variant& operator = (void* rhs)
  393. {
  394. SetType(VAR_PTR);
  395. value_.ptr_ = rhs;
  396. return *this;
  397. }
  398. /// Assign from a resource reference.
  399. Variant& operator = (const ResourceRef& rhs)
  400. {
  401. SetType(VAR_RESOURCEREF);
  402. *(reinterpret_cast<ResourceRef*>(&value_)) = rhs;
  403. return *this;
  404. }
  405. /// Assign from a resource reference list.
  406. Variant& operator = (const ResourceRefList& rhs)
  407. {
  408. SetType(VAR_RESOURCEREFLIST);
  409. *(reinterpret_cast<ResourceRefList*>(&value_)) = rhs;
  410. return *this;
  411. }
  412. /// Assign from a variant vector.
  413. Variant& operator = (const VariantVector& rhs)
  414. {
  415. SetType(VAR_VARIANTVECTOR);
  416. *(reinterpret_cast<VariantVector*>(&value_)) = rhs;
  417. return *this;
  418. }
  419. /// Assign from a variant map.
  420. Variant& operator = (const VariantMap& rhs)
  421. {
  422. SetType(VAR_VARIANTMAP);
  423. *(reinterpret_cast<VariantMap*>(&value_)) = rhs;
  424. return *this;
  425. }
  426. /// Test for equality with another variant.
  427. bool operator == (const Variant& rhs) const;
  428. /// Test for inequality with another variant.
  429. bool operator != (const Variant& rhs) const { return !(*this == rhs); }
  430. /// Test for equality with an integer. To return true, both the type and value must match.
  431. bool operator == (int rhs) const
  432. {
  433. if (type_ == VAR_INT)
  434. return value_.int_ == rhs;
  435. else
  436. return false;
  437. }
  438. /// Test for equality with an unsigned integer. To return true, both the type and value must match.
  439. bool operator == (unsigned rhs) const
  440. {
  441. if (type_ == VAR_INT)
  442. return value_.int_ == (int)rhs;
  443. else
  444. return false;
  445. }
  446. /// Test for equality with a bool. To return true, both the type and value must match.
  447. bool operator == (bool rhs) const
  448. {
  449. if (type_ == VAR_BOOL)
  450. return value_.bool_ == rhs;
  451. else
  452. return false;
  453. }
  454. /// Test for equality with a float. To return true, both the type and value must match.
  455. bool operator == (float rhs) const
  456. {
  457. if (type_ == VAR_FLOAT)
  458. return value_.float_ == rhs;
  459. else
  460. return false;
  461. }
  462. /// Test for equality with a Vector2. To return true, both the type and value must match.
  463. bool operator == (const Vector2& rhs) const
  464. {
  465. if (type_ == VAR_VECTOR2)
  466. return *(reinterpret_cast<const Vector2*>(&value_)) == rhs;
  467. else
  468. return false;
  469. }
  470. /// Test for equality with a Vector3. To return true, both the type and value must match.
  471. bool operator == (const Vector3& rhs) const
  472. {
  473. if (type_ == VAR_VECTOR3)
  474. return *(reinterpret_cast<const Vector3*>(&value_)) == rhs;
  475. else
  476. return false;
  477. }
  478. /// Test for equality with a Vector4. To return true, both the type and value must match.
  479. bool operator == (const Vector4& rhs) const
  480. {
  481. if (type_ == VAR_VECTOR4)
  482. return *(reinterpret_cast<const Vector4*>(&value_)) == rhs;
  483. else
  484. return false;
  485. }
  486. /// Test for equality with a quaternion. To return true, both the type and value must match.
  487. bool operator == (const Quaternion& rhs) const
  488. {
  489. if (type_ == VAR_QUATERNION)
  490. return *(reinterpret_cast<const Quaternion*>(&value_)) == rhs;
  491. else
  492. return false;
  493. }
  494. /// Test for equality with a color. To return true, both the type and value must match.
  495. bool operator == (const Color& rhs) const
  496. {
  497. if (type_ == VAR_COLOR)
  498. return *(reinterpret_cast<const Color*>(&value_)) == rhs;
  499. else
  500. return false;
  501. }
  502. /// Test for equality with a string. To return true, both the type and value must match.
  503. bool operator == (const String& rhs) const
  504. {
  505. if (type_ == VAR_STRING)
  506. return *(reinterpret_cast<const String*>(&value_)) == rhs;
  507. else
  508. return false;
  509. }
  510. /// Test for equality with a buffer. To return true, both the type and value must match.
  511. bool operator == (const PODVector<unsigned char>& rhs) const
  512. {
  513. if (type_ == VAR_BUFFER)
  514. return *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)) == rhs;
  515. else
  516. return false;
  517. }
  518. /// Test for equality with a pointer. To return true, both the type and value must match.
  519. bool operator == (void* rhs) const
  520. {
  521. if (type_ == VAR_PTR)
  522. return value_.ptr_ == rhs;
  523. else
  524. return false;
  525. }
  526. /// Test for equality with a resource reference. To return true, both the type and value must match.
  527. bool operator == (const ResourceRef& rhs) const
  528. {
  529. if (type_ == VAR_RESOURCEREF)
  530. return *(reinterpret_cast<const ResourceRef*>(&value_)) == rhs;
  531. else
  532. return false;
  533. }
  534. /// Test for equality with a resource reference list. To return true, both the type and value must match.
  535. bool operator == (const ResourceRefList& rhs) const
  536. {
  537. if (type_ == VAR_RESOURCEREFLIST)
  538. return *(reinterpret_cast<const ResourceRefList*>(&value_)) == rhs;
  539. else
  540. return false;
  541. }
  542. /// Test for equality with a variant vector. To return true, both the type and value must match.
  543. bool operator == (const VariantVector& rhs) const
  544. {
  545. if (type_ == VAR_VARIANTVECTOR)
  546. return *(reinterpret_cast<const VariantVector*>(&value_)) == rhs;
  547. else
  548. return false;
  549. }
  550. /// Test for equality with a variant map. To return true, both the type and value must match.
  551. bool operator == (const VariantMap& rhs) const
  552. {
  553. if (type_ == VAR_VARIANTMAP)
  554. return *(reinterpret_cast<const VariantMap*>(&value_)) == rhs;
  555. else
  556. return false;
  557. }
  558. /// Test for equality with a StringHash. To return true, both the type and value must match.
  559. bool operator == (const StringHash& rhs) const
  560. {
  561. if (type_ == VAR_INT)
  562. return (unsigned)value_.int_ == rhs.GetValue();
  563. else
  564. return false;
  565. }
  566. /// Test for equality with a ShortStringHash. To return true, both the type and value must match.
  567. bool operator == (const ShortStringHash& rhs) const
  568. {
  569. if (type_ == VAR_INT)
  570. return (unsigned short)value_.int_ == rhs.GetValue();
  571. else
  572. return false;
  573. }
  574. /// Test for inequality with an integer.
  575. bool operator != (int rhs) const { return !(*this == rhs); }
  576. /// Test for inequality with an unsigned integer.
  577. bool operator != (unsigned rhs) const { return !(*this == rhs); }
  578. /// Test for inequality with a bool.
  579. bool operator != (bool rhs) const { return !(*this == rhs); }
  580. /// Test for inequality with a float.
  581. bool operator != (float rhs) const { return !(*this == rhs); }
  582. /// Test for inequality with a Vector2.
  583. bool operator != (const Vector2& rhs) const { return !(*this == rhs); }
  584. /// Test for inequality with a Vector3.
  585. bool operator != (const Vector3& rhs) const { return !(*this == rhs); }
  586. /// Test for inequality with an Vector4.
  587. bool operator != (const Vector4& rhs) const { return !(*this == rhs); }
  588. /// Test for inequality with a Quaternion.
  589. bool operator != (const Quaternion& rhs) const { return !(*this == rhs); }
  590. /// Test for inequality with a string.
  591. bool operator != (const String& rhs) const { return !(*this == rhs); }
  592. /// Test for inequality with a buffer.
  593. bool operator != (const PODVector<unsigned char>& rhs) const { return !(*this == rhs); }
  594. /// Test for inequality with a pointer.
  595. bool operator != (void* rhs) const { return !(*this == rhs); }
  596. /// Test for inequality with a resource reference.
  597. bool operator != (const ResourceRef& rhs) const { return !(*this == rhs); }
  598. /// Test for inequality with a resource reference list.
  599. bool operator != (const ResourceRefList& rhs) const { return !(*this == rhs); }
  600. /// Test for inequality with a variant vector.
  601. bool operator != (const VariantVector& rhs) const { return !(*this == rhs); }
  602. /// Test for inequality with a variant map.
  603. bool operator != (const VariantMap& rhs) const { return !(*this == rhs); }
  604. /// Test for inequality with a StringHash.
  605. bool operator != (const StringHash& rhs) const { return !(*this == rhs); }
  606. /// Test for inequality with a ShortStringHash.
  607. bool operator != (const ShortStringHash& rhs) const { return !(*this == rhs); }
  608. /// %Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  609. void FromString(const String& type, const String& value);
  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 char* type, const char* value);
  612. /// %Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  613. void FromString(VariantType type, const String& 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 char* value);
  616. /// %Set buffer type from a memory area.
  617. void SetBuffer(const void* data, unsigned size);
  618. /// Return int or zero on type mismatch.
  619. int GetInt() const
  620. {
  621. if (type_ != VAR_INT)
  622. return 0;
  623. return value_.int_;
  624. }
  625. /// Return unsigned int or zero on type mismatch.
  626. int GetUInt() const
  627. {
  628. if (type_ != VAR_INT)
  629. return 0;
  630. return (unsigned)value_.int_;
  631. }
  632. /// Return StringHash or zero on type mismatch.
  633. StringHash GetStringHash() const
  634. {
  635. if (type_ != VAR_INT)
  636. return StringHash::ZERO;
  637. return StringHash(value_.int_);
  638. }
  639. /// Return ShortStringHash or zero on type mismatch.
  640. ShortStringHash GetShortStringHash() const
  641. {
  642. if (type_ != VAR_INT)
  643. return ShortStringHash::ZERO;
  644. return ShortStringHash(value_.int_);
  645. }
  646. /// Return bool or false on type mismatch.
  647. bool GetBool() const
  648. {
  649. if (type_ != VAR_BOOL)
  650. return false;
  651. return value_.bool_;
  652. }
  653. /// Return float or zero on type mismatch.
  654. float GetFloat() const
  655. {
  656. if (type_ != VAR_FLOAT)
  657. return 0.0f;
  658. return value_.float_;
  659. }
  660. /// Return Vector2 or zero on type mismatch.
  661. const Vector2& GetVector2() const
  662. {
  663. if (type_ != VAR_VECTOR2)
  664. return Vector2::ZERO;
  665. return *reinterpret_cast<const Vector2*>(&value_);
  666. }
  667. /// Return Vector3 or zero on type mismatch.
  668. const Vector3& GetVector3() const
  669. {
  670. if (type_ != VAR_VECTOR3)
  671. return Vector3::ZERO;
  672. return *reinterpret_cast<const Vector3*>(&value_);
  673. }
  674. /// Return Vector4 or zero on type mismatch.
  675. const Vector4& GetVector4() const
  676. {
  677. if (type_ != VAR_VECTOR4)
  678. return Vector4::ZERO;
  679. return *reinterpret_cast<const Vector4*>(&value_);
  680. }
  681. /// Return quaternion or identity on type mismatch.
  682. const Quaternion& GetQuaternion() const
  683. {
  684. if (type_ != VAR_QUATERNION)
  685. return Quaternion::IDENTITY;
  686. return *reinterpret_cast<const Quaternion*>(&value_);
  687. }
  688. /// Return color or default on type mismatch.
  689. const Color& GetColor() const
  690. {
  691. if (type_ != VAR_COLOR)
  692. return Color::WHITE;
  693. return *reinterpret_cast<const Color*>(&value_);
  694. }
  695. /// Return string or empty on type mismatch.
  696. const String& GetString() const
  697. {
  698. if (type_ != VAR_STRING)
  699. return emptyString;
  700. return *reinterpret_cast<const String*>(&value_);
  701. }
  702. /// Return buffer or empty on type mismatch.
  703. const PODVector<unsigned char>& GetBuffer() const
  704. {
  705. if (type_ != VAR_BUFFER)
  706. return emptyBuffer;
  707. return *reinterpret_cast<const PODVector<unsigned char>*>(&value_);
  708. }
  709. /// Return pointer or null on type mismatch.
  710. void* GetPtr() const
  711. {
  712. if (type_ != VAR_PTR)
  713. return 0;
  714. return value_.ptr_;
  715. }
  716. /// Return a resource reference or empty on type mismatch.
  717. const ResourceRef& GetResourceRef() const
  718. {
  719. if (type_ != VAR_RESOURCEREF)
  720. return emptyResourceRef;
  721. return *reinterpret_cast<const ResourceRef*>(&value_);
  722. }
  723. /// Return a resource reference list or empty on type mismatch.
  724. const ResourceRefList& GetResourceRefList() const
  725. {
  726. if (type_ != VAR_RESOURCEREFLIST)
  727. return emptyResourceRefList;
  728. return *reinterpret_cast<const ResourceRefList*>(&value_);
  729. }
  730. /// Return a variant vector or empty on type mismatch.
  731. const VariantVector& GetVariantVector() const
  732. {
  733. if (type_ != VAR_VARIANTVECTOR)
  734. return emptyVariantVector;
  735. return *reinterpret_cast<const VariantVector*>(&value_);
  736. }
  737. /// Return a variant map or empty on type mismatch
  738. const VariantMap& GetVariantMap() const
  739. {
  740. if (type_ != VAR_VARIANTMAP)
  741. return emptyVariantMap;
  742. return *reinterpret_cast<const VariantMap*>(&value_);
  743. }
  744. /// Return the value, template version.
  745. template <class T> T Get() const;
  746. /// Return value's type.
  747. VariantType GetType() const { return type_; }
  748. /// Return value's type name.
  749. const String& GetTypeName() const;
  750. /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
  751. String ToString() const;
  752. /// Return name for variant type.
  753. static const String& GetTypeName(VariantType type);
  754. /// Return variant type from type name.
  755. static VariantType GetTypeFromName(const String& typeName);
  756. /// Return variant type from type name.
  757. static VariantType GetTypeFromName(const char* typeName);
  758. /// Empty variant.
  759. static const Variant EMPTY;
  760. private:
  761. /// %Set new type and allocate/deallocate memory as necessary.
  762. void SetType(VariantType newType);
  763. /// Variant type.
  764. VariantType type_;
  765. /// Variant value.
  766. VariantValue value_;
  767. /// Empty string.
  768. static const String emptyString;
  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. };