libcommonc++  0.7
Variant Class Reference

A general-purpose variant type. More...

#include <Variant.h++>

Collaboration diagram for Variant:

Public Types

enum  Type {
  TypeNull = 0, TypeBool, TypeChar, TypeChar16,
  TypeInt, TypeUInt, TypeFloat, TypeDouble,
  TypeInt64, TypeUInt64, TypeString, TypeBlob,
  TypeOpaque, TypeList, TypeMap
}
 Variant value types. More...
 

Public Member Functions

 Variant ()
 Construct a new Variant with a null type. More...
 
 Variant (const Variant &other)
 Copy constructor. More...
 
 Variant (bool val)
 Construct a new bool variant. More...
 
 Variant (char val)
 Construct a new char variant. More...
 
 Variant (char16_t val)
 Construct a new char16_t variant. More...
 
 Variant (int32_t val)
 Construct a new int32_t variant. More...
 
 Variant (uint32_t val)
 Construct a new uint32_t variant. More...
 
 Variant (float val)
 Construct a new float variant. More...
 
 Variant (const double &val)
 Construct a new double variant. More...
 
 Variant (const int64_t &val)
 Construct a new int64_t variant. More...
 
 Variant (const uint64_t &val)
 Construct a new uint64_t variant. More...
 
 Variant (const String &val)
 Construct a new String variant. More...
 
 Variant (const char *val)
 Construct a new String variant. More...
 
 Variant (const char16_t *val)
 Construct a new String variant. More...
 
 Variant (const Blob &val)
 Construct a new Blob variant. More...
 
 Variant (void *val)
 Construct a new opaque pointer value variant. More...
 
virtual ~Variant ()
 Destructor. More...
 
Type getType () const
 Get the type of this Variant. More...
 
void setType (Type type)
 Set the type of this variant. More...
 
bool isNull () const
 Test if the Variant is null. More...
 
bool isBool () const
 Test if the Variant's value is a bool. More...
 
bool isChar () const
 Test if the Variant's value is a char. More...
 
bool isChar16 () const
 Test if the Variant's value is a char16_t. More...
 
bool isInt () const
 Test if the Variant's value is an int32_t. More...
 
bool isUInt () const
 Test if the Variant's value is a uint32_t. More...
 
bool isFloat () const
 Test if the Variant's value is a float. More...
 
bool isDouble () const
 Test if the Variant's value is a double. More...
 
bool isInt64 () const
 Test if the Variant's value is an int64_t. More...
 
bool isUInt64 () const
 Test if the Variant's value is a uint64_t. More...
 
bool isString () const
 Test if the Variant's value is a String. More...
 
bool isBlob () const
 Test if the Variant's value is a Blob. More...
 
bool isOpaque () const
 Test if the Variant's value is opaque. More...
 
bool isList () const
 Test if the Variant's value is a list. More...
 
bool isMap () const
 Test if the Variant's value is a map. More...
 
bool toBool () const
 Return the Variant's value as a bool, converting it if necessary, or return false if a conversion is not possible or not meaningful. More...
 
char toChar () const
 Return the Variant's value as a char, converting it if necessary, or return '\0' if a conversion is not possible or not meaningful. More...
 
char16_t toChar16 () const
 Return the Variant's value as a char16_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful. More...
 
int32_t toInt () const
 Return the Variant's value as an int32_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful. More...
 
uint32_t toUInt () const
 Return the Variant's value as a uint32_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful. More...
 
float toFloat () const
 Return the Variant's value as a float, converting it if necessary, or return 0.0f if a conversion is not possible or not meaningful. More...
 
double toDouble () const
 Return the Variant's value as a double, converting it if necessary, or return 0.0 if a conversion is not possible or not meaningful. More...
 
int64_t toInt64 () const
 Return the Variant's value as an int64_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful. More...
 
uint64_t toUInt64 () const
 Return the Variant's value as a uint64_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful. More...
 
void * toOpaque () const
 Return the Variant's value as an opaque pointer, or NULL if the value is not an opaque. More...
 
String toString () const
 Return the Variant's value as a String, converting it if necessary, or return String::null if a conversion is not possible or not meaningful. More...
 
