libRoadRunner C++ API  1.3
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Types | Public Member Functions | Static Public Member Functions | List of all members
rr::Variant Class Reference

#include <Variant.h>

Public Types

enum  TypeId

Public Member Functions

TypeId type () const
 Variant ()
template<typename T >
 Variant (const T &val)
 Variant (const Variant &other)
template<typename T >
Variantoperator= (const T &value)
Variantoperator= (const Variant &other)
virtual ~Variant ()
const std::type_info & typeInfo () const
template<typename T >
convert () const
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

Static Public Member Functions

static Variant parse (const std::string &val)

Detailed Description

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:

// store an int:
int i = 23;
Variant v = i;
// store a double:
Variant v = (double)0.123;

Extraction: To retrieve the stored data, uses the convert function, this is templated so it can convert and extract to any type:

Variant v = getSomeVariant();
// to convert to integer:
int i = v.convert<int>();
// to convert to string:
std::string s = v.convert<std::string>();

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 API 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 API 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.

Member Enumeration Documentation

list of currently supported types that a Variant can hold.

Constructor & Destructor Documentation

rr::Variant::Variant ( )

creates an emtpy variant

template<typename T >
rr::Variant::Variant ( const T &  val)

create a new variant from an existing supported data type. This templated constructor can assign any primitive type:

Variant v = (int)1;
Variant v = std::string("a string");
rr::Variant::Variant ( const Variant other)

Copy constructor.

rr::Variant::~Variant ( )

clean up any data owned by this object.

Member Function Documentation

template<typename T >
T rr::Variant::convert ( ) const

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.

// convert to int:
int i = v.convert<int>();
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 interger 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.

template<typename T >
Variant& rr::Variant::operator= ( const T &  value)

Assignment operator for assigning POD to Var same as the constructor, this assigns a value to an existing Variant.

Variant & rr::Variant::operator= ( const Variant other)

Assignment operator. Assign one variant to another.

Variant rr::Variant::parse ( const std::string &  val)

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:

Variant v = Variant::parse("0.123");
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.

Variant::TypeId rr::Variant::type ( ) const

the current type that this Variant is.

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.

// to check if this is an integer:
Variant v = ...
if (v.type() == typeid(int))
cout << "its an int";
// to check if its a string:
if (v.type() == typeid(std::string))
cout << "ints a string";

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