rttr::associative_container_mapper< T > Struct Template - 0.9.6 |RTTR
rttr::associative_container_mapper< T > Struct Template Reference

The associative_container_mapper class is a class template to access an associative container via one common interface. More...

#include <associative_mapper.h>

Public Types

using const_itr_t = typename T::const_iterator
 An alias delcaration to the const iterator. More...
 
using container_t = T
 An alias declaration to the container type itself. More...
 
using itr_t = typename T::iterator
 An alias delcaration to the iterator. More...
 
using key_t = typename T::key_type
 An alias to the key type. More...
 
using value_t = typename T::mapped_type
 

Static Public Member Functions

static itr_t begin (container_t &container)
 Returns an iterator to the first element of the container. More...
 
static const_itr_t begin (const container_t &container)
 Returns an iterator to the first element of the container. More...
 
static void clear (container_t &container)
 Removes all elements from the container. More...
 
static itr_t end (container_t &container)
 Returns an iterator to the element following the last element of the container. More...
 
static const_itr_t end (const container_t &container)
 Returns an iterator to the element following the last element of the container. More...
 
static std::pair< itr_t, itr_tequal_range (container_t &container, const key_t &key)
 Returns a range containing all elements with the given key in the container. More...
 
static std::pair< const_itr_t, const_itr_tequal_range (const container_t &container, const key_t &key)
 Returns a range containing all elements with the given key in the container. More...
 
static std::size_t erase (container_t &container, const key_t &key)
 Removes the element (if one exists) with the key equivalent to key. More...
 
static itr_t find (container_t &container, const key_t &key)
 Finds an element with key equivalent to key and returns its iterator. More...
 
static const_itr_t find (const container_t &container, const key_t &key)
 Finds an element with key equivalent to key and returns its iterator. More...
 
static const key_tget_key (const const_itr_t &itr)
 Returns the current iterator's key as a const reference. More...
 
static std::size_t get_size (const container_t &container)
 Returns the number of elements in the container. More...
 
static value_tget_value (itr_t &itr)
 Returns the current iterator's value as reference. More...
 
static const value_tget_value (const const_itr_t &itr)
 Returns the current iterator's value as const reference. More...
 
static std::pair< itr_t, bool > insert_key (container_t &container, const key_t &key)
 Inserts a key into the container. More...
 
static std::pair< itr_t, bool > insert_key_value (container_t &container, const key_t &key, const value_t &value)
 Inserts a key-value into the container. More...
 
static bool is_empty (const container_t &container)
 Returns the number of elements in the container. More...
 

Detailed Description

template<typename T>
struct rttr::associative_container_mapper< T >

The associative_container_mapper class is a class template to access an associative container via one common interface.

This class will be only used internally by RTTR via the variant_associative_view class to get access to elements of an associative container. In order to use your own custom associative container type, you have to provide a specialization of this class.

Out of the box, RTTR has specialization for following associative container 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>
  • std::unordered_multimap<Key, T>

Custom associative container

For a specialization of the class associative_container_mapper<T> you have to provide some nested alias templates:

  1. using container_t = T;
  2. using key_t = typename T::key_type;
  3. using value_t = typename T::mapped_type;
    Remarks
    When you have a key-only container, like std::set<T>, use void; i.e. using value_t = void;
  4. using itr_t = typename T::iterator;
  5. using const_itr_t = typename T::const_iterator;

and following member functions:

  1. static const key_t& get_key(const const_itr_t& itr);
  2. static value_t& get_value(itr_t& itr);
  3. static const value_t& get_value(const const_itr_t& itr);
  4. static itr_t begin(container_t& container);
  5. static const_itr_t begin(const container_t& container);
  6. static const_itr_t end(const container_t& container);
  7. static itr_t find(container_t& container, const key_t& key);
  8. static const_itr_t find(const container_t& container, const key_t& key);
  9. static std::pair<itr_t, itr_t> equal_range(container_t& container, const key_t& key);
  10. static std::pair<const_itr_t, const_itr_t> equal_range(const container_t& container, const key_t& key);
  11. static void clear(container_t& container);
  12. static bool is_empty(const container_t& container);
  13. static std::size_t get_size(const container_t& container);
  14. static std::size_t erase(container_t& container, const key_t& key);
  15. static std::pair<itr_t, bool> insert_key(container_t& container, const key_t& key);
    Remarks
    This method needs to be implemented only when you have a key-only container.
  16. static std::pair<itr_t, bool> insert_key_value(container_t& container, const key_t& key, const value_t& value);
    Remarks
    This method needs to be implemented only when you have a key-value container.
    Following code example for the associative container QHash<K, T> illustrates how to add a specialization:
