rttr  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Classes | Public Member Functions | List of all members
rttr::variant Class Reference

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 &param)
 Constructs a new variant with the given value param of type T. More...
 
template<typename T >
 variant (T &&param)
 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 >
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 >
variantoperator= (T &&other)
 Assigns the value of the other object to this variant. More...
 
variantoperator= (variant &&other)
 Assigns the value of the other variant to this variant. More...
 
variantoperator= (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...
 

Detailed Description

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().

Copying and Assignment

A variant object is can be copied and assigned, however each copy will perform a copy of the contained value.

Typical Usage

variant var;
var = 23; // copy integer
int x = var.to_int(); // x = 23
var = std::string("Hello World"); // variant contains now a std::string
var = "Hello World"; // variant contains now a char[12] array
int y = var.to_int(); // y = 0, because invalid conversion
std::string text = var.to_string(); // text = "Hello World", char array to string converted
var = "42"; // contains now char[3] array
std::cout << var.to_int(); // convert char array to integer and prints "42"
int my_array[100];
var = my_array; // copies the content of my_array into var
auto& arr = var.get_value<int[100]>(); // extracts the content of var by reference

It's of course also possible to store own custom types in a variant, take a look at following code:

struct custom_type
{
//...
};
variant var = custom_type(...);
bool b = var.is_type<custom_type>(); // b = true
custom_type& value = var.get_value<custom_type>(); // extracts the value by reference

The variant class allows to convert also custom types, therefore you have to register a convert function:

std::string converter_func(const custom_type& value, bool& ok)
{
ok = true;
// convert value to std::string
return std::string(...);
}
//...
variant var = custom_type(...);
var.can_convert<std::string>(); // return false
// will register a convert from custom_type to std::string
var.can_convert<std::string>(); // return true
var.to_string(); // converts from custom_type to std::string
See also
variant_array

Constructor & Destructor Documentation

rttr::variant::variant ( )

Constructs an invalid variant.

That means a valid which contains no data.

See also
is_valid()
template<typename T >
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.

template<typename T >
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.

Member Function Documentation

template<typename T >
bool rttr::variant::can_convert ( ) const

Returns true if the contained value can be converted to the given type T.

Otherwise false.

Returns
True if this variant can be converted to 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.

Returns
True if this variant can be converted to 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.

See also
can_convert()
Returns
True if this variant can be converted to target_type; otherwise false.
template<typename T >
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.

Remarks
Only call this method when it is possible to return the containing value as type T. Use therefore the method can_convert(). Otherwise the call leads to undefined behaviour.
See also
can_convert()
Returns
The converted value as type T.
type rttr::variant::get_type ( ) const

Returns the type object of underlying data type.

Remarks
When the variant has not stored any data, then an invalid type object is returned.
Returns
type of the underlying data type.
template<typename T >
T& rttr::variant::get_value ( ) const

Returns a reference to the containing value as type T.

Remarks
Only call this method when it is possible to return the containing value as the given type 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.
See also
is_type()
Returns
A reference to the stored value.
template<typename T >
bool rttr::variant::is_type ( ) const

Returns true if this variant data is of the given template type T.

Returns
True if variant is the same like T, otherwise false.
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.

Returns
True if this variant is valid, otherwise false.
template<typename T >
variant& rttr::variant::operator= ( T &&  other)

Assigns the value of the other object to this variant.

Returns
A reference to the variant with the new data.
variant& rttr::variant::operator= ( variant &&  other)

Assigns the value of the other variant to this variant.

Returns
A reference to the variant with the new data.
variant& rttr::variant::operator= ( const variant other)

Assigns the value of the other variant to this variant.

Returns
A reference to the variant with the new data.
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:

int obj_array[100];
variant var = obj_array; // copies the content of obj_array into var
variant_array array = var.to_array(); // Copies the content of var to a variant_array object
auto x = array.get_size(); // set x to 100
array.set_value(0, 42); // set the first index to the value 42
See also
can_convert(), convert()
Returns
A variant_array object.
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.

See also
can_convert(), is_type()
Returns
A bool value.
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.

See also
can_convert(), is_type()
Returns
An double value.
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.

See also
can_convert(), is_type()
Returns
An float value.
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.

See also
can_convert(), is_type()
Returns
An integer value.
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.

See also
can_convert(), is_type()
Returns
An std::string value.

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