Variant class is inspired by Javascript's Var semantics. It maintains data of different kinds such as ints, doubles, strings. It also stores objects with properties, arrays of variants, and function objects.
#include <var.h>
Collaboration diagram for jvar::Variant:Classes | |
| struct | VarData |
Public Types | |
| enum | Type { V_EMPTY, V_NULL, V_INT, V_BOOL, V_DOUBLE, V_STRING, V_ARRAY, V_OBJECT, V_FUNCTION, V_POINTER } |
| typedef int(* | Compare )(const Variant *, const Variant *) |
Public Member Functions | |
| Variant () | |
| Variant (longint i) | |
| Variant (int i) | |
| Variant (double d) | |
| Variant (std::string s) | |
| Variant (const char *s) | |
| Variant (Variant const &src) | |
| ~Variant () | |
| Variant & | operator= (const Variant &src) |
| Variant & | operator= (const Variant *src) |
| Variant & | operator= (const char *src) |
| Variant & | operator= (const std::string &src) |
| Variant & | operator= (bool src) |
| Variant & | operator= (longint src) |
| Variant & | operator= (int src) |
| Variant & | operator= (double src) |
| Variant & | operator= (float src) |
| Variant & | operator+= (const Variant &rhs) |
| Variant & | operator+= (longint rhs) |
| Variant & | operator+= (int rhs) |
| Variant & | operator+= (const char *rhs) |
| Variant & | operator+= (const std::string &rhs) |
| Variant & | operator+= (double rhs) |
| Variant & | operator++ () |
| Variant | operator++ (int) |
| longint | toInt () const |
| double | toDouble () const |
| bool | toBool () const |
| operator longint () const | |
| operator double () const | |
| operator bool () const | |
| operator const char * () | |
| const char * | c_str () const |
| std::string & | s () |
| std::string | toString () const |
| std::string | toFixed (int digs=0) const |
| std::string | toJsonString () const |
| bool | parseJson (const char *jsontxt) |
| void | format (const char *fmt,...) |
| Type | type () const |
| const char * | typeName () const |
| bool | isNull () const |
| bool | isEmpty () const |
| bool | isObject () const |
| bool | isArray () const |
| bool | isPointer () const |
| bool | isNaN () |
| int | length () const |
| Variant & | operator[] (int i) |
| const Variant & | operator[] (int i) const |
| Variant & | operator[] (const char *key) |
| const Variant & | operator[] (const char *key) const |
| Variant & | path (const char *pathkey) |
| Variant & | path (const std::string &pathkey) |
| void | createArray (const char *initvalue=NULL) |
| Variant * | append (const Variant &elem=VEMPTY) |
| void | push (const Variant &elem) |
| Variant | pop () |
| Variant | shift () |
| void | sort (Compare comp) |
| void | createObject (const char *initvalue=NULL) |
| Variant & | addProperty (const char *key, const Variant &value=vEmpty) |
| bool | hasProperty (const char *key) |
| const char * | getKey (int n) |
| void | createFunction (Variant(*func)(Variant &env, Variant &arg)) |
| bool | addEnv (const char *varname, const Variant &value=vEmpty) |
| Variant | operator() () |
| Variant | operator() (const Variant &value1) |
| Variant | operator() (const Variant &value1, const Variant &value2) |
| Variant | operator() (const Variant &value1, const Variant &value2, const Variant &value3) |
| Variant | operator() (const Variant &value1, const Variant &value2, const Variant &value3, const Variant &value4) |
| bool | forEach (Iter< Variant > &iter) |
| void | clear () |
| void | setModified () |
| void | clearModified () |
| bool | isModified () |
Static Public Attributes | |
| static Variant | vEmpty |
| static Variant | vNull |
| jvar::Variant::Variant | ( | ) | [inline] |
Default constructor-- an Empty object
| jvar::Variant::Variant | ( | longint | i | ) | [inline] |
Constructs an Integer object with a longint
| jvar::Variant::Variant | ( | int | i | ) | [inline] |
Constructs an Integer object with an int
| jvar::Variant::Variant | ( | double | d | ) | [inline] |
Constructs an double object
| jvar::Variant::Variant | ( | std::string | s | ) | [inline] |
Constructs an string object using a std::string
| jvar::Variant::Variant | ( | const char * | s | ) | [inline] |
Constructs an string object
| jvar::Variant::Variant | ( | Variant const & | src | ) | [inline] |
Copy constructor
| jvar::Variant::~Variant | ( | ) | [inline] |
Destructor deletes all data in the object
| bool jvar::Variant::addEnv | ( | const char * | varname, |
| const Variant & | value = vEmpty |
||
| ) |
Adds a variable to the enviroment of the function object
| varname | Key name for the variable |
| value | Optional value for the variable |
| Variant & jvar::Variant::addProperty | ( | const char * | key, |
| const Variant & | value = vEmpty |
||
| ) |
Adds a new property to the object
| key | Key name for the property |
| value | Optional value |
References setModified(), and type().
| Variant * jvar::Variant::append | ( | const Variant & | elem = VEMPTY | ) |
Adds a variant at the end of an array
| elem | Variant to add |
References setModified(), and type().
Referenced by push().
| const char* jvar::Variant::c_str | ( | ) | const [inline] |
Returns a pointer to the string. Returns NULL if not string type.
Referenced by operator const char *().
| void jvar::Variant::clear | ( | ) | [inline] |
Clears the object by deleting all data
| void jvar::Variant::clearModified | ( | ) | [inline] |
Clears the modified flag
| void jvar::Variant::createArray | ( | const char * | initvalue = NULL | ) |
Creates an array
| initvalue | Optional JSON to initialize the array |
References jvar::JsonParser::FLAG_ARRAYONLY, and jvar::JsonParser::FLAG_FLEXQUOTES.
| void jvar::Variant::createFunction | ( | Variant(*)(Variant &env, Variant &arg) | func | ) |
| void jvar::Variant::createObject | ( | const char * | initvalue = NULL | ) |
Creates an objects with optional initial value
| initvalue | Json style initial value |
References jvar::JsonParser::FLAG_FLEXQUOTES, and jvar::JsonParser::FLAG_OBJECTONLY.
| bool jvar::Variant::forEach | ( | Iter< Variant > & | iter | ) | [inline] |
Returns an iterator to go over all elements in an array or object
| iter | Iterator |
| void jvar::Variant::format | ( | const char * | fmt, |
| ... | |||
| ) |
Formats a new string using printf style formatting and assigns it to the variant
Referenced by toFixed().
| const char * jvar::Variant::getKey | ( | int | n | ) |
Returns the key name at a given index
| n | Index position |
| bool jvar::Variant::hasProperty | ( | const char * | key | ) |
Determines if the object has a property
| key | Key name for the property |
| bool jvar::Variant::isModified | ( | ) | [inline] |
Checks modified flag
| int jvar::Variant::length | ( | ) | const |
Returns the length. If an array, returns array length. If an object, returns the number of properties. Otherwise, returns 1.
| jvar::Variant::operator bool | ( | ) | const [inline] |
Typecast the value as an bool
| jvar::Variant::operator const char * | ( | ) | [inline] |
Returns a pointer to the string. Returns NULL if not string type.
References c_str().
| jvar::Variant::operator double | ( | ) | const [inline] |
Typecast the value as an double
| jvar::Variant::operator longint | ( | ) | const [inline] |
Typecast the value as an int
| Variant& jvar::Variant::operator++ | ( | ) | [inline] |
Prefix (++v) Adding Variants
| Variant jvar::Variant::operator++ | ( | int | ) | [inline] |
Postfix (v++) Adding Variants
| Variant& jvar::Variant::operator+= | ( | longint | rhs | ) | [inline] |
| Variant& jvar::Variant::operator+= | ( | int | rhs | ) | [inline] |
| Variant& jvar::Variant::operator+= | ( | const char * | rhs | ) | [inline] |
| Variant& jvar::Variant::operator+= | ( | const std::string & | rhs | ) | [inline] |
| Variant& jvar::Variant::operator+= | ( | double | rhs | ) | [inline] |
| Variant& jvar::Variant::operator= | ( | const char * | src | ) | [inline] |
Assignms a string (changes type if needed)
| Variant& jvar::Variant::operator= | ( | const std::string & | src | ) | [inline] |
Assignms a string (changes type if needed)
| Variant& jvar::Variant::operator= | ( | bool | src | ) | [inline] |
Assignms a bool (changes type if needed)
| Variant& jvar::Variant::operator= | ( | longint | src | ) | [inline] |
Assignms a longint (changes type if needed)
| Variant& jvar::Variant::operator= | ( | int | src | ) | [inline] |
Assignms a int (changes type if needed)
| Variant& jvar::Variant::operator= | ( | double | src | ) | [inline] |
Assignms a double (changes type if needed)
| Variant& jvar::Variant::operator= | ( | float | src | ) | [inline] |
Assignms a float (changes type if needed)
| Variant & jvar::Variant::operator[] | ( | int | i | ) |
Returns a referene to the variant in an array
| const Variant & jvar::Variant::operator[] | ( | int | i | ) | const |
Returns a const referene to the variant in an array
| Variant & jvar::Variant::operator[] | ( | const char * | key | ) |
Returns a referene to the variant inside an object or a fuction using key
| const Variant & jvar::Variant::operator[] | ( | const char * | key | ) | const |
Returns a const referene to the variant inside an object or a fuction using key
| bool jvar::Variant::parseJson | ( | const char * | jsontxt | ) |
Parses json text and loads the data structure into the variant
| jsontxt | Json text string |
References jvar::Parser::failed(), and setModified().
| Variant & jvar::Variant::path | ( | const char * | pathkey | ) |
Returns a refernece to a variant by parsing properties and indexes using a path syntax (ex: "obj.propA.2.name")
| pathkey | [description] |
References jvar::Parser::advance(), jvar::Parser::eof(), jvar::Parser::token(), and jvar::Parser::tokenEquals().
Referenced by path().
| Variant& jvar::Variant::path | ( | const std::string & | pathkey | ) | [inline] |
Returns a refernece to a variant by parsing properties and indexes using a path syntax (ex: "obj.propA.2.name")
| pathkey | [description] |
References path().
Removes the last item from the array and returns it
| void jvar::Variant::push | ( | const Variant & | elem | ) | [inline] |
| std::string & jvar::Variant::s | ( | ) |
Returns a *reference* to the string object. If not string type, the type is automatically *changed* to string by this function.
NOTE 1: Modifies the type of the object to string NOTE 2: Should not be called with type==V_NULL, or VNULL or VEMPTY.
References setModified().
Referenced by toFixed(), toJsonString(), and toString().
| void jvar::Variant::setModified | ( | ) | [inline] |
Sets the modified flag
Referenced by addProperty(), append(), parseJson(), and s().
Removes the first item from the array and returns it
| void jvar::Variant::sort | ( | Compare | comp | ) |
Sorts an array
| comp | Optional compare function |
| bool jvar::Variant::toBool | ( | ) | const [inline] |
Return the value as an bool
| double jvar::Variant::toDouble | ( | ) | const [inline] |
Return the value as an double
| std::string jvar::Variant::toFixed | ( | int | digs = 0 | ) | const |
Returns as a formatted string for a double, otherwise returns a copy of string performing any conversions
| digs | Number of digits to show in double |
References format(), s(), and toString().
| longint jvar::Variant::toInt | ( | ) | const [inline] |
Return the value as an int
| std::string jvar::Variant::toJsonString | ( | ) | const |
Returns a json text representing the object.
References s().
| std::string jvar::Variant::toString | ( | ) | const |
Returns a *copy* of the string performing any conversions. Format is similar to json but does not do any escaping (for proper Json strings, use toJsonString()
References s().
Referenced by toFixed().
| Type jvar::Variant::type | ( | ) | const [inline] |
Returns the variant type V_xxx for the object
Referenced by addProperty(), and append().
| const char * jvar::Variant::typeName | ( | ) | const |
Returns the variant type of the object as a string
References jvar::KeywordArray::toKeyword().