Variant.h 32 KB

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