Blob toBlob () const
 Return the Variant's value as a Blob, converting it if necessary, or return Blob::null if a conversion is not possible or not meaningful. More...
 
 operator bool () const
 Cast operator. More...
 
 operator char () const
 Cast operator. More...
 
 operator char16_t () const
 Cast operator. More...
 
 operator int32_t () const
 Cast operator. More...
 
 operator uint32_t () const
 Cast operator. More...
 
 operator float () const
 Cast operator. More...
 
 operator double () const
 Cast operator. More...
 
 operator String () const
 Cast operator. More...
 
 operator Blob () const
 Cast operator. More...
 
 operator int64_t () const
 Cast operator. More...
 
 operator uint64_t () const
 Cast operator. More...
 
 operator const void * () const
 Boolean evaluation cast operator. More...
 
bool operator! () const
 Equivalent to isNull(). More...
 
uint_t length () const
 Get the length of the value. More...
 
void clear ()
 Clear the Variant. More...
 
void setNull ()
 Set the value to null. More...
 
Variantoperator= (bool val)
 Assign a bool value to the Variant. More...
 
Variantoperator= (char val)
 Assign a char value to the Variant. More...
 
Variantoperator= (char16_t val)
 Assign a char16_t value to the Variant. More...
 
Variantoperator= (int32_t val)
 Assign an int32_t value to the Variant. More...
 
Variantoperator= (uint32_t val)
 Assign a uint32_t value to the Variant. More...
 
Variantoperator= (float val)
 Assign a float value to the Variant. More...
 
Variantoperator= (const double &val)
 Assign a double value to the Variant. More...
 
Variantoperator= (const int64_t &val)
 Assign an int64_t value to the Variant. More...
 
Variantoperator= (const uint64_t &val)
 Assign a uint64_t value to the Variant. More...
 
Variantoperator= (const String &val)
 Assign a String value to the Variant. More...
 
Variantoperator= (const char *val)
 Assign a string value to the Variant. More...
 
Variantoperator= (const Blob &val)
 Assign a Blob value to the Variant. More...
 
Variantoperator= (void *val)
 Assign an opaque pointer value to the Variant. More...
 
Variantoperator= (const Variant &other)
 Assignment operator. More...
 
Variantoperator[] (const String &name)
 Return the value for the given name from this Variant, whose value is assumed to be a map. More...
 
Variantoperator[] (const char *name)
 Return the value for the given name from this Variant, whose value is assumed to be a map. More...
 
const Variantget (const String &name) const
 Return the value for the given name from this Variant, whose value is assumed to be a map. More...
 
const Variantoperator[] (const String &name) const
 Return the value for the given name from this Variant, whose value is assumed to be a map. More...
 
const Variantoperator[] (const char *name) const
 Return the value for the given name from this Variant, whose value is assumed to be a map. More...
 
Variantoperator[] (int index)
 Return the value at the given index from this Variant, whose value is assumed to be a list. More...
 
const Variantget (int index) const
 Return the value at the given index from this Variant, whose value is assumed to be a list. More...
 
const Variantoperator[] (int index) const
 Return the value at the given index from this Variant, whose value is assumed to be a list. More...
 
bool getKeys (StringVec &vec) const
 If this Variant is a map, stores a list of the map keys into the supplied vector and returns true. More...
 
bool hasKey (const String &key) const
 If this Variant is a map, determines if the map contains the specified key. More...
 
bool operator== (const Variant &other) const
 Equality operator. More...
 
bool operator!= (const Variant &other) const
 Inequality operator. More...
 
void write (std::ostream &stream) const
 Write the value of this Variant to a stream. More...
 

Static Public Attributes

static const Variant null
 A null Variant. More...
 

Detailed Description

A general-purpose variant type.

Variant can hold a value of one of the following types: null, bool, char, char16_t, int32_t, uint32_t, float, double, int64_t, uint64_t, String, Blob, opaque, list, or map. The latter two allow for the construction of arbitrarily complex data structures composed of Variant objects.

Author
Mark Lindner

Member Enumeration Documentation

◆ Type

enum Type

Variant value types.

Enumerator
TypeNull 

null value

TypeBool 

bool value

TypeChar 

char value

TypeChar16 

char16_t value

TypeInt 

int value

TypeUInt 

unsigned int value

TypeFloat 

float value

TypeDouble 

double value

TypeInt64 

64-bit int value

TypeUInt64 

64-bit unsigned int value

TypeString 

String value.

TypeBlob 

Blob value.

TypeOpaque 

opaque pointer value

TypeList 

