Variant.h 28 KB

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