Variant Class Reference

A general-purpose variant type. More...

#include <Variant.h++>

Collaboration diagram for Variant:

Collaboration graph
[legend]

List of all members.

Public Types

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

Public Member Functions

 Variant ()
 Construct a new Variant with a null type.
 Variant (const Variant &other)
 Copy constructor.
 Variant (bool val)
 Construct a new bool variant.
 Variant (char val)
 Construct a new char variant.
 Variant (wchar_t val)
 Construct a new wchar_t variant.
 Variant (int32_t val)
 Construct a new int32_t variant.
 Variant (uint32_t val)
 Construct a new uint32_t variant.
 Variant (float val)
 Construct a new float variant.
 Variant (const double &val)
 Construct a new double variant.
 Variant (const int64_t &val)
 Construct a new int64_t variant.
 Variant (const uint64_t &val)
 Construct a new uint64_t variant.
 Variant (const String &val)
 Construct a new String variant.
 Variant (const WString &val)
 Construct a new WString variant.
 Variant (const char *val)
 Construct a new string variant.
 Variant (const wchar_t *val)
 Construct a new wide string variant.
 Variant (const Blob &val)
 Construct a new Blob variant.
 Variant (void *val)
 Construct a new opaque pointer value variant.
virtual ~Variant ()
 Destructor.
Type getType () const
 Get the type of this Variant.
void setType (Type type)
 Set the type of this variant.
bool isNull () const
 Test if the Variant is null.
bool isBool () const
 Test if the Variant's value is a bool.
bool isChar () const
 Test if the Variant's value is a char.
bool isWChar () const
 Test if the Variant's value is a wchar_t.
bool isInt () const
 Test if the Variant's value is an int32_t.
bool isUInt () const
 Test if the Variant's value is a uint32_t.
bool isFloat () const
 Test if the Variant's value is a float.
bool isDouble () const
 Test if the Variant's value is a double.
bool isInt64 () const
 Test if the Variant's value is an int64_t.
bool isUInt64 () const
 Test if the Variant's value is a uint64_t.
bool isString () const
 Test if the Variant's value is a String.
bool isWString () const
 Test if the Variant's value is a WString.
bool isBlob () const
 Test if the Variant's value is a Blob.
bool isOpaque () const
 Test if the Variant's value is opaque.
bool isList () const
 Test if the Variant's value is a list.
bool isMap () const
 Test if the Variant's value is a map.
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.
wchar_t toWChar () const
 Return the Variant's value as a wchar_t, converting it if necessary, or return L'\0' 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.
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.
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.
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.
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.
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 * 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.
WString toWString () const
 Return the Variant's value as a WString, converting it if necessary, or return WString::null if a conversion is not possible or not meaningful.
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.
 operator bool () const
 Cast operator.
 operator char () const
 Cast operator.
 operator wchar_t () const
 Cast operator.
 operator int32_t () const
 Cast operator.
 operator uint32_t () const
 Cast operator.
 operator float () const
 Cast operator.
 operator double () const
 Cast operator.
 operator String () const
 Cast operator.
 operator WString () const
 Cast operator.
 operator Blob () const
 Cast operator.
 operator int64_t () const
 Cast operator.
 operator uint64_t () const
 Cast operator.
 operator const void * () const throw ()
 Boolean evaluation cast operator.
bool operator! () const throw ()
 Equivalent to isNull().
size_t length () const
 Get the length of the value.
void clear ()
 Clear the Variant.
void setNull ()
 Set the value to null.
Variantoperator= (bool val)
 Assign a bool value to the Variant.
Variantoperator= (char val)
 Assign a char value to the Variant.
Variantoperator= (wchar_t val)
 Assign a wchar_t value to the Variant.
Variantoperator= (int32_t val)
 Assign an int32_t value to the Variant.
Variantoperator= (uint32_t val)
 Assign a uint32_t value to the Variant.
Variantoperator= (float val)
 Assign a float value to the Variant.
Variantoperator= (const double &val)
 Assign a double value to the Variant.
Variantoperator= (const int64_t &val)
 Assign an int64_t value to the Variant.
Variantoperator= (const uint64_t &val)
 Assign a uint64_t value to the Variant.
Variantoperator= (const String &val)
 Assign a String value to the Variant.
Variantoperator= (const WString &val)
 Assign a WString value to the Variant.
Variantoperator= (const char *val)
 Assign a string value to the Variant.
Variantoperator= (const wchar_t *val)
 Assign a wide string value to the Variant.
Variantoperator= (const Blob &val)
 Assign a Blob value to the Variant.
Variantoperator= (void *val)
 Assign an opaque pointer value to the Variant.
Variantoperator= (const Variant &other)
 Assignment operator.
