rttr::variant_array Class - 0.9.0 |RTTR
rttr::variant_array Class Reference

The variant_array class is a specialization of a variant, but for array types. More...

#include <variant_array.h>

Public Member Functions

 variant_array ()
 Constructs an invalid variant_array object. More...
 
template<typename T >
 variant_array (const T &value)
 Constructs a variant_array from the given argument value of type T. More...
 
template<typename T >
 variant_array (T &&value)
 Perfect forwarding of a value. More...
 
 variant_array (const variant_array &other)
 Constructs a copy of the given variant_array other. More...
 
 variant_array (variant_array &&other)
 Constructs a new variant_array via move constructor. More...
 
 ~variant_array ()
 Destroys the variant_array and the contained data. More...
 
std::size_t get_rank () const
 Gets the rank (number of dimensions) of the array. More...
 
type get_rank_type (std::size_t index) const
 Gets the type of the given rank index. More...
 
std::size_t get_size () const
 Returns the size of the first dimension from the array. More...
 
std::size_t get_size (std::size_t index_1) const
 Returns the size of the array at the second dimension at index index_1. More...
 
std::size_t get_size (std::size_t index_1, std::size_t index_2) const
 Returns the size of the array at the third dimension at index index_2, relative to the first dimension at index index_1. More...
 
std::size_t get_size_variadic (const std::vector< std::size_t > &index_list) const
 Returns the size from of the array at the specified indices in list index_list. More...
 
type get_type () const
 Returns the type object of this array. More...
 
variant get_value (std::size_t index_1) const
 Returns the value of the array in the first dimension at index index_1. More...
 
variant get_value (std::size_t index_1, std::size_t index_2) const
 Returns the value of the array in the second dimension at index index_2, relative to the first dimension at index index_1. More...
 
variant get_value (std::size_t index_1, std::size_t index_2, std::size_t index_3) const
 Returns the value of the array in the third dimension at index index_3, relative to the first dimension at index index_1 and second dimension at index index_2. More...
 
variant get_value_variadic (const std::vector< std::size_t > &index_list) const
 Returns the value of the array relative to to indices given in the list index_list. More...
 
bool insert_value (std::size_t index_1, detail::argument arg)
 Inserts the given argument arg into the array, in the first dimension at index index_1. More...
 
bool insert_value (std::size_t index_1, std::size_t index_2, detail::argument arg)
 Inserts the given argument arg into the array, in the second dimension at index index_2, relative to the first dimension at index index_1. More...
 
bool insert_value (std::size_t index_1, std::size_t index_2, std::size_t index_3, detail::argument arg)
 Inserts the given argument arg into the array, in the third dimension at index index_3, relative to the first dimension at index index_1 and the second dimension at index index_2. More...
 
bool insert_value_variadic (const std::vector< std::size_t > &index_list, detail::argument arg)
 Inserts the given argument arg into the array, relative to to indices given in the list index_list. More...
 
bool is_dynamic () const
 Returns true if this array is dynamic, that means the size can be changed; otherwise false. More...
 
bool is_raw_array () const
 Returns true when this array is a raw array i.e. More...
 
bool is_valid () const
 Returns true if this variant_array is valid, otherwise false. More...
 
template<typename T >
variant_arrayoperator= (T &&other)
 Assigns the value of the other object to this variant_array. More...
 
variant_arrayoperator= (variant_array &&other)
 Assigns the value of the other variant_array to this variant_array. More...
 
variant_arrayoperator= (const variant_array &other)
 Assigns the value of the other variant_array to this variant_array. More...
 
bool remove_value (std::size_t index_1)
 Removes the value at index index_1 in the first dimension of the array. More...
 
bool remove_value (std::size_t index_1, std::size_t index_2)
 Removes the value at index index_2 in the second dimension of the array relative to the first dimension at index index_1. More...
 
bool remove_value (std::size_t index_1, std::size_t index_2, std::size_t index_3)
 Removes the value at index index_3 in the third dimension of the array. More...
 
bool remove_value_variadic (const std::vector< std::size_t > &index_list)
 Removes the value at index index_1 in the first dimension of the array. More...
 
bool set_size (std::size_t new_size)
 Sets the size of the array at the first dimension to new_size. More...
 
bool set_size (std::size_t new_size, std::size_t index_1)
 Sets the size of the array at the second dimension relative to the first dimension at index index_1 to new_size. More...
 
