![]() |
Trading System API
3.0
Library for Simulating and Deploying Trading and Investment Strategies
|
variant objects can represent values of different types. More...
#include <TSAVariant.h>
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::string & | get_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::string & | str (void) const |
template<typename T > | |
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 variant & | operator= (const variant &other) |
Assignment operator. Self will be identical to other. | |
const variant & | operator= (unsigned val) |
Assignment operator. Type is set to type_t::uint32. | |
const variant & | operator= (const date_time &val) |
Assignment operator. Type is set to type_t::datetime. | |
const variant & | operator= (const date &val) |
Assignment operator. Type is set to type_t::date. | |
const variant & | operator= (int val) |
Assignment operator. Type is set to type_t::int32. | |
const variant & | operator= (int64_t val) |
Assignment operator. Type is set to type_t::int64. | |
const variant & | operator= (uint64_t val) |
Assignment operator. Type is set to type_t::int64. | |
const variant & | operator= (bool val) |
Assignment operator. Type is set to boolean. | |
const variant & | operator= (float val) |
Assignment operator. Type is set to type_t::float32. | |
const variant & | operator= (double val) |
Assignment operator. Type is set to type_t::float64. | |
const variant & | operator= (void *val) |
Assignment operator. Type is set to type_t::void_ptr. | |
const variant & | operator= (const char *val) |
Assignment operator. Type is set to type_t::string. | |
const variant & | operator= (const tsa::string &val) |
const variant & | operator= (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 > | |
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(). | |
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
To put a variant object back into an 'undefined' state, call the clear() member.
Use the get<T>() template function to access the variant value such as:
If the variant does not already have the requested type, then get() will throw an exception.
In order to convert access a value as a specific type that may not match the variant type, use members such as:
such as:
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.
It is also possible to cast a variant instance using 'explicit' cast operators:
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.
.
All values are considered 'true' unless in the following list:
|
explicit |
Constructor. Self is initialized to given type. The object remains 'undefined' until a value is assigned. The defined() member returns false.
type | The type. |
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'.
other | The 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'.
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.
T | type parameter. Must be one of the following:
|
bool tsa::variant::is_equal | ( | const variant & | other | ) | const |
Returns true if self is identical to other.
other | The other. |
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.
T tsa::variant::to | ( | void | ) | const |
Gets to.
T | Generic type parameter. |
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.
All values are considered 'true' unless in the following list:
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.
date_time tsa::variant::to_datetime | ( | void | ) | const |
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.
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.
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.
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.
std::string tsa::variant::to_string | ( | void | ) | const |
Returns this object's data as a std::string, converting the data if required.
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.
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.