Covenience class on top of associative container to facilitate work with value type std::any
.
More...
|
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_type & | reference |
| value_type&
|
|
typedef const value_type & | const_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.
|
|
|
| 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_map & | operator= (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...
|
|
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:
-
contains_key - checks if the record with a given key exists
-
get - returns optional_ref of the value.
-
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
-
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
-
_MapType | base 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.
template<typename _MapType >
template<typename T , typename... Args>
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
-
T | type of the value to return |
Args | types of the arguments to construct a mapped value. |
- Parameters
-
key | Key to be searched for. |
args | argument to create the mapped value if it doesn't exist |
- Returns
- reference to the found or created value.
- Exceptions
-
std::bad_any_cast | if the value is found, but couldn't be casted to the requested type |
template<typename _MapType >
template<typename T , typename... Args>
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
-
T | type of the value to associate with the key |
Args | types of the arguments to construct a mapped value. |
- Parameters
-
key | Key to be searched for. |
args | argument to create the mapped value if it doesn't exist |
- Returns
- reference to the found or created value.
- Exceptions
-
std::bad_any_cast | if the value is found, but couldn't be casted to the requested type |
template<typename _MapType >
template<typename T , typename... Args>
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
-
T | type of the value to associate with the key |
Args | types of the arguments to construct a mapped value. |
- Parameters
-
key | key with which the specified value is to be associated |
args | argument to create the mapped value |
- Returns
bool
denoting whether the insertion took place.
template<typename _MapType >
template<typename T , typename... Args>
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
-
T | type of the value to associate with the key |
Args | types of the arguments to construct a mapped value. |
- Parameters
-
key | key with which the specified value is to be associated |
args | argument to create the mapped value |
- Returns
bool
denoting whether the insertion took place.