Variant.h 28 KB

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