mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
servlet::any_map< _MapType > Class Template Reference

Covenience class on top of associative container to facilitate work with value type std::any. More...

#include <any_map.h>

Inheritance diagram for servlet::any_map< _MapType >:
servlet::http_session

Public Types

typedef _MapType map_type
 Type definition for this map implementation.
 
typedef map_type::key_type key_type
 Container's key type.
 
typedef map_type::mapped_type mapped_type
 Container's mapped type.
 
typedef map_type::value_type value_type
 Container's value type: std::pair<const key_type, mapped_type>
 
typedef map_type::allocator_type allocator_type
 Container's allocator type.
 
typedef value_typereference
 value_type&
 
typedef const value_typeconst_reference
 const value_type&
 
typedef map_type::pointer pointer
 Pointer type to value_type.
 
typedef map_type::const_pointer const_pointer
 Constant pointer type to value_type.
 
typedef map_type::size_type size_type
 An unsigned integral type to represent the size of this container.
 
typedef map_type::difference_type difference_type
 A signed integral type to represent distance between iterators.
 
typedef map_type::iterator iterator
 Bidirectional iterator type.
 
typedef map_type::const_iterator const_iterator
 Bidirectional constant iterator type.
 
typedef map_type::reverse_iterator reverse_iterator
 Reverse iterator type.
 
typedef
map_type::const_reverse_iterator 
const_reverse_iterator
 Constant reverse iterator type.
 

Public Member Functions

 any_map ()=default
 Constructs an empty container, with no elements.
 
template<typename... Args>
 any_map (Args &&...args)
 Forwarding constructor. More...
 
 ~any_map ()=default
 Destroys the object.
 
template<typename... Args>
any_mapoperator= (Args &&...args)
 Forwarding assignment. More...
 
template<typename KeyType >
bool contains_key (const KeyType &key) const
 Tests whether value with a given key exists in this container. More...
 
template<typename T , typename KeyType >
optional_ref< const T > get (const KeyType &key) const
 Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<typename T , typename KeyType >
optional_ref< T > get (const KeyType &key)
 Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<typename T , typename... Args>
T & ensure_get (key_type &&key, Args &&...args)
 Returns reference to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<typename T , typename... Args>
T & ensure_get (const key_type &key, Args &&...args)
 Returns reference to a value with a specified type, if that value exists and can be casted to the requested type. More...
 