list value

TypeMap 

map value

Constructor & Destructor Documentation

◆ Variant() [1/16]

Variant ( )

Construct a new Variant with a null type.

◆ Variant() [2/16]

Variant ( const Variant other)

Copy constructor.

◆ Variant() [3/16]

Variant ( bool  val)
explicit

Construct a new bool variant.

Parameters
valThe initial value.

◆ Variant() [4/16]

Variant ( char  val)
explicit

Construct a new char variant.

Parameters
valThe initial value.

◆ Variant() [5/16]

Variant ( char16_t  val)
explicit

Construct a new char16_t variant.

Parameters
valThe initial value.

◆ Variant() [6/16]

Variant ( int32_t  val)
explicit

Construct a new int32_t variant.

Parameters
valThe initial value.

◆ Variant() [7/16]

Variant ( uint32_t  val)
explicit

Construct a new uint32_t variant.

Parameters
valThe initial value.

◆ Variant() [8/16]

Variant ( float  val)
explicit

Construct a new float variant.

Parameters
valThe initial value.

◆ Variant() [9/16]

Variant ( const double &  val)
explicit

Construct a new double variant.

Parameters
valThe initial value.

◆ Variant() [10/16]

Variant ( const int64_t &  val)
explicit

Construct a new int64_t variant.

Parameters
valThe initial value.

◆ Variant() [11/16]

Variant ( const uint64_t &  val)
explicit

Construct a new uint64_t variant.

Parameters
valThe initial value.

◆ Variant() [12/16]

Variant ( const String val)
explicit

Construct a new String variant.

Parameters
valThe initial value.

◆ Variant() [13/16]

Variant ( const char *  val)
explicit

Construct a new String variant.

Parameters
valThe initial value.

◆ Variant() [14/16]

Variant ( const char16_t val)
explicit

Construct a new String variant.

Parameters
valThe initial value.

◆ Variant() [15/16]

Variant ( const Blob val)
explicit

Construct a new Blob variant.

Parameters
valThe initial value.

◆ Variant() [16/16]

Variant ( void *  val)
explicit

Construct a new opaque pointer value variant.

Parameters
valThe initial value.

◆ ~Variant()

~Variant ( )
virtual

Destructor.

Member Function Documentation

◆ clear()

void clear ( )

Clear the Variant.

Sets the value to the default value for the current type.

◆ get() [1/2]

const Variant & get ( const String name) const

Return the value for the given name from this Variant, whose value is assumed to be a map.

Parameters
nameThe name of the value.
Returns
The value for the given name, or Variant::null if no value exists for the given name, or if this Variant is not a map.

◆ get() [2/2]

const Variant & get ( int  index) const

Return the value at the given index from this Variant, whose value is assumed to be a list.

Parameters
indexThe index.
Returns
The value at the given index, or Variant::null if the index is out of range, or if this Variant is not a list.
Exceptions
OutOfBoundsExceptionIf the index is negative.

◆ getKeys()

bool getKeys ( StringVec vec) const

If this Variant is a map, stores a list of the map keys into the supplied vector and returns true.

Otherwise, returns false.

Parameters
vecThe vector in which to store the keys.

◆ getType()

Type getType ( ) const
inline

Get the type of this Variant.

◆ hasKey()

bool hasKey ( const String key) const

If this Variant is a map, determines if the map contains the specified key.

Parameters
keyThe key to search for.
Returns
true if the key is found, false otherwise.

◆ isBlob()

bool isBlob ( ) const
inline

Test if the Variant's value is a Blob.

◆ isBool()

bool isBool ( ) const
inline

Test if the Variant's value is a bool.

◆ isChar()

bool isChar ( ) const
inline

Test if the Variant's value is a char.

◆ isChar16()

bool isChar16 ( ) const
inline

Test if the Variant's value is a char16_t.

◆ isDouble()

bool isDouble ( ) const
inline

Test if the Variant's value is a double.

◆ isFloat()

bool isFloat ( ) const
inline

Test if the Variant's value is a float.

◆ isInt()

bool isInt ( ) const
inline

Test if the Variant's value is an int32_t.

◆ isInt64()

bool isInt64 ( ) const
inline

Test if the Variant's value is an int64_t.

◆ isList()

bool isList ( ) const
inline

Test if the Variant's value is a list.

◆ isMap()

bool isMap ( ) const
inline

