rttr::variant_sequential_view Class Reference

The variant_sequential_view describes a class that refers to an sequence container (e.g: std::vector) inside a variant. More...

#include <variant_sequential_view.h>

Classes

class  const_iterator
 The variant_sequential_view::const_iterator allows iteration over an sequential container in a variant. More...
 

Public Member Functions

 variant_sequential_view ()
 Constructs an invalid variant_sequential_view object. More...
 
 variant_sequential_view (const variant_sequential_view &other)
 Constructs a copy of the given variant_sequential_view other. More...
 
 ~variant_sequential_view () noexcept
 Destroys the variant_sequential_view. More...
 
const_iterator begin () const
 Returns an iterator to the first element of the container. More...
 
void clear ()
 Removes all elements from the container. More...
 
const_iterator end () const
 Returns an iterator to the element following the last element of the container. More...
 
const_iterator erase (const const_iterator &pos)
 Removes the element (if one exists) at the position pos. More...
 
std::size_t get_rank () const noexcept
 Gets the rank (number of dimensions) of this sequential container. More...
 
type get_rank_type (std::size_t index) const noexcept
 Gets the type of the given rank index. More...
 
std::size_t get_size () const noexcept
 Returns the number of elements in the sequential container. More...
 
type get_type () const noexcept
 Returns the type object of this sequential container. More...
 
variant get_value (std::size_t index) const
 Returns the current value at index index. More...
 
type get_value_type () const noexcept
 Returns the type object from the value of this sequential container. More...
 
const_iterator insert (const const_iterator &pos, argument value)
 Insert a value into the container. More...
 
bool is_dynamic () const noexcept
 Returns true if this sequential view is dynamic, otherwise false. More...
 
bool is_empty () const noexcept
 Checks if the container has no elements. More...
 
bool is_valid () const noexcept
 Returns true if this variant_sequential_view is valid, that means the object is holding some data. More...
 
 operator bool () const noexcept
 Convenience function to check if this variant_sequential_view is valid or not. More...
 
variant_sequential_viewoperator= (const variant_sequential_view &other) noexcept
 Assigns the value of the other variant_sequential_view to this variant_sequential_view. More...
 
bool set_size (std::size_t size) const noexcept
 Sets the size of the sequential container. More...
 
bool set_value (std::size_t index, argument arg)
 Set the content of the the argument arg at the specified index index into the underlying sequential container. More...
 
void swap (variant_sequential_view &other) noexcept
 Swaps this variant_sequential_view with the other variant_sequential_view. More...
 

Detailed Description

The variant_sequential_view describes a class that refers to an sequence container (e.g: std::vector) inside a variant.

With an instance of that class you can set/get values of such container, without having access to the type declaration of the type or it's elements.

A variant_sequential_view can be created directly from a variant with its member function create_sequential_view().

Remarks
The instance of an variant_sequential_view is always valid as long as the referenced variant is valid, otherwise accessing a variant_sequential_view is undefined behaviour.

Meta Information

RTTR recognize whether a type is an sequential container or not with the help of the sequential_container_mapper class template. This call can access different container types via one common interface. At the moment there exist specializations for following types: std::vector<T>, std::array<T, std::size_t>, std::list<T>, std::deque<T>, std::initializer_list<T> and raw arrays

Copying and Assignment

A variant_sequential_view object can be copied and assigned, however each copy will reference the data of same underlying variant value.

Typical Usage

std::vector<int> my_vec = { 1, 2, 3, 4, 5};
variant var = my_vec; // copies data into variant
if (var.is_sequential_container())
{
variant_sequential_view view = var.create_sequential_view(); // performs no copy of the underlying vector
std::cout << view.get_size() << std::endl; // prints: '5'
for (const auto& item : view)
{
// remark that the value is stored inside a 'std::reference_wrapper', however there is an automatic conversion for wrapper classes implemented.
std::cout << "data: " << item.to_string() << " ";
}
}

Output:

1 2 3 4 5
See also
variant, type::is_sequential_container()

Constructor & Destructor Documentation

rttr::variant_sequential_view::variant_sequential_view ( )

Constructs an invalid variant_sequential_view object.

See also
is_valid()
rttr::variant_sequential_view::variant_sequential_view ( const variant_sequential_view other)

Constructs a copy of the given variant_sequential_view other.

rttr::variant_sequential_view::~variant_sequential_view ( )
noexcept