template<typename T , typename... Args>
bool put (key_type &&key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 
template<typename T , typename... Args>
bool put (const key_type &key, Args &&...args)
 Associates a value of specified type created with a given arguments with the specified key in this map. More...
 

Detailed Description

template<typename _MapType>
class servlet::any_map< _MapType >

Covenience class on top of associative container to facilitate work with value type std::any.

This class implements several convenience method to make it easy to use map. The methods are:

  1. contains_key - checks if the record with a given key exists

  2. get - returns optional_ref of the value.

  3. ensure_get - returns the value reference for a given key and if the record doesn't exist new record is created and reference to the value returned

  4. put - inserts or replaces the value for a given key

This class inherits either std::map or std::unordered_map specified as template parameter.

All the regular std::map or std::unordered_map methods are inherited as is.

In general this object can be used in folowing way:

tree_any_map m;
m.ensure_get<std::vector<std::string>>("vector").push_back("value");
std::cout << m.get<std::vector<std::string>>("vector")->size() << std::endl;
m.ensure_get<int>("int") = 3;
std::cout << m.get<int>("int") << std::endl;
m.put<double>("int", 3.14);
std::cout << m.get<double>("int") << std::endl;

The above program will output:

1
3
3.14
Template Parameters
_MapTypebase map type from which this class inherits. For now it can be either std::map of std::unordered_map.
See Also
tree_any_map
hash_any_map
Todo:
reimplement new value creations when any constructors with in_place_type_t are available in the std lib.

Constructor & Destructor Documentation

template<typename _MapType >
template<typename... Args>
servlet::any_map< _MapType >::any_map ( Args &&...  args)
inline

Forwarding constructor.

It forwards all arguments to the underlying map constructor.

Template Parameters
Argstypes of the arguments to forward
Parameters
argsArguments to forward to the underlying map constructor

Member Function Documentation

template<typename _MapType >
template<typename KeyType >
bool servlet::any_map< _MapType >::contains_key ( const KeyType &  key) const
inline

Tests whether value with a given key exists in this container.

Template Parameters
KeyTypea type comparable to std::string
Parameters
keyKey to test.
Returns
true if a value with a given key exists in this container, false otherwise.
template<typename _MapType >
template<typename T , typename... Args>
T& servlet::any_map< _MapType >::ensure_get ( key_type &&  key,
Args &&...  args 
)
inline

Returns reference to a value with a specified type, if that value exists and can be casted to the requested type.

If the value with a given key doesn't exists new value will be created and emplaced into this container with a given key.

Template Parameters
Ttype of the value to return
Argstypes of the arguments to construct a mapped value.
Parameters
keyKey to be searched for.
argsargument to create the mapped value if it doesn't exist
Returns
reference to the found or created value.
Exceptions
std::bad_any_castif the value is found, but couldn't be casted to the requested type
template<typename _MapType >
template<typename T , typename... Args>
T& servlet::any_map< _MapType >::ensure_get ( const key_type key,
Args &&...  args 
)
inline

Returns reference to a value with a specified type, if that value exists and can be casted to the requested type.

If the value with a given key doesn't exists new value will be created and emplaced into this container with a given key.

Template Parameters
Ttype of the value to associate with the key
Argstypes of the arguments to construct a mapped value.
Parameters
keyKey to be searched for.
argsargument to create the mapped value if it doesn't exist
Returns
reference to the found or created value.
Exceptions
std::bad_any_castif the value is found, but couldn't be casted to the requested type
template<typename _MapType >
template<typename T , typename KeyType >
optional_ref<const T> servlet::any_map< _MapType >::get ( const KeyType &  key) const
inline

Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type.

If the value with a given key doesn't exists empty optional_ref will be returned.

Template Parameters
KeyTypea type comparable to std::string
Ttype of the value to return
Parameters
keyKey to be searched for.
Returns
optional_ref to the found value, or empty reference if a value with a given key doesn't exists in this container.
Exceptions
bad_any_castif the value is found, but couldn't be casted to the requested type
template<typename _MapType >
template<typename T , typename KeyType >
optional_ref<T> servlet::any_map< _MapType >::get ( const KeyType &  key)
inline

Returns optional_ref object to a value with a specified type, if that value exists and can be casted to the requested type.

If the value with a given key doesn't exists empty optional_ref will be returned.

Template Parameters
KeyTypea type comparable to std::string
Ttype of the value to return
Parameters
keyKey to be searched for.
Returns
optional_ref to the found value, or empty reference if a value with a given key doesn't exists in this container.
Exceptions
std::bad_any_castif the value is found, but couldn't be casted to the requested type
template<typename _MapType >
template<typename... Args>
any_map& servlet::any_map< _MapType >::operator= ( Args &&...  args)
inline

Forwarding assignment.

It forwards all arguments to the underlying map assignment operator.

Template Parameters
Argstypes of the arguments to forward
Parameters
argsArguments to forward to the underlying map constructor
Returns
reference to self
template<typename _MapType >
template<typename T , typename... Args>
bool servlet::any_map< _MapType >::put ( key_type &&  key,
Args &&...  args 
)
inline

Associates a value of specified type created with a given arguments with the specified key in this map.

If the map previously contained a mapping for the key, the old value is replaced.

Template Parameters
Ttype of the value to associate with the key
Argstypes of the arguments to construct a mapped value.
Parameters
keykey with which the specified value is to be associated
argsargument to create the mapped value
Returns
bool denoting whether the insertion took place.
template<typename _MapType >
template<typename T , typename... Args>
bool servlet::any_map< _MapType >::put ( const key_type key,
Args &&...  args 
)
inline

Associates a value of specified type created with a given arguments with the specified key in this map.

If the map previously contained a mapping for the key, the old value is replaced.

Template Parameters
Ttype of the value to associate with the key
Argstypes of the arguments to construct a mapped value.
Parameters
keykey with which the specified value is to be associated
argsargument to create the mapped value
Returns
bool denoting whether the insertion took place.

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