Test if the Variant's value is a map.

◆ isNull()

bool isNull ( ) const
inline

Test if the Variant is null.

◆ isOpaque()

bool isOpaque ( ) const
inline

Test if the Variant's value is opaque.

◆ isString()

bool isString ( ) const
inline

Test if the Variant's value is a String.

◆ isUInt()

bool isUInt ( ) const
inline

Test if the Variant's value is a uint32_t.

◆ isUInt64()

bool isUInt64 ( ) const
inline

Test if the Variant's value is a uint64_t.

◆ length()

uint_t length ( ) const

Get the length of the value.

For scalar types, this method returns 0. For maps and lists, it returns the number of items in the map or list.

◆ operator Blob()

operator Blob ( ) const
inline

Cast operator.

Equivalent to toBlob().

◆ operator bool()

operator bool ( ) const
inline

Cast operator.

Equivalent to toBool().

◆ operator char()

operator char ( ) const
inline

Cast operator.

Equivalent to toChar().

◆ operator char16_t()

operator char16_t ( ) const
inline

Cast operator.

Equivalent to toChar16().

◆ operator const void *()

operator const void * ( ) const
inline

Boolean evaluation cast operator.

This is not equivalent to toOpaque().

◆ operator double()

operator double ( ) const
inline

Cast operator.

Equivalent to toDouble().

◆ operator float()

operator float ( ) const
inline

Cast operator.

Equivalent to toFloat().

◆ operator int32_t()

operator int32_t ( ) const
inline

Cast operator.

Equivalent to toInt().

◆ operator int64_t()

operator int64_t ( ) const
inline

Cast operator.

Equivalent to toInt64().

◆ operator String()

operator String ( ) const
inline

Cast operator.

Equivalent to toString().

◆ operator uint32_t()

operator uint32_t ( ) const
inline

Cast operator.

Equivalent to toUInt().

◆ operator uint64_t()

operator uint64_t ( ) const
inline

Cast operator.

Equivalent to toUInt64().

◆ operator!()

bool operator! ( ) const
inline

Equivalent to isNull().

◆ operator!=()

bool operator!= ( const Variant other) const
inline

Inequality operator.

◆ operator=() [1/14]

Variant & operator= ( bool  val)

Assign a bool value to the Variant.

◆ operator=() [2/14]

Variant & operator= ( char  val)

Assign a char value to the Variant.

◆ operator=() [3/14]

Variant & operator= ( char16_t  val)

Assign a char16_t value to the Variant.

◆ operator=() [4/14]

Variant & operator= ( int32_t  val)

Assign an int32_t value to the Variant.

◆ operator=() [5/14]

Variant & operator= ( uint32_t  val)

Assign a uint32_t value to the Variant.

◆ operator=() [6/14]

Variant & operator= ( float  val)

Assign a float value to the Variant.

◆ operator=() [7/14]

Variant & operator= ( const double &  val)

Assign a double value to the Variant.

◆ operator=() [8/14]

Variant & operator= ( const int64_t &  val)

Assign an int64_t value to the Variant.

◆ operator=() [9/14]

Variant & operator= ( const uint64_t &  val)

Assign a uint64_t value to the Variant.

◆ operator=() [10/14]

Variant & operator= ( const String val)

Assign a String value to the Variant.

◆ operator=() [11/14]

Variant& operator= ( const char *  val)
inline

Assign a string value to the Variant.

◆ operator=() [12/14]

Variant & operator= ( const Blob val)

Assign a Blob value to the Variant.

◆ operator=() [13/14]

Variant & operator= ( void *  val)

Assign an opaque pointer value to the Variant.

◆ operator=() [14/14]

Variant & operator= ( const Variant other)

Assignment operator.

◆ operator==()

bool operator== ( const Variant other) const

Equality operator.

◆ operator[]() [1/6]

Variant & operator[] ( const String name)

Return the value for the given name from this Variant, whose value is assumed to be a map.

If the variant is not a map, it is automatically re-initialized as a map. If no value with the given name exists, a new one is automatically inserted into the map.

Parameters
nameThe name of the value.
Returns
The value for the given name, or if the requested value was not found, the new, null value that was added to the map.

◆ operator[]() [2/6]

Variant& operator[] ( const char *  name)
inline

Return the value for the given name from this Variant, whose value is assumed to be a map.

