Variant.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. //
  2. // Copyright (c) 2008-2014 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 "Ptr.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_VOIDPTR,
  47. VAR_RESOURCEREF,
  48. VAR_RESOURCEREFLIST,
  49. VAR_VARIANTVECTOR,
  50. VAR_VARIANTMAP,
  51. VAR_INTRECT,
  52. VAR_INTVECTOR2,
  53. VAR_PTR,
  54. MAX_VAR_TYPES
  55. };
  56. /// Union for the possible variant values. Also stores non-POD objects such as String which must not exceed 16 bytes in size.
  57. struct VariantValue
  58. {
  59. union
  60. {
  61. int int_;
  62. bool bool_;
  63. float float_;
  64. void* ptr_;
  65. };
  66. union
  67. {
  68. int int2_;
  69. float float2_;
  70. void* ptr2_;
  71. };
  72. union
  73. {
  74. int int3_;
  75. float float3_;
  76. void* ptr3_;
  77. };
  78. union
  79. {
  80. int int4_;
  81. float float4_;
  82. void* ptr4_;
  83. };
  84. };
  85. /// Typed resource reference.
  86. struct URHO3D_API ResourceRef
  87. {
  88. /// Construct.
  89. ResourceRef()
  90. {
  91. }
  92. /// Construct with type only and empty id.
  93. ResourceRef(ShortStringHash type) :
  94. type_(type)
  95. {
  96. }
  97. /// Construct with type and resource name.
  98. ResourceRef(ShortStringHash type, const String& name) :
  99. type_(type),
  100. name_(name)
  101. {
  102. }
  103. // Construct from another ResourceRef.
  104. ResourceRef(const ResourceRef& rhs) :
  105. type_(rhs.type_),
  106. name_(rhs.name_)
  107. {
  108. }
  109. /// Object type.
  110. ShortStringHash type_;
  111. /// Object name.
  112. String name_;
  113. /// Test for equality with another reference.
  114. bool operator == (const ResourceRef& rhs) const { return type_ == rhs.type_ && name_ == rhs.name_; }
  115. /// Test for inequality with another reference.
  116. bool operator != (const ResourceRef& rhs) const { return type_ != rhs.type_ || name_ != rhs.name_; }
  117. };
  118. /// %List of typed resource references.
  119. struct URHO3D_API ResourceRefList
  120. {
  121. /// Construct.
  122. ResourceRefList()
  123. {
  124. }
  125. /// Construct with type only.
  126. ResourceRefList(ShortStringHash type) :
  127. type_(type)
  128. {
  129. }
  130. /// Construct with type and id list.
  131. ResourceRefList(ShortStringHash type, const Vector<String>& names) :
  132. type_(type),
  133. names_(names)
  134. {
  135. }
  136. /// Object type.
  137. ShortStringHash type_;
  138. /// List of object names.
  139. Vector<String> names_;
  140. /// Test for equality with another reference list.
  141. bool operator == (const ResourceRefList& rhs) const { return type_ == rhs.type_ && names_ == rhs.names_; }
  142. /// Test for inequality with another reference list.
  143. bool operator != (const ResourceRefList& rhs) const { return type_ != rhs.type_ || names_ != rhs.names_; }
  144. };
  145. class Variant;
  146. /// Vector of variants.
  147. typedef Vector<Variant> VariantVector;
  148. /// Map of variants.
  149. typedef HashMap<ShortStringHash, Variant> VariantMap;
  150. /// Variable that supports a fixed set of types.
  151. class URHO3D_API Variant
  152. {
  153. public:
  154. /// Construct empty.
  155. Variant() :
  156. type_(VAR_NONE)
  157. {
  158. }
  159. /// Construct from integer.
  160. Variant(int value) :
  161. type_(VAR_NONE)
  162. {
  163. *this = value;
  164. }
  165. /// Construct from unsigned integer.
  166. Variant(unsigned value) :
  167. type_(VAR_NONE)
  168. {
  169. *this = (int)value;
  170. }
  171. /// Construct from a string hash (convert to integer).
  172. Variant(const StringHash& value) :
  173. type_(VAR_NONE)
  174. {
  175. *this = (int)value.Value();
  176. }
  177. /// Construct from a short string hash (convert to integer.)
  178. Variant(const ShortStringHash& value) :
  179. type_(VAR_NONE)
  180. {
  181. *this = (int)value.Value();
  182. }
  183. /// Construct from a bool.
  184. Variant(bool value) :
  185. type_(VAR_NONE)
  186. {
  187. *this = value;
  188. }
  189. /// Construct from a float.
  190. Variant(float value) :
  191. type_(VAR_NONE)
  192. {
  193. *this = value;
  194. }
  195. /// Construct from a Vector2.
  196. Variant(const Vector2& value) :
  197. type_(VAR_NONE)
  198. {
  199. *this = value;
  200. }
  201. /// Construct from a Vector3.
  202. Variant(const Vector3& value) :
  203. type_(VAR_NONE)
  204. {
  205. *this = value;
  206. }
  207. /// Construct from a Vector4.
  208. Variant(const Vector4& value) :
  209. type_(VAR_NONE)
  210. {
  211. *this = value;
  212. }
  213. /// Construct from a quaternion.
  214. Variant(const Quaternion& value) :
  215. type_(VAR_NONE)
  216. {
  217. *this = value;
  218. }
  219. /// Construct from a color.
  220. Variant(const Color& value) :
  221. type_(VAR_NONE)
  222. {
  223. *this = value;
  224. }
  225. /// Construct from a string.
  226. Variant(const String& value) :
  227. type_(VAR_NONE)
  228. {
  229. *this = value;
  230. }
  231. /// Construct from a C string.
  232. Variant(const char* value) :
  233. type_(VAR_NONE)
  234. {
  235. *this = value;
  236. }
  237. /// Construct from a buffer.
  238. Variant(const PODVector<unsigned char>& value) :
  239. type_(VAR_NONE)
  240. {
  241. *this = value;
  242. }
  243. /// Construct from a pointer.
  244. Variant(void* value) :
  245. type_(VAR_NONE)
  246. {
  247. *this = value;
  248. }
  249. /// Construct from a resource reference.
  250. Variant(const ResourceRef& value) :
  251. type_(VAR_NONE)
  252. {
  253. *this = value;
  254. }
  255. /// Construct from a resource reference list.
  256. Variant(const ResourceRefList& value) :
  257. type_(VAR_NONE)
  258. {
  259. *this = value;
  260. }
  261. /// Construct from a variant vector.
  262. Variant(const VariantVector& value) :
  263. type_(VAR_NONE)
  264. {
  265. *this = value;
  266. }
  267. /// Construct from a variant map.
  268. Variant(const VariantMap& value) :
  269. type_(VAR_NONE)
  270. {
  271. *this = value;
  272. }
  273. /// Construct from an integer rect.
  274. Variant(const IntRect& value) :
  275. type_(VAR_NONE)
  276. {
  277. *this = value;
  278. }
  279. /// Construct from an IntVector2.
  280. Variant(const IntVector2& value) :
  281. type_(VAR_NONE)
  282. {
  283. *this = value;
  284. }
  285. /// Construct from a RefCounted pointer. The object will be stored internally in a WeakPtr so that its expiration can be detected safely.
  286. Variant(RefCounted* value) :
  287. type_(VAR_NONE)
  288. {
  289. *this = value;
  290. }
  291. /// Construct from type and value.
  292. Variant(const String& type, const String& value) :
  293. type_(VAR_NONE)
  294. {
  295. FromString(type, value);
  296. }
  297. /// Construct from type and value.
  298. Variant(VariantType type, const String& value) :
  299. type_(VAR_NONE)
  300. {
  301. FromString(type, value);
  302. }
  303. /// Construct from type and value.
  304. Variant(const char* type, const char* value) :
  305. type_(VAR_NONE)
  306. {
  307. FromString(type, value);
  308. }
  309. /// Construct from type and value.
  310. Variant(VariantType type, const char* value) :
  311. type_(VAR_NONE)
  312. {
  313. FromString(type, value);
  314. }
  315. /// Copy-construct from another variant.
  316. Variant(const Variant& value) :
  317. type_(VAR_NONE)
  318. {
  319. *this = value;
  320. }
  321. /// Destruct.
  322. ~Variant()
  323. {
  324. SetType(VAR_NONE);
  325. }
  326. /// Reset to empty.
  327. void Clear()
  328. {
  329. SetType(VAR_NONE);
  330. }
  331. /// Assign from another variant.
  332. Variant& operator = (const Variant& rhs);
  333. /// Assign from an integer.
  334. Variant& operator = (int rhs)
  335. {
  336. SetType(VAR_INT);
  337. value_.int_ = rhs;
  338. return *this;
  339. }
  340. /// Assign from an unsigned integer.
  341. Variant& operator = (unsigned rhs)
  342. {
  343. SetType(VAR_INT);
  344. value_.int_ = (int)rhs;
  345. return *this;
  346. }
  347. /// Assign from a StringHash (convert to integer.)
  348. Variant& operator = (const StringHash& rhs)
  349. {
  350. SetType(VAR_INT);
  351. value_.int_ = (int)rhs.Value();
  352. return *this;
  353. }
  354. /// Assign from a ShortStringHash (convert to integer)
  355. Variant& operator = (const ShortStringHash& rhs)
  356. {
  357. SetType(VAR_INT);
  358. value_.int_ = (int)rhs.Value();
  359. return *this;
  360. }
  361. /// Assign from a bool.
  362. Variant& operator = (bool rhs)
  363. {
  364. SetType(VAR_BOOL);
  365. value_.bool_ = rhs;
  366. return *this;
  367. }
  368. /// Assign from a float.
  369. Variant& operator = (float rhs)
  370. {
  371. SetType(VAR_FLOAT);
  372. value_.float_ = rhs;
  373. return *this;
  374. }
  375. /// Assign from a Vector2.
  376. Variant& operator = (const Vector2& rhs)
  377. {
  378. SetType(VAR_VECTOR2);
  379. *(reinterpret_cast<Vector2*>(&value_)) = rhs;
  380. return *this;
  381. }
  382. /// Assign from a Vector3.
  383. Variant& operator = (const Vector3& rhs)
  384. {
  385. SetType(VAR_VECTOR3);
  386. *(reinterpret_cast<Vector3*>(&value_)) = rhs;
  387. return *this;
  388. }
  389. /// Assign from a Vector4.
  390. Variant& operator = (const Vector4& rhs)
  391. {
  392. SetType(VAR_VECTOR4);
  393. *(reinterpret_cast<Vector4*>(&value_)) = rhs;
  394. return *this;
  395. }
  396. /// Assign from a quaternion.
  397. Variant& operator = (const Quaternion& rhs)
  398. {
  399. SetType(VAR_QUATERNION);
  400. *(reinterpret_cast<Quaternion*>(&value_)) = rhs;
  401. return *this;
  402. }
  403. /// Assign from a color.
  404. Variant& operator = (const Color& rhs)
  405. {
  406. SetType(VAR_COLOR);
  407. *(reinterpret_cast<Color*>(&value_)) = rhs;
  408. return *this;
  409. }
  410. /// Assign from a string.
  411. Variant& operator = (const String& rhs)
  412. {
  413. SetType(VAR_STRING);
  414. *(reinterpret_cast<String*>(&value_)) = rhs;
  415. return *this;
  416. }
  417. /// Assign from a C string.
  418. Variant& operator = (const char* rhs)
  419. {
  420. SetType(VAR_STRING);
  421. *(reinterpret_cast<String*>(&value_)) = String(rhs);
  422. return *this;
  423. }
  424. /// Assign from a buffer.
  425. Variant& operator = (const PODVector<unsigned char>& rhs)
  426. {
  427. SetType(VAR_BUFFER);
  428. *(reinterpret_cast<PODVector<unsigned char>*>(&value_)) = rhs;
  429. return *this;
  430. }
  431. /// Assign from a void pointer.
  432. Variant& operator = (void* rhs)
  433. {
  434. SetType(VAR_VOIDPTR);
  435. value_.ptr_ = rhs;
  436. return *this;
  437. }
  438. /// Assign from a resource reference.
  439. Variant& operator = (const ResourceRef& rhs)
  440. {
  441. SetType(VAR_RESOURCEREF);
  442. *(reinterpret_cast<ResourceRef*>(&value_)) = rhs;
  443. return *this;
  444. }
  445. /// Assign from a resource reference list.
  446. Variant& operator = (const ResourceRefList& rhs)
  447. {
  448. SetType(VAR_RESOURCEREFLIST);
  449. *(reinterpret_cast<ResourceRefList*>(&value_)) = rhs;
  450. return *this;
  451. }
  452. /// Assign from a variant vector.
  453. Variant& operator = (const VariantVector& rhs)
  454. {
  455. SetType(VAR_VARIANTVECTOR);
  456. *(reinterpret_cast<VariantVector*>(&value_)) = rhs;
  457. return *this;
  458. }
  459. /// Assign from a variant map.
  460. Variant& operator = (const VariantMap& rhs)
  461. {
  462. SetType(VAR_VARIANTMAP);
  463. *(reinterpret_cast<VariantMap*>(&value_)) = rhs;
  464. return *this;
  465. }
  466. /// Assign from an integer rect.
  467. Variant& operator = (const IntRect& rhs)
  468. {
  469. SetType(VAR_INTRECT);
  470. *(reinterpret_cast<IntRect*>(&value_)) = rhs;
  471. return *this;
  472. }
  473. /// Assign from an IntVector2.
  474. Variant& operator = (const IntVector2& rhs)
  475. {
  476. SetType(VAR_INTVECTOR2);
  477. *(reinterpret_cast<IntVector2*>(&value_)) = rhs;
  478. return *this;
  479. }
  480. /// Assign from a RefCounted pointer. The object will be stored internally in a WeakPtr so that its expiration can be detected safely.
  481. Variant& operator = (RefCounted* rhs)
  482. {
  483. SetType(VAR_PTR);
  484. *(reinterpret_cast<WeakPtr<RefCounted>*>(&value_)) = rhs;
  485. return *this;
  486. }
  487. /// Test for equality with another variant.
  488. bool operator == (const Variant& rhs) const;
  489. /// Test for equality with an integer. To return true, both the type and value must match.
  490. bool operator == (int rhs) const { return type_ == VAR_INT ? value_.int_ == rhs : false; }
  491. /// Test for equality with an unsigned integer. To return true, both the type and value must match.
  492. bool operator == (unsigned rhs) const { return type_ == VAR_INT ? value_.int_ == (int)rhs : false; }
  493. /// Test for equality with a bool. To return true, both the type and value must match.
  494. bool operator == (bool rhs) const { return type_ == VAR_BOOL ? value_.bool_ == rhs : false; }
  495. /// Test for equality with a float. To return true, both the type and value must match.
  496. bool operator == (float rhs) const { return type_ == VAR_FLOAT ? value_.float_ == rhs : false; }
  497. /// Test for equality with a Vector2. To return true, both the type and value must match.
  498. bool operator == (const Vector2& rhs) const { return type_ == VAR_VECTOR2 ? *(reinterpret_cast<const Vector2*>(&value_)) == rhs : false; }
  499. /// Test for equality with a Vector3. To return true, both the type and value must match.
  500. bool operator == (const Vector3& rhs) const { return type_ == VAR_VECTOR3 ? *(reinterpret_cast<const Vector3*>(&value_)) == rhs : false; }
  501. /// Test for equality with a Vector4. To return true, both the type and value must match.
  502. bool operator == (const Vector4& rhs) const { return type_ == VAR_VECTOR4 ? *(reinterpret_cast<const Vector4*>(&value_)) == rhs : false; }
  503. /// Test for equality with a quaternion. To return true, both the type and value must match.
  504. bool operator == (const Quaternion& rhs) const { return type_ == VAR_QUATERNION ? *(reinterpret_cast<const Quaternion*>(&value_)) == rhs : false; }
  505. /// Test for equality with a color. To return true, both the type and value must match.
  506. bool operator == (const Color& rhs) const { return type_ == VAR_COLOR ? *(reinterpret_cast<const Color*>(&value_)) == rhs : false; }
  507. /// Test for equality with a string. To return true, both the type and value must match.
  508. bool operator == (const String& rhs) const { return type_ == VAR_STRING ? *(reinterpret_cast<const String*>(&value_)) == rhs : false; }
  509. /// Test for equality with a buffer. To return true, both the type and value must match.
  510. bool operator == (const PODVector<unsigned char>& rhs) const { return type_ == VAR_BUFFER ? *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)) == rhs : false; }
  511. /// Test for equality with a void pointer. To return true, both the type and value must match, with the exception that a RefCounted pointer is also allowed.
  512. bool operator == (void* rhs) const
  513. {
  514. if (type_ == VAR_VOIDPTR)
  515. return value_.ptr_ == rhs;
  516. else if (type_ == VAR_PTR)
  517. return *(reinterpret_cast<const WeakPtr<RefCounted>*>(&value_)) == rhs;
  518. else
  519. return false;
  520. }
  521. /// Test for equality with a resource reference. To return true, both the type and value must match.
  522. bool operator == (const ResourceRef& rhs) const { return type_ == VAR_RESOURCEREF ? *(reinterpret_cast<const ResourceRef*>(&value_)) == rhs : false; }
  523. /// Test for equality with a resource reference list. To return true, both the type and value must match.
  524. bool operator == (const ResourceRefList& rhs) const { return type_ == VAR_RESOURCEREFLIST ? *(reinterpret_cast<const ResourceRefList*>(&value_)) == rhs : false; }
  525. /// Test for equality with a variant vector. To return true, both the type and value must match.
  526. bool operator == (const VariantVector& rhs) const { return type_ == VAR_VARIANTVECTOR ? *(reinterpret_cast<const VariantVector*>(&value_)) == rhs : false; }
  527. /// Test for equality with a variant map. To return true, both the type and value must match.
  528. bool operator == (const VariantMap& rhs) const { return type_ == VAR_VARIANTMAP ? *(reinterpret_cast<const VariantMap*>(&value_)) == rhs : false; }
  529. /// Test for equality with an integer rect. To return true, both the type and value must match.
  530. bool operator == (const IntRect& rhs) const { return type_ == VAR_INTRECT ? *(reinterpret_cast<const IntRect*>(&value_)) == rhs : false; }
  531. /// Test for equality with an IntVector2. To return true, both the type and value must match.
  532. bool operator == (const IntVector2& rhs) const { return type_ == VAR_INTVECTOR2 ? *(reinterpret_cast<const IntVector2*>(&value_)) == rhs : false; }
  533. /// Test for equality with a StringHash. To return true, both the type and value must match.
  534. bool operator == (const StringHash& rhs) const { return type_ == VAR_INT ? (unsigned)value_.int_ == rhs.Value() : false; }
  535. /// Test for equality with a ShortStringHash. To return true, both the type and value must match.
  536. bool operator == (const ShortStringHash& rhs) const { return type_ == VAR_INT ? value_.int_ == rhs.Value() : false; }
  537. /// Test for equality with a RefCounted pointer. To return true, both the type and value must match, with the exception that void pointer is also allowed.
  538. bool operator == (RefCounted* rhs) const
  539. {
  540. if (type_ == VAR_PTR)
  541. return *(reinterpret_cast<const WeakPtr<RefCounted>*>(&value_)) == rhs;
  542. else if (type_ == VAR_VOIDPTR)
  543. return value_.ptr_ == rhs;
  544. else
  545. return false;
  546. }
  547. /// Test for inequality with another variant.
  548. bool operator != (const Variant& rhs) const { return !(*this == rhs); }
  549. /// Test for inequality with an integer.
  550. bool operator != (int rhs) const { return !(*this == rhs); }
  551. /// Test for inequality with an unsigned integer.
  552. bool operator != (unsigned rhs) const { return !(*this == rhs); }
  553. /// Test for inequality with a bool.
  554. bool operator != (bool rhs) const { return !(*this == rhs); }
  555. /// Test for inequality with a float.
  556. bool operator != (float rhs) const { return !(*this == rhs); }
  557. /// Test for inequality with a Vector2.
  558. bool operator != (const Vector2& rhs) const { return !(*this == rhs); }
  559. /// Test for inequality with a Vector3.
  560. bool operator != (const Vector3& rhs) const { return !(*this == rhs); }
  561. /// Test for inequality with an Vector4.
  562. bool operator != (const Vector4& rhs) const { return !(*this == rhs); }
  563. /// Test for inequality with a Quaternion.
  564. bool operator != (const Quaternion& rhs) const { return !(*this == rhs); }
  565. /// Test for inequality with a string.
  566. bool operator != (const String& rhs) const { return !(*this == rhs); }
  567. /// Test for inequality with a buffer.
  568. bool operator != (const PODVector<unsigned char>& rhs) const { return !(*this == rhs); }
  569. /// Test for inequality with a pointer.
  570. bool operator != (void* rhs) const { return !(*this == rhs); }
  571. /// Test for inequality with a resource reference.
  572. bool operator != (const ResourceRef& rhs) const { return !(*this == rhs); }
  573. /// Test for inequality with a resource reference list.
  574. bool operator != (const ResourceRefList& rhs) const { return !(*this == rhs); }
  575. /// Test for inequality with a variant vector.
  576. bool operator != (const VariantVector& rhs) const { return !(*this == rhs); }
  577. /// Test for inequality with a variant map.
  578. bool operator != (const VariantMap& rhs) const { return !(*this == rhs); }
  579. /// Test for inequality with an integer rect.
  580. bool operator != (const IntRect& rhs) const { return !(*this == rhs); }
  581. /// Test for inequality with an IntVector2.
  582. bool operator != (const IntVector2& rhs) const { return !(*this == rhs); }
  583. /// Test for inequality with a StringHash.
  584. bool operator != (const StringHash& rhs) const { return !(*this == rhs); }
  585. /// Test for inequality with a ShortStringHash.
  586. bool operator != (const ShortStringHash& rhs) const { return !(*this == rhs); }
  587. /// Test for inequality with a RefCounted pointer.
  588. bool operator != (RefCounted* rhs) const { return !(*this == rhs); }
  589. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  590. void FromString(const String& type, const String& value);
  591. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  592. void FromString(const char* type, const char* value);
  593. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  594. void FromString(VariantType type, const String& value);
  595. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  596. void FromString(VariantType type, const char* value);
  597. /// Set buffer type from a memory area.
  598. void SetBuffer(const void* data, unsigned size);
  599. /// Return int or zero on type mismatch.
  600. int GetInt() const { return type_ == VAR_INT ? value_.int_ : 0; }
  601. /// Return unsigned int or zero on type mismatch.
  602. int GetUInt() const { return type_ == VAR_INT ? (unsigned)value_.int_ : 0; }
  603. /// Return StringHash or zero on type mismatch.
  604. StringHash GetStringHash() const { return StringHash(GetUInt()); }
  605. /// Return ShortStringHash or zero on type mismatch.
  606. ShortStringHash GetShortStringHash() const { return ShortStringHash(GetUInt()); }
  607. /// Return bool or false on type mismatch.
  608. bool GetBool() const { return type_ == VAR_BOOL ? value_.bool_ : false; }
  609. /// Return float or zero on type mismatch.
  610. float GetFloat() const { return type_ == VAR_FLOAT ? value_.float_ : 0.0f; }
  611. /// Return Vector2 or zero on type mismatch.
  612. const Vector2& GetVector2() const { return type_ == VAR_VECTOR2 ? *reinterpret_cast<const Vector2*>(&value_) : Vector2::ZERO; }
  613. /// Return Vector3 or zero on type mismatch.
  614. const Vector3& GetVector3() const { return type_ == VAR_VECTOR3 ? *reinterpret_cast<const Vector3*>(&value_) : Vector3::ZERO; }
  615. /// Return Vector4 or zero on type mismatch.
  616. const Vector4& GetVector4() const { return type_ == VAR_VECTOR4 ? *reinterpret_cast<const Vector4*>(&value_) : Vector4::ZERO; }
  617. /// Return quaternion or identity on type mismatch.
  618. const Quaternion& GetQuaternion() const { return type_ == VAR_QUATERNION ? *reinterpret_cast<const Quaternion*>(&value_) : Quaternion::IDENTITY; }
  619. /// Return color or default on type mismatch.
  620. const Color& GetColor() const { return type_ == VAR_COLOR ? *reinterpret_cast<const Color*>(&value_) : Color::WHITE; }
  621. /// Return string or empty on type mismatch.
  622. const String& GetString() const { return type_ == VAR_STRING ? *reinterpret_cast<const String*>(&value_) : String::EMPTY; }
  623. /// Return buffer or empty on type mismatch.
  624. const PODVector<unsigned char>& GetBuffer() const { return type_ == VAR_BUFFER ? *reinterpret_cast<const PODVector<unsigned char>*>(&value_) : emptyBuffer; }
  625. /// Return void pointer or null on type mismatch. RefCounted pointer will be converted.
  626. void* GetVoidPtr() const
  627. {
  628. if (type_ == VAR_VOIDPTR)
  629. return value_.ptr_;
  630. else if (type_ == VAR_PTR)
  631. return *reinterpret_cast<const WeakPtr<RefCounted>*>(&value_);
  632. else
  633. return 0;
  634. }
  635. /// Return a resource reference or empty on type mismatch.
  636. const ResourceRef& GetResourceRef() const { return type_ == VAR_RESOURCEREF ? *reinterpret_cast<const ResourceRef*>(&value_) : emptyResourceRef; }
  637. /// Return a resource reference list or empty on type mismatch.
  638. const ResourceRefList& GetResourceRefList() const { return type_ == VAR_RESOURCEREFLIST ? *reinterpret_cast<const ResourceRefList*>(&value_) : emptyResourceRefList; }
  639. /// Return a variant vector or empty on type mismatch.
  640. const VariantVector& GetVariantVector() const { return type_ == VAR_VARIANTVECTOR ? *reinterpret_cast<const VariantVector*>(&value_) : emptyVariantVector; }
  641. /// Return a variant map or empty on type mismatch.
  642. const VariantMap& GetVariantMap() const { return type_ == VAR_VARIANTMAP ? *reinterpret_cast<const VariantMap*>(&value_) : emptyVariantMap; }
  643. /// Return an integer rect or empty on type mismatch.
  644. const IntRect& GetIntRect() const { return type_ == VAR_INTRECT ? *reinterpret_cast<const IntRect*>(&value_) : IntRect::ZERO; }
  645. /// Return an IntVector2 or empty on type mismatch.
  646. const IntVector2& GetIntVector2() const { return type_ == VAR_INTVECTOR2 ? *reinterpret_cast<const IntVector2*>(&value_) : IntVector2::ZERO; }
  647. /// Return a RefCounted pointer or null on type mismatch. Will return null if holding a void pointer, as it can not be safely verified that the object is a RefCounted.
  648. RefCounted* GetPtr() const { return type_ == VAR_PTR ? *reinterpret_cast<const WeakPtr<RefCounted>*>(&value_) : (RefCounted*)0; }
  649. /// Return value's type.
  650. VariantType GetType() const { return type_; }
  651. /// Return value's type name.
  652. String GetTypeName() const;
  653. /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
  654. String ToString() const;
  655. /// Return true when the variant value is considered zero according to its actual type.
  656. bool IsZero() const;
  657. /// Return true when the variant is empty (i.e. not initialized yet).
  658. bool IsEmpty() const { return type_ == VAR_NONE; }
  659. /// Return the value, template version.
  660. template <class T> T Get() const;
  661. /// Return a pointer to a modifiable buffer or null on type mismatch.
  662. PODVector<unsigned char>* GetBufferPtr() { return type_ == VAR_BUFFER ? reinterpret_cast<PODVector<unsigned char>*>(&value_) : 0; }
  663. /// Return a pointer to a modifiable variant vector or null on type mismatch.
  664. VariantVector* GetVariantVectorPtr() { return type_ == VAR_VARIANTVECTOR ? reinterpret_cast<VariantVector*>(&value_) : 0; }
  665. /// Return a pointer to a modifiable variant map or null on type mismatch.
  666. VariantMap* GetVariantMapPtr() { return type_ == VAR_VARIANTMAP ? reinterpret_cast<VariantMap*>(&value_) : 0; }
  667. /// Return name for variant type.
  668. static String GetTypeName(VariantType type);
  669. /// Return variant type from type name.
  670. static VariantType GetTypeFromName(const String& typeName);
  671. /// Return variant type from type name.
  672. static VariantType GetTypeFromName(const char* typeName);
  673. /// Empty variant.
  674. static const Variant EMPTY;
  675. /// Empty buffer.
  676. static const PODVector<unsigned char> emptyBuffer;
  677. /// Empty resource reference.
  678. static const ResourceRef emptyResourceRef;
  679. /// Empty resource reference list.
  680. static const ResourceRefList emptyResourceRefList;
  681. /// Empty variant map.
  682. static const VariantMap emptyVariantMap;
  683. /// Empty variant vector.
  684. static const VariantVector emptyVariantVector;
  685. private:
  686. /// Set new type and allocate/deallocate memory as necessary.
  687. void SetType(VariantType newType);
  688. /// Variant type.
  689. VariantType type_;
  690. /// Variant value.
  691. VariantValue value_;
  692. };
  693. }