Variant.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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 (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 (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 URHO3D_API 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 equality with an integer. To return true, both the type and value must match.
  475. bool operator == (int rhs) const { return type_ == VAR_INT ? value_.int_ == rhs : false; }
  476. /// Test for equality with an unsigned integer. To return true, both the type and value must match.
  477. bool operator == (unsigned rhs) const { return type_ == VAR_INT ? value_.int_ == (int)rhs : false; }
  478. /// Test for equality with a bool. To return true, both the type and value must match.
  479. bool operator == (bool rhs) const { return type_ == VAR_BOOL ? value_.bool_ == rhs : false; }
  480. /// Test for equality with a float. To return true, both the type and value must match.
  481. bool operator == (float rhs) const { return type_ == VAR_FLOAT ? value_.float_ == rhs : false; }
  482. /// Test for equality with a Vector2. To return true, both the type and value must match.
  483. bool operator == (const Vector2& rhs) const { return type_ == VAR_VECTOR2 ? *(reinterpret_cast<const Vector2*>(&value_)) == rhs : false; }
  484. /// Test for equality with a Vector3. To return true, both the type and value must match.
  485. bool operator == (const Vector3& rhs) const { return type_ == VAR_VECTOR3 ? *(reinterpret_cast<const Vector3*>(&value_)) == rhs : false; }
  486. /// Test for equality with a Vector4. To return true, both the type and value must match.
  487. bool operator == (const Vector4& rhs) const { return type_ == VAR_VECTOR4 ? *(reinterpret_cast<const Vector4*>(&value_)) == rhs : false; }
  488. /// Test for equality with a quaternion. To return true, both the type and value must match.
  489. bool operator == (const Quaternion& rhs) const { return type_ == VAR_QUATERNION ? *(reinterpret_cast<const Quaternion*>(&value_)) == rhs : false; }
  490. /// Test for equality with a color. To return true, both the type and value must match.
  491. bool operator == (const Color& rhs) const { return type_ == VAR_COLOR ? *(reinterpret_cast<const Color*>(&value_)) == rhs : false; }
  492. /// Test for equality with a string. To return true, both the type and value must match.
  493. bool operator == (const String& rhs) const { return type_ == VAR_STRING ? *(reinterpret_cast<const String*>(&value_)) == rhs : false; }
  494. /// Test for equality with a buffer. To return true, both the type and value must match.
  495. bool operator == (const PODVector<unsigned char>& rhs) const { return type_ == VAR_BUFFER ? *(reinterpret_cast<const PODVector<unsigned char>*>(&value_)) == rhs : false; }
  496. /// Test for equality with a pointer. To return true, both the type and value must match.
  497. bool operator == (void* rhs) const { return type_ == VAR_PTR ? value_.ptr_ == rhs : false; }
  498. /// Test for equality with a resource reference. To return true, both the type and value must match.
  499. bool operator == (const ResourceRef& rhs) const { return type_ == VAR_RESOURCEREF ? *(reinterpret_cast<const ResourceRef*>(&value_)) == rhs : false; }
  500. /// Test for equality with a resource reference list. To return true, both the type and value must match.
  501. bool operator == (const ResourceRefList& rhs) const { return type_ == VAR_RESOURCEREFLIST ? *(reinterpret_cast<const ResourceRefList*>(&value_)) == rhs : false; }
  502. /// Test for equality with a variant vector. To return true, both the type and value must match.
  503. bool operator == (const VariantVector& rhs) const { return type_ == VAR_VARIANTVECTOR ? *(reinterpret_cast<const VariantVector*>(&value_)) == rhs : false; }
  504. /// Test for equality with a variant map. To return true, both the type and value must match.
  505. bool operator == (const VariantMap& rhs) const { return type_ == VAR_VARIANTMAP ? *(reinterpret_cast<const VariantMap*>(&value_)) == rhs : false; }
  506. /// Test for equality with an integer rect. To return true, both the type and value must match.
  507. bool operator == (const IntRect& rhs) const { return type_ == VAR_INTRECT ? *(reinterpret_cast<const IntRect*>(&value_)) == rhs : false; }
  508. /// Test for equality with an IntVector2. To return true, both the type and value must match.
  509. bool operator == (const IntVector2& rhs) const { return type_ == VAR_INTVECTOR2 ? *(reinterpret_cast<const IntVector2*>(&value_)) == rhs : false; }
  510. /// Test for equality with a StringHash. To return true, both the type and value must match.
  511. bool operator == (const StringHash& rhs) const { return type_ == VAR_INT ? (unsigned)value_.int_ == rhs.Value() : false; }
  512. /// Test for equality with a ShortStringHash. To return true, both the type and value must match.
  513. bool operator == (const ShortStringHash& rhs) const { return type_ == VAR_INT ? value_.int_ == rhs.Value() : false; }
  514. /// Test for inequality with another variant.
  515. bool operator != (const Variant& rhs) const { return !(*this == rhs); }
  516. /// Test for inequality with an integer.
  517. bool operator != (int rhs) const { return !(*this == rhs); }
  518. /// Test for inequality with an unsigned integer.
  519. bool operator != (unsigned rhs) const { return !(*this == rhs); }
  520. /// Test for inequality with a bool.
  521. bool operator != (bool rhs) const { return !(*this == rhs); }
  522. /// Test for inequality with a float.
  523. bool operator != (float rhs) const { return !(*this == rhs); }
  524. /// Test for inequality with a Vector2.
  525. bool operator != (const Vector2& rhs) const { return !(*this == rhs); }
  526. /// Test for inequality with a Vector3.
  527. bool operator != (const Vector3& rhs) const { return !(*this == rhs); }
  528. /// Test for inequality with an Vector4.
  529. bool operator != (const Vector4& rhs) const { return !(*this == rhs); }
  530. /// Test for inequality with a Quaternion.
  531. bool operator != (const Quaternion& rhs) const { return !(*this == rhs); }
  532. /// Test for inequality with a string.
  533. bool operator != (const String& rhs) const { return !(*this == rhs); }
  534. /// Test for inequality with a buffer.
  535. bool operator != (const PODVector<unsigned char>& rhs) const { return !(*this == rhs); }
  536. /// Test for inequality with a pointer.
  537. bool operator != (void* rhs) const { return !(*this == rhs); }
  538. /// Test for inequality with a resource reference.
  539. bool operator != (const ResourceRef& rhs) const { return !(*this == rhs); }
  540. /// Test for inequality with a resource reference list.
  541. bool operator != (const ResourceRefList& rhs) const { return !(*this == rhs); }
  542. /// Test for inequality with a variant vector.
  543. bool operator != (const VariantVector& rhs) const { return !(*this == rhs); }
  544. /// Test for inequality with a variant map.
  545. bool operator != (const VariantMap& rhs) const { return !(*this == rhs); }
  546. /// Test for inequality with an integer rect.
  547. bool operator != (const IntRect& rhs) const { return !(*this == rhs); }
  548. /// Test for inequality with an IntVector2.
  549. bool operator != (const IntVector2& rhs) const { return !(*this == rhs); }
  550. /// Test for inequality with a StringHash.
  551. bool operator != (const StringHash& rhs) const { return !(*this == rhs); }
  552. /// Test for inequality with a ShortStringHash.
  553. bool operator != (const ShortStringHash& rhs) const { return !(*this == rhs); }
  554. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  555. void FromString(const String& type, const String& value);
  556. /// Set from typename and value strings. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  557. void FromString(const char* type, const char* value);
  558. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  559. void FromString(VariantType type, const String& value);
  560. /// Set from type and value string. Pointers will be set to null, and VariantBuffer or VariantMap types are not supported.
  561. void FromString(VariantType type, const char* value);
  562. /// Set buffer type from a memory area.
  563. void SetBuffer(const void* data, unsigned size);
  564. /// Return int or zero on type mismatch.
  565. int GetInt() const { return type_ == VAR_INT ? value_.int_ : 0; }
  566. /// Return unsigned int or zero on type mismatch.
  567. int GetUInt() const { return type_ == VAR_INT ? (unsigned)value_.int_ : 0; }
  568. /// Return StringHash or zero on type mismatch.
  569. StringHash GetStringHash() const { return StringHash(GetUInt()); }
  570. /// Return ShortStringHash or zero on type mismatch.
  571. ShortStringHash GetShortStringHash() const { return ShortStringHash(GetUInt()); }
  572. /// Return bool or false on type mismatch.
  573. bool GetBool() const { return type_ == VAR_BOOL ? value_.bool_ : false; }
  574. /// Return float or zero on type mismatch.
  575. float GetFloat() const { return type_ == VAR_FLOAT ? value_.float_ : 0.0f; }
  576. /// Return Vector2 or zero on type mismatch.
  577. const Vector2& GetVector2() const { return type_ == VAR_VECTOR2 ? *reinterpret_cast<const Vector2*>(&value_) : Vector2::ZERO; }
  578. /// Return Vector3 or zero on type mismatch.
  579. const Vector3& GetVector3() const { return type_ == VAR_VECTOR3 ? *reinterpret_cast<const Vector3*>(&value_) : Vector3::ZERO; }
  580. /// Return Vector4 or zero on type mismatch.
  581. const Vector4& GetVector4() const { return type_ == VAR_VECTOR4 ? *reinterpret_cast<const Vector4*>(&value_) : Vector4::ZERO; }
  582. /// Return quaternion or identity on type mismatch.
  583. const Quaternion& GetQuaternion() const { return type_ == VAR_QUATERNION ? *reinterpret_cast<const Quaternion*>(&value_) : Quaternion::IDENTITY; }
  584. /// Return color or default on type mismatch.
  585. const Color& GetColor() const { return type_ == VAR_COLOR ? *reinterpret_cast<const Color*>(&value_) : Color::WHITE; }
  586. /// Return string or empty on type mismatch.
  587. const String& GetString() const { return type_ == VAR_STRING ? *reinterpret_cast<const String*>(&value_) : String::EMPTY; }
  588. /// Return buffer or empty on type mismatch.
  589. const PODVector<unsigned char>& GetBuffer() const { return type_ == VAR_BUFFER ? *reinterpret_cast<const PODVector<unsigned char>*>(&value_) : emptyBuffer; }
  590. /// Return pointer or null on type mismatch.
  591. void* GetPtr() const { return type_ == VAR_PTR ? value_.ptr_ : 0; }
  592. /// Return a resource reference or empty on type mismatch.
  593. const ResourceRef& GetResourceRef() const { return type_ == VAR_RESOURCEREF ? *reinterpret_cast<const ResourceRef*>(&value_) : emptyResourceRef; }
  594. /// Return a resource reference list or empty on type mismatch.
  595. const ResourceRefList& GetResourceRefList() const { return type_ == VAR_RESOURCEREFLIST ? *reinterpret_cast<const ResourceRefList*>(&value_) : emptyResourceRefList; }
  596. /// Return a variant vector or empty on type mismatch.
  597. const VariantVector& GetVariantVector() const { return type_ == VAR_VARIANTVECTOR ? *reinterpret_cast<const VariantVector*>(&value_) : emptyVariantVector; }
  598. /// Return a variant map or empty on type mismatch.
  599. const VariantMap& GetVariantMap() const { return type_ == VAR_VARIANTMAP ? *reinterpret_cast<const VariantMap*>(&value_) : emptyVariantMap; }
  600. /// Return an integer rect or empty on type mismatch.
  601. const IntRect& GetIntRect() const { return type_ == VAR_INTRECT ? *reinterpret_cast<const IntRect*>(&value_) : IntRect::ZERO; }
  602. /// Return an IntVector2 or empty on type mismatch.
  603. const IntVector2& GetIntVector2() const { return type_ == VAR_INTVECTOR2 ? *reinterpret_cast<const IntVector2*>(&value_) : IntVector2::ZERO; }
  604. /// Return a pointer to a modifiable buffer or null on type mismatch.
  605. PODVector<unsigned char>* GetBufferPtr() { return type_ == VAR_BUFFER ? reinterpret_cast<PODVector<unsigned char>*>(&value_) : 0; }
  606. /// Return a pointer to a modifiable variant vector or null on type mismatch.
  607. VariantVector* GetVariantVectorPtr() { return type_ == VAR_VARIANTVECTOR ? reinterpret_cast<VariantVector*>(&value_) : 0; }
  608. /// Return a pointer to a modifiable variant map or null on type mismatch.
  609. VariantMap* GetVariantMapPtr() { return type_ == VAR_VARIANTMAP ? reinterpret_cast<VariantMap*>(&value_) : 0; }
  610. /// Return the value, template version.
  611. template <class T> T Get() const;
  612. /// Return value's type.
  613. VariantType GetType() const { return type_; }
  614. /// Return value's type name.
  615. String GetTypeName() const;
  616. /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
  617. String ToString() const;
  618. /// Return true when the variant value is considered zero according to its actual type.
  619. bool IsZero() const;
  620. /// Return true when the variant is empty (i.e. not initialized yet).
  621. bool IsEmpty() const { return GetType() == VAR_NONE; }
  622. /// Return name for variant type.
  623. static String GetTypeName(VariantType type);
  624. /// Return variant type from type name.
  625. static VariantType GetTypeFromName(const String& typeName);
  626. /// Return variant type from type name.
  627. static VariantType GetTypeFromName(const char* typeName);
  628. /// Empty variant.
  629. static const Variant EMPTY;
  630. /// Empty buffer.
  631. static const PODVector<unsigned char> emptyBuffer;
  632. /// Empty resource reference.
  633. static const ResourceRef emptyResourceRef;
  634. /// Empty resource reference list.
  635. static const ResourceRefList emptyResourceRefList;
  636. /// Empty variant map.
  637. static const VariantMap emptyVariantMap;
  638. /// Empty variant vector.
  639. static const VariantVector emptyVariantVector;
  640. private:
  641. /// Set new type and allocate/deallocate memory as necessary.
  642. void SetType(VariantType newType);
  643. /// Variant type.
  644. VariantType type_;
  645. /// Variant value.
  646. VariantValue value_;
  647. };
  648. }