C++ Web Framework  3.0
qmapthreadsafety.h
1 /*
2  Copyright 2017 Herik Lima de Castro and Marcelo Medeiros Eler
3  Distributed under MIT license, or public domain if desired and
4  recognized in your jurisdiction.
5  See file LICENSE for detail.
6 */
7 
8 #ifndef QMAPTHREADSAFETY_H
9 #define QMAPTHREADSAFETY_H
10 
11 #include <QMap>
12 #include <QPair>
13 #include <QMutex>
14 #include <QMutexLocker>
15 #include <atomic>
16 #include "cppwebframework_global.h"
17 
18 CWF_BEGIN_NAMESPACE
19 template <typename Key, typename T>
24 {
25  mutable QMap<Key, T> m_map;
26  mutable QMutex mutex;
27 public:
28  typedef typename QMap<Key, T>::iterator iterator;
29 
30  typedef typename QMap<Key, T>::const_iterator const_iterator;
31 
32  QMapThreadSafety() = default;
33 
34  explicit QMapThreadSafety(std::initializer_list<std::pair<Key, T>> &list) : m_map(list){}
35 
36  explicit QMapThreadSafety(const QMap<Key, T> &other) : m_map(other){}
37 
38  explicit QMapThreadSafety(const std::map<Key, T> &other) : m_map(other){}
39 
40  explicit QMapThreadSafety(QMap<Key, T> &&other) : m_map(other){}
41 
46  iterator begin() const
47  {
48  QMutexLocker locker(&mutex);
49  return m_map.begin();
50  }
51 
52  const_iterator cbegin() const
53  {
54  QMutexLocker locker(&mutex);
55  return m_map.cbegin();
56  }
57 
58  const_iterator cend() const
59  {
60  QMutexLocker locker(&mutex);
61  return m_map.cend();
62  }
63 
64  const_iterator constBegin() const
65  {
66  QMutexLocker locker(&mutex);
67  return m_map.constBegin();
68  }
69 
70  const_iterator constEnd() const
71  {
72  QMutexLocker locker(&mutex);
73  return m_map.constEnd();
74  }
75 
76  const_iterator constFind(const Key &key) const
77  {
78  QMutexLocker locker(&mutex);
79  return m_map.constFind(key);
80  }
86  bool contains(const Key &key) const
87  {
88  QMutexLocker locker(&mutex);
89  return m_map.contains(key);
90  }
91 
92  int count(const Key &key) const
93  {
94  QMutexLocker locker(&mutex);
95  return m_map.count(key);
96  }
97 
98  int count() const
99  {
100  QMutexLocker locker(&mutex);
101  return m_map.count();
102  }
103 
104  bool empty() const
105  {
106  QMutexLocker locker(&mutex);
107  return m_map.empty();
108  }
113  iterator end()
114  {
115  QMutexLocker locker(&mutex);
116  return m_map.end();
117  }
118 
119  QPair<iterator, iterator> equal_range(const Key &key)
120  {
121  QMutexLocker locker(&mutex);
122  return m_map.equal_range(key);
123  }
124 
125  iterator erase(iterator pos)
126  {
127  QMutexLocker locker(&mutex);
128  return m_map.erase(pos);
129  }
130 
131  iterator find(const Key &key)
132  {
133  QMutexLocker locker(&mutex);
134  return m_map.find(key);
135  }
136 
137  const_iterator find(const Key &key) const
138  {
139  QMutexLocker locker(&mutex);
140  return m_map.find(key);
141  }
142 
143  T & first()
144  {
145  QMutexLocker locker(&mutex);
146  return m_map.first();
147  }
148 
149  const T & first() const
150  {
151  QMutexLocker locker(&mutex);
152  return m_map.first();
153  }
154 
155  const Key & firstKey() const
156  {
157  QMutexLocker locker(&mutex);
158  return m_map.firstKey();
159  }
160 
166  iterator insert(const Key &key, const T &value)
167  {
168  QMutexLocker locker(&mutex);
169  return m_map.insert(key, value);
170  }
171 
172  iterator insert(const_iterator pos, const Key &key, const T &value)
173  {
174  QMutexLocker locker(&mutex);
175  return m_map.insert(pos, key, value);
176  }
177 
178  iterator insertMulti(const Key &key, const T &value)
179  {
180  QMutexLocker locker(&mutex);
181  return m_map.insertMulti(key, value);
182  }
183 
184  bool isEmpty() const
185  {
186  QMutexLocker locker(&mutex);
187  return m_map.isEmpty();
188  }
189 
190  QList<Key> keys() const
191  {
192  QMutexLocker locker(&mutex);
193  return m_map.keys();
194  }
195 
196  QList<Key> keys(const T &value) const
197  {
198  QMutexLocker locker(&mutex);
199  return m_map.keys();
200  }
201 
202  T &last()
203  {
204  QMutexLocker locker(&mutex);
205  return m_map.last();
206  }
207 
208  const T &last() const
209  {
210  QMutexLocker locker(&mutex);
211  return m_map.last();
212  }
213 
214 
215  iterator lowerBound(const Key &key)
216  {
217  QMutexLocker locker(&mutex);
218  return m_map.lowerBound(key);
219  }
220 
221  const_iterator lowerBound(const Key &key) const
222  {
223  QMutexLocker locker(&mutex);
224  return m_map.lowerBound(key);
225  }
231  int remove(const Key &key)
232  {
233  QMutexLocker locker(&mutex);
234  return m_map.remove(key);
235  }
236 
237  int size() const
238  {
239  QMutexLocker locker(&mutex);
240  return m_map.size();
241  }
242 
243  void swap(QMap<Key, T> & other)
244  {
245  QMutexLocker locker(&mutex);
246  m_map.swap(other);
247  }
248 
249  T take(const Key &key)
250  {
251  QMutexLocker locker(&mutex);
252  return m_map.take(key);
253  }
254 
255  std::map<Key, T> toStdMap() const
256  {
257  QMutexLocker locker(&mutex);
258  return m_map.toStdMap();
259  }
260 
261  QList<Key> uniqueKeys() const
262  {
263  QMutexLocker locker(&mutex);
264  return m_map.uniqueKeys();
265  }
266 
267  QMap<Key, T> &unite(const QMap<Key, T> & other)
268  {
269  QMutexLocker locker(&mutex);
270  return m_map.unite(other);
271  }
272 
273  iterator upperBound(const Key & key)
274  {
275  QMutexLocker locker(&mutex);
276  return m_map.upperBound(key);
277  }
278 
279  const_iterator upperBound(const Key & key) const
280  {
281  QMutexLocker locker(&mutex);
282  return m_map.upperBound(key);
283  }
284 
285  const T value(const Key & key, const T & defaultValue = T()) const
286  {
287  QMutexLocker locker(&mutex);
288  return m_map.value(key, defaultValue);
289  }
290 
291  QList<T> values() const
292  {
293  QMutexLocker locker(&mutex);
294  return m_map.values();
295  }
296 
297  QList<T> values(const Key & key) const
298  {
299  QMutexLocker locker(&mutex);
300  return m_map.values(key);
301  }
302 
303  bool operator!=(const QMap<Key, T> & other) const
304  {
305  QMutexLocker locker(&mutex);
306  return m_map.operator !=(other);
307  }
308 
309  QMap<Key, T> & operator=(const QMap<Key, T> & other)
310  {
311  QMutexLocker locker(&mutex);
312  return m_map.operator =(other);
313  }
314 
315  QMap<Key, T> & operator=(QMap<Key, T> && other)
316  {
317  QMutexLocker locker(&mutex);
318  return m_map.operator =(other);
319  }
320 
321  bool operator==(const QMap<Key, T> & other) const
322  {
323  QMutexLocker locker(&mutex);
324  return m_map.operator ==(other);
325  }
326 
327  T & operator[](const Key & key)
328  {
329  QMutexLocker locker(&mutex);
330  return m_map.operator [](key);
331  }
332 
338  const T operator [](const Key &key) const
339  {
340  QMutexLocker locker(&mutex);
341  return m_map.operator [](key);
342  }
343 };
344 
345 CWF_END_NAMESPACE
346 
347 #endif // QMAPTHREADSAFETY_H
iterator end()
This method retuns the end iterator.
Definition: qmapthreadsafety.h:113
iterator insert(const Key &key, const T &value)
This method inserts a new key and value in the map.
Definition: qmapthreadsafety.h:166
bool contains(const Key &key) const
This method checks if the map contains and specific element given a specific key. ...
Definition: qmapthreadsafety.h:86
The QMapThreadSafety class is a thread safe QMap.
Definition: qmapthreadsafety.h:23
iterator begin() const
This method retuns the begin iterator.
Definition: qmapthreadsafety.h:46