If the variant is not a map, it is automatically re-initialized as a map. If no value with the given name exists, a new one is automatically inserted into the map.

Parameters
nameThe name of the value.
Returns
The value for the given name, or if the requested value was not found, the new, null value that was added to the map.

◆ operator[]() [3/6]

const Variant& operator[] ( const String name) const
inline

Return the value for the given name from this Variant, whose value is assumed to be a map.

Parameters
nameThe name of the value.
Returns
The value for the given name, or Variant::null if no value exists for the given name, or if this Variant is not a map.

◆ operator[]() [4/6]

const Variant& operator[] ( const char *  name) const
inline

Return the value for the given name from this Variant, whose value is assumed to be a map.

Equivalent to get().

Parameters
nameThe name of the value.
Returns
The value for the given name, or Variant::null if no value exists for the given name, or if this Variant is not a map.

◆ operator[]() [5/6]

Variant & operator[] ( int  index)

Return the value at the given index from this Variant, whose value is assumed to be a list.

If the variant is not a list, it is automatically re-initialized as a list. If the index is out of range, the list is automatically extended with new values up to that index.

Returns
The at the given index, or Variant::null if this Variant is not a list.
Parameters
indexThe index.
Exceptions
OutOfBoundsExceptionIf the index is negative.

◆ operator[]() [6/6]

const Variant& operator[] ( int  index) const
inline

Return the value at the given index from this Variant, whose value is assumed to be a list.

Equivalent to get().

Parameters
indexThe index.
Returns
The value at the given index, or Variant::null if the index is out of range, or if this Variant is not a list.
Exceptions
OutOfBoundsExceptionIf the index is negative.

◆ setNull()

void setNull ( )

Set the value to null.

Any previous value is deleted.

◆ setType()

void setType ( Variant::Type  type)

Set the type of this variant.

The value is initialized to the default value for the given type. Any previous value is deleted.

Parameters
typeThe new type for the Variant.

◆ toBlob()

Blob toBlob ( ) const

Return the Variant's value as a Blob, converting it if necessary, or return Blob::null if a conversion is not possible or not meaningful.

◆ toBool()

bool toBool ( ) const

Return the Variant's value as a bool, converting it if necessary, or return false if a conversion is not possible or not meaningful.

◆ toChar()

char toChar ( ) const

Return the Variant's value as a char, converting it if necessary, or return '\0' if a conversion is not possible or not meaningful.

◆ toChar16()

char16_t toChar16 ( ) const

Return the Variant's value as a char16_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful.

◆ toDouble()

double toDouble ( ) const

Return the Variant's value as a double, converting it if necessary, or return 0.0 if a conversion is not possible or not meaningful.

◆ toFloat()

float toFloat ( ) const

Return the Variant's value as a float, converting it if necessary, or return 0.0f if a conversion is not possible or not meaningful.

◆ toInt()

int32_t toInt ( ) const

Return the Variant's value as an int32_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful.

◆ toInt64()

int64_t toInt64 ( ) const

Return the Variant's value as an int64_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful.

◆ toOpaque()

void * toOpaque ( ) const

Return the Variant's value as an opaque pointer, or NULL if the value is not an opaque.

◆ toString()

String toString ( ) const

Return the Variant's value as a String, converting it if necessary, or return String::null if a conversion is not possible or not meaningful.

◆ toUInt()

uint32_t toUInt ( ) const

Return the Variant's value as a uint32_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful.

◆ toUInt64()

uint64_t toUInt64 ( ) const

Return the Variant's value as a uint64_t, converting it if necessary, or return 0 if a conversion is not possible or not meaningful.

◆ write()

void write ( std::ostream &  stream) const

Write the value of this Variant to a stream.

Parameters
streamThe stream to write to.

Member Data Documentation

◆ blobVal

Blob* blobVal

◆ boolVal

bool boolVal

◆ char16Val

char16_t char16Val

◆ charVal

char charVal

◆ doubleVal

double doubleVal

◆ floatVal

float floatVal

◆ int64Val

int64_t int64Val

◆ intVal

int32_t intVal

◆ listVal

VariantList* listVal

◆ mapVal

VariantMap* mapVal

◆ null

const Variant null
static

A null Variant.

◆ opaqueVal

void* opaqueVal

◆ stringVal

String* stringVal

◆ uint64Val

uint64_t uint64Val

◆ uintVal

uint32_t uintVal

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