| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- $#include "Variant.h"
- /// Variant's supported types.
- enum VariantType
- {
- VAR_NONE = 0,
- VAR_INT,
- VAR_BOOL,
- VAR_FLOAT,
- VAR_VECTOR2,
- VAR_VECTOR3,
- VAR_VECTOR4,
- VAR_QUATERNION,
- VAR_COLOR,
- VAR_STRING,
- VAR_BUFFER,
- VAR_PTR,
- VAR_RESOURCEREF,
- VAR_RESOURCEREFLIST,
- VAR_VARIANTVECTOR,
- VAR_VARIANTMAP,
- VAR_INTRECT,
- VAR_INTVECTOR2,
- MAX_VAR_TYPES
- };
- /// Typed resource reference.
- struct ResourceRef
- {
- /// Construct.
- ResourceRef();
- /// Construct with type only and empty id.
- ResourceRef(ShortStringHash type);
- /// Construct with type and id.
- ResourceRef(ShortStringHash type, StringHash id);
- // Construct from another ResourceRef.
- ResourceRef(const ResourceRef& rhs);
- /// Object type.
- ShortStringHash type_ @ type;
- StringHash id_ @ id;
- /// Test for equality with another reference.
- bool operator == (const ResourceRef& rhs) const;
- };
- /// %List of typed resource references.
- struct ResourceRefList
- {
- /// Construct.
- ResourceRefList();
- /// Construct with type only.
- ResourceRefList(ShortStringHash type);
- /// Object type.
- ShortStringHash type_ @ type;
- /// Test for equality with another reference list.
- bool operator == (const ResourceRefList& rhs) const;
- };
- /// Variable that supports a fixed set of types.
- class Variant
- {
- public:
- /// Construct empty.
- Variant();
- /// Construct from integer.
- Variant(int value);
- /// Construct from unsigned integer.
- Variant(unsigned value);
- /// Construct from a string hash (convert to integer).
- Variant(const StringHash& value);
- /// Construct from a short string hash (convert to integer.)
- Variant(const ShortStringHash& value);
- /// Construct from a bool.
- Variant(bool value);
- /// Construct from a float.
- Variant(float value);
- /// Construct from a Vector2.
- Variant(const Vector2& value);
- /// Construct from a Vector3.
- Variant(const Vector3& value);
- /// Construct from a Vector4.
- Variant(const Vector4& value);
- /// Construct from a quaternion.
- Variant(const Quaternion& value);
- /// Construct from a color.
- Variant(const Color& value);
- /// Construct from a string.
- Variant(const String& value);
- /// Construct from a C string.
- Variant(const char* value);
- /// Construct from a pointer.
- Variant(void* value);
- /// Construct from a resource reference.
- Variant(const ResourceRef& value);
- /// Construct from a resource reference list.
- Variant(const ResourceRefList& value);
- /// Construct from an integer rect.
- Variant(const IntRect& value);
- /// Construct from an IntVector2.
- Variant(const IntVector2& value);
- /// Construct from type and value.
- Variant(const String& type, const String& value);
- /// Construct from type and value.
- Variant(VariantType type, const String& value) ;
- /// Construct from type and value.
- Variant(VariantType type, const char* value);
- /// Copy-construct from another variant.
- Variant(const Variant& value);
- /// Destruct.
- ~Variant();
- /// Reset to empty.
- void Clear();
- /// Test for equality with another variant.
- bool operator == (const Variant& rhs) const;
- /// Test for equality with an integer. To return true, both the type and value must match.
- bool operator == (int rhs) const;
- /// Test for equality with an unsigned integer. To return true, both the type and value must match.
- bool operator == (unsigned rhs) const;
- /// Test for equality with a bool. To return true, both the type and value must match.
- bool operator == (bool rhs) const;
- /// Test for equality with a float. To return true, both the type and value must match.
- bool operator == (float rhs) const;
- /// Test for equality with a Vector2. To return true, both the type and value must match.
- bool operator == (const Vector2& rhs);
- /// Test for equality with a Vector3. To return true, both the type and value must match.
- bool operator == (const Vector3& rhs) const;
- /// Test for equality with a Vector4. To return true, both the type and value must match.
- bool operator == (const Vector4& rhs) const;
- /// Test for equality with a quaternion. To return true, both the type and value must match.
- bool operator == (const Quaternion& rhs) const;
- /// Test for equality with a color. To return true, both the type and value must match.
- bool operator == (const Color& rhs) const;
- /// Test for equality with a string. To return true, both the type and value must match.
- bool operator == (const String& rhs) const;
- /// Test for equality with a pointer. To return true, both the type and value must match.
- bool operator == (void* rhs) const;
- /// Test for equality with a resource reference. To return true, both the type and value must match.
- bool operator == (const ResourceRef& rhs) const;
- /// Test for equality with a resource reference list. To return true, both the type and value must match.
- bool operator == (const ResourceRefList& rhs) const;
- /// Test for equality with an integer rect. To return true, both the type and value must match.
- bool operator == (const IntRect& rhs) const;
- /// Test for equality with an IntVector2. To return true, both the type and value must match.
- bool operator == (const IntVector2& rhs) const;
- /// Test for equality with a StringHash. To return true, both the type and value must match.
- bool operator == (const StringHash& rhs) const;
- /// Test for equality with a ShortStringHash. To return true, both the type and value must match.
- bool operator == (const ShortStringHash& rhs) const;
- /// Return int or zero on type mismatch.
- int GetInt() const;
- /// Return unsigned int or zero on type mismatch.
- int GetUInt() const;
- /// Return StringHash or zero on type mismatch.
- StringHash GetStringHash();
- /// Return ShortStringHash or zero on type mismatch.
- ShortStringHash GetShortStringHash();
- /// Return bool or false on type mismatch.
- bool GetBool() const;
- /// Return float or zero on type mismatch.
- float GetFloat() const;
- /// Return Vector2 or zero on type mismatch.
- const Vector2& GetVector2() const;
- /// Return Vector3 or zero on type mismatch.
- const Vector3& GetVector3() const;
- /// Return Vector4 or zero on type mismatch.
- const Vector4& GetVector4() const;
- /// Return quaternion or identity on type mismatch.
- const Quaternion& GetQuaternion() const;
- /// Return color or default on type mismatch.
- const Color& GetColor() const;
- /// Return string or empty on type mismatch.
- const String& GetString() const;
- /// Return pointer or null on type mismatch.
- void* GetPtr() const;
- /// Return a resource reference or empty on type mismatch.
- const ResourceRef& GetResourceRef() const;
- /// Return a resource reference list or empty on type mismatch.
- const ResourceRefList& GetResourceRefList() const;
- /// Return an integer rect or empty on type mismatch.
- const IntRect& GetIntRect() const;
- /// Return an IntVector2 or empty on type mismatch.
- const IntVector2& GetIntVector2() const;
- /// Return value's type.
- VariantType GetType() const;
- /// Return value's type name.
- String GetTypeName() const;
- /// Convert value to string. Pointers are returned as null, and VariantBuffer or VariantMap are not supported and return empty.
- String ToString() const;
- /// Return true when the variant value is considered zero according to its actual type.
- bool IsZero() const;
- /// Return true when the variant is empty (i.e. not initialized yet).
- bool IsEmpty() const;
- };
- class VariantMap
- {
- public:
- VariantMap();
- tolua_outside void VariantMapSetInt @ SetInt(const char* key, int value);
- tolua_outside void VariantMapSetBool @ SetBool(const char* key, bool value);
- tolua_outside void VariantMapSetFloat @ SetFloat(const char* key, float value);
- tolua_outside void VariantMapSetVector2 @ SetVector2(const char* key, const Vector2& value);
- tolua_outside void VariantMapSetVector3 @ SetVector3(const char* key, const Vector3& value);
- tolua_outside void VariantMapSetVector4 @ SetVector4(const char* key, const Vector4& value);
- tolua_outside void VariantMapSetQuaternion @ SetQuaternion(const char* key, const Quaternion& value);
- tolua_outside void VariantMapSetColor @ SetColor(const char* key, const Color& value);
- tolua_outside void VariantMapSetString @ SetString(const char* key, const String& value);
- tolua_outside void VariantMapSetPtr @ SetPtr(const char* key, void* value);
- tolua_outside void VariantMapSetResourceRef @ SetResourceRef(const char* key, const ResourceRef& value);
- tolua_outside void VariantMapSetResourceRefList @ SetResourceRefList(const char* key, const ResourceRefList& value);
- tolua_outside void VariantMapSetIntRect @ SetIntRect(const char* key, const IntRect& value);
- tolua_outside void VariantMapSetIntVector2 @ SetIntVector2(const char* key, const IntVector2& value);
-
- tolua_outside void VariantMapSetCamera @ SetCamera(const char* key, Camera* camera);
- tolua_outside void VariantMapSetConnection @ SetConnection(const char* key, Connection* connection);
- tolua_outside void VariantMapSetNode @ SetNode(const char* key, Node* node);
- tolua_outside void VariantMapSetPhysicsWorld @ SetPhysicsWorld(const char* key, PhysicsWorld* physicsworld);
- tolua_outside void VariantMapSetRigidBody @ SetRigidBody(const char* key, RigidBody* rigidbody);
- tolua_outside void VariantMapSetScene @ SetScene(const char* key, Scene* scene);
- tolua_outside void VariantMapSetUIElement @ SetUIElement(const char* key, UIElement* element);
-
- tolua_outside int VariantMapGetInt @ GetInt(const char* key);
- tolua_outside bool VariantMapGetBool @ GetBool(const char* key);
- tolua_outside float VariantMapGetFloat @ GetFloat(const char* key);
- tolua_outside const Vector2& VariantMapGetVector2 @ GetVector2(const char* key);
- tolua_outside const Vector3& VariantMapGetVector3 @ GetVector3(const char* key);
- tolua_outside const Vector4& VariantMapGetVector4 @ GetVector4(const char* key);
- tolua_outside const Quaternion& VariantMapGetQuaternion @ GetQuaternion(const char* key);
- tolua_outside const Color& VariantMapGetColor @ GetColor(const char* key);
- tolua_outside const String& VariantMapGetString @ GetString(const char* key);
- tolua_outside const void* VariantMapGetPtr @ GetPtr(const char* key);
- tolua_outside const ResourceRef& VariantMapGetResourceRef @ GetResourceRef(const char* key);
- tolua_outside const ResourceRefList& VariantMapGetResourceRefList @ GetResourceRefList(const char* key);
- tolua_outside const IntRect& VariantMapGetIntRect @ GetIntRect(const char* key);
- tolua_outside const IntVector2& VariantMapGetIntVector2 @ GetIntVector2(const char* key);
-
- tolua_outside Camera* VariantMapGetCamera @ GetCamera(const char* key);
- tolua_outside Connection* VariantMapGetConnection @ GetConnection(const char* key);
- tolua_outside Node* VariantMapGetNode @ GetNode(const char* key);
- tolua_outside PhysicsWorld* VariantMapGetPhysicsWorld @ GetPhysicsWorld(const char* key);
- tolua_outside RigidBody* VariantMapGetRigidBody @ GetRigidBody(const char* key);
- tolua_outside Scene* VariantMapGetScene @ GetScene(const char* key);
- tolua_outside UIElement* VariantMapGetUIElement @ GetUIElement(const char* key);
- };
- ${
- void VariantMapSetInt(VariantMap* vmap, const char* key, int value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetBool(VariantMap* vmap, const char* key, bool value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetFloat(VariantMap* vmap, const char* key, float value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetVector2(VariantMap* vmap, const char* key, const Vector2& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetVector3(VariantMap* vmap, const char* key, const Vector3& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetVector4(VariantMap* vmap, const char* key, const Vector4& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetQuaternion(VariantMap* vmap, const char* key, const Quaternion& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetColor(VariantMap* vmap, const char* key, const Color& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetString(VariantMap* vmap, const char* key, const String& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetPtr(VariantMap* vmap, const char* key, void* value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetResourceRef(VariantMap* vmap, const char* key, const ResourceRef& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetResourceRefList(VariantMap* vmap, const char* key, const ResourceRefList& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetIntRect(VariantMap* vmap, const char* key, const IntRect& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetIntVector2(VariantMap* vmap, const char* key, const IntVector2& value)
- {
- (*vmap)[ShortStringHash(key)] = value;
- }
- void VariantMapSetCamera(VariantMap* vmap, const char* key, Camera* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- void VariantMapSetConnection(VariantMap* vmap, const char* key, Connection* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- void VariantMapSetNode(VariantMap* vmap, const char* key, Node* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- void VariantMapSetPhysicsWorld(VariantMap* vmap, const char* key, PhysicsWorld* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- void VariantMapSetRigidBody(VariantMap* vmap, const char* key, RigidBody* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- void VariantMapSetScene(VariantMap* vmap, const char* key, Scene* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- void VariantMapSetUIElement(VariantMap* vmap, const char* key, UIElement* pointer)
- {
- (*vmap)[ShortStringHash(key)] = (void*)pointer;
- }
- const Variant& FindVariant(const VariantMap* vmap, const char* key)
- {
- VariantMap::ConstIterator i = vmap->Find(ShortStringHash(key));
- return i != vmap->End() ? i->second_ : Variant::EMPTY;
- }
- int VariantMapGetInt(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetInt();
- }
- bool VariantMapGetBool(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetBool();
- }
- float VariantMapGetFloat(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetFloat();
- }
- const Vector2& VariantMapGetVector2(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetVector2();
- }
- const Vector3& VariantMapGetVector3(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetVector3();
- }
- const Vector4& VariantMapGetVector4(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetVector4();
- }
- const Quaternion& VariantMapGetQuaternion(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetQuaternion();
- }
- const Color& VariantMapGetColor(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetColor();
- }
- const String& VariantMapGetString(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetString();
- }
- const void* VariantMapGetPtr(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetPtr();
- }
- const ResourceRef& VariantMapGetResourceRef(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetResourceRef();
- }
- const ResourceRefList& VariantMapGetResourceRefList(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetResourceRefList();
- }
- const IntRect& VariantMapGetIntRect(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetIntRect();
- }
- const IntVector2& VariantMapGetIntVector2(const VariantMap* vmap, const char* key)
- {
- return FindVariant(vmap, key).GetIntVector2();
- }
- Camera* VariantMapGetCamera(const VariantMap* vmap, const char* key)
- {
- return (Camera*)FindVariant(vmap, key).GetPtr();
- }
- Connection* VariantMapGetConnection(const VariantMap* vmap, const char* key)
- {
- return (Connection*)FindVariant(vmap, key).GetPtr();
- }
- Node* VariantMapGetNode(const VariantMap* vmap, const char* key)
- {
- return (Node*)FindVariant(vmap, key).GetPtr();
- }
- PhysicsWorld* VariantMapGetPhysicsWorld(const VariantMap* vmap, const char* key)
- {
- return (PhysicsWorld*)FindVariant(vmap, key).GetPtr();
- }
- RigidBody* VariantMapGetRigidBody(const VariantMap* vmap, const char* key)
- {
- return (RigidBody*)FindVariant(vmap, key).GetPtr();
- }
- Scene* VariantMapGetScene(const VariantMap* vmap, const char* key)
- {
- return (Scene*)FindVariant(vmap, key).GetPtr();
- }
- UIElement* VariantMapGetUIElement(const VariantMap* vmap, const char* key)
- {
- return (UIElement*)FindVariant(vmap, key).GetPtr();
- }
- $}
|