Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
tsa::variant Class Reference

variant objects can represent values of different types. More...

#include <TSAVariant.h>

Inheritance diagram for tsa::variant:
tsa::auto_cast_variant

Public Member Functions

void clear (void)
 Returns self to an 'undefined' state where defined() returns 'false'. 'undefined' essentially means 'no data available' / 'N/A' or 'null' as used by some database systems. Note that an 'undefined' variant can still have a type.
 
bool defined (void) const
 Returns true if a value has been assigned and the object is in a 'defined' date. In other words it is 'not-null'. More...
 
std::stringget_string_ref (void) const
 
bool is_equal (const variant &other) const
 Returns true if self is identical to other. More...
 
bool is_numeric (void) const
 Returns true if value type is numeric. Numeric types are: More...
 
bool is_owner (void) const
 Returns true if the object is the owner of the pointer value. More...
 
std::stringstr (void) const
 
template<typename T >
to (void) const
 Gets to. More...
 
bool to_bool (void) const
 Returns this object's data as a bool, converting the data if required. Throws an exception if conversion fails. More...
 
date to_date (void) const
 Returns this object's data as a date, converting the data if required. Throws an exception if conversion fails. More...
 
date_time to_datetime (void) const
 Returns this object's data as a date_time, converting the data if required. Throws an exception if conversion fails. More...
 
double to_double (void) const
 Returns this object's data as a double, converting the data if required. Throws an exception if conversion fails. More...
 
float to_float (void) const
 Returns this object's data as a float, converting the data if required. Throws an exception if conversion fails. More...
 
int to_int (void) const
 Returns this object's data as an int, converting the data if required. Throws an exception if conversion fails. More...
 
int64_t to_int64 (void) const
 Returns this object's data as a int64_t, converting the data if required. Throws an exception if conversion fails. More...
 
std::string to_string (void) const
 Returns this object's data as a std::string, converting the data if required. More...
 
unsigned to_uint (void) const
 Returns this object's data as an unsigned it, converting the data if required. Throws an exception if conversion fails. More...
 
void * to_void_ptr (void) const
 Returns this object's data as a void_ptr. Throws an exception if type is not type_t::void_ptr. More...
 
type_t type (void) const
 Returns the type.
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &stream, const variant &var)
 Writes var to stream as text. More...
 

Constructors / Destructor

 variant (void)
 Constructor. Object state is 'undefined'. defined() returns false.
 
 variant (type_t type)
 Constructor. Self is initialized to given type. The object remains 'undefined' until a value is assigned. The defined() member returns false. More...
 
 variant (const variant &other)
 Copy constructor.
 
 variant (const date_time &val)
 Constructor. Type member is initialized to type_t::datetime and value to val.
 
 variant (const date &val)
 Constructor. Type member is initialized to type_t::date and value to val.
 
 variant (double val)
 Constructor. Type member is initialized to type_t::float64 and value to val.
 
 variant (int val)
 Constructor. Type member is initialized to type_t::int32 and value to val.
 
 variant (int64_t val)
 Constructor. Type member is initialized to type_t::int64 and value to val.
 
 variant (uint64_t val)
 Constructor. Type member is initialized to type_t::int64 and value to val. (value will be signed)
 
 variant (bool val)
 Constructor. Type member is initialized to type_t::boolean and value to val.
 
 variant (unsigned val)
 Constructor. Type member is initialized to type_t::uint32 and value to val.
 
 variant (float val)
 Constructor. Type member is initialized to type_t::float32 and value to val.
 
 variant (const tsa::string &val)
 Constructor. Type member is initialized to string and value to val.
 
 variant (const std::string &val)
 Constructor. Type member is initialized to type_t::string and value to val.
 
 variant (const char *val)
 Constructor. Type member is initialized to type_t::string and value to val.
 
 variant (char val)
 Constructor. Type member is initialized to type_t::string and value to val.
 
 variant (const object *val)
 
 variant (const void *val)
 Constructor. Type member is initialized to type_t::void_ptr and data member to val. Does NOT assume ownership of the pointer.
 
 ~variant (void)
 Destructor.
 

Assignment

const variantoperator= (const variant &other)
 Assignment operator. Self will be identical to other.
 
const variantoperator= (unsigned val)
 Assignment operator. Type is set to type_t::uint32.
 
const variantoperator= (const date_time &val)
 Assignment operator. Type is set to type_t::datetime.
 
const variantoperator= (const date &val)
 Assignment operator. Type is set to type_t::date.
 
const variantoperator= (int val)
 Assignment operator. Type is set to type_t::int32.
 
const variantoperator= (int64_t val)
 Assignment operator. Type is set to type_t::int64.
 
const variantoperator= (uint64_t val)
 Assignment operator. Type is set to type_t::int64.
 
const variantoperator= (bool val)
 Assignment operator. Type is set to boolean.
 
const variantoperator= (float val)
 Assignment operator. Type is set to type_t::float32.
 
const variantoperator= (double val)
 Assignment operator. Type is set to type_t::float64.
 
const variantoperator= (void *val)
 Assignment operator. Type is set to type_t::void_ptr.
 
const variantoperator= (const char *val)
 Assignment operator. Type is set to type_t::string.
 
const variantoperator= (const tsa::string &val)
 
const variantoperator= (const std::string &val)
 Assignment operator. Type is set to type_t::string.
 
void assign__no_type_chg (object *val)
 
