Variant.pkg 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. tolua_outside int VariantMapGetInt @ GetInt(const char* key);
  193. tolua_outside bool VariantMapGetBool @ GetBool(const char* key);
  194. tolua_outside float VariantMapGetFloat @ GetFloat(const char* key);
  195. tolua_outside const Vector2& VariantMapGetVector2 @ GetVector2(const char* key);
  196. tolua_outside const Vector3& VariantMapGetVector3 @ GetVector3(const char* key);
  197. tolua_outside const Vector4& VariantMapGetVector4 @ GetVector4(const char* key);
  198. tolua_outside const Quaternion& VariantMapGetQuaternion @ GetQuaternion(const char* key);
  199. tolua_outside const Color& VariantMapGetColor @ GetColor(const char* key);
  200. tolua_outside const String& VariantMapGetString @ GetString(const char* key);
  201. tolua_outside const void* VariantMapGetPtr @ GetPtr(const char* key);
  202. tolua_outside const ResourceRef& VariantMapGetResourceRef @ GetResourceRef(const char* key);
  203. tolua_outside const ResourceRefList& VariantMapGetResourceRefList @ GetResourceRefList(const char* key);
  204. tolua_outside const IntRect& VariantMapGetIntRect @ GetIntRect(const char* key);
  205. tolua_outside const IntVector2& VariantMapGetIntVector2 @ GetIntVector2(const char* key);
  206. tolua_outside void VariantMapSetInt @ SetInt(const char* key, int value);
  207. tolua_outside void VariantMapSetBool @ SetBool(const char* key, bool value);
  208. tolua_outside void VariantMapSetFloat @ SetFloat(const char* key, float value);
  209. tolua_outside void VariantMapSetVector2 @ SetVector2(const char* key, const Vector2& value);
  210. tolua_outside void VariantMapSetVector3 @ SetVector3(const char* key, const Vector3& value);
  211. tolua_outside void VariantMapSetVector4 @ SetVector4(const char* key, const Vector4& value);
  212. tolua_outside void VariantMapSetQuaternion @ SetQuaternion(const char* key, const Quaternion& value);
  213. tolua_outside void VariantMapSetColor @ SetColor(const char* key, const Color& value);
  214. tolua_outside void VariantMapSetString @ SetString(const char* key, const String& value);
  215. tolua_outside void VariantMapSetPtr @ SetPtr(const char* key, const void*& value);
  216. tolua_outside void VariantMapSetResourceRef @ SetResourceRef(const char* key, const ResourceRef& value);
  217. tolua_outside void VariantMapSetResourceRefList @ SetResourceRefList(const char* key, const ResourceRefList& value);
  218. tolua_outside void VariantMapSetIntRect @ SetIntRect(const char* key, const IntRect& value);
  219. tolua_outside void VariantMapSetIntVector2 @ SetIntVector2(const char* key, const IntVector2& value);
  220. };