ug4
ug::Variant Class Reference

A variant can represent variables of different types. More...

#include <variant.h>

Public Types

enum  Type {
  VT_INVALID = 0 , VT_BOOL = 1 , VT_INT = 2 , VT_SIZE_T = 3 ,
  VT_FLOAT = 4 , VT_DOUBLE = 5 , VT_CSTRING = 6 , VT_STDSTRING = 7 ,
  VT_POINTER = 8 , VT_CONST_POINTER = 9 , VT_SMART_POINTER = 10 , VT_CONST_SMART_POINTER = 11
}
 

Public Member Functions

bool is_valid () const
 
const Variantoperator= (const Variant &v)
 
template<typename T >
to () const
 
template<>
bool to () const
 
template<>
int to () const
 
template<>
size_t to () const
 
template<>
float to () const
 
template<>
double to () const
 
template<>
const char * to () const
 
template<>
void * to () const
 
template<>
const void * to () const
 
template<>
ConstSmartPtr< void > to () const
 
template<>
SmartPtr< void > to () const
 
bool to_bool () const
 
const char * to_c_string () const
 
const void * to_const_pointer () const
 
ConstSmartPtr< void > to_const_smart_pointer () const
 
double to_double () const
 
float to_float () const
 
int to_int () const
 
number to_number () const
 
void * to_pointer () const
 
size_t to_size_t () const
 
SmartPtr< void > to_smart_pointer () const
 
const std::string & to_std_string () const
 
Type type () const
 
 Variant ()
 
 Variant (bool val)
 
 Variant (const char *val)
 
 Variant (const ConstSmartPtr< void > &val)
 
 Variant (const SmartPtr< void > &val)
 
 Variant (const std::string &val)
 
 Variant (const Variant &v)
 
 Variant (const void *val)
 
 Variant (double val)
 
 Variant (float val)
 
 Variant (int val)
 
 Variant (size_t val)
 
 Variant (void *val)
 
 ~Variant ()
 

Static Public Member Functions

template<typename T >
static Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 
template<>
Variant::Type type ()
 

Private Member Functions

void assign_variant (const Variant &v)
 this method only performs assignment, no prior clean up More...
 
const char * type_name () const
 returns the name of the current type More...
 

Private Attributes

union {
   bool   m_bool
 
   const void *   m_constptr
 
   ConstSmartPtr< void > *   m_constsmartptr
 
   const char *   m_cstring
 
   double   m_double
 
   float   m_float
 
   int   m_int
 
   void *   m_pointer
 
   size_t   m_size_t
 
   SmartPtr< void > *   m_smartptr
 
   std::string *   m_stdstring
 
}; 
 
Type m_type
 

Detailed Description

A variant can represent variables of different types.

A variant will represent the value, which was assigned during construction or in an assignment operation. Use the different to_.. methods, to retrieve the encapsulated value. Take care only to retrieve values, which are compatible with the encapsulated value.

Number and boolean types are automatically converted to each other: VT_BOOL, VT_INT, VT_FLOAT, VT_DOUBLE.

A stdstring is automatically converted to a cstr, but not vice-versa.

You may inspect the type represented by a variant by calling the type() method, which will return a constant enumerated in Variant::Type.

Member Enumeration Documentation

◆ Type

Enumerator
VT_INVALID 
VT_BOOL 
VT_INT 
VT_SIZE_T 
VT_FLOAT 
VT_DOUBLE 
VT_CSTRING 
VT_STDSTRING 
VT_POINTER 
VT_CONST_POINTER 
VT_SMART_POINTER 
VT_CONST_SMART_POINTER 

Constructor & Destructor Documentation

◆ Variant() [1/13]

ug::Variant::Variant ( )

◆ Variant() [2/13]

ug::Variant::Variant ( bool  val)

◆ Variant() [3/13]

ug::Variant::Variant ( int  val)

◆ Variant() [4/13]

ug::Variant::Variant ( size_t  val)

◆ Variant() [5/13]

ug::Variant::Variant ( float  val)

◆ Variant() [6/13]

ug::Variant::Variant ( double  val)

◆ Variant() [7/13]

ug::Variant::Variant ( const char *  val)

◆ Variant() [8/13]

ug::Variant::Variant ( const std::string &  val)

References new().

◆ Variant() [9/13]

ug::Variant::Variant ( void *  val)

