The Variant class. This is a variant type inspired by types from several different scripting languages. More...
#include <Variant/Variant.h>
Public Types | |
| typedef VariantRefImpl< Variant > | VariantRef |
| typedef std::vector< Variant > | List |
|
typedef std::map< std::string, Variant > | Map |
| typedef Map::iterator | MapIterator |
| typedef Map::const_iterator | ConstMapIterator |
| typedef List::iterator | ListIterator |
| typedef List::const_iterator | ConstListIterator |
Public Types inherited from libvariant::VariantDefines | |
| enum | Type_t { NullType = 0, BoolType, IntegerType, UnsignedType, FloatType, StringType, ListType, MapType, BlobType } |
Public Member Functions | |
| Variant (VariantDefines::Type_t type) | |
| Variant (bool v) | |
| Variant (int v) | |
| Variant (unsigned v) | |
| Variant (long v) | |
| Variant (unsigned long v) | |
| Variant (long long v) | |
| Variant (unsigned long long v) | |
| Variant (double v) | |
| Variant (long double v) | |
| Variant (BlobPtr b) | |
| Variant (const std::string &v) | |
| Variant (const char *v) | |
| Variant (const Variant &v) | |
| Variant (const VariantRefImpl< Variant > &v) | |
| template<typename T > | |
| Variant (const std::vector< T > &v) | |
| Construct a list from a std::vector. | |
| template<typename T > | |
| Variant (const std::map< std::string, T > &v) | |
| Construct a map from a std::map. | |
| Variant & | operator= (const Variant &o) |
| Variant & | operator= (VariantDefines::Type_t v) |
| Variant & | operator= (bool v) |
| Variant & | operator= (int v) |
| Variant & | operator= (unsigned v) |
| Variant & | operator= (long v) |
| Variant & | operator= (unsigned long v) |
| Variant & | operator= (long long v) |
| Variant & | operator= (unsigned long long v) |
| Variant & | operator= (double v) |
| Variant & | operator= (long double v) |
| Variant & | operator= (BlobPtr v) |
| Variant & | operator= (const std::string &v) |
| Variant & | operator= (const char *v) |
| template<typename T > | |
| Variant & | operator= (const std::vector< T > &v) |
| template<typename T > | |
| Variant & | operator= (const std::map< std::string, T > &v) |
| Type_t | GetType () const |
| Return the type of this Variant. | |
| bool | IsNull () const |
| bool | IsBool () const |
| bool | IsTrue () const |
| bool | IsFalse () const |
| bool | IsNumber () const |
| bool | IsInt () const |
| bool | IsUnsigned () const |
| bool | IsFloat () const |
| bool | IsString () const |
| bool | IsList () const |
| bool | IsMap () const |
| bool | IsBlob () const |
| bool | AsBool () const |
| Return the value as a bool. | |
| long double | AsLongDouble () const |
| Return the value as a long double. | |
| double | AsDouble () const |
| uintmax_t | AsUnsigned () const |
| Return the value as an unsigned. | |
| intmax_t | AsInt () const |
| Return the value as an integer. | |
| template<typename T > | |
| T | AsNumber () const |
| std::string | AsString () const |
| Return the value as a string. | |
| BlobPtr | AsBlob () |
| ConstBlobPtr | AsBlob () const |
| template<typename T > | |
| T | As () const |
| unsigned | Size () const |
| If the type is string, list, map or blob returns its size, otherwise throws an exception. | |
| bool | Empty () const |
| If the type is string, list or map return if size == 0 otherwise throw an exception. | |
| void | Clear () |
| If the type is string, list or map, empty the container otherwise set type to NullType. | |
| List & | AsList () |
| const List & | AsList () const |
| ListIterator | ListBegin () |
| ConstListIterator | ListBegin () const |
| < Equivalent to AsList().begin() | |
| ListIterator | ListEnd () |
| ConstListIterator | ListEnd () const |
| < Equivalent to AsList().end() | |
| VariantRef | At (unsigned i) |
| VariantRef | At (unsigned i, Variant def) |
| const Variant & | At (unsigned i) const |
| Variant | Get (unsigned i) const |
| Variant | Get (unsigned i, Variant def) const |
| Variant & | Set (unsigned i, Variant v) |
| Variant & | Append (Variant value) |
| Only valid when of type ListType Returns *this, so you can do Variant().Append(v1).Append(v2)... | |
| bool | Contains (unsigned i) const |
| If list return true if list is at least i in size, if map then convert i to a string, otherwise throw. | |
| unsigned | Index (Variant v) const |
| Return the index hat v is at. If v is not contained in the list it return this->Size() | |
| void | Erase (unsigned i) |
| If list set index i to null, if index at end, reduce size by 1 throw if not a list. | |
| Map & | AsMap () |
| const Map & | AsMap () const |
| MapIterator | MapBegin () |
| ConstMapIterator | MapBegin () const |
| < Equivalent to AsMap().begin() | |
| MapIterator | MapEnd () |
| ConstMapIterator | MapEnd () const |
| < Equivalent to AsMap().end() | |
| VariantRef | At (const std::string &s) |
| VariantRef | At (const std::string &s, Variant def) |
| const Variant & | At (const std::string &s) const |
| Variant | Get (const std::string &s) const |
| If s in *this: return this->At(s); else throw;. | |
| Variant | Get (const std::string &s, Variant def) const |
| If s in *this: return this->At(s); else return def Only works if type is map. | |
| Variant & | Set (const std::string &s, Variant v) |
| Set the key s to v. | |
| bool | Contains (const std::string &s) const |
| If map return true if s is a key in the map, if list then convert s to an unsigned and the above Contains, otherwise throw. | |
| void | Erase (const std::string &key) |
| Erase a key in a map, throws if type is not MapType. | |
| VariantRef | operator[] (int i) |
| const Variant & | operator[] (int i) const |
| VariantRef | operator[] (unsigned i) |
| const Variant & | operator[] (unsigned i) const |
| VariantRef | operator[] (const std::string &s) |
| const Variant & | operator[] (const std::string &s) const |
| VariantRef | AtPath (const Path &path) |
| VariantRef | AtPath (const std::string &path) |
| VariantRef | AtPath (const Path &path, Variant def) |
| VariantRef | AtPath (const std::string &path, Variant def) |
| const Variant & | AtPath (const Path &path) const |
| const Variant & | AtPath (const std::string &path) const |
| Variant | GetPath (const Path &path) const |
| Like Variant::Get but it takes a path instead. | |
| Variant | GetPath (const std::string &path) const |
| Variant | GetPath (const Path &path, Variant def) const |
| Like Variant::Get but it takes a path instead. The path is very like xpath, /key[index]/key/key. See Variant/Path.h. | |
| Variant | GetPath (const std::string &path, Variant def) const |
| Variant & | SetPath (const Path &path, Variant val) |
| Set the value at the path, creating elements if they do not exist. throws if an intermediate path element exists but is not a map or list. returns *this. | |
| Variant & | SetPath (const std::string &path, Variant val) |
| Variant & | ErasePath (const Path &path, bool remove_empty=false) |
| Erase a value at a path, if remove_empty is false only erase the very end otherwise erase empty lists and maps in the path. returns *this. | |
| Variant & | ErasePath (const std::string &path, bool remove_empty=false) |
| bool | HasPath (const Path &path) const |
| Tester functions for paths, returns true if path is valid and exists. | |
| bool | HasPath (const std::string &path) const |
| template<typename T > | |
| void | GetInto (T &lvalue, const std::string &s) const |
| Just like Variant::Get except a reference to the value to set is passed in and Variant does the casting. | |
| template<typename T > | |
| void | GetPathInto (T &lvalue, const Path &path) const |
| An into version of GetPath. | |
| template<typename T > | |
| void | GetPathInto (T &lvalue, const std::string &path) const |
| template<typename T > | |
| void | GetInto (T &lvalue, const std::string &s, const T &def) const |
| Just like Variant::Get except a reference to the value to set is passed in and Variant does the casting. | |
| template<typename T > | |
| void | GetPathInto (T &lvalue, const Path &path, const T &def) const |
| An into version of GetPath. | |
| template<typename T > | |
| void | GetPathInto (T &lvalue, const std::string &path, const T &def) const |
| void | Merge (Variant other) |
| This type and other type must be MapType. for (k, v) in other: this[k] = v. | |
| int | Compare (const Variant &other) const |
| Comparison operator Returns -1 if we are less than other 0 if we are equal 1 if we are greater than This function always returns 1 if Comparable returns false This compare is not completely consistent i.e. it can be such that two Variants are both < and > (ex map and list types) | |
| bool | Comparable (const Variant &other) const |
| Return true if this and other are comparable. Two types are considered comparable if they have the same type or if they are both number types. | |
| void | Incr () |
| void | Decr () |
| void | Add (const Variant &o) |
| void | Sub (const Variant &o) |
| void | Mul (const Variant &o) |
| void | Div (const Variant &o) |
| void | Rem (const Variant &o) |
| Variant | Neg () const |
| Variant & | operator++ () |
| Variant & | operator-- () |
| Variant | operator++ (int) |
| Variant | operator-- (int) |
| Variant | operator- () const |
| Variant | operator+ () const |
| Variant & | operator+= (const Variant &o) |
| Variant & | operator-= (const Variant &o) |
| Variant & | operator*= (const Variant &o) |
| Variant & | operator/= (const Variant &o) |
| Variant & | operator%= (const Variant &o) |
| VariantRef | Ref () |
| Variant | Copy () const |
| Variant & | Resolve () |
| const Variant & | Resolve () const |
| Variant & | Resolve (const Variant &def) |
| Return *this or Variant referenced to if exists, otherwise create it with def. | |
| bool | Exists () const |
| Return weather or not Resolve() const would throw. | |
| void | Assign (const Variant &other) |
| Make this equal other. | |
| void | Assign (VariantDefines::Type_t type) |
| void | Assign (bool v) |
| void | Assign (int v) |
| void | Assign (unsigned v) |
| void | Assign (long v) |
| void | Assign (unsigned long v) |
| void | Assign (long long v) |
| void | Assign (unsigned long long v) |
| void | Assign (double v) |
| void | Assign (long double v) |
| void | Assign (BlobPtr b) |
| void | Assign (const std::string &v) |
| void | Assign (const char *v) |
| template<typename T > | |
| void | Assign (const std::vector< T > &v) |
| template<typename T > | |
| void | Assign (const std::map< std::string, T > &v) |
Protected Member Functions | |
| Variant (const RefTag &, const Variant &o) | |
| Variant (const RefTag &, Variant &o, Path::const_iterator b, Path::const_iterator e) | |
| void | ReassignRef (const Variant &o) |
Friends | |
| class | Internal::VTable |
Additional Inherited Members | |
Protected Types inherited from libvariant::VariantDefines | |
| enum | Type_t { NullType = 0, BoolType, IntegerType, UnsignedType, FloatType, StringType, ListType, MapType, BlobType } |
Protected Attributes inherited from libvariant::Internal::Data | |
| const VTable * | vtable |
| union { | |
| bool b | |
| long double f | |
| intmax_t i | |
| uintmax_t u | |
| std::string * s | |
| MapStorage * m | |
| ListStorage * l | |
| BlobPtr * blob | |
| Data * ref | |
| ProxyStorage * proxy | |
| }; | |
| shared_ptr< RefData > | ref_data |
| shared_ptr< Storage > | storage |
The Variant class. This is a variant type inspired by types from several different scripting languages.
This class gives the power and convenience of a scripting language variable to C++ with all the same /downsides/ that such a variable has as well. The main idea is that things should be simple, lists are behind the scenes an std::vector and maps are an std::map.
A Variant may be null, a number, a string, a boolean, a reference to a list, or a reference to a map. This means that const Variant of a map is not a const map but a const reference. Lists and maps are reference counted. If one wants to copy use the Copy member function.
If you need a reference to a Variant to assign to it or change its type, use VariantRef. A const Variant & should be used for an immutable reference. E.g., void foo(VariantRef inout_param, const Variant &in_param); That is to say, if you think you want Variant &, you don't, you want VariantRef. The library is written so that: Variant v; void bar(Variant &); bar(v["foo"]); is a compile error. But Variant v; void moo(const Variant &); moo(v["moo"]); is correct.
Note that this implementation will let you create a back reference (i.e., a map with itself as a member) but unless the user explicitly breaks it, it will result in a memory leak. Checking for this case was deemed too expensive.
A Variant will only change its own type if the type is null. That is to say that a null Variant that gets treated as a map will become a map. But, a Variant of any other type will never automatically convert.
|
protected |
Reassing this reference, only available through the VariantRef interface. The regular Assign makes what this is refering to equal the new value, this function makes this reference refer to the new value.
1.8.7