Variantoperator[] (const String &name)
 Return the value for the given name from this Variant, whose value is assumed to be a map.
Variantoperator[] (const char *name)
 Return the value for the given name from this Variant, whose value is assumed to be a map.
const Variantget (const String &name) const
 Return the value for the given name from this Variant, whose value is assumed to be a map.
const Variantoperator[] (const String &name) const
 Return the value for the given name from this Variant, whose value is assumed to be a map.
const Variantoperator[] (const char *name) const
 Return the value for the given name from this Variant, whose value is assumed to be a map.
Variantoperator[] (int index) throw (OutOfBoundsException)
 Return the value at the given index from this Variant, whose value is assumed to be a list.
const Variantget (int index) const throw (OutOfBoundsException)
 Return the value at the given index from this Variant, whose value is assumed to be a list.
const Variantoperator[] (int index) const throw (OutOfBoundsException)
 Return the value at the given index from this Variant, whose value is assumed to be a list.
bool getKeys (StringVec &vec) const
 If this Variant is a map, stores a list of the map keys into the supplied vector and return true.
bool hasKey (const String &key) const
 If this Variant is a map, determines if the map contains the specified key.
bool operator== (const Variant &other) const
 Equality operator.
bool operator!= (const Variant &other) const
 Inequality operator.
void write (std::ostream &stream) const
 Write the value of this Variant to a stream.

Static Public Attributes

static const Variant null
 A null Variant.


Detailed Description

A general-purpose variant type.

Variant can hold a value of one of the following types: null, bool, char, wchar_t, int32_t, uint32_t, float, double, int64_t, uint64_t, String, WString, 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

enum Type

Variant value types.

Enumerator:
TypeNull 
TypeBool 
TypeChar 
TypeWChar 
TypeInt 
TypeUInt 
TypeFloat 
TypeDouble 
TypeInt64 
TypeUInt64 
TypeString 
TypeWString 
TypeBlob 
TypeOpaque 
TypeList 
TypeMap 


Constructor & Destructor Documentation

Variant (  ) 

Construct a new Variant with a null type.

Variant ( const Variant other  ) 

Copy constructor.

Variant ( bool  val  )  [explicit]

Construct a new bool variant.

Parameters:
val The initial value.

Variant ( char  val  )  [explicit]

Construct a new char variant.

Parameters:
val The initial value.

Variant ( wchar_t  val  )  [explicit]

Construct a new wchar_t variant.

Parameters:
val The initial value.

Variant ( int32_t  val  )  [explicit]

Construct a new int32_t variant.

Parameters:
val The initial value.

Variant ( uint32_t  val  )  [explicit]

Construct a new uint32_t variant.

Parameters:
val The initial value.

Variant ( float  val  )  [explicit]

Construct a new float variant.

Parameters:
val The initial value.

Variant ( const double &  val  )  [explicit]

Construct a new double variant.

Parameters:
val The initial value.

Variant ( const int64_t &  val  )  [explicit]

Construct a new int64_t variant.

Parameters:
val The initial value.

Variant ( const uint64_t &  val  )  [explicit]

Construct a new uint64_t variant.

Parameters:
val The initial value.

Variant ( const String val  )  [explicit]

Construct a new String variant.

Parameters:
val The initial value.

Variant ( const WString val  )  [explicit]

Construct a new WString variant.

Parameters:
val The initial value.

Variant ( const char *  val  )  [explicit]

Construct a new string variant.

Parameters:
val The initial value.

Variant ( const wchar_t *  val  )  [explicit]

Construct a new wide string variant.

Parameters:
val The initial value.

Variant ( const Blob val  )  [explicit]

Construct a new Blob variant.

Parameters:
val The initial value.

Variant ( void *  val  )  [explicit]

Construct a new opaque pointer value variant.

Parameters:
val The initial value.

~Variant (  )  [virtual]

Destructor.


Member Function Documentation

void clear (  ) 

Clear the Variant.

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

const Variant & get ( int  index  )  const throw (OutOfBoundsException)

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

Parameters:
index The 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:
OutOfBoundsException If the index is negative.

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:
name The 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.

bool getKeys ( StringVec vec  )  const

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

Otherwise, returns false.

Parameters:
vec The vector in which to store the keys.

Type getType (  )  const [inline]

Get the type of this Variant.

bool hasKey ( const String key  )  const

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

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

bool isBlob (  )  const [inline]

Test if the Variant's value is a Blob.

bool isBool (  )  const [inline]

Test if the Variant's value is a bool.

bool isChar (  )  const [inline]

Test if the Variant's value is a char.

bool isDouble (  )  const [inline]

Test if the Variant's value is a double.