Destroys the variant_sequential_view.

Remarks
The underlying data is not destroyed.

Member Function Documentation

const_iterator rttr::variant_sequential_view::begin ( ) const

Returns an iterator to the first element of the container.

See also
end()
Returns
Iterator to the first element .
void rttr::variant_sequential_view::clear ( )

Removes all elements from the container.

Remarks
Invalidates all references, pointers, or iterators referring to contained elements.
const_iterator rttr::variant_sequential_view::end ( ) const

Returns an iterator to the element following the last element of the container.

See also
begin()
Returns
Iterator to the element following the last element.
const_iterator rttr::variant_sequential_view::erase ( const const_iterator pos)

Removes the element (if one exists) at the position pos.

Returns
Iterator following the last removed element.
std::size_t rttr::variant_sequential_view::get_rank ( ) const
noexcept

Gets the rank (number of dimensions) of this sequential container.

Take a look at following return values:

  • int[4] => 1
  • int[4][4] => 2
  • int[4][4][4] => 3
  • ...
Returns
Returns the rank of the sequential container.
type rttr::variant_sequential_view::get_rank_type ( std::size_t  index) const
noexcept

Gets the type of the given rank index.

Take a look at following return value for an array of type: int[2][10]

  • get_rank_type(0) => int[2][10]
  • get_rank_type(1) => int[10]
  • get_rank_type(2) => int
  • get_rank_type(3) => INVALID
Returns
The rank type at the given dimension index.
std::size_t rttr::variant_sequential_view::get_size ( ) const
noexcept

Returns the number of elements in the sequential container.

Returns
The number of elements in the sequential container.
type rttr::variant_sequential_view::get_type ( ) const
noexcept

Returns the type object of this sequential container.

Remarks
When the view is not valid, this function will return an invalid type object.
Returns
Type of the sequential container.
variant rttr::variant_sequential_view::get_value ( std::size_t  index) const

Returns the current value at index index.

Returns
The data at the specified index index, wrapped inside a std::reference_wrapper<T>.
Remarks
Make sure the index is in a valid range, otherwise undefined behaviour may occurr. For MSVC 2013 and raw arrays, the address of the data is returned (because of a bug).
See also
get_size()
type rttr::variant_sequential_view::get_value_type ( ) const
noexcept

Returns the type object from the value of this sequential container.

Remarks
When the view is not valid, this function will return an invalid type object.
Returns
Type from the value of the sequential container.
const_iterator rttr::variant_sequential_view::insert ( const const_iterator pos,
argument  value 
)

Insert a value into the container.

Returns
An iterator to the inserted element, otherwise an invalid iterator, when the insertion was not possible.
bool rttr::variant_sequential_view::is_dynamic ( ) const
noexcept

Returns true if this sequential view is dynamic, otherwise false.

When an sequential view is dynamic, it is possible to change its size, clear its content or insert and erase values from it.

See also
set_size(), insert(), erase(), clear()
Returns
A boolean flag which indicates whether this sequential container is dynamic or not.
bool rttr::variant_sequential_view::is_empty ( ) const
noexcept

Checks if the container has no elements.

Returns
true if container is empty, otherwise false.
bool rttr::variant_sequential_view::is_valid ( ) const
noexcept

Returns true if this variant_sequential_view is valid, that means the object is holding some data.

When the variant_sequential_view doesn't hold any data it will return false.

Returns
True if this variant_sequential_view is valid, otherwise false.
rttr::variant_sequential_view::operator bool ( ) const
explicitnoexcept

Convenience function to check if this variant_sequential_view is valid or not.

See also
is_valid()
Returns
True if this variant_sequential_view is valid, otherwise false.
variant_sequential_view& rttr::variant_sequential_view::operator= ( const variant_sequential_view other)
noexcept

Assigns the value of the other variant_sequential_view to this variant_sequential_view.

Returns
A reference to the variant_sequential_view with the new data.
bool rttr::variant_sequential_view::set_size ( std::size_t  size) const
noexcept

Sets the size of the sequential container.

Returns
true, when the size of the container could be changed, otherwise false.
See also
is_dynamic()
bool rttr::variant_sequential_view::set_value ( std::size_t  index,
argument  arg 
)

Set the content of the the argument arg at the specified index index into the underlying sequential container.

Returns
true if the value could be set, otherwise false.
void rttr::variant_sequential_view::swap ( variant_sequential_view other)
noexcept

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