rttr::variant_associative_view Class Reference

The variant_associative_view describes a class that refers to an associative container (e.g: std::map) inside a variant. More...

#include <variant_associative_view.h>

Classes

class  const_iterator
 The variant_associative_view::const_iterator allows iteration over an associative container in a variant. More...
 

Public Member Functions

 variant_associative_view ()
 Constructs an invalid variant_associative_view object. More...
 
 variant_associative_view (const variant_associative_view &other)
 Constructs a copy of the given variant_associative_view other. More...
 
 ~variant_associative_view () noexcept
 Destroys the variant_associative_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...
 
std::pair< const_iterator, const_iteratorequal_range (argument key)
 Returns a range containing all elements with the given key in the container. More...
 
std::size_t erase (argument key)
 Removes the element (if one exists) with the key equivalent to key. More...
 
const_iterator find (argument key)
 Finds an element with specific key key . More...
 
type get_key_type () const noexcept
 Returns the type from the key of this associative container. More...
 
std::size_t get_size () const noexcept
 Returns the number of elements in the associative container. More...
 
type get_type () const noexcept
 Returns the type object of this associative container. More...
 
type get_value_type () const noexcept
 Returns the type object from the value of this associative container. More...
 
std::pair< const_iterator, bool > insert (argument key)
 Insert a key into the container. More...
 
std::pair< const_iterator, bool > insert (argument key, argument value)
 Insert a key-value pair into the container. More...
 
bool is_empty () const noexcept
 Checks if the container has no elements. More...
 
bool is_key_only_type () const noexcept
 Returns true, when this associative container stores only keys. More...
 
bool is_valid () const noexcept
 Returns true if this variant_associative_view is valid, that means the object is holding some data. More...
 
 operator bool () const noexcept
 Convenience function to check if this variant_associative_view is valid or not. More...
 
variant_associative_viewoperator= (const variant_associative_view &other) noexcept
 Assigns the value of the other variant_associative_view to this variant_associative_view. More...
 
void swap (variant_associative_view &other) noexcept
 Swaps this variant_associative_view with the other variant_associative_view. More...
 

Detailed Description

The variant_associative_view describes a class that refers to an associative container (e.g: std::map) 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_associative_view can be created directly from a variant with its member function create_associative_view().

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

Meta Information

RTTR recognize whether a type is an associative container or not with the help of the associative_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::set<Key>, std::map<Key, T>, std::multiset<Key>, std::multimap<Key, T>, std::unordered_set<Key>, std::unordered_map<Key, T>, std::unordered_multiset<Key> and std::unordered_multimap<Key, T>.

Copying and Assignment

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

Typical Usage

std::map<int, std::string> my_map = { { 1, "one" }, { 2, "two" }, { 3, "three" } };
variant var = my_map;
if (var.is_associative_container())
{
variant_associative_view view = var.create_associative_view();
std::cout << view.get_size() << std::endl; // prints: '3'
for (const auto& item : view)
{
// remark that the key and value are stored inside a 'std::reference_wrapper'
std::cout << "Key: " << item.first.extract_wrapped_value().to_string() << " ";
std::cout << "Value: " << item.second.extract_wrapped_value().to_string() << std::endl;
}
}
See also
variant

Constructor & Destructor Documentation

rttr::variant_associative_view::variant_associative_view ( )

Constructs an invalid variant_associative_view object.

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

Constructs a copy of the given variant_associative_view other.

rttr::variant_associative_view::~variant_associative_view ( )
noexcept

Destroys the variant_associative_view.

Remarks
The underlying data is not destroyed.

Member Function Documentation

const_iterator rttr::variant_associative_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_associative_view::clear ( )

Removes all elements from the container.

Remarks
Invalidates all references, pointers, or iterators referring to contained elements.
const_iterator rttr::variant_associative_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.
std::pair<const_iterator, const_iterator> rttr::variant_associative_view::equal_range ( argument  key)

Returns a range containing all elements with the given key in the container.

Example code:

auto multimap = std::multimap<int, std::string>{ { 1, "A" }, { 2, "B" }, { 2, "C"}, { 2, "D" },
{ 3, "E" }, { 3, "F" } };
variant var = multimap;
auto view = var.create_associative_view();
for (int i = 1; i <= 3; ++i)
{
std::cout << i << " =>";
auto range = view.equal_range(i);
for (auto itr = range.first; itr != range.second; ++itr)
{
std::cout << " " << itr.value().extract_wrapped_value().to_string();
}
std::cout << std::endl;
}

Output:

1 => A
2 => B C D
3 => E, F
Returns
std::pair containing a pair of iterators defining the wanted range: the first pointing to the first element that is not less than key and the second pointing to the first element greater than key.
std::size_t rttr::variant_associative_view::erase ( argument  key)

Removes the element (if one exists) with the key equivalent to key.

Returns
The number of elements removed.
const_iterator rttr::variant_associative_view::find ( argument  key)

Finds an element with specific key key .

Returns
The element with key equivalent to key. If no element is found an invalid iterator is returned.
type rttr::variant_associative_view::get_key_type ( ) const
noexcept

Returns the type from the key of this associative container.

Remarks
When the view is not valid, this function will return an invalid type object.
Returns
Type from the key of the associative container.
std::size_t rttr::variant_associative_view::get_size ( ) const
noexcept

Returns the number of elements in the associative container.

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

Returns the type object of this associative container.

Remarks
When the view is not valid, this function will return an invalid type object.
Returns
Type of the associative container.
type rttr::variant_associative_view::get_value_type ( ) const
noexcept

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

Remarks
When the view is not valid, this function will return an invalid type object.
Returns
Type from the value of the associative container.
std::pair<const_iterator, bool> rttr::variant_associative_view::insert ( argument  key)

Insert a key into the container.

Returns
A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.
std::pair<const_iterator, bool> rttr::variant_associative_view::insert ( argument  key,
argument  value 
)

Insert a key-value pair into the container.

Returns
A pair consisting of an iterator to the inserted element (or to the element that prevented the insertion) and a bool denoting whether the insertion took place.
bool rttr::variant_associative_view::is_empty ( ) const
noexcept

Checks if the container has no elements.

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

Returns true, when this associative container stores only keys.

When also value are stored, it will return false.

For example an std::set has no values, it's a key-only associative container. Then this method returns true. An std::map<K,V> on the other hand contains keys and values. So this method will then return false. A simple convenience method instead of calling: get_value_type().is_valid() == false;

Returns
true, when this associative container stores only keys; otherwise false.
bool rttr::variant_associative_view::is_valid ( ) const
noexcept

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

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

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

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

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

Assigns the value of the other variant_associative_view to this variant_associative_view.

Returns
A reference to the variant_associative_view with the new data.
void rttr::variant_associative_view::swap ( variant_associative_view other)
noexcept

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