The variant class allows to store data of any type and convert between these types transparently. More...
#include <variant.h>
Public Member Functions | |
variant () | |
Constructs an invalid variant. More... | |
template<typename T > | |
variant (const T ¶m) | |
Constructs a new variant with the given value param of type T . More... | |
template<typename T > | |
variant (T &¶m) | |
Constructs a new variant with the new value param . More... | |
variant (const variant &other) | |
Constructs a new variant object from the given variant other . More... | |
variant (variant &&other) | |
Constructs a new variant via move constructor. More... | |
~variant () | |
Destroys the variant and the contained object. More... | |
template<typename T > | |
bool | can_convert () const |
Returns true if the contained value can be converted to the given type T . More... | |
bool | can_convert (const type &target_type) const |
Returns true if the contained value can be converted to the given type target_type ; otherwise false. More... | |
bool | convert (const type &target_type) |
Converts the containing variant internally to the given type target_type . More... | |
template<typename T > | |
T | convert (bool *ok=nullptr) const |
Converts the containing value to type T and returns the value. More... | |
type | get_type () const |
Returns the type object of underlying data type. More... | |
template<typename T > | |
T & | get_value () const |
Returns a reference to the containing value as type T . More... | |
template<typename T > | |
bool | is_type () const |
Returns true if this variant data is of the given template type T. More... | |
bool | is_valid () const |
Returns true if this variant is valid, that means the variant is holding some data. More... | |
template<typename T > | |
variant & | operator= (T &&other) |
Assigns the value of the other object to this variant. More... | |
variant & | operator= (variant &&other) |
Assigns the value of the other variant to this variant. More... | |
variant & | operator= (const variant &other) |
Assigns the value of the other variant to this variant. More... | |
void | swap (variant &other) |
Swaps the content of this variant with the other variant. More... | |
variant_array | to_array () const |
Creates a valid variant_array object from the underlying value when the containing type is an array or it contains a pointer to an array type. More... | |
bool | to_bool () const |
Returns the variant as a bool if the variant has is_type() bool. More... | |
double | to_double (bool *ok=nullptr) |
Returns the containing variant as double value when the type is a double , or when a conversion function for the underlying type to double was registered; otherwise returns 0. More... | |
float | to_float (bool *ok=nullptr) |
Returns the containing variant as float value when the type is a float , or when a conversion function for the underlying type to float was registered; otherwise returns 0. More... | |
int | to_int (bool *ok=nullptr) const |
Returns the containing variant as int value when the type is an integer, or when a conversion function for the underlying type to int was registered; otherwise returns 0. More... | |
std::string | to_string (bool *ok=nullptr) const |
Returns the containing variant as std::string when the type is an std::string , or when a conversion function for the underlying type to std::string was registered; otherwise returns an empty default constructed std::string object is returned. More... | |
The variant class allows to store data of any type and convert between these types transparently.
This class serves as container for any given single type. It can hold one value at a time (using containers you can hold multiple types e.g. std::vector<int>
). Remark that the content is copied into the variant class. Even raw arrays (e.g. int[10]
) are copied.
In order to use this class with your custom Type
, you have to register it with RTTR_DECLARE_TYPE(Type). This class is mainly used for returning values from calls. See et.al. property::get_value() or method::invoke().
A variant object is can be copied and assigned, however each copy will perform a copy of the contained value.
It's of course also possible to store own custom types in a variant, take a look at following code:
The variant class allows to convert also custom types, therefore you have to register a convert function:
rttr::variant::variant | ( | ) |
rttr::variant::variant | ( | const T & | param | ) |
Constructs a new variant with the given value param
of type T
.
The value will be copied into the variant.
rttr::variant::variant | ( | T && | param | ) |
Constructs a new variant with the new value param
.
The value will be moved into the variant.
rttr::variant::variant | ( | const variant & | other | ) |
Constructs a new variant object from the given variant other
.
rttr::variant::variant | ( | variant && | other | ) |
Constructs a new variant via move constructor.
rttr::variant::~variant | ( | ) |
Destroys the variant and the contained object.
bool rttr::variant::can_convert | ( | ) | const |
Returns true if the contained value can be converted to the given type T
.
Otherwise false.
T
; otherwise false. bool rttr::variant::can_convert | ( | const type & | target_type | ) | const |
Returns true if the contained value can be converted to the given type target_type
; otherwise false.
target_type
; otherwise false. bool rttr::variant::convert | ( | const type & | target_type | ) |
Converts the containing variant internally to the given type target_type
.
When the conversion was successfully the function will return true. When the conversion fails, then the containing variant value stays the same and the function will return false.
A variant containing a pointer to a custom type will also convert and return true for this function if a rttr_cast to the type described by target_type
would succeed.
target_type
; otherwise false. T rttr::variant::convert | ( | bool * | ok = nullptr | ) | const |
Converts the containing value to type T
and returns the value.
If ok is non-null: *ok is set to true if the value could be converted to an T
; otherwise *ok is set to false.
T
. Use therefore the method can_convert(). Otherwise the call leads to undefined behaviour.T
. type rttr::variant::get_type | ( | ) | const |
Returns the type object of underlying data type.
T& rttr::variant::get_value | ( | ) | const |
Returns a reference to the containing value as type T
.
T
. Use therefore the method is_type(). Otherwise the call leads to undefined behaviour. Make sure you don't clean this variant, when you still hold a reference to the containing value.bool rttr::variant::is_type | ( | ) | const |
Returns true if this variant data is of the given template type T.
bool rttr::variant::is_valid | ( | ) | const |
Returns true if this variant is valid, that means the variant is holding some data.
When the variant doesn't hold any data it will return false.
variant& rttr::variant::operator= | ( | T && | other | ) |
Assigns the value of the other
object to this variant.
Assigns the value of the other variant to this variant.
Assigns the value of the other variant to this variant.
void rttr::variant::swap | ( | variant & | other | ) |
Swaps the content of this variant with the other
variant.
variant_array rttr::variant::to_array | ( | ) | const |
Creates a valid variant_array object from the underlying value when the containing type is an array or it contains a pointer to an array type.
A typical example is the following:
bool rttr::variant::to_bool | ( | ) | const |
Returns the variant as a bool
if the variant has is_type() bool.
Returns true if the variant is of type int
, bool
, float
, double
and the value is non-zero. or if the variant has type std::string
or char[]
and its lower-case content is not one of the following: empty, "0"
or "false"
; otherwise returns false.
When the type is a custom type and a conversion function to bool
was registered, then this call will try to convert the value to bool
.
double rttr::variant::to_double | ( | bool * | ok = nullptr | ) |
Returns the containing variant as double
value when the type is a double
, or when a conversion function for the underlying type to double
was registered; otherwise returns 0.
If ok is non-null: *ok is set to true if the value could be converted to an double
; otherwise *ok is set to false.
float rttr::variant::to_float | ( | bool * | ok = nullptr | ) |
Returns the containing variant as float
value when the type is a float
, or when a conversion function for the underlying type to float
was registered; otherwise returns 0.
If ok is non-null: *ok is set to true if the value could be converted to an float
; otherwise *ok is set to false.
int rttr::variant::to_int | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as int
value when the type is an integer, or when a conversion function for the underlying type to int
was registered; otherwise returns 0.
If ok is non-null: *ok is set to true if the value could be converted to an int
; otherwise *ok is set to false.
std::string rttr::variant::to_string | ( | bool * | ok = nullptr | ) | const |
Returns the containing variant as std::string
when the type is an std::string
, or when a conversion function for the underlying type to std::string
was registered; otherwise returns an empty default constructed std::string
object is returned.
If ok is non-null: *ok is set to true if the value could be converted to an std::string
; otherwise *ok is set to false.