C API Documentation
|
#include <Variant.h>
Public Types | |
enum | TypeId { STRING , BOOL , INT32 , UINT32 , INT64 , UINT64 , FLOAT , DOUBLE , CHAR , UCHAR , EMPTY , DOUBLEVECTOR } |
Public Member Functions | |
TypeId | type () const |
Variant () | |
template<typename T > | |
Variant (const T &val) | |
Variant (const char *str) | |
Variant (const Variant &other) | |
template<typename T > | |
Variant & | operator= (const T &value) |
Variant & | operator= (const Variant &other) |
virtual | ~Variant () |
const std::type_info & | typeInfo () const |
template<typename T > | |
T | convert () const |
VARIANT_IMPLICIT_CONVERT (std::string) | |
VARIANT_IMPLICIT_CONVERT (long) | |
VARIANT_IMPLICIT_CONVERT (bool) | |
VARIANT_IMPLICIT_CONVERT (float) | |
VARIANT_IMPLICIT_CONVERT (double) | |
VARIANT_IMPLICIT_CONVERT (unsigned long) | |
VARIANT_IMPLICIT_CONVERT (int) | |
VARIANT_IMPLICIT_CONVERT (unsigned int) | |
VARIANT_IMPLICIT_CONVERT (char) | |
VARIANT_IMPLICIT_CONVERT (unsigned char) | |
VARIANT_IMPLICIT_CONVERT (std::vector< double >) | |
std::string | toString () const |
std::string | pythonRepr () const |
Convert to Python-compatible representation. More... | |
bool | isString () const |
bool | isInteger () const |
bool | isNumeric () const |
bool | isBool () const |
bool | isEmpty () const |
bool | isSigned () const |
bool | isDoubleVector () const |
Static Public Member Functions | |
static Variant | parse (const std::string &val) |
A basic type to hold a variety of data types.
Unfourtunatly C++ does not have a standard variant type so we have to create one here.
This is a fairly low performance class and is intended soley for infrequent operations such as setting configuration parameters.
If Python support is enabled, this class can convert to and from a Python object.
Usage: This class can convert to and from any primitive data type, and some collections types. More type conversions will be added as needed.
To store a value, just assign it, the assignment operator automatically takes care of everything for you:
Extraction: To retrieve the stored data, uses the convert function, this is templated so it can convert and extract to any type:
Rationale: C++ does not have a built in variant type. Other variant types exist such as boost::any and Poco dynamic var. However including the one of these as part of our public wrappers would forever tie us to that particular library, and would impose yet another dependency.
Only very basic variant type support is needed and this class exposes the absolute minimum possible wrappers to support such features, and provides just two methods of getting and storing native C++ types in it.
This is currently implemented by Poco but the implementation is fully opaque and may change in the future.
enum rr::Variant::TypeId |
list of currently supported types that a Variant can hold.
rr::Variant::Variant | ( | ) |
creates an emtpy variant
|
inline |
rr::Variant::Variant | ( | const Variant & | other | ) |
Copy constructor.
|
virtual |
clean up any data owned by this object.
|
inline |
Convert this variant to a supported data type.
This method will try to perform type coercion, i.e. if this variant contains a string, and it is asked to convert to a int, the string will be parsed as an int. Similary, doubles will be rounded to int, so forth.
bool rr::Variant::isBool | ( | ) | const |
is this a boolean type.
bool rr::Variant::isEmpty | ( | ) | const |
true if empty.
bool rr::Variant::isInteger | ( | ) | const |
was an integer stored here.
bool rr::Variant::isNumeric | ( | ) | const |
is this a numeric type.
bool rr::Variant::isSigned | ( | ) | const |
true if this is a signed number.
bool rr::Variant::isString | ( | ) | const |
is this variant a string.
|
inline |
Assignment operator for assigning POD to Var same as the constructor, this assigns a value to an existing Variant.
Assignment operator. Assign one variant to another.
|
static |
Parses the string which must be in JSON format. This is a common way to read a Variant from a file or create a new one from a string:
std::string rr::Variant::pythonRepr | ( | ) | const |
Convert to Python-compatible representation.
std::string rr::Variant::toString | ( | ) | const |
Converts the Variant to a string in JSON format.
const std::type_info& rr::Variant::typeInfo | ( | ) | const |
get the type id of the stored data type. This will let you check what kind of data is strored in this variant.