libcommonc++
0.7
|
A general-purpose variant type. More...
#include <Variant.h++>
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... | |
Variant & | operator= (bool val) |
Assign a bool value to the Variant. More... | |
Variant & | operator= (char val) |
Assign a char value to the Variant. More... | |
Variant & | operator= (char16_t val) |
Assign a char16_t value to the Variant. More... | |
Variant & | operator= (int32_t val) |
Assign an int32_t value to the Variant. More... | |
Variant & | operator= (uint32_t val) |
Assign a uint32_t value to the Variant. More... | |
Variant & | operator= (float val) |
Assign a float value to the Variant. More... | |
Variant & | operator= (const double &val) |
Assign a double value to the Variant. More... | |
Variant & | operator= (const int64_t &val) |
Assign an int64_t value to the Variant. More... | |
Variant & | operator= (const uint64_t &val) |
Assign a uint64_t value to the Variant. More... | |
Variant & | operator= (const String &val) |
Assign a String value to the Variant. More... | |
Variant & | operator= (const char *val) |
Assign a string value to the Variant. More... | |
Variant & | operator= (const Blob &val) |
Assign a Blob value to the Variant. More... | |
Variant & | operator= (void *val) |
Assign an opaque pointer value to the Variant. More... | |
Variant & | operator= (const Variant &other) |
Assignment operator. More... | |
Variant & | operator[] (const String &name) |
Return the value for the given name from this Variant, whose value is assumed to be a map. More... | |
Variant & | operator[] (const char *name) |
Return the value for the given name from this Variant, whose value is assumed to be a map. More... | |
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. More... | |
const Variant & | operator[] (const String &name) const |
Return the value for the given name from this Variant, whose value is assumed to be a map. More... | |
const Variant & | operator[] (const char *name) const |
Return the value for the given name from this Variant, whose value is assumed to be a map. More... | |
Variant & | operator[] (int index) |
Return the value at the given index from this Variant, whose value is assumed to be a list. More... | |
const Variant & | get (int index) const |
Return the value at the given index from this Variant, whose value is assumed to be a list. More... | |
const Variant & | operator[] (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... | |
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.
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 |
|
explicit |
Construct a new bool variant.
val | The initial value. |
|
explicit |
Construct a new char variant.
val | The initial value. |
|
explicit |
Construct a new int32_t variant.
val | The initial value. |
|
explicit |
Construct a new uint32_t variant.
val | The initial value. |
|
explicit |
Construct a new float variant.
val | The initial value. |
|
explicit |
Construct a new double variant.
val | The initial value. |
|
explicit |
Construct a new int64_t variant.
val | The initial value. |
|
explicit |
Construct a new uint64_t variant.
val | The initial value. |
Construct a new String variant.
val | The initial value. |
|
explicit |
Construct a new String variant.
val | The initial value. |
Construct a new String variant.
val | The initial value. |
|
explicit |
Construct a new opaque pointer value variant.
val | The initial value. |
|
virtual |
Destructor.
void clear | ( | ) |
Clear the Variant.
Sets the value to the default value for the current type.
Return the value for the given name from this Variant, whose value is assumed to be a map.
name | The name of the value. |
const Variant & get | ( | int | index | ) | const |
Return the value at the given index from this Variant, whose value is assumed to be a list.
index | The index. |
OutOfBoundsException | If the index is negative. |
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.
vec | The vector in which to store the keys. |
bool hasKey | ( | const String & | key | ) | const |
If this Variant is a map, determines if the map contains the specified key.
key | The key to search for. |
|
inline |
Test if the Variant's value is a bool.
|
inline |
Test if the Variant's value is a char.
|
inline |
Test if the Variant's value is a char16_t.
|
inline |
Test if the Variant's value is a double.
|
inline |
Test if the Variant's value is a float.
|
inline |
Test if the Variant's value is an int32_t.
|
inline |
Test if the Variant's value is an int64_t.
|
inline |
Test if the Variant's value is a list.
|
inline |
Test if the Variant's value is a map.
|
inline |
Test if the Variant is null.
|
inline |
Test if the Variant's value is opaque.
|
inline |
Test if the Variant's value is a uint32_t.
|
inline |
Test if the Variant's value is a uint64_t.
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.
|
inline |
Cast operator.
Equivalent to toBool().
|
inline |
Cast operator.
Equivalent to toChar().
|
inline |
Cast operator.
Equivalent to toChar16().
|
inline |
Boolean evaluation cast operator.
This is not equivalent to toOpaque().
|
inline |
Cast operator.
Equivalent to toDouble().
|
inline |
Cast operator.
Equivalent to toFloat().
|
inline |
Cast operator.
Equivalent to toInt().
|
inline |
Cast operator.
Equivalent to toInt64().
|
inline |
Cast operator.
Equivalent to toString().
|
inline |
Cast operator.
Equivalent to toUInt().
|
inline |
Cast operator.
Equivalent to toUInt64().
|
inline |
Equivalent to isNull().
bool operator== | ( | const Variant & | other | ) | const |
Equality operator.
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.
name | The name of the value. |
|
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.
name | The name of the value. |
Return the value for the given name from this Variant, whose value is assumed to be a map.
name | The name of the value. |
|
inline |
Return the value for the given name from this Variant, whose value is assumed to be a map.
Equivalent to get().
name | The name of the value. |
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.
index | The index. |
OutOfBoundsException | If the index is negative. |
|
inline |
Return the value at the given index from this Variant, whose value is assumed to be a list.
Equivalent to get().
index | The index. |
OutOfBoundsException | If the index is negative. |
void setNull | ( | ) |
Set the value to null.
Any previous value is deleted.
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.
type | The new type for the Variant. |
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.
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.
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.
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.
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.
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.
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.
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.
void * toOpaque | ( | ) | const |
Return the Variant's value as an opaque pointer, or NULL
if the value is not an opaque.
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.
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.
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.
void write | ( | std::ostream & | stream | ) | const |
Write the value of this Variant to a stream.
stream | The stream to write to. |
Blob* blobVal |
bool boolVal |
char16_t char16Val |
char charVal |
double doubleVal |
float floatVal |
int64_t int64Val |
int32_t intVal |
VariantList* listVal |
VariantMap* mapVal |
void* opaqueVal |
String* stringVal |
uint64_t uint64Val |
uint32_t uintVal |