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

#include <evo/var.h>

Inheritance diagram for VarObject:
Inheritance graph
[legend]

Detailed Description

Explicit variant of Var used for initializing as an object with an initializer list of key/value pairs (C++11).

Classes

struct  InitPair
 Initializer key/value pair. More...
 

Public Types

typedef List< Var > ListType
 Item list type. More...
 
typedef MapList< String, Var > ObjectType
 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

 VarObject ()
 Default constructor creates an empty object. More...
 
 VarObject (std::initializer_list< InitPair > init)
 Sequence constructor for object initializer. More...
 
 VarObject (VarObject &&src)
 Move constructor. More...
 
 VarObject (const VarObject &)=delete
 
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= (VarObject &&src)
 Move assignment operator. More...
 
VarObject & operator= (const VarObject &)=delete
 
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...
 

Member Typedef Documentation

◆ ListType

typedef List<Var> ListType
inherited

Item list type.

◆ ObjectType

typedef MapList<String,Var> ObjectType
inherited

Object map type.

◆ Size

typedef SizeT Size
inherited

Size type used.

Member Enumeration Documentation

◆ Type

enum Type
inherited

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

◆ VarObject() [1/4]

VarObject ( )
inline

Default constructor creates an empty object.

◆ VarObject() [2/4]

VarObject ( std::initializer_list< InitPair >  init)
inline

Sequence constructor for object initializer.

Parameters
initInitializer list of key/value pairs, each key/value pair is a nested intializer list with 2 items (key and value, where key is a string)

◆ VarObject() [3/4]

VarObject ( VarObject &&  src)
inline

Move constructor.

Parameters
srcSource to move from

◆ VarObject() [4/4]

VarObject ( const VarObject &  )
delete

Member Function Documentation

◆ boolref()

bool& boolref ( )
inlineinherited

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
inlineinherited

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
inlineinherited

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)
inlineinherited

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)
inlineinherited

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
inlineinherited

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
inlineinherited

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 ( )
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

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
inlineinherited

Get whether a boolean type (tBOOL).

Returns
Whether a boolean type

◆ is_container()

bool is_container ( ) const
inlineinherited

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
inlineinherited

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

Returns
Whether a floating-point number type

◆ is_integer()

bool is_integer ( ) const
inlineinherited

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

Returns
Whether an integer number type

◆ is_list()

bool is_list ( ) const
inlineinherited

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
inlineinherited

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

Returns
Whether a number type

◆ is_object()

bool is_object ( ) const
inlineinherited

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
inlineinherited

Get whether a signed integer number type (tINTEGER).

Returns
Whether a signed integer number type

◆ is_string()

bool is_string ( ) const
inlineinherited

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
inlineinherited

Get whether an unsigned integer number type (tUNSIGNED).

Returns
Whether an unsigned integer number type

◆ list()

ListType& list ( )
inlineinherited

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
inlineinherited

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 ( )
inlineinherited

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 ( )
inlineinherited

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 ( )
inlineinherited

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 ( )
inlineinherited

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/2]

Var& operator= ( VarObject &&  src)
inline

Move assignment operator.

Parameters
srcSource to move from
Returns
This

◆ operator=() [2/2]

VarObject& operator= ( const VarObject &  )
delete

◆ operator[]() [1/2]

Var& operator[] ( const StringBase &  key)
inlineinherited

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)
inlineinherited

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 ( )
inlineinherited

Set as null type/value.

Returns
This

◆ shared()

bool shared ( ) const
inlineinherited

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
inlineinherited

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
inlineinherited

Get size as number of children.

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

◆ string()

String& string ( )
inlineinherited

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
inlineinherited

Get current type.

Returns
Current type

◆ unshare()

Var& unshare ( )
inlineinherited

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 ( )
inlineinherited

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_
inherited

◆ buf_float_

double buf_float_
inherited

◆ buf_int_

int64 buf_int_
inherited

◆ buf_list_

char buf_list_[sizeof(List< char >)]
inherited

◆ buf_obj_

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

◆ buf_str_

char buf_str_[sizeof(String)]
inherited

◆ buf_uint_

uint64 buf_uint_
inherited

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