namespace rttr
{
template<typename K, typename T>
struct associative_container_mapper<QHash<K, T>>
{
using container_t = QHash<K, T>;
using key_t = typename QHash<K, T>::key_type;
using value_t = typename QHash<K, T>::mapped_type;
using itr_t = typename QHash<K, T>::iterator;
using const_itr_t = typename QHash<K, T>::const_iterator;
static const key_t& get_key(const const_itr_t& itr)
{
return itr.key();
}
static value_t& get_value(itr_t& itr)
{
return itr.value();
}
static const value_t& get_value(const const_itr_t& itr)
{
return itr.value();
}
static itr_t begin(container_t& container)
{
return container.begin();
}
static const_itr_t begin(const container_t& container)
{
return container.begin();
}
static itr_t end(container_t& container)
{
return container.end();
}
static const_itr_t end(const container_t& container)
{
return container.end();
}
static itr_t find(container_t& container, key_t& key)
{
return container.find(key);
}
static const_itr_t find(const container_t& container, key_t& key)
{
return container.find(key);
}
static std::pair<itr_t, itr_t> equal_range(container_t& container, key_t& key)
{
auto ret = container.equal_range(key);
return std::make_pair(ret.first, ret.second);
}
static std::pair<const_itr_t, const_itr_t> equal_range(const container_t& container, key_t& key)
{
auto ret = container.equal_range(key);
return std::make_pair(ret.first, ret.second);
}
static void clear(container_t& container)
{
container.clear();
}
static bool is_empty(const container_t& container)
{
return container.isEmpty();
}
static std::size_t get_size(const container_t& container)
{
return container.size();
}
static std::size_t erase(container_t& container, key_t& key)
{
return container.remove(key);
}
static std::pair<itr_t, bool> insert_key_value(container_t& container, key_t& key, value_t& value)
{
return std::make_pair(container.insert(key, value), true);
}
};
} // end namespace rttr
Remarks
Make sure you put your specialization inside the namespace rttr. The best place for this code, is below the declaration of your custom associative container type. When this is not possible, include your specialization code before registering your types to RTTR.

Member Typedef Documentation

template<typename T >
using rttr::associative_container_mapper< T >::const_itr_t = typename T::const_iterator

An alias delcaration to the const iterator.

template<typename T >
using rttr::associative_container_mapper< T >::container_t = T

An alias declaration to the container type itself.

template<typename T >
using rttr::associative_container_mapper< T >::itr_t = typename T::iterator

An alias delcaration to the iterator.

template<typename T >
using rttr::associative_container_mapper< T >::key_t = typename T::key_type

An alias to the key type.

template<typename T >
using rttr::associative_container_mapper< T >::value_t = typename T::mapped_type

An alias to the value type.

Remarks
When you have a key only container use void as value type. Then you also dont need to add a insert_key_value() function

Member Function Documentation

template<typename T >
static itr_t rttr::associative_container_mapper< T >::begin ( container_t container)
inlinestatic

Returns an iterator to the first element of the container.

template<typename T >
static const_itr_t rttr::associative_container_mapper< T >::begin ( const container_t container)
inlinestatic

Returns an iterator to the first element of the container.

template<typename T >
static void rttr::associative_container_mapper< T >::clear ( container_t container)
inlinestatic

Removes all elements from the container.

template<typename T >
static itr_t rttr::associative_container_mapper< T >::end ( container_t container)
inlinestatic

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

template<typename T >
static const_itr_t rttr::associative_container_mapper< T >::end ( const container_t container)
inlinestatic

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

template<typename T >
static std::pair<itr_t, itr_t> rttr::associative_container_mapper< T >::equal_range ( container_t container,
const key_t key 
)
inlinestatic

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

The range is defined by two iterators, one pointing to the first element that is not less than key and another pointing to the first element greater than key.

template<typename T >
static std::pair<const_itr_t, const_itr_t> rttr::associative_container_mapper< T >::equal_range ( const container_t container,
const key_t key 
)
inlinestatic

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

The range is defined by two constant iterators, one pointing to the first element that is not less than key and another pointing to the first element greater than key.

template<typename T >
static std::size_t rttr::associative_container_mapper< T >::erase ( container_t container,
const key_t key 
)
inlinestatic

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

template<typename T >
static itr_t rttr::associative_container_mapper< T >::find ( container_t container,
const key_t key 
)
inlinestatic

Finds an element with key equivalent to key and returns its iterator.

template<typename T >
static const_itr_t rttr::associative_container_mapper< T >::find ( const container_t container,
const key_t key 
)
inlinestatic

Finds an element with key equivalent to key and returns its iterator.

template<typename T >
static const key_t& rttr::associative_container_mapper< T >::get_key ( const const_itr_t itr)
inlinestatic

Returns the current iterator's key as a const reference.

template<typename T >
static std::size_t rttr::associative_container_mapper< T >::get_size ( const container_t container)
inlinestatic

Returns the number of elements in the container.

template<typename T >
static value_t& rttr::associative_container_mapper< T >::get_value ( itr_t itr)
inlinestatic

Returns the current iterator's value as reference.

template<typename T >
static const value_t& rttr::associative_container_mapper< T >::get_value ( const const_itr_t itr)
inlinestatic

Returns the current iterator's value as const reference.

template<typename T >
static std::pair<itr_t, bool> rttr::associative_container_mapper< T >::insert_key ( container_t container,
const key_t key 
)
inlinestatic

Inserts a key into the container.

Remarks
This method is only necessary, when you have a key-only container. Like std::set<T>. Otherwise you don't need to declare it.
template<typename T >
static std::pair<itr_t, bool> rttr::associative_container_mapper< T >::insert_key_value ( container_t container,
const key_t key,
const value_t value 
)
inlinestatic

Inserts a key-value into the container.

Remarks
This method is only necessary, when you have a key-value container. Like std::map<T>. Otherwise you don't need to declare it.
template<typename T >
static bool rttr::associative_container_mapper< T >::is_empty ( const container_t container)
inlinestatic

Returns the number of elements in the container.


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