identt
SharedPairMap.hpp
Go to the documentation of this file.
1 
33 #ifndef _IDENTT_UTILS_SHARED_PAIR_MAP_HPP_
34 #define _IDENTT_UTILS_SHARED_PAIR_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 LeftT,class RightT>
46 public:
47  using ValueT = std::pair<LeftT,RightT>;
48  using PairMapT = std::unordered_map<KeyT,ValueT>;
49  using LockT = boost::shared_mutex;
50  using WriteLockT = boost::unique_lock< LockT >;
51  using ReadLockT = boost::shared_lock< LockT >;
52 
57  SharedPairMap(const SharedPairMap&) = delete;
58  SharedPairMap& operator=(const SharedPairMap&) = delete;
59 
65 
69  virtual ~SharedPairMap () {}
70 
83  void AddOne(KeyT key, ValueT value)
84  {
85  WriteLockT writelock(mutex_);
86  t_[key]=value;
87  }
88 
101  void AddLeft(KeyT key, LeftT value)
102  {
103  WriteLockT writelock(mutex_);
104  typename PairMapT::iterator it = t_.find(key);
105  if (it!=t_.end()) it.second.first = value;
106  else t_[key]=std::make_pair(value,RightT());
107  }
108 
121  void AddRight(KeyT key, RightT value)
122  {
123  WriteLockT writelock(mutex_);
124  typename PairMapT::iterator it = t_.find(key);
125  if (it!=t_.end()) it.second.second = value;
126  else t_[key]=std::make_pair(LeftT(),value);
127  }
128 
138  void Swap(PairMapT m)
139  {
140  WriteLockT writelock(mutex_);
141  t_.swap(m);
142  }
143 
153  void AddMany(const PairMapT& m)
154  {
155  WriteLockT writelock(mutex_);
156  for (auto& p : m) {
157  t_[p.first]=p.second;
158  }
159  }
160 
170  void AddMany(PairMapT&& m)
171  {
172  WriteLockT writelock(mutex_);
173  for (auto& p : m) {
174  t_[p.first]=p.second;
175  }
176  }
177 
193  bool GetValues(KeyT&& key, LeftT& left, RightT& right)
194  {
195  ReadLockT readlock(mutex_);
196  typename PairMapT::const_iterator it = t_.find(key);
197  bool stat = (it!=t_.end());
198  left= stat ? it.second.first : LeftT();
199  right= stat ? it.second.second : RightT();
200  return stat;
201  }
202 
218  bool GetValues(const KeyT& key, LeftT& left, RightT& right)
219  {
220  ReadLockT readlock(mutex_);
221  typename PairMapT::const_iterator it = t_.find(key);
222  bool stat = (it!=t_.end());
223  left= stat ? it.second.first : LeftT();
224  right= stat ? it.second.second : RightT();
225  return stat;
226  }
227 
234  PairMapT GetCopy()
235  {
236  ReadLockT readlock(mutex_);
237  return t_;
238  }
239 
246  size_t GetSize()
247  {
248  ReadLockT readlock(mutex_);
249  return t_.size();
250  }
251 
258  void Reset()
259  {
260  WriteLockT writelock(mutex_);
261  t_.clear();
262  }
263 
264 
265 protected:
266  PairMapT t_;
267  LockT mutex_;
268 
269 };
270 } // namespace utils
271 } // namespace identt
272 #endif /* _IDENTT_UTILS_SHARED_PAIR_MAP_HPP_ */
size_t GetSize()
GetSize: get size of set.
Definition: SharedPairMap.hpp:246
bool GetValues(const KeyT &key, LeftT &left, RightT &right)
GetValues : get values by address.
Definition: SharedPairMap.hpp:218
void AddMany(const PairMapT &m)
AddMany : add from map by address.
Definition: SharedPairMap.hpp:153
void Reset()
Reset: clear the set.
Definition: SharedPairMap.hpp:258
void Swap(PairMapT m)
Swap : swap from map.
Definition: SharedPairMap.hpp:138
void AddLeft(KeyT key, LeftT value)
AddLeft : add one value.
Definition: SharedPairMap.hpp:101
Definition: CryptoBase.hpp:49
PairMapT GetCopy()
GetCopy : get a copy of the map.
Definition: SharedPairMap.hpp:234
void AddMany(PairMapT &&m)
AddMany : add from map by lvalue ref.
Definition: SharedPairMap.hpp:170
Definition: SharedPairMap.hpp:45
virtual ~SharedPairMap()
destructor
Definition: SharedPairMap.hpp:69
void AddRight(KeyT key, RightT value)
AddRight : add one value.
Definition: SharedPairMap.hpp:121
bool GetValues(KeyT &&key, LeftT &left, RightT &right)
GetValues : get values.
Definition: SharedPairMap.hpp:193
SharedPairMap()
Constructor : default.
Definition: SharedPairMap.hpp:64
void AddOne(KeyT key, ValueT value)
AddOne : add one value.
Definition: SharedPairMap.hpp:83