void assign__no_type_chg (float value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (double value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (unsigned value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (int value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (int64_t value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (unsigned long int value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (bool value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (const variant &other)
 Assigns other to self while retaining self's type and attempting to convert other's data. If other is 'undefined', self will also be set to 'undefined'. More...
 
void assign__no_type_chg (void *value)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (const tsa::string &val)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (const std::string &val)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (const char *val)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (const date_time &val)
 Assigns value. Type stays unchanged.
 
void assign__no_type_chg (const date &val)
 Assigns value. Type stays unchanged.
 

Casting Operators

template<typename T >
get (void) const
 Returns the objects data. The object's type must match the template typename. If not, an exception is thrown. More...
 
 operator float (void) const
 Identical to to_float().
 
 operator unsigned (void) const
 Identical to to_float().
 
 operator double (void) const
 Identical to to_float().
 
 operator int (void) const
 Identical to to_float().
 
 operator int64_t (void) const
 Identical to to_float().
 
 operator uint64_t (void) const
 
 operator bool (void) const
 Identical to to_bool().
 
 operator void * (void) const
 Identical to to_void_ptr().
 

Detailed Description

variant objects can represent values of different types.

Data types are enumerated by tsa::type_t:

The default constructor leaves the object in an 'undefined' state. In this case, the defined() member returns false. Constructors that declare a value parameter will take on the type of the given argument. For example

variant v(3.3);
assert(v.type() == type_t::float64);

To put a variant object back into an 'undefined' state, call the clear() member.

Value Access:

Use the get<T>() template function to access the variant value such as:

variant phi = 1.618;
double d = phi.get&lt;double&gt;();

If the variant does not already have the requested type, then get() will throw an exception.

Value Conversion

In order to convert access a value as a specific type that may not match the variant type, use members such as:

such as:

variant phi = "1.618";
double d = phi.as_double();

Note that class variant will do its best to convert values between types, even if a loss of precision is possible. It will however throw and exception if the conversion is not possible.

Cast Operators

It is also possible to cast a variant instance using 'explicit' cast operators:

variant phi = "1.618";
double d = (double)phi;
Type Conversion during Assignment

Normally a variant will take on the type of object that is assigned to it. It is also possible to force the variant to retain its type during assigment using the assign__no_type_chg() members.

variant phi = "";
assert(phi.type() == type_t::string;
phi.assign__no_type_chg(1.618); //assign a double
assert(phi.type() == type_t::string); //instance still has type type_t::string.

.

Conversion Rules for Boolean Conversion

All values are considered 'true' unless in the following list:

  • 0
  • 0.0
  • "0"
  • "false" or "FALSE" (case insensitive)
  • "f" or "F" (case insensitive)
  • nullptr.

Constructor & Destructor Documentation

tsa::variant::variant ( type_t  type)
explicit

Constructor. Self is initialized to given type. The object remains 'undefined' until a value is assigned. The defined() member returns false.

Parameters
typeThe type.

Member Function Documentation

void tsa::variant::assign__no_type_chg ( const variant other)

Assigns other to self while retaining self's type and attempting to convert other's data. If other is 'undefined', self will also be set to 'undefined'.

Parameters
otherThe other variant.
bool tsa::variant::defined ( void  ) const

Returns true if a value has been assigned and the object is in a 'defined' date. In other words it is 'not-null'.

Returns
true if defined, else false.
template<typename T >
T tsa::variant::get ( void  ) const

Returns the objects data. The object's type must match the template typename. If not, an exception is thrown.

Template Parameters
Ttype parameter. Must be one of the following:
Returns
The object value.
bool tsa::variant::is_equal ( const variant other) const

Returns true if self is identical to other.

Parameters
otherThe other.
Returns
true if equal, false if not.
bool tsa::variant::is_numeric ( void  ) const

Returns true if value type is numeric. Numeric types are:

bool tsa::variant::is_owner ( void  ) const

Returns true if the object is the owner of the pointer value.

This is only relevant in case self is of pointer type (e.g. type_t::string, type_t::void_ptr). If If self is the owner, then self's destructor will delete the data pointed to by the pointer.

Returns
true if owner, false if not.
template<typename T >
T tsa::variant::to ( void  ) const

Gets to.

Template Parameters
TGeneric type parameter.
Returns
A T.
bool tsa::variant::to_bool ( void  ) const

Returns this object's data as a bool, converting the data if required. Throws an exception if conversion fails.

Conversion Rules

All values are considered 'true' unless in the following list:

  • 0
  • 0.0
  • "0"
  • "false" or "FALSE" (case insensitive)
  • "f" or "F" (case insensitive)
  • nullptr.
    Returns
    This object as a bool.
date tsa::variant::to_date ( void  ) const

Returns this object's data as a date, converting the data if required. Throws an exception if conversion fails.

Returns
date.
date_time tsa::variant::to_datetime ( void  ) const

Returns this object's data as a date_time, converting the data if required. Throws an exception if conversion fails.

Returns
date_time.
double tsa::variant::to_double ( void  ) const

Returns this object's data as a double, converting the data if required. Throws an exception if conversion fails.

Returns
double.
float tsa::variant::to_float ( void  ) const

Returns this object's data as a float, converting the data if required. Throws an exception if conversion fails.

Returns
float.
int tsa::variant::to_int ( void  ) const

Returns this object's data as an int, converting the data if required. Throws an exception if conversion fails.

Returns
int.
int64_t tsa::variant::to_int64 ( void  ) const

Returns this object's data as a int64_t, converting the data if required. Throws an exception if conversion fails.

Returns
int64_t.
std::string tsa::variant::to_string ( void  ) const

Returns this object's data as a std::string, converting the data if required.

Returns
This object as a std::string.
unsigned tsa::variant::to_uint ( void  ) const

Returns this object's data as an unsigned it, converting the data if required. Throws an exception if conversion fails.

Returns
unsigned int.
void * tsa::variant::to_void_ptr ( void  ) const

Returns this object's data as a void_ptr. Throws an exception if type is not type_t::void_ptr.

Returns
This object as a void*.