Variant.h 40 KB

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