Variant.h 31 KB

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