bool set_size (std::size_t new_size, std::size_t index_1, std::size_t index_2)
 Sets the size of the array at the third dimension relative to the first dimension at index index_1 and the second dimension at index index_2 to new_size. More...
 
bool set_size_variadic (std::size_t new_size, const std::vector< std::size_t > &index_list)
 Sets the size of the array at the specified indices in list index_list. More...
 
bool set_value (detail::argument arg)
 Copies the content of the the array arg into the underlying array. More...
 
bool set_value (std::size_t index_1, detail::argument arg)
 Set the content of the the argument arg into the in the first dimension of the array at index index_1. More...
 
bool set_value (std::size_t index_1, std::size_t index_2, detail::argument arg)
 Set the content of the the argument arg into the in the second dimension at index_2 of the array relative to the first dimension at index_1. More...
 
bool set_value (std::size_t index_1, std::size_t index_2, std::size_t index_3, detail::argument arg)
 Set the content of the the argument arg into the in the third dimension at index_3 of the array relative to the first dimension at index_1 and second dimension at index_2. More...
 
bool set_value_variadic (const std::vector< std::size_t > &index_list, detail::argument arg)
 Set the content of the the argument arg into the array in the n-th dimension given in the list index_list. More...
 
void swap (variant_array &other)
 Swaps this variant_array with the other variant_array. More...
 

Detailed Description

The variant_array class is a specialization of a variant, but for array types.

With an instance of that class you can set and get values of an array with any dimension level.

A variant_array can be created directly with an array type or from a variant with variant::to_array(). Use before variant::is_array(), otherwise it might not be possible to create a valid variant_array object.

Meta Information

An array is defined by its rank, it's size and whether he is dynamic or not.

The rank of an array describes the number of dimensions. E.g. int[10] has a rank of 1. int[2][10] has an rank of 2 and so on. RTTR allows you to register types with an arbitrary rank count. For retrieving the size of an array use get_size(). With this function it is also possible to determine the size of the array relative to its rank level and it's index. Take a look at following example:

std::vector<std::vector<int>> obj(10, std::vector<int>(20, 0));
variant_array array = obj;
std::cout << array.get_size() << std::endl; // prints "10"
std::cout << array.get_size(0) << std::endl; // prints "20"
std::cout << array.get_size(1) << std::endl; // prints "20"
// INVALID call, max index is 9
std::cout << array.get_size(10) << std::endl; // undefined behavior

When the given array type is dynamic you can change the size of the array, therefore set_size() should be used. A value of an array can be accessed with get_value() or set with set_value. These function expect an index for up to rank level 3. The array class has here one interesting feature, you can set and get the value of an array up to its rank count. e.g:

int obj[2][10];
int sub_obj[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
array.set_value(0, sub_obj); // set the content of the obj[0] to zeros
array.set_value(0, 1, 23); // equivalent to call obj[0][1] == 23

When you have arrays bigger then this rank, use the counterpart functions: get_value_variadic and set_value_variadic which expects a list of indices. When the array is dynamic it is also possible to insert or remove values.

RTTR recognize whether a type is an array or not with the help of an array_mapper class template. This class does the mapping for the standard access function defined in the array class. At the moment there exist specializations for following types: std::array<T, N>, std::vector<T>, std::list<T> and raw-arrays T[N].

When you need to bind an own custom array type, then you have the implement following functions in the class array_mapper.

namespace rttr
{
namespace detail
{
template <typename T>
struct array_mapper< my_fancy_array<T> >
{
using raw_type = typename array_mapper<T>::raw_type;
using sub_type = T;
static bool is_dynamic()
{
...
}
static std::size_t get_size(const my_fancy_array<T>&)
{
...
}
static bool set_size(my_fancy_array<T>&)
{
...
}
static const T& get_value(const my_fancy_array<T>& arr, std::size_t index)
{
...
}
static T& get_value(my_fancy_array<T>& arr, std::size_t index)
{
...
}
static bool insert_value(my_fancy_array<T>&, std::size_t, const T&)
{
...
}
static bool remove_value(my_fancy_array<T>&, std::size_t)
{
...
}
};
} // end namespace detail
} // end namespace rttr

Remark the namespaces rttr::detail, otherwise your specialization will not be recognized.

Copying and Assignment

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

Typical Usage

int obj[2][10];
variant var = obj;
if (var.is_array())
{
variant_array array = var.to_array();
for (std::size_t index_1 = 0; index_1 < array.get_size(); ++index_1)
{
for (std::size_t index_2 = 0; index_2 < array.get_size(index_1); ++index_2)
{
array.set_value(index_1, index_2, 0);
}
}
// it is also possible to set the sub array in one step
for (std::size_t index_1 = 0; index_1 < array.get_size(); ++index_1)
{
int zeros[10] = {0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0};
array.set_value(index_1, zeros);
}
}
See also
variant

Constructor & Destructor Documentation

rttr::variant_array::variant_array ( )

Constructs an invalid variant_array object.

See also
is_valid()
template<typename T >
rttr::variant_array::variant_array ( const T &  value)

Constructs a variant_array from the given argument value of type T.

template<typename T >
rttr::variant_array::variant_array ( T &&  value)

Perfect forwarding of a value.

rttr::variant_array::variant_array ( const variant_array other)

Constructs a copy of the given variant_array other.

rttr::variant_array::variant_array ( variant_array &&  other)

Constructs a new variant_array via move constructor.

rttr::variant_array::~variant_array ( )

Destroys the variant_array and the contained data.

Member Function Documentation

std::size_t rttr::variant_array::get_rank ( ) const

Gets the rank (number of dimensions) of the array.

For example, int[10] returns 1; int[2][10] returns 2, and so on.

Returns
Returns the rank of the array.
type rttr::variant_array::get_rank_type ( std::size_t  index) const

Gets the type of the given rank index.

For example, an array of type int[2][10] returns for get_rank_type(0) => int[2][10] get_rank_type(1) => int[10]; get_rank_type(2) => int

Returns
The rank type at the given dimension index.
std::size_t rttr::variant_array::get_size ( ) const

Returns the size of the first dimension from the array.

Returns
The size of the array.
std::size_t rttr::variant_array::get_size ( std::size_t  index_1) const

Returns the size of the array at the second dimension at index index_1.

Returns
The size of the array.
std::size_t rttr::variant_array::get_size ( std::size_t  index_1,
std::size_t  index_2 
) const

Returns the size of the array at the third dimension at index index_2, relative to the first dimension at index index_1.

Returns
The size of the array.
std::size_t rttr::variant_array::get_size_variadic ( const std::vector< std::size_t > &  index_list) const

Returns the size from of the array at the specified indices in list index_list.

The index count specify the array dimension.

Returns
The size of the array.
type rttr::variant_array::get_type ( ) const

Returns the type object of this array.

Remarks
When the array is not valid, this function will return an invalid type object.
Returns
Type of the array.
variant rttr::variant_array::get_value ( std::size_t  index_1) const

Returns the value of the array in the first dimension at index index_1.

Returns
The value of the given array at the specified indices.
variant rttr::variant_array::get_value ( std::size_t  index_1,
std::size_t  index_2 
) const

Returns the value of the array in the second dimension at index index_2, relative to the first dimension at index index_1.

Returns
The value of the given array at the specified indices.
variant rttr::variant_array::get_value ( std::size_t  index_1,
std::size_t  index_2,
std::size_t  index_3 
) const

Returns the value of the array in the third dimension at index index_3, relative to the first dimension at index index_1 and second dimension at index index_2.

Returns
The value of the given array at the specified indices.
variant rttr::variant_array::get_value_variadic ( const std::vector< std::size_t > &  index_list) const

Returns the value of the array relative to to indices given in the list index_list.

Returns
The value of the given array at the specified indices.
bool rttr::variant_array::insert_value ( std::size_t  index_1,
detail::argument  arg 
)

Inserts the given argument arg into the array, in the first dimension at index index_1.

Remarks
This operation is only possible when the array is dynamic.
Returns
True if arg could be inserted, otherwise false.
bool rttr::variant_array::insert_value ( std::size_t  index_1,
std::size_t  index_2,
detail::argument  arg 
)

Inserts the given argument arg into the array, in the second dimension at index index_2, relative to the first dimension at index index_1.

Remarks
This operation is only possible when the array is dynamic.
Returns
True if arg could be inserted, otherwise false.
bool rttr::variant_array::insert_value ( std::size_t  index_1,
std::size_t  index_2,
std::size_t  index_3,
detail::argument  arg 
)

Inserts the given argument arg into the array, in the third dimension at index index_3, relative to the first dimension at index index_1 and the second dimension at index index_2.

Remarks
This operation is only possible when the array is dynamic.
Returns
True if arg could be inserted, otherwise false.
bool rttr::variant_array::insert_value_variadic ( const std::vector< std::size_t > &  index_list,
detail::argument  arg 
)

Inserts the given argument arg into the array, relative to to indices given in the list index_list.

Remarks
This operation is only possible when the array is dynamic.
Returns
True if arg could be inserted, otherwise false.
bool rttr::variant_array::is_dynamic ( ) const

Returns true if this array is dynamic, that means the size can be changed; otherwise false.

See also
get_size()
Returns
A boolean flag which indicates whether this array is dynamic or not.
bool rttr::variant_array::is_raw_array ( ) const

Returns true when this array is a raw array i.e.

int[10].

Returns
True if the array is a raw build in array, otherwise false.
bool rttr::variant_array::is_valid ( ) const

Returns true if this variant_array is valid, otherwise false.

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

Assigns the value of the other object to this variant_array.

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

Assigns the value of the other variant_array to this variant_array.

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

Assigns the value of the other variant_array to this variant_array.

Returns
A reference to the variant_array with the new data.
bool rttr::variant_array::remove_value ( std::size_t  index_1)

Removes the value at index index_1 in the first dimension of the array.

Returns
True if the value could be removed, otherwise false.
bool rttr::variant_array::remove_value ( std::size_t  index_1,
std::size_t  index_2 
)

Removes the value at index index_2 in the second dimension of the array relative to the first dimension at index index_1.

Returns
True if the value could be removed, otherwise false.
bool rttr::variant_array::remove_value ( std::size_t  index_1,
std::size_t  index_2,
std::size_t  index_3 
)

Removes the value at index index_3 in the third dimension of the array.

relative to the first dimension at index index_1 and the second dimension at index index_2.

Returns
True if the value could be removed, otherwise false.
bool rttr::variant_array::remove_value_variadic ( const std::vector< std::size_t > &  index_list)

Removes the value at index index_1 in the first dimension of the array.

Returns
True if the value could be removed, otherwise false.
bool rttr::variant_array::set_size ( std::size_t  new_size)

Sets the size of the array at the first dimension to new_size.

Returns
True, when the size of the array could be changed, otherwise false.
bool rttr::variant_array::set_size ( std::size_t  new_size,
std::size_t  index_1 
)

Sets the size of the array at the second dimension relative to the first dimension at index index_1 to new_size.

Returns
True, when the size of the array could be changed, otherwise false.
bool rttr::variant_array::set_size ( std::size_t  new_size,
std::size_t  index_1,
std::size_t  index_2 
)

Sets the size of the array at the third dimension relative to the first dimension at index index_1 and the second dimension at index index_2 to new_size.

Returns
True, when the size of the array could be changed, otherwise false.
bool rttr::variant_array::set_size_variadic ( std::size_t  new_size,
const std::vector< std::size_t > &  index_list 
)

Sets the size of the array at the specified indices in list index_list.

The index count specify the array dimension.

Returns
True, when the size of the array could be changed, otherwise false.
bool rttr::variant_array::set_value ( detail::argument  arg)

Copies the content of the the array arg into the underlying array.

Returns
True if the value could be set, otherwise false.
bool rttr::variant_array::set_value ( std::size_t  index_1,
detail::argument  arg 
)

Set the content of the the argument arg into the in the first dimension of the array at index index_1.

Returns
True if the value could be set, otherwise false.
bool rttr::variant_array::set_value ( std::size_t  index_1,
std::size_t  index_2,
detail::argument  arg 
)

Set the content of the the argument arg into the in the second dimension at index_2 of the array relative to the first dimension at index_1.

Returns
True if the value could be set, otherwise false.
bool rttr::variant_array::set_value ( std::size_t  index_1,
std::size_t  index_2,
std::size_t  index_3,
detail::argument  arg 
)

Set the content of the the argument arg into the in the third dimension at index_3 of the array relative to the first dimension at index_1 and second dimension at index_2.

Returns
True if the value could be set, otherwise false.
bool rttr::variant_array::set_value_variadic ( const std::vector< std::size_t > &  index_list,
detail::argument  arg 
)

Set the content of the the argument arg into the array in the n-th dimension given in the list index_list.

Remarks
Use this function when you want to set a value into a dimension which is bigger then three. Otherwise use the corresponding functions of set_value() .
Returns
True if the value could be set, otherwise false.
void rttr::variant_array::swap ( variant_array other)

Swaps this variant_array with the other variant_array.


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