identt
SharedMap.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_UTILS_SHARED_MAP_HPP_
34 #define _IDENTT_UTILS_SHARED_MAP_HPP_
35 
36 #include <unordered_map>
37 #include <utility>
38 #include <functional>
39 #include <boost/thread/locks.hpp>
40 #include <boost/thread/shared_mutex.hpp>
41 
42 namespace identt {
43 namespace utils {
44 template<class KeyT,class ValueT>
45 class SharedMap {
46 public:
47  using MapT = std::unordered_map<KeyT,ValueT>;
48  using LockT = boost::shared_mutex;
49  using WriteLockT = boost::unique_lock< LockT >;
50  using ReadLockT = boost::shared_lock< LockT >;
51 
56  SharedMap(const SharedMap&) = delete;
57  SharedMap& operator=(const SharedMap&) = delete;
58 
63  SharedMap() {}
64 
68  virtual ~SharedMap () {}
69 
82  void AddOne(KeyT key, ValueT value)
83  {
84  WriteLockT writelock(mutex_);
85  t_[key]=value;
86  }
87 
97  void AddMany(const MapT& m)
98  {
99  WriteLockT writelock(mutex_);
100  for (auto& p : m) {
101  t_[p.first]=p.second;
102  }
103  }
104 
114  void AddMany(MapT&& m)
115  {
116  WriteLockT writelock(mutex_);
117  for (auto& p : m) {
118  t_[p.first]=p.second;
119  }
120  }
121 
134  bool GetValues(KeyT& key, ValueT& value)
135  {
136  ReadLockT readlock(mutex_);
137  typename MapT::const_iterator it = t_.find(key);
138  bool stat = (it!=t_.end());
139  value = stat ? it.second : ValueT();
140  return stat;
141  }
142 
155  bool GetValues(KeyT&& key, ValueT& value)
156  {
157  ReadLockT readlock(mutex_);
158  typename MapT::const_iterator it = t_.find(key);
159  bool stat = (it!=t_.end());
160  value = stat ? it.second : ValueT();
161  return stat;
162  }
163 
170  MapT GetCopy()
171  {
172  ReadLockT readlock(mutex_);
173  return t_;
174  }
175 
182  size_t GetSize()
183  {
184  ReadLockT readlock(mutex_);
185  return t_.size();
186  }
187 
194  void Reset()
195  {
196  WriteLockT writelock(mutex_);
197  t_.clear();
198  }
199 
206  void Swap(MapT t)
207  {
208  WriteLockT writelock(mutex_);
209  t_.swap(t);
210  }
211 
212 
213 protected:
214  MapT t_;
215  LockT mutex_;
216 
217 };
218 } // namespace utils
219 } // namespace identt
220 #endif /* _IDENTT_UTILS_SHARED_MAP_HPP_ */
virtual ~SharedMap()
destructor
Definition: SharedMap.hpp:68
bool GetValues(KeyT &&key, ValueT &value)
GetValues : get values by lvalue ref.
Definition: SharedMap.hpp:155
SharedMap()
Constructor : default.
Definition: SharedMap.hpp:63
void Reset()
Reset: clear the set.
Definition: SharedMap.hpp:194
void AddMany(const MapT &m)
AddMany : add from map by address.
Definition: SharedMap.hpp:97
void AddOne(KeyT key, ValueT value)
AddOne : add one value.
Definition: SharedMap.hpp:82
void Swap(MapT t)
Swap : get by swap.
Definition: SharedMap.hpp:206
MapT GetCopy()
GetCopy : get a copy of the map.
Definition: SharedMap.hpp:170
Definition: SharedMap.hpp:45
Definition: CryptoBase.hpp:49
bool GetValues(KeyT &key, ValueT &value)
GetValues : get values by address.
Definition: SharedMap.hpp:134
size_t GetSize()
GetSize: get size of set.
Definition: SharedMap.hpp:182
void AddMany(MapT &&m)
AddMany : add from map by lvalue ref.
Definition: SharedMap.hpp:114