Variant.h 37 KB

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