Variant.pkg 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 void VariantMapSetInt @ SetInt(const char* key, int value);
  194. tolua_outside void VariantMapSetBool @ SetBool(const char* key, bool value);
  195. tolua_outside void VariantMapSetFloat @ SetFloat(const char* key, float value);
  196. tolua_outside void VariantMapSetVector2 @ SetVector2(const char* key, const Vector2& value);
  197. tolua_outside void VariantMapSetVector3 @ SetVector3(const char* key, const Vector3& value);
  198. tolua_outside void VariantMapSetVector4 @ SetVector4(const char* key, const Vector4& value);
  199. tolua_outside void VariantMapSetQuaternion @ SetQuaternion(const char* key, const Quaternion& value);
  200. tolua_outside void VariantMapSetColor @ SetColor(const char* key, const Color& value);
  201. tolua_outside void VariantMapSetString @ SetString(const char* key, const String& value);
  202. tolua_outside void VariantMapSetPtr @ SetPtr(const char* key, void* value);
  203. tolua_outside void VariantMapSetResourceRef @ SetResourceRef(const char* key, const ResourceRef& value);
  204. tolua_outside void VariantMapSetResourceRefList @ SetResourceRefList(const char* key, const ResourceRefList& value);
  205. tolua_outside void VariantMapSetIntRect @ SetIntRect(const char* key, const IntRect& value);
  206. tolua_outside void VariantMapSetIntVector2 @ SetIntVector2(const char* key, const IntVector2& value);
  207. tolua_outside void VariantMapSetCamera @ SetCamera(const char* key, Camera* camera);
  208. tolua_outside void VariantMapSetConnection @ SetConnection(const char* key, Connection* connection);
  209. tolua_outside void VariantMapSetNode @ SetNode(const char* key, Node* node);
  210. tolua_outside void VariantMapSetPhysicsWorld @ SetPhysicsWorld(const char* key, PhysicsWorld* physicsworld);
  211. tolua_outside void VariantMapSetRigidBody @ SetRigidBody(const char* key, RigidBody* rigidbody);
  212. tolua_outside void VariantMapSetScene @ SetScene(const char* key, Scene* scene);
  213. tolua_outside void VariantMapSetUIElement @ SetUIElement(const char* key, UIElement* element);
  214. tolua_outside int VariantMapGetInt @ GetInt(const char* key);
  215. tolua_outside bool VariantMapGetBool @ GetBool(const char* key);
  216. tolua_outside float VariantMapGetFloat @ GetFloat(const char* key);
  217. tolua_outside const Vector2& VariantMapGetVector2 @ GetVector2(const char* key);
  218. tolua_outside const Vector3& VariantMapGetVector3 @ GetVector3(const char* key);
  219. tolua_outside const Vector4& VariantMapGetVector4 @ GetVector4(const char* key);
  220. tolua_outside const Quaternion& VariantMapGetQuaternion @ GetQuaternion(const char* key);
  221. tolua_outside const Color& VariantMapGetColor @ GetColor(const char* key);
  222. tolua_outside const String& VariantMapGetString @ GetString(const char* key);
  223. tolua_outside const void* VariantMapGetPtr @ GetPtr(const char* key);
  224. tolua_outside const ResourceRef& VariantMapGetResourceRef @ GetResourceRef(const char* key);
  225. tolua_outside const ResourceRefList& VariantMapGetResourceRefList @ GetResourceRefList(const char* key);
  226. tolua_outside const IntRect& VariantMapGetIntRect @ GetIntRect(const char* key);
  227. tolua_outside const IntVector2& VariantMapGetIntVector2 @ GetIntVector2(const char* key);
  228. tolua_outside Camera* VariantMapGetCamera @ GetCamera(const char* key);
  229. tolua_outside Connection* VariantMapGetConnection @ GetConnection(const char* key);
  230. tolua_outside Node* VariantMapGetNode @ GetNode(const char* key);
  231. tolua_outside PhysicsWorld* VariantMapGetPhysicsWorld @ GetPhysicsWorld(const char* key);
  232. tolua_outside RigidBody* VariantMapGetRigidBody @ GetRigidBody(const char* key);
  233. tolua_outside Scene* VariantMapGetScene @ GetScene(const char* key);
  234. tolua_outside UIElement* VariantMapGetUIElement @ GetUIElement(const char* key);
  235. };
  236. ${
  237. void VariantMapSetInt(VariantMap* vmap, const char* key, int value)
  238. {
  239. (*vmap)[ShortStringHash(key)] = value;
  240. }
  241. void VariantMapSetBool(VariantMap* vmap, const char* key, bool value)
  242. {
  243. (*vmap)[ShortStringHash(key)] = value;
  244. }
  245. void VariantMapSetFloat(VariantMap* vmap, const char* key, float value)
  246. {
  247. (*vmap)[ShortStringHash(key)] = value;
  248. }
  249. void VariantMapSetVector2(VariantMap* vmap, const char* key, const Vector2& value)
  250. {
  251. (*vmap)[ShortStringHash(key)] = value;
  252. }
  253. void VariantMapSetVector3(VariantMap* vmap, const char* key, const Vector3& value)
  254. {
  255. (*vmap)[ShortStringHash(key)] = value;
  256. }
  257. void VariantMapSetVector4(VariantMap* vmap, const char* key, const Vector4& value)
  258. {
  259. (*vmap)[ShortStringHash(key)] = value;
  260. }
  261. void VariantMapSetQuaternion(VariantMap* vmap, const char* key, const Quaternion& value)
  262. {
  263. (*vmap)[ShortStringHash(key)] = value;
  264. }
  265. void VariantMapSetColor(VariantMap* vmap, const char* key, const Color& value)
  266. {
  267. (*vmap)[ShortStringHash(key)] = value;
  268. }
  269. void VariantMapSetString(VariantMap* vmap, const char* key, const String& value)
  270. {
  271. (*vmap)[ShortStringHash(key)] = value;
  272. }
  273. void VariantMapSetPtr(VariantMap* vmap, const char* key, void* value)
  274. {
  275. (*vmap)[ShortStringHash(key)] = value;
  276. }
  277. void VariantMapSetResourceRef(VariantMap* vmap, const char* key, const ResourceRef& value)
  278. {
  279. (*vmap)[ShortStringHash(key)] = value;
  280. }
  281. void VariantMapSetResourceRefList(VariantMap* vmap, const char* key, const ResourceRefList& value)
  282. {
  283. (*vmap)[ShortStringHash(key)] = value;
  284. }
  285. void VariantMapSetIntRect(VariantMap* vmap, const char* key, const IntRect& value)
  286. {
  287. (*vmap)[ShortStringHash(key)] = value;
  288. }
  289. void VariantMapSetIntVector2(VariantMap* vmap, const char* key, const IntVector2& value)
  290. {
  291. (*vmap)[ShortStringHash(key)] = value;
  292. }
  293. void VariantMapSetCamera(VariantMap* vmap, const char* key, Camera* pointer)
  294. {
  295. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  296. }
  297. void VariantMapSetConnection(VariantMap* vmap, const char* key, Connection* pointer)
  298. {
  299. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  300. }
  301. void VariantMapSetNode(VariantMap* vmap, const char* key, Node* pointer)
  302. {
  303. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  304. }
  305. void VariantMapSetPhysicsWorld(VariantMap* vmap, const char* key, PhysicsWorld* pointer)
  306. {
  307. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  308. }
  309. void VariantMapSetRigidBody(VariantMap* vmap, const char* key, RigidBody* pointer)
  310. {
  311. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  312. }
  313. void VariantMapSetScene(VariantMap* vmap, const char* key, Scene* pointer)
  314. {
  315. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  316. }
  317. void VariantMapSetUIElement(VariantMap* vmap, const char* key, UIElement* pointer)
  318. {
  319. (*vmap)[ShortStringHash(key)] = (void*)pointer;
  320. }
  321. const Variant& FindVariant(const VariantMap* vmap, const char* key)
  322. {
  323. VariantMap::ConstIterator i = vmap->Find(ShortStringHash(key));
  324. return i != vmap->End() ? i->second_ : Variant::EMPTY;
  325. }
  326. int VariantMapGetInt(const VariantMap* vmap, const char* key)
  327. {
  328. return FindVariant(vmap, key).GetInt();
  329. }
  330. bool VariantMapGetBool(const VariantMap* vmap, const char* key)
  331. {
  332. return FindVariant(vmap, key).GetBool();
  333. }
  334. float VariantMapGetFloat(const VariantMap* vmap, const char* key)
  335. {
  336. return FindVariant(vmap, key).GetFloat();
  337. }
  338. const Vector2& VariantMapGetVector2(const VariantMap* vmap, const char* key)
  339. {
  340. return FindVariant(vmap, key).GetVector2();
  341. }
  342. const Vector3& VariantMapGetVector3(const VariantMap* vmap, const char* key)
  343. {
  344. return FindVariant(vmap, key).GetVector3();
  345. }
  346. const Vector4& VariantMapGetVector4(const VariantMap* vmap, const char* key)
  347. {
  348. return FindVariant(vmap, key).GetVector4();
  349. }
  350. const Quaternion& VariantMapGetQuaternion(const VariantMap* vmap, const char* key)
  351. {
  352. return FindVariant(vmap, key).GetQuaternion();
  353. }
  354. const Color& VariantMapGetColor(const VariantMap* vmap, const char* key)
  355. {
  356. return FindVariant(vmap, key).GetColor();
  357. }
  358. const String& VariantMapGetString(const VariantMap* vmap, const char* key)
  359. {
  360. return FindVariant(vmap, key).GetString();
  361. }
  362. const void* VariantMapGetPtr(const VariantMap* vmap, const char* key)
  363. {
  364. return FindVariant(vmap, key).GetPtr();
  365. }
  366. const ResourceRef& VariantMapGetResourceRef(const VariantMap* vmap, const char* key)
  367. {
  368. return FindVariant(vmap, key).GetResourceRef();
  369. }
  370. const ResourceRefList& VariantMapGetResourceRefList(const VariantMap* vmap, const char* key)
  371. {
  372. return FindVariant(vmap, key).GetResourceRefList();
  373. }
  374. const IntRect& VariantMapGetIntRect(const VariantMap* vmap, const char* key)
  375. {
  376. return FindVariant(vmap, key).GetIntRect();
  377. }
  378. const IntVector2& VariantMapGetIntVector2(const VariantMap* vmap, const char* key)
  379. {
  380. return FindVariant(vmap, key).GetIntVector2();
  381. }
  382. Camera* VariantMapGetCamera(const VariantMap* vmap, const char* key)
  383. {
  384. return (Camera*)FindVariant(vmap, key).GetPtr();
  385. }
  386. Connection* VariantMapGetConnection(const VariantMap* vmap, const char* key)
  387. {
  388. return (Connection*)FindVariant(vmap, key).GetPtr();
  389. }
  390. Node* VariantMapGetNode(const VariantMap* vmap, const char* key)
  391. {
  392. return (Node*)FindVariant(vmap, key).GetPtr();
  393. }
  394. PhysicsWorld* VariantMapGetPhysicsWorld(const VariantMap* vmap, const char* key)
  395. {
  396. return (PhysicsWorld*)FindVariant(vmap, key).GetPtr();
  397. }
  398. RigidBody* VariantMapGetRigidBody(const VariantMap* vmap, const char* key)
  399. {
  400. return (RigidBody*)FindVariant(vmap, key).GetPtr();
  401. }
  402. Scene* VariantMapGetScene(const VariantMap* vmap, const char* key)
  403. {
  404. return (Scene*)FindVariant(vmap, key).GetPtr();
  405. }
  406. UIElement* VariantMapGetUIElement(const VariantMap* vmap, const char* key)
  407. {
  408. return (UIElement*)FindVariant(vmap, key).GetPtr();
  409. }
  410. $}