mod_servlet
C++Servlets
 All Classes Files Functions Variables Typedefs Macros Pages
any_map.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2016 Alexei Novakov
3 https://github.com/novalexei
4 
5 Distributed under the Boost Software License, Version 1.0.
6 http://boost.org/LICENSE_1_0.txt
7 */
8 #ifndef MOD_SERVLET_ANY_MAP_H
9 #define MOD_SERVLET_ANY_MAP_H
10 
17 #include <map>
18 #include <unordered_map>
19 #include <experimental/any>
20 
21 #include <servlet/lib/optional.h>
22 
23 namespace servlet
24 {
25 
26 using std::experimental::any;
27 using std::experimental::any_cast;
28 
83 template<typename _MapType>
84 class any_map : public _MapType
85 {
86 public:
90  typedef _MapType map_type;
91 
95  typedef typename map_type::key_type key_type;
99  typedef typename map_type::mapped_type mapped_type;
103  typedef typename map_type::value_type value_type;
107  typedef typename map_type::allocator_type allocator_type;
115  typedef const value_type& const_reference;
119  typedef typename map_type::pointer pointer;
123  typedef typename map_type::const_pointer const_pointer;
127  typedef typename map_type::size_type size_type;
131  typedef typename map_type::difference_type difference_type;
132 
136  typedef typename map_type::iterator iterator;
140  typedef typename map_type::const_iterator const_iterator;
144  typedef typename map_type::reverse_iterator reverse_iterator;
148  typedef typename map_type::const_reverse_iterator const_reverse_iterator;
149 
153  any_map() = default;
160  template<typename... Args>
161  any_map(Args &&... args) : map_type{std::forward<Args>(args)...} {}
162 
166  ~any_map() = default;
167 
175  template<typename... Args>
176  any_map& operator=(Args &&... args) { map_type::operator=(std::forward<Args>(args)...); return *this; }
177 
185  template<typename KeyType>
186  bool contains_key(const KeyType &key) const { return this->find(key) != this->end(); }
187 
202  template<typename T, typename KeyType>
203  optional_ref<const T> get(const KeyType& key) const
204  {
205  auto it = this->find(key);
206  return it == this->end() ? optional_ref<const T>{} : optional_ref<const T>{any_cast<const T&>(it->second)};
207  }
222  template<typename T, typename KeyType>
223  optional_ref<T> get(const KeyType& key)
224  {
225  auto it = this->find(key);
226  return it == this->end() ? optional_ref<T>{} : optional_ref<T>{any_cast<T&>(it->second)};
227  }
228 
243  template<typename T, typename... Args>
244  T& ensure_get(key_type&& key, Args &&... args)
245  {
246  std::pair<iterator, bool> res = this->try_emplace(std::move(key));
247  if (res.second) res.first->second = T{std::forward<Args>(args)...};
248  return any_cast<T&>(res.first->second);
249  }
264  template<typename T, typename... Args>
265  T& ensure_get(const key_type& key, Args &&... args)
266  {
267  std::pair<iterator, bool> res = this->try_emplace(key);
268  if (res.second) res.first->second = T{std::forward<Args>(args)...};
269  return any_cast<T&>(res.first->second);
270  }
271 
282  template<typename T, typename... Args>
283  bool put(key_type&& key, Args &&... args)
284  {
285  std::pair<iterator, bool> res = this->try_emplace(std::move(key));
286  res.first->second = T{std::forward<Args>(args)...};
287  return res.second;
288  }
299  template<typename T, typename... Args>
300  bool put(const key_type& key, Args &&... args)
301  {
302  std::pair<iterator, bool> res = this->try_emplace(key);
303  res.first->second = T{std::forward<Args>(args)...};
304  return res.second;
305  }
306 };
307 
311 template <typename _Key = std::string, typename _Compare = std::less<>,
312  typename _Alloc = std::allocator<std::pair<const _Key, any>>>
314 
320 
324 template <typename _Key = std::string, typename _Hash = std::hash<_Key>,
325  typename _Pred = std::equal_to<_Key>, typename _Alloc = std::allocator<std::pair<const _Key, any>>>
327 
333 
334 } // end of servlet namespace
335 
336 #endif // MOD_SERVLET_ANY_MAP_H
_MapType map_type
Type definition for this map implementation.
Definition: any_map.h:90
map_type::allocator_type allocator_type
Container's allocator type.
Definition: any_map.h:107
~any_map()=default
Destroys the object.
bool put(key_type &&key, Args &&...args)
Associates a value of specified type created with a given arguments with the specified key in this ma...
Definition: any_map.h:283
Defines optional container objects and related methods.
map_type::size_type size_type
An unsigned integral type to represent the size of this container.
Definition: any_map.h:127
map_type::const_pointer const_pointer
Constant pointer type to value_type.
Definition: any_map.h:123
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 ma...
Definition: any_map.h:300
map_type::const_iterator const_iterator
Bidirectional constant iterator type.
Definition: any_map.h:140
Optional reference implementation.
Definition: optional.h:221
bool contains_key(const KeyType &key) const
Tests whether value with a given key exists in this container.
Definition: any_map.h:186
const value_type & const_reference
const value_type&
Definition: any_map.h:115
Covenience class on top of associative container to facilitate work with value type std::any...
Definition: any_map.h:84
map_type::mapped_type mapped_type
Container's mapped type.
Definition: any_map.h:99
map_type::iterator iterator
Bidirectional iterator type.
Definition: any_map.h:136
any_map()=default
Constructs an empty container, with no elements.
map_type::difference_type difference_type
A signed integral type to represent distance between iterators.
Definition: any_map.h:131
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 req...
Definition: any_map.h:265
map_type::key_type key_type
Container's key type.
Definition: any_map.h:95
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 req...
Definition: any_map.h:244
map_type::pointer pointer
Pointer type to value_type.
Definition: any_map.h:119
map_type::reverse_iterator reverse_iterator
Reverse iterator type.
Definition: any_map.h:144
value_type & reference
value_type&
Definition: any_map.h:111
map_type::value_type value_type
Container's value type: std::pair<const key_type, mapped_type>
Definition: any_map.h:103
any_map & operator=(Args &&...args)
Forwarding assignment.
Definition: any_map.h:176
any_map(Args &&...args)
Forwarding constructor.
Definition: any_map.h:161
map_type::const_reverse_iterator const_reverse_iterator
Constant reverse iterator type.
Definition: any_map.h:148