◆ Variant() [10/13]

ug::Variant::Variant ( const void *  val)

◆ Variant() [11/13]

ug::Variant::Variant ( const SmartPtr< void > &  val)

References new().

◆ Variant() [12/13]

ug::Variant::Variant ( const ConstSmartPtr< void > &  val)

References new().

◆ Variant() [13/13]

ug::Variant::Variant ( const Variant v)

References assign_variant().

◆ ~Variant()

Member Function Documentation

◆ assign_variant()

void ug::Variant::assign_variant ( const Variant v)
private

◆ is_valid()

bool ug::Variant::is_valid ( ) const
inline

◆ operator=()

◆ to() [1/10]

◆ to() [2/10]

template<>
int ug::Variant::to ( ) const
inline

References to_int().

◆ to() [3/10]

template<>
size_t ug::Variant::to ( ) const
inline

References to_size_t().

◆ to() [4/10]

template<>
float ug::Variant::to ( ) const
inline

References to_float().

◆ to() [5/10]

template<>
double ug::Variant::to ( ) const
inline

References to_double().

◆ to() [6/10]

template<>
const char* ug::Variant::to ( ) const
inline

References to_c_string().

◆ to() [7/10]

template<>
void* ug::Variant::to ( ) const
inline

References to_pointer().

◆ to() [8/10]

template<>
const void* ug::Variant::to ( ) const
inline

References to_const_pointer().

◆ to() [9/10]

template<>
SmartPtr<void> ug::Variant::to ( ) const
inline

◆ to() [10/10]

template<>
ConstSmartPtr<void> ug::Variant::to ( ) const
inline

◆ to_bool()

◆ to_c_string()

const char * ug::Variant::to_c_string ( ) const

◆ to_const_pointer()

const void * ug::Variant::to_const_pointer ( ) const

◆ to_const_smart_pointer()

ConstSmartPtr< void > ug::Variant::to_const_smart_pointer ( ) const

◆ to_double()

double ug::Variant::to_double ( ) const

◆ to_float()

float ug::Variant::to_float ( ) const

◆ to_int()

int ug::Variant::to_int ( ) const

◆ to_number()

number ug::Variant::to_number ( ) const

◆ to_pointer()

void * ug::Variant::to_pointer ( ) const

References m_pointer, m_type, type_name(), UG_THROW, and VT_POINTER.

Referenced by to().

◆ to_size_t()

size_t ug::Variant::to_size_t ( ) const

References m_bool, m_size_t, m_type, type_name(), UG_THROW, VT_BOOL, and VT_SIZE_T.

Referenced by operator<<(), and to().

◆ to_smart_pointer()

SmartPtr< void > ug::Variant::to_smart_pointer ( ) const

◆ to_std_string()

const std::string & ug::Variant::to_std_string ( ) const

◆ type() [1/11]

template<typename T >
static Type ug::Variant::type ( )
inlinestatic

◆ type() [2/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_INT.

◆ type() [3/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_SIZE_T.

◆ type() [4/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_FLOAT.

◆ type() [5/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_DOUBLE.

◆ type() [6/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_CSTRING.

◆ type() [7/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_POINTER.

◆ type() [8/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

References VT_CONST_POINTER.

◆ type() [9/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

◆ type() [10/11]

template<>
Variant::Type ug::Variant::type ( )
inlinestatic

◆ type() [11/11]

Type ug::Variant::type ( ) const
inline

Referenced by operator<<(), and ug::Serialize().

◆ type_name()

Member Data Documentation

◆ 

union { ... }

◆ m_bool

bool ug::Variant::m_bool

◆ m_constptr

const void* ug::Variant::m_constptr

Referenced by assign_variant(), and to_const_pointer().

◆ m_constsmartptr

ConstSmartPtr<void>* ug::Variant::m_constsmartptr

◆ m_cstring

const char* ug::Variant::m_cstring

Referenced by assign_variant(), and to_c_string().

◆ m_double

double ug::Variant::m_double

◆ m_float

float ug::Variant::m_float

◆ m_int

int ug::Variant::m_int

◆ m_pointer

void* ug::Variant::m_pointer

◆ m_size_t

size_t ug::Variant::m_size_t

◆ m_smartptr

SmartPtr<void>* ug::Variant::m_smartptr

◆ m_stdstring

std::string* ug::Variant::m_stdstring

◆ m_type


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