Evo C++ Library v0.5.1
Public Types | Public Member Functions | List of all members
Var Class Reference

#include <evo/var.h>

Inheritance diagram for Var:
Inheritance graph
[legend]

Detailed Description

General purpose variable that can hold a basic value (string, number, bool, etc) or children with nested values (object, list).

Example
#include <evo/var.h>
#include <evo/io.h>
using namespace evo;
int main() {
Var var;
var["name"] = "John Doe";
var["age"] = 21;
var["balance"] = 99.99;
var["active"] = true;
var["nothing"] = vNULL;
var["list"][0] = 1;
var["list"][1] = 2;
var["list"][2] = 3;
var["object"]["field"] = "value";
var.dump(con().out);
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
#include <evo/var.h>
#include <evo/io.h>
using namespace evo;
int main() {
Console& c = con();
// Create and dump an object with nested lists and object
Var var1 = VarObject{
{"key1", VarList{1, 2, 3, {4, 5}}},
{"key2", VarObject{
{"nested-key1", "value1"},
{"nested-key2", "value2"},
}},
};
var1.dump(c.out);
// Create and dump a list with various values, including a nested object
Var var2 = {1, "two", {3.0, 4}, 5, false, vNULL, VarObject{
{"key1", "value1"},
{"key2", "value2"},
}};
var2.dump(c.out);
// Create and dump a list with nested lists
Var var3 = {1, {2, 3, {4}}, "five"};
var3.dump(c.out);
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']

Public Types

typedef List< VarListType
 Item list type. More...
 
typedef MapList< String, VarObjectType
 Object map type. More...
 
typedef SizeT Size
 Size type used. More...
 
enum  Type {
  tOBJECT, tLIST, tSTRING, tFLOAT,
  tUNSIGNED, tINTEGER, tBOOL, tNULL
}
 Variable type. More...
 

Public Member Functions

 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 Varchild (const StringBase &key) const
 Find child in object using key (const). More...
 
const Varchild (Size index) const
 Find child in list using index (const). More...
 
VarchildM (const StringBase &key)
 Find child in object using key (mutable). More...
 
VarchildM (Size index)
 Find child in list using index (mutable). More...
 
const Varchildref (const StringBase &key) const
 Get child in object using key, default to null instance (const). More...
 
const Varchildref (Size index) const
 Get child in list using index, default to null instance (const). More...
 
Varclear ()
 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 ListTypeget_list () const
 Get read-only list reference (const). More...
 
const ObjectTypeget_object () const
 Get read-only object reference (const). More...
 
const Stringget_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...
 
ListTypelist ()
 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...
 
ObjectTypeobject ()
 Get object reference, recreate as object if needed. More...
 
Varoperator= (Var &&src)
 Move assignment operator (C++11). More...
 
Varoperator= (const Var &src)
 Assignment copy operator. More...
 
Varoperator= (const ObjectType &val)
 Assignment operator to copy from object map type. More...
 
Varoperator= (const ListType &val)
 Assignment operator to copy from list type. More...
 
Varoperator= (const String &val)
 Assignment operator to copy from string value. More...
 
Varoperator= (const StringBase &val)
 Assignment operator to copy from string value. More...
 
Varoperator= (const char *val)
 Assignment operator to copy from terminated string value. More...
 
Varoperator= (double val)
 Assignment operator to set as floating-point value. More...
 
Varoperator= (ushort val)
 Assignment operator to set as unsigned integer value (ushort). More...
 
Varoperator= (uint val)
 Assignment operator to set as unsigned integer value (uint). More...
 
Varoperator= (ulong val)
 Assignment operator to set as unsigned integer value (ulong). More...
 
Varoperator= (ulongl val)
 Assignment operator to set as unsigned integer value (ulongl). More...
 
Varoperator= (short val)
 Assignment operator to set as signed integer value (short). More...
 
Varoperator= (int val)
 Assignment operator to set as signed integer value (int). More...
 
Varoperator= (long val)
 Assignment operator to set as signed integer value (long). More...
 
Varoperator= (longl val)
 Assignment operator to set as signed integer value (longl). More...
 
Varoperator= (bool val)
 Assignment operator to set as boolean value (bool). More...
 
Varoperator= (ValNull)
 Assignment operator to set as null using vNULL. More...
 
Varoperator[] (const StringBase &key)
 Object field access operator. More...
 
Varoperator[] (Size index)
 List item access operator. More...
 
Varset ()
 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...
 
Stringstring ()
 Get string reference, recreate as string if needed. More...
 
Type type () const
 Get current type. More...
 
Varunshare ()
 Unshare current value. More...
 
Varunshare_all ()
 Unshare current value and all nested objects, lists, and strings. More...
 

Member Typedef Documentation

◆ ListType

typedef List<Var> ListType

Item list type.

◆ ObjectType

Object map type.

◆ Size

typedef SizeT Size

Size type used.

Member Enumeration Documentation

◆ Type

enum Type

Variable type.

Enumerator
tOBJECT 

Object with key/value fields.

tLIST 

List of values

tSTRING 

String value

tFLOAT 

Floating-point value.

tUNSIGNED 

Integer value (unsigned)

tINTEGER 

Integer value (signed)

tBOOL 

Boolean value.

tNULL 

Null value

Constructor & Destructor Documentation

◆ Var() [1/20]

Var ( std::initializer_list< Var init)
inline

Sequence constructor (C++11).

Parameters
initInitializer list, passed as comma-separated values in braces { }

◆ Var() [2/20]

Var ( Var &&  src)
inline

Move constructor (C++11).

Parameters
srcSource to move

◆ Var() [3/20]

Var ( )
inline

Default constructor sets as null.

◆ Var() [4/20]

Var ( const Var src)
inline

Copy constructor.

◆ Var() [5/20]

Var ( const ObjectType val)
inline

Constructor to copy from an object map type.

Parameters
valObject map value to copy

◆ Var() [6/20]

Var ( const ListType val)
inline

Constructor to copy from an object list type.

Parameters
valList value to copy

◆ Var() [7/20]

Var ( const String val)
inline

Constructor to copy from a string value.

Parameters
valString to copy

◆ Var() [8/20]

Var ( const StringBase val)
inline

Constructor to copy from a string value.

  • This makes a unique copy – doesn't use Sharing
Parameters
valString to copy

◆ Var() [9/20]

Var ( const char *  val)
inline

Constructor to copy from an string value.

Parameters
valString to copy, must be terminated, NULL for construct an empty string

◆ Var() [10/20]

Var ( double  val)
inline

Constructor to initialize as floating-point value.

Parameters
valValue to copy

◆ Var() [11/20]

Var ( ushort  val)
inline

Constructor to initialize as unsigned integer value (ushort).

Parameters
valValue to copy

◆ Var() [12/20]

Var ( uint  val)
inline

Constructor to initialize as unsigned integer value (uint).

Parameters
valValue to copy

◆ Var() [13/20]

Var ( ulong  val)
inline

Constructor to initialize as unsigned integer value (ulong).

Parameters
valValue to copy

◆ Var() [14/20]

Var ( ulongl  val)
inline

Constructor to initialize as unsigned integer value (ulongl).

Parameters
valValue to copy

◆ Var() [15/20]

Var ( short  val)
inline

Constructor to initialize as integer value (short).

Parameters
valValue to copy

◆ Var() [16/20]

Var ( int  val)
inline

Constructor to initialize as integer value (int).

Parameters
valValue to copy

◆ Var() [17/20]

Var ( long  val)
inline

Constructor to initialize as integer value (long).

Parameters
valValue to copy

◆ Var() [18/20]

Var ( longl  val)
inline

Constructor to initialize as integer value (longl).

Parameters
valValue to copy

◆ Var() [19/20]

Var ( bool  val)
inline

Constructor to initialize as boolean value (bool).

Parameters
valValue to copy

◆ Var() [20/20]

Var ( ValNull  )
inline

Constructor to initialize as as null with vNULL as argument.

◆ ~Var()

~Var ( )
inline

Destructor.

Member Function Documentation

◆ boolref()

bool& boolref ( )
inline

Get bool reference, recreate as bool if needed.

  • If this isn't a bool, this will recreate this as a bool (previous value is lost, new value is false)
Returns
Value bool reference for this

◆ child() [1/2]

const Var* child ( const StringBase key) const
inline

Find child in object using key (const).

Returns
Pointer to found child, NULL if not found or not an object

◆ child() [2/2]

const Var* child ( Size  index) const
inline

Find child in list using index (const).

Returns
Pointer to found child, NULL if not found or not a list

◆ childM() [1/2]

Var* childM ( const StringBase key)
inline

Find child in object using key (mutable).

Returns
Pointer to found child, NULL if not found or not an object

◆ childM() [2/2]

Var* childM ( Size  index)
inline

Find child in list using index (mutable).

Returns
Pointer to found child, NULL if not found or not a list

◆ childref() [1/2]

const Var& childref ( const StringBase key) const
inline

Get child in object using key, default to null instance (const).

  • This can be chained and let's you pretend there's always a child – example:
    Var var; // no children
    int64 num = var.childref("top").childref("num").get_int();
  • To create children if needed, use operator[]()
Returns
Reference to found child, or null var if not found

◆ childref() [2/2]

const Var& childref ( Size  index) const
inline

Get child in list using index, default to null instance (const).

  • This can be chained and let's you pretend there's always a child – example:
    Var var; // no children
    int64 num = var.childref(0).childref(1).get_int();
  • To create children if needed, use operator[](Size)
Returns
Reference to found child, or null var if not found

◆ clear()

Var& clear ( )
inline

Clear current value.

  • This removes all children (if an object or list), or removes all characters (if string), or sets to 0 if a number, or sets to fale if bool
Returns
This

◆ dump()

T& dump ( T &  out,
const NewlineValue newline = NL,
uint  indent = 0 
) const
inline

Dump all children and values to stream or string.

  • This dumps with a syntax similar to JSON but with Smart Quoting on string keys and values
    • Key strings are unquoted if possible, string values are always quoted
Template Parameters
TOutput string or stream type – inferred from out argument, usually String, Stream, or StreamOut
Parameters
outStream or string to dump (write) to
newlineNewline type to use, NL for default
indentStarting space indent count, usually 0
Returns
Reference to out param

◆ empty()

bool empty ( ) const
inline

Get whether empty.

  • This is considered empty if there aren't any children, i.e. size() is 0
Returns
Whether empty

◆ get_bool()

bool get_bool ( ) const
inline

Get boolean value (const).

  • For string, number, or bool type this will return that value converted to a bool – 0 is false and non-zero is true
  • Strings are converted using String::getbool<bool>()
Returns
Value as bool, false if null or an object or list

◆ get_float()

double get_float ( ) const
inline

Get value as a floating-point number (const).

  • For string, integer, or bool type this will return that value converted to floating-point
  • Strings are converted using String::getnumf<double>()
Returns
Value as floating-point number, 0.0 if null or an object or list

◆ get_int()

int64 get_int ( ) const
inline

Get signed integer number value (const).

  • For unsigned integer type, this will cast to a signed int
  • For string, floating-point, or bool type this will return that value converted to an integer
  • Strings are converted using String::getnum<int64>()
Returns
Value as signed integer, 0 if null or an object or list

◆ get_list()

const ListType& get_list ( ) const
inline

Get read-only list reference (const).

  • If not a list, this returns a reference to static null list
Returns
List reference

◆ get_object()

const ObjectType& get_object ( ) const
inline

Get read-only object reference (const).

  • If not an object, this returns a reference to static null object
Returns
Object reference

◆ get_str()

const String& get_str ( ) const
inline

Get read-only string value reference (const).

  • If not a string, this returns a reference to static null string
Returns
String reference

◆ get_uint()

uint64 get_uint ( ) const
inline

Get unsigned integer number value (const).

  • For signed integer type, this will cast to an unsigned int
  • For string, floating-point, or bool type this will return that value converted to an integer
  • Strings are converted using String::getnum<int64>()
Returns
Value as unsigned integer, 0 if null or an object or list

◆ get_val()

bool get_val ( String val) const
inline

Get value as a string (const).

  • This will format current value as a string if not a string and not an object or list
  • If current value is a string then it's copied with operator=() – see Sharing
Parameters
valStores result string value on success, set to null if current value is null (including null object/list) [out]
Returns
Whether successful, false if an object or list or null

◆ is_bool()

bool is_bool ( ) const
inline

Get whether a boolean type (tBOOL).

Returns
Whether a boolean type

◆ is_container()

bool is_container ( ) const
inline

Get whether a container type (object or list).

  • String is not considered a container here
Returns
Whether a container type

◆ is_float()

bool is_float ( ) const
inline

Get whether a floating-point number type (tFLOAT).

Returns
Whether a floating-point number type

◆ is_integer()

bool is_integer ( ) const
inline

Get whether an integer number type (tUNSIGNED or tINTEGER).

Returns
Whether an integer number type

◆ is_list()

bool is_list ( ) const
inline

Get whether a list type (tLIST).

  • Note that the List type can be explicitly set to null
Returns
Whether a list type

◆ is_number()

bool is_number ( ) const
inline

Get whether a number type (tFLOAT, tUNSIGNED, tINTEGER).

Returns
Whether a number type

◆ is_object()

bool is_object ( ) const
inline

Get whether an object type (tOBJECT).

  • Note that the object (MapList) type can be explicitly set to null
Returns
Whether an object type

◆ is_signed()

bool is_signed ( ) const
inline

Get whether a signed integer number type (tINTEGER).

Returns
Whether a signed integer number type

◆ is_string()

bool is_string ( ) const
inline

Get whether a string type (tSTRING).

  • Note that the String type can be explicitly set to null
Returns
Whether a string type

◆ is_unsigned()

bool is_unsigned ( ) const
inline

Get whether an unsigned integer number type (tUNSIGNED).

Returns
Whether an unsigned integer number type

◆ list()

ListType& list ( )
inline

Get list reference, recreate as list if needed.

  • If this isn't a list, this will recreate this as an empty list (previous value is lost)
Returns
List reference for this

◆ null()

bool null ( ) const
inline

Get whether null.

Returns
Whether null, true if null type or if object, list, or string type was explicitly set to null

◆ numf()

double& numf ( )
inline

Get floating-point number reference, recreate as floating-point if needed.

  • If this isn't a floating-point number, this will recreate this as a floating-point number
  • When recreated, the previous value is converted if another number type (tINTEGER), otherwise previous value is lost and the new value is 0.0
Returns
Value floating-point number reference for this

◆ numi()

int64& numi ( )
inline

Get signed integer number reference, recreate as signed integer if not an integer.

  • If this is an unsigned integer, the type is set to signed and the reference is casted to signed so the value is accessed as-is
  • If this isn't any integer type, this will recreate this as a signed integer
  • When recreated, the previous value is converted if another number type (tFLOAT), otherwise previous value is lost and the new value is 0
Returns
Value number reference for this

◆ numu()

uint64& numu ( )
inline

Get unsigned integer number reference, recreate as unsigned integer if not an integer.

  • If this is a signed integer, the type is set to unsigned and the reference is casted to unsigned so the value is accessed as-is
  • If this isn't any integer type, this will recreate this as an unsigned integer
  • When recreated, the previous value is converted if another number type (tFLOAT), otherwise previous value is lost and the new value is 0
Returns
Value number reference for this

◆ object()

ObjectType& object ( )
inline

Get object reference, recreate as object if needed.

  • If this isn't an object, this will recreate this as an empty object (previous value is lost)
Returns
Object map reference for this, with field name for keys

◆ operator=() [1/18]

Var& operator= ( Var &&  src)
inline

Move assignment operator (C++11).

Parameters
srcSource to move
Returns
This

◆ operator=() [2/18]

Var& operator= ( const Var src)
inline

Assignment copy operator.

Parameters
srcSource to copy from
Returns
This

◆ operator=() [3/18]

Var& operator= ( const ObjectType val)
inline

Assignment operator to copy from object map type.

Parameters
valValue to copy from
Returns
This

◆ operator=() [4/18]

Var& operator= ( const ListType val)
inline

Assignment operator to copy from list type.

Parameters
valValue to copy from
Returns
This

◆ operator=() [5/18]

Var& operator= ( const String val)
inline

Assignment operator to copy from string value.

Parameters
valValue to copy from
Returns
This

◆ operator=() [6/18]

Var& operator= ( const StringBase val)
inline

Assignment operator to copy from string value.

  • This makes a unique copy – doesn't use Sharing
Parameters
valValue to copy from
Returns
This

◆ operator=() [7/18]

Var& operator= ( const char *  val)
inline

Assignment operator to copy from terminated string value.

Parameters
valValue to copy from, must be terminated string, NULL for empty string
Returns
This

◆ operator=() [8/18]

Var& operator= ( double  val)
inline

Assignment operator to set as floating-point value.

Parameters
valValue to copy
Returns
This

◆ operator=() [9/18]

Var& operator= ( ushort  val)
inline

Assignment operator to set as unsigned integer value (ushort).

Parameters
valValue to copy
Returns
This

◆ operator=() [10/18]

Var& operator= ( uint  val)
inline

Assignment operator to set as unsigned integer value (uint).

Parameters
valValue to copy
Returns
This

◆ operator=() [11/18]

Var& operator= ( ulong  val)
inline

Assignment operator to set as unsigned integer value (ulong).

Parameters
valValue to copy
Returns
This

◆ operator=() [12/18]

Var& operator= ( ulongl  val)
inline

Assignment operator to set as unsigned integer value (ulongl).

Parameters
valValue to copy
Returns
This

◆ operator=() [13/18]

Var& operator= ( short  val)
inline

Assignment operator to set as signed integer value (short).

Parameters
valValue to copy
Returns
This

◆ operator=() [14/18]

Var& operator= ( int  val)
inline

Assignment operator to set as signed integer value (int).

Parameters
valValue to copy
Returns
This

◆ operator=() [15/18]

Var& operator= ( long  val)
inline

Assignment operator to set as signed integer value (long).

Parameters
valValue to copy
Returns
This

◆ operator=() [16/18]

Var& operator= ( longl  val)
inline

Assignment operator to set as signed integer value (longl).

Parameters
valValue to copy
Returns
This

◆ operator=() [17/18]

Var& operator= ( bool  val)
inline

Assignment operator to set as boolean value (bool).

Parameters
valValue to copy
Returns
This

◆ operator=() [18/18]

Var& operator= ( ValNull  )
inline

Assignment operator to set as null using vNULL.

Returns
This

◆ operator[]() [1/2]

Var& operator[] ( const StringBase key)
inline

Object field access operator.

  • This gets the value under key, creating a new value if it doesn't exist
  • If not an object, this recreates as an object first and previous value is lost
Parameters
keyField key to use
Returns
Reference to field value

◆ operator[]() [2/2]

Var& operator[] ( Size  index)
inline

List item access operator.

  • This gets the value under index, creating a new value if it doesn't exist
  • If not a list, this recreates as a list first and previous value is lost
Parameters
indexItem index to use
Returns
Reference to list item value

◆ set()

Var& set ( )
inline

Set as null type/value.

Returns
This

◆ shared()

bool shared ( ) const
inline

Get whether this has shared data.

  • Note that an unshared object or list may still have nested children that themselves are shared
  • See Sharing
Returns
Whether shared

◆ shared_scan()

bool shared_scan ( ) const
inline

Get whether this or any nested key or value has any shared data.

  • This recursively scans all children and returns true if any shared key or value is found
  • Use unshare_all() to make this value and all children unique
  • See Sharing
Returns
Whether this or any nested part is shared

◆ size()

Size size ( ) const
inline

Get size as number of children.

Returns
Number of children, 0 if not an object or list

◆ string()

String& string ( )
inline

Get string reference, recreate as string if needed.

  • If this isn't a string, this will recreate this as an empty string (previous value is lost)
Returns
Value string reference for this

◆ type()

Type type ( ) const
inline

Get current type.

Returns
Current type

◆ unshare()

Var& unshare ( )
inline

Unshare current value.

  • Nested values may be still be shared, use unshare_all() to unshare all nested values too
  • See Sharing
Returns
This

◆ unshare_all()

Var& unshare_all ( )
inline

Unshare current value and all nested objects, lists, and strings.

  • This recursively unshares all nested object keys and values
  • Use this before passing an instance to another thread
  • See Sharing
Returns
This

Member Data Documentation

◆ buf_bool_

bool buf_bool_

◆ buf_float_

double buf_float_

◆ buf_int_

int64 buf_int_

◆ buf_list_

char buf_list_[sizeof(List< char >)]

◆ buf_obj_

char buf_obj_[sizeof(MapList< String, char >)]

◆ buf_str_

char buf_str_[sizeof(String)]

◆ buf_uint_

uint64 buf_uint_

The documentation for this class was generated from the following file: