Variant.pkg 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. $#include "Variant.h"
  2. /// Variant's supported types.
  3. enum VariantType
  4. {
  5. VAR_NONE = 0,
  6. VAR_INT,
  7. VAR_BOOL,
  8. VAR_FLOAT,
  9. VAR_VECTOR2,
  10. VAR_VECTOR3,
  11. VAR_VECTOR4,
  12. VAR_QUATERNION,
  13. VAR_COLOR,
  14. VAR_STRING,
  15. VAR_BUFFER,
  16. VAR_PTR,
  17. VAR_RESOURCEREF,
  18. VAR_RESOURCEREFLIST,
  19. VAR_VARIANTVECTOR,
  20. VAR_VARIANTMAP,
  21. VAR_INTRECT,
  22. VAR_INTVECTOR2,
  23. MAX_VAR_TYPES
  24. };
  25. /// Typed resource reference.
  26. struct ResourceRef
  27. {
  28. /// Construct.
  29. ResourceRef();
  30. /// Construct with type only and empty id.
  31. ResourceRef(ShortStringHash type);
  32. /// Construct with type and id.
  33. ResourceRef(ShortStringHash type, StringHash id);
  34. // Construct from another ResourceRef.
  35. ResourceRef(const ResourceRef& rhs);
  36. /// Object type.
  37. ShortStringHash type_ @ type;
  38. StringHash id_ @ id;
  39. /// Test for equality with another reference.
  40. bool operator == (const ResourceRef& rhs) const;
  41. };
  42. /// %List of typed resource references.
  43. struct ResourceRefList
  44. {
  45. /// Construct.
  46. ResourceRefList();
  47. /// Construct with type only.
  48. ResourceRefList(ShortStringHash type);
  49. /// Object type.
  50. ShortStringHash type_ @ type;
  51. /// Test for equality with another reference list.
  52. bool operator == (const ResourceRefList& rhs) const;
  53. };
  54. /// Variable that supports a fixed set of types.
  55. class Variant
  56. {
  57. public:
  58. /// Construct empty.
  59. Variant();
  60. /// Construct from integer.
  61. Variant(int value);
  62. /// Construct from unsigned integer.
  63. Variant(unsigned value);
  64. /// Construct from a string hash (convert to integer).
  65. Variant(const StringHash& value);
  66. /// Construct from a short string hash (convert to integer.)
  67. Variant(const ShortStringHash& value);
  68. /// Construct from a bool.
  69. Variant(bool value);
  70. /// Construct from a float.
  71. Variant(float value);
  72. /// Construct from a Vector2.
  73. Variant(const Vector2& value);
  74. /// Construct from a Vector3.
  75. Variant(const Vector3& value);
  76. /// Construct from a Vector4.
  77. Variant(const Vector4& value);
  78. /// Construct from a quaternion.
  79. Variant(const Quaternion& value);
  80. /// Construct from a color.
  81. Variant(const Color& value);
  82. /// Construct from a string.
  83. Variant(const String& value);
  84. /// Construct from a C string.
  85. Variant(const char* value);
  86. /// Construct from a pointer.
  87. Variant(void* value);
  88. /// Construct from a resource reference.
  89. Variant(const ResourceRef& value);
  90. /// Construct from a resource reference list.
  91. Variant(const ResourceRefList& value);
  92. /// Construct from an integer rect.
  93. Variant(const IntRect& value);
  94. /// Construct from an IntVector2.
  95. Variant(const IntVector2& value);
  96. /// Construct from type and value.
  97. Variant(const String& type, const String& value);
  98. /// Construct from type and value.
  99. Variant(VariantType type, const String& value) ;
  100. /// Construct from type and value.
  101. Variant(VariantType type, const char* value);
  102. /// Copy-construct from another variant.
  103. Variant(const Variant& value);
  104. /// Destruct.
  105. ~Variant();
  106. /// Reset to empty.
  107. void Clear();
  108. /// Test for equality with another variant.
  109. bool operator == (const Variant& rhs) const;
  110. /// Test for equality with an integer. To return true, both the type and value must match.
  111. bool operator == (int rhs) const;
  112. /// Test for equality with an unsigned integer. To return true, both the type and value must match.
  113. bool operator == (unsigned rhs) const;
  114. /// Test for equality with a bool. To return true, both the type and value must match.
  115. bool operator == (bool rhs) const;
  116. /// Test for equality with a float. To return true, both the type and value must match.
  117. bool operator == (float rhs) const;
  118. /// Test for equality with a Vector2. To return true, both the type and value must match.
  119. bool operator == (const Vector2& rhs);
  120. /// Test for equality with a Vector3. To return true, both the type and value must match.
  121. bool operator == (const Vector3& rhs) const;
  122. /// Test for equality with a Vector4. To return true, both the type and value must match.
  123. bool operator == (const Vector4& rhs) const;
  124. /// Test for equality with a quaternion. To return true, both the type and value must match.
  125. bool operator == (const Quaternion& rhs) const;
  126. /// Test for equality with a color. To return true, both the type and value must match.
  127. bool operator == (const Color& rhs) const;
  128. /// Test for equality with a string. To return true, both the type and value must match.
  129. bool operator == (const String& rhs) const;
  130. /// Test for equality with a pointer. To return true, both the type and value must match.
  131. bool operator == (void* rhs) const;
  132. /// Test for equality with a resource reference. To return true, both the type and value must match.
  133. bool operator == (const ResourceRef& rhs) const;
  134. /// Test for equality with a resource reference list. To return true, both the type and value must match.
  135. bool operator == (const ResourceRefList& rhs) const;
  136. /// Test for equality with an integer rect. To return true, both the type and value must match.
  137. bool operator == (const IntRect& rhs) const;
  138. /// Test for equality with an IntVector2. To return true, both the type and value must match.
  139. bool operator == (const IntVector2& rhs) const;
  140. /// Test for equality with a StringHash. To return true, both the type and value must match.
  141. bool operator == (const StringHash& rhs) const;
  142. /// Test for equality with a ShortStringHash. To return true, both the type and value must match.
  143. bool operator == (const ShortStringHash& rhs) const;
  144. /// Return int or zero on type mismatch.
  145. int GetInt() const;
  146. /// Return unsigned int or zero on type mismatch.
  147. int GetUInt() const;
  148. /// Return StringHash or zero on type mismatch.
  149. StringHash GetStringHash();
  150. /// Return ShortStringHash or zero on type mismatch.
  151. ShortStringHash GetShortStringHash();
  152. /// Return bool or false on type mismatch.
  153. bool GetBool() const;
  154. /// Return float or zero on type mismatch.
  155. float GetFloat() const;
  156. /// Return Vector2 or zero on type mismatch.
  157. const Vector2& GetVector2() const;
  158. /// Return Vector3 or zero on type mismatch.
  159. const Vector3& GetVector3() const;
  160. /// Return Vector4 or zero on type mismatch.
  161. const Vector4& GetVector4() const;
  162. /// Return quaternion or identity on type mismatch.
  163. const Quaternion& GetQuaternion() const;
  164. /// Return color or default on type mismatch.
  165. const Color& GetColor() const;
  166. /// Return string or empty on type mismatch.
  167. const String& GetString() const;
  168. /// Return pointer or null on type mismatch.
  169. void* GetPtr() const;
  170. /// Return a resource reference or empty on type mismatch.
  171. const ResourceRef& GetResourceRef() const;
  172. /// Return a resource reference list or empty on type mismatch.
  173. const ResourceRefList& GetResourceRefList() const;
  174. /// Return an integer rect or empty on type mismatch.
  175. const IntRect& GetIntRect() const;
  176. /// Return an IntVector2 or empty on type mismatch.
  177. const IntVector2& GetIntVector2() const;
  178. /// Return value's type.
  179. VariantType GetType() const;
  180. /// Return value's type name.
  181. String GetTypeName() const;
  182. /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
  183. String ToString() const;
  184. /// Return true when the variant value is considered zero according to its actual type.
  185. bool IsZero() const;
  186. /// Return true when the variant is empty (i.e. not initialized yet).
  187. bool IsEmpty() const;
  188. };
  189. class VariantMap
  190. {
  191. public:
  192. VariantMap();
  193. tolua_outside int VariantMapGetInt @ GetInt(const char* key);
  194. tolua_outside bool VariantMapGetBool @ GetBool(const char* key);
  195. tolua_outside float VariantMapGetFloat @ GetFloat(const char* key);
  196. tolua_outside const Vector2& VariantMapGetVector2 @ GetVector2(const char* key);
  197. tolua_outside const Vector3& VariantMapGetVector3 @ GetVector3(const char* key);
  198. tolua_outside const Vector4& VariantMapGetVector4 @ GetVector4(const char* key);
  199. tolua_outside const Quaternion& VariantMapGetQuaternion @ GetQuaternion(const char* key);
  200. tolua_outside const Color& VariantMapGetColor @ GetColor(const char* key);
  201. tolua_outside const String& VariantMapGetString @ GetString(const char* key);
  202. tolua_outside const void* VariantMapGetPtr @ GetPtr(const char* key);
  203. tolua_outside const ResourceRef& VariantMapGetResourceRef @ GetResourceRef(const char* key);
  204. tolua_outside const ResourceRefList& VariantMapGetResourceRefList @ GetResourceRefList(const char* key);
  205. tolua_outside const IntRect& VariantMapGetIntRect @ GetIntRect(const char* key);
  206. tolua_outside const IntVector2& VariantMapGetIntVector2 @ GetIntVector2(const char* key);
  207. tolua_outside void VariantMapSetInt @ SetInt(const char* key, int value);
  208. tolua_outside void VariantMapSetBool @ SetBool(const char* key, bool value);
  209. tolua_outside void VariantMapSetFloat @ SetFloat(const char* key, float value);
  210. tolua_outside void VariantMapSetVector2 @ SetVector2(const char* key, const Vector2& value);
  211. tolua_outside void VariantMapSetVector3 @ SetVector3(const char* key, const Vector3& value);
  212. tolua_outside void VariantMapSetVector4 @ SetVector4(const char* key, const Vector4& value);
  213. tolua_outside void VariantMapSetQuaternion @ SetQuaternion(const char* key, const Quaternion& value);
  214. tolua_outside void VariantMapSetColor @ SetColor(const char* key, const Color& value);
  215. tolua_outside void VariantMapSetString @ SetString(const char* key, const String& value);
  216. tolua_outside void VariantMapSetPtr @ SetPtr(const char* key, void* value);
  217. tolua_outside void VariantMapSetResourceRef @ SetResourceRef(const char* key, const ResourceRef& value);
  218. tolua_outside void VariantMapSetResourceRefList @ SetResourceRefList(const char* key, const ResourceRefList& value);
  219. tolua_outside void VariantMapSetIntRect @ SetIntRect(const char* key, const IntRect& value);
  220. tolua_outside void VariantMapSetIntVector2 @ SetIntVector2(const char* key, const IntVector2& value);
  221. };
  222. ${
  223. const Variant& FindVariant(const VariantMap* vmap, const char* key)
  224. {
  225. VariantMap::ConstIterator i = vmap->Find(ShortStringHash(key));
  226. return i != vmap->End() ? i->second_ : Variant::EMPTY;
  227. }
  228. int VariantMapGetInt(const VariantMap* vmap, const char* key)
  229. {
  230. return FindVariant(vmap, key).GetInt();
  231. }
  232. bool VariantMapGetBool(const VariantMap* vmap, const char* key)
  233. {
  234. return FindVariant(vmap, key).GetBool();
  235. }
  236. float VariantMapGetFloat(const VariantMap* vmap, const char* key)
  237. {
  238. return FindVariant(vmap, key).GetFloat();
  239. }
  240. const Vector2& VariantMapGetVector2(const VariantMap* vmap, const char* key)
  241. {
  242. return FindVariant(vmap, key).GetVector2();
  243. }
  244. const Vector3& VariantMapGetVector3(const VariantMap* vmap, const char* key)
  245. {
  246. return FindVariant(vmap, key).GetVector3();
  247. }
  248. const Vector4& VariantMapGetVector4(const VariantMap* vmap, const char* key)
  249. {
  250. return FindVariant(vmap, key).GetVector4();
  251. }
  252. const Quaternion& VariantMapGetQuaternion(const VariantMap* vmap, const char* key)
  253. {
  254. return FindVariant(vmap, key).GetQuaternion();
  255. }
  256. const Color& VariantMapGetColor(const VariantMap* vmap, const char* key)
  257. {
  258. return FindVariant(vmap, key).GetColor();
  259. }
  260. const String& VariantMapGetString(const VariantMap* vmap, const char* key)
  261. {
  262. return FindVariant(vmap, key).GetString();
  263. }
  264. const void* VariantMapGetPtr(const VariantMap* vmap, const char* key)
  265. {
  266. return FindVariant(vmap, key).GetPtr();
  267. }
  268. const ResourceRef& VariantMapGetResourceRef(const VariantMap* vmap, const char* key)
  269. {
  270. return FindVariant(vmap, key).GetResourceRef();
  271. }
  272. const ResourceRefList& VariantMapGetResourceRefList(const VariantMap* vmap, const char* key)
  273. {
  274. return FindVariant(vmap, key).GetResourceRefList();
  275. }
  276. const IntRect& VariantMapGetIntRect(const VariantMap* vmap, const char* key)
  277. {
  278. return FindVariant(vmap, key).GetIntRect();
  279. }
  280. const IntVector2& VariantMapGetIntVector2(const VariantMap* vmap, const char* key)
  281. {
  282. return FindVariant(vmap, key).GetIntVector2();
  283. }
  284. void VariantMapSetInt(VariantMap* vmap, const char* key, int value)
  285. {
  286. (*vmap)[ShortStringHash(key)] = value;
  287. }
  288. void VariantMapSetBool(VariantMap* vmap, const char* key, bool value)
  289. {
  290. (*vmap)[ShortStringHash(key)] = value;
  291. }
  292. void VariantMapSetFloat(VariantMap* vmap, const char* key, float value)
  293. {
  294. (*vmap)[ShortStringHash(key)] = value;
  295. }
  296. void VariantMapSetVector2(VariantMap* vmap, const char* key, const Vector2& value)
  297. {
  298. (*vmap)[ShortStringHash(key)] = value;
  299. }
  300. void VariantMapSetVector3(VariantMap* vmap, const char* key, const Vector3& value)
  301. {
  302. (*vmap)[ShortStringHash(key)] = value;
  303. }
  304. void VariantMapSetVector4(VariantMap* vmap, const char* key, const Vector4& value)
  305. {
  306. (*vmap)[ShortStringHash(key)] = value;
  307. }
  308. void VariantMapSetQuaternion(VariantMap* vmap, const char* key, const Quaternion& value)
  309. {
  310. (*vmap)[ShortStringHash(key)] = value;
  311. }
  312. void VariantMapSetColor(VariantMap* vmap, const char* key, const Color& value)
  313. {
  314. (*vmap)[ShortStringHash(key)] = value;
  315. }
  316. void VariantMapSetString(VariantMap* vmap, const char* key, const String& value)
  317. {
  318. (*vmap)[ShortStringHash(key)] = value;
  319. }
  320. void VariantMapSetPtr(VariantMap* vmap, const char* key, void* value)
  321. {
  322. (*vmap)[ShortStringHash(key)] = value;
  323. }
  324. void VariantMapSetResourceRef(VariantMap* vmap, const char* key, const ResourceRef& value)
  325. {
  326. (*vmap)[ShortStringHash(key)] = value;
  327. }
  328. void VariantMapSetResourceRefList(VariantMap* vmap, const char* key, const ResourceRefList& value)
  329. {
  330. (*vmap)[ShortStringHash(key)] = value;
  331. }
  332. void VariantMapSetIntRect(VariantMap* vmap, const char* key, const IntRect& value)
  333. {
  334. (*vmap)[ShortStringHash(key)] = value;
  335. }
  336. void VariantMapSetIntVector2(VariantMap* vmap, const char* key, const IntVector2& value)
  337. {
  338. (*vmap)[ShortStringHash(key)] = value;
  339. }
  340. $}