General purpose variable that can hold a basic value (string, number, bool, etc) or children with nested values (object, list).
- Useful for creating a general hierarchy of values without using explicit types
- Use
operator[]()
to set children, or assignment for basic values (number, string, bool, etc)
- Use
child()
methods to get object field or list item, or use get_*()
methods to get value as given type
- This is optimized to minimize memory allocations:
- Creating an empty string, object, or list type doesn't allocate memory
- This uses
union
internally to hold only one of the supported types:
- MapList is used for objects
- List is used for lists
- String is used for strings
- Primitives are used for other types (
int64
, uint64
, double
, bool
)
- Object, list, and string types use Sharing
- C++11:
- Use an initialization list to create from an array of values, which may have nested lists or objects
- Use VarObject to create an object from an initialization list of key/value pairs – each key/value pair is a nested list of 2 items (where the key must be a string):
{"key1", 123},
{"key2", 456},
};
- When nesting initializer lists inside objects you must specify the type each time (VarObject or VarList), unless it's a list in a list:
{
"key1",
VarList{1, 2, 3, {4, 5}}},
{"key2", 456},
};
- Lists are assumed until you use a VarObject:
Var var3 = {1, {2, 3}, 4};
- If nesting isn't done correctly you'll get compiler errors
- Only use VarObject and VarList with initializer lists – do not use as variable or parameter type, use Var instead
- Example
int main() {
var["name"] = "John Doe";
var["age"] = 21;
var["balance"] = 99.99;
var["active"] = true;
var["list"][0] = 1;
var["list"][1] = 2;
var["list"][2] = 3;
var["object"]["field"] = "value";
return 0;
}
{
active:true,
age:21,
balance:99.99,
list:[1,2,3],
name:'John Doe',
nothing:null,
object:{
field:'value'
}
}
- Example with C++11 Initialier Lists
int main() {
{
"key1",
VarList{1, 2, 3, {4, 5}}},
{"nested-key1", "value1"},
{"nested-key2", "value2"},
}},
};
{"key1", "value1"},
{"key2", "value2"},
}};
Var var3 = {1, {2, 3, {4}},
"five"};
return 0;
}
Output:
{
key1:[1,2,3,[4,5]],
key2:{
nested-key1:'value1',
nested-key2:'value2'
}
}
[1,'two',[3,4],5,false,null,{
key1:'value1',
key2:'value2'
}]
[1,[2,3,[4]],'five']
|
| Var (std::initializer_list< Var > init) |
| Sequence constructor (C++11). More...
|
|
| Var (Var &&src) |
| Move constructor (C++11). More...
|
|
| Var () |
| Default constructor sets as null. More...
|
|
| Var (const Var &src) |
| Copy constructor. More...
|
|
| Var (const ObjectType &val) |
| Constructor to copy from an object map type. More...
|
|
| Var (const ListType &val) |
| Constructor to copy from an object list type. More...
|
|
| Var (const String &val) |
| Constructor to copy from a string value. More...
|
|
| Var (const StringBase &val) |
| Constructor to copy from a string value. More...
|
|
| Var (const char *val) |
| Constructor to copy from an string value. More...
|
|
| Var (double val) |
| Constructor to initialize as floating-point value. More...
|
|
| Var (ushort val) |
| Constructor to initialize as unsigned integer value (ushort). More...
|
|
| Var (uint val) |
| Constructor to initialize as unsigned integer value (uint). More...
|
|
| Var (ulong val) |
| Constructor to initialize as unsigned integer value (ulong). More...
|
|
| Var (ulongl val) |
| Constructor to initialize as unsigned integer value (ulongl). More...
|
|
| Var (short val) |
| Constructor to initialize as integer value (short). More...
|
|
| Var (int val) |
| Constructor to initialize as integer value (int). More...
|
|
| Var (long val) |
| Constructor to initialize as integer value (long). More...
|
|
| Var (longl val) |
| Constructor to initialize as integer value (longl). More...
|
|
| Var (bool val) |
| Constructor to initialize as boolean value (bool). More...
|
|
| Var (ValNull) |
| Constructor to initialize as as null with vNULL as argument. More...
|
|
| ~Var () |
| Destructor. More...
|
|
bool & | boolref () |
| Get bool reference, recreate as bool if needed. More...
|
|
const Var * | child (const StringBase &key) const |
| Find child in object using key (const). More...
|
|
const Var * | child (Size index) const |
| Find child in list using index (const). More...
|
|
Var * | childM (const StringBase &key) |
| Find child in object using key (mutable). More...
|
|
Var * | childM (Size index) |
| Find child in list using index (mutable). More...
|
|
const Var & | childref (const StringBase &key) const |
| Get child in object using key, default to null instance (const). More...
|
|
const Var & | childref (Size index) const |
| Get child in list using index, default to null instance (const). More...
|
|
Var & | clear () |
| Clear current value. More...
|
|
template<class T > |
T & | dump (T &out, const NewlineValue &newline=NL, uint indent=0) const |
| Dump all children and values to stream or string. More...
|
|
bool | empty () const |
| Get whether empty. More...
|
|
bool | get_bool () const |
| Get boolean value (const). More...
|
|
double | get_float () const |
| Get value as a floating-point number (const). More...
|
|
int64 | get_int () const |
| Get signed integer number value (const). More...
|
|
const ListType & | get_list () const |
| Get read-only list reference (const). More...
|
|
const ObjectType & | get_object () const |
| Get read-only object reference (const). More...
|
|
const String & | get_str () const |
| Get read-only string value reference (const). More...
|
|
uint64 | get_uint () const |
| Get unsigned integer number value (const). More...
|
|
bool | get_val (String &val) const |
| Get value as a string (const). More...
|
|
bool | is_bool () const |
| Get whether a boolean type (tBOOL). More...
|
|
bool | is_container () const |
| Get whether a container type (object or list). More...
|
|
bool | is_float () const |
| Get whether a floating-point number type (tFLOAT). More...
|
|
bool | is_integer () const |
| Get whether an integer number type (tUNSIGNED or tINTEGER). More...
|
|
bool | is_list () const |
| Get whether a list type (tLIST). More...
|
|
bool | is_number () const |
| Get whether a number type (tFLOAT, tUNSIGNED, tINTEGER). More...
|
|
bool | is_object () const |
| Get whether an object type (tOBJECT). More...
|
|
bool | is_signed () const |
| Get whether a signed integer number type (tINTEGER). More...
|
|
bool | is_string () const |
| Get whether a string type (tSTRING). More...
|
|
bool | is_unsigned () const |
| Get whether an unsigned integer number type (tUNSIGNED). More...
|
|
ListType & | list () |
| Get list reference, recreate as list if needed. More...
|
|
bool | null () const |
| Get whether null. More...
|
|
double & | numf () |
| Get floating-point number reference, recreate as floating-point if needed. More...
|
|
int64 & | numi () |
| Get signed integer number reference, recreate as signed integer if not an integer. More...
|
|
uint64 & | numu () |
| Get unsigned integer number reference, recreate as unsigned integer if not an integer. More...
|
|
ObjectType & | object () |
| Get object reference, recreate as object if needed. More...
|
|
Var & | operator= (Var &&src) |
| Move assignment operator (C++11). More...
|
|
Var & | operator= (const Var &src) |
| Assignment copy operator. More...
|
|
Var & | operator= (const ObjectType &val) |
| Assignment operator to copy from object map type. More...
|
|
Var & | operator= (const ListType &val) |
| Assignment operator to copy from list type. More...
|
|
Var & | operator= (const String &val) |
| Assignment operator to copy from string value. More...
|
|
Var & | operator= (const StringBase &val) |
| Assignment operator to copy from string value. More...
|
|
Var & | operator= (const char *val) |
| Assignment operator to copy from terminated string value. More...
|
|
Var & | operator= (double val) |
| Assignment operator to set as floating-point value. More...
|
|
Var & | operator= (ushort val) |
| Assignment operator to set as unsigned integer value (ushort). More...
|
|
Var & | operator= (uint val) |
| Assignment operator to set as unsigned integer value (uint). More...
|
|
Var & | operator= (ulong val) |
| Assignment operator to set as unsigned integer value (ulong). More...
|
|
Var & | operator= (ulongl val) |
| Assignment operator to set as unsigned integer value (ulongl). More...
|
|
Var & | operator= (short val) |
| Assignment operator to set as signed integer value (short). More...
|
|
Var & | operator= (int val) |
| Assignment operator to set as signed integer value (int). More...
|
|
Var & | operator= (long val) |
| Assignment operator to set as signed integer value (long). More...
|
|
Var & | operator= (longl val) |
| Assignment operator to set as signed integer value (longl). More...
|
|
Var & | operator= (bool val) |
| Assignment operator to set as boolean value (bool). More...
|
|
Var & | operator= (ValNull) |
| Assignment operator to set as null using vNULL. More...
|
|
Var & | operator[] (const StringBase &key) |
| Object field access operator. More...
|
|
Var & | operator[] (Size index) |
| List item access operator. More...
|
|
Var & | set () |
| Set as null type/value. More...
|
|
bool | shared () const |
| Get whether this has shared data. More...
|
|
bool | shared_scan () const |
| Get whether this or any nested key or value has any shared data. More...
|
|
Size | size () const |
| Get size as number of children. More...
|
|
String & | string () |
| Get string reference, recreate as string if needed. More...
|
|
Type | type () const |
| Get current type. More...
|
|
Var & | unshare () |
| Unshare current value. More...
|
|
Var & | unshare_all () |
| Unshare current value and all nested objects, lists, and strings. More...
|
|