bool isFloat (  )  const [inline]

Test if the Variant's value is a float.

bool isInt (  )  const [inline]

Test if the Variant's value is an int32_t.

bool isInt64 (  )  const [inline]

Test if the Variant's value is an int64_t.

bool isList (  )  const [inline]

Test if the Variant's value is a list.

bool isMap (  )  const [inline]

Test if the Variant's value is a map.

bool isNull (  )  const [inline]

Test if the Variant is null.

bool isOpaque (  )  const [inline]

Test if the Variant's value is opaque.

bool isString (  )  const [inline]

Test if the Variant's value is a String.

bool isUInt (  )  const [inline]

Test if the Variant's value is a uint32_t.

bool isUInt64 (  )  const [inline]

Test if the Variant's value is a uint64_t.

bool isWChar (  )  const [inline]

Test if the Variant's value is a wchar_t.

bool isWString (  )  const [inline]

Test if the Variant's value is a WString.

size_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 (  )  const [inline]

Cast operator.

Equivalent to toBlob().

operator bool (  )  const [inline]

Cast operator.

Equivalent to toBool().

operator char (  )  const [inline]

Cast operator.

Equivalent to toChar().

operator const void * (  )  const throw () [inline]

Boolean evaluation cast operator.

This is not equivalent to toOpaque().

operator double (  )  const [inline]

Cast operator.

Equivalent to toDouble().

operator float (  )  const [inline]

Cast operator.

Equivalent to toFloat().

operator int32_t (  )  const [inline]

Cast operator.

Equivalent to toInt().

operator int64_t (  )  const [inline]

Cast operator.

Equivalent to toInt64().

operator String (  )  const [inline]

Cast operator.

Equivalent to toString().

operator uint32_t (  )  const [inline]

Cast operator.

Equivalent to toUInt().

operator uint64_t (  )  const [inline]

Cast operator.

Equivalent to toUInt64().

operator wchar_t (  )  const [inline]

Cast operator.

Equivalent to toWChar().

operator WString (  )  const [inline]

Cast operator.

Equivalent to toWString().

bool operator! (  )  const throw () [inline]

Equivalent to isNull().

bool operator!= ( const Variant other  )  const [inline]

Inequality operator.

Variant & operator= ( const Variant other  ) 

Assignment operator.

Variant & operator= ( void *  val  ) 

Assign an opaque pointer value to the Variant.

Variant & operator= ( const Blob val  ) 

Assign a Blob value to the Variant.

Variant& operator= ( const wchar_t *  val  )  [inline]

Assign a wide string value to the Variant.

Variant& operator= ( const char *  val  )  [inline]

Assign a string value to the Variant.

Variant& operator= ( const WString val  ) 

Assign a WString value to the Variant.

Variant & operator= ( const String val  ) 

Assign a String value to the Variant.

Variant & operator= ( const uint64_t &  val  ) 

Assign a uint64_t value to the Variant.

Variant & operator= ( const int64_t &  val  ) 

Assign an int64_t value to the Variant.

Variant & operator= ( const double &  val  ) 

Assign a double value to the Variant.

Variant & operator= ( float  val  ) 

Assign a float value to the Variant.

Variant & operator= ( uint32_t  val  ) 

Assign a uint32_t value to the Variant.

Variant & operator= ( int32_t  val  ) 

Assign an int32_t value to the Variant.

Variant & operator= ( wchar_t  val  ) 

Assign a wchar_t value to the Variant.

Variant & operator= ( char  val  ) 

Assign a char value to the Variant.

Variant & operator= ( bool  val  ) 

Assign a bool value to the Variant.

bool operator== ( const Variant other  )  const

Equality operator.

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

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

Equivalent to get().

Parameters:
index The 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:
OutOfBoundsException If the index is negative.

Variant & operator[] ( int  index  )  throw (OutOfBoundsException)

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:
index The index.
Exceptions:
OutOfBoundsException If the index is negative.

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:
name The 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.

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:
name The 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.

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:
name The 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.

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:
name The 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.

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.

Parameters:
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.

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.

wchar_t toWChar (  )  const

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

WString toWString (  )  const

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

void write ( std::ostream &  stream  )  const

Write the value of this Variant to a stream.

Parameters:
stream The stream to write to.


Member Data Documentation

bool boolVal

char charVal

double doubleVal

float floatVal

int64_t int64Val

int32_t intVal

VariantList* listVal

VariantMap* mapVal

const Variant null [static]

A null Variant.

void* opaqueVal

uint64_t uint64Val

uint32_t uintVal

wchar_t wcharVal


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

Generated on Tue May 26 21:47:17 2009 for libcommonc++ by  doxygen 1.5.9