qmapthreadsafety.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #ifndef QMAPTHREADSAFETY_H
  8. #define QMAPTHREADSAFETY_H
  9. #include <QMap>
  10. #include <QPair>
  11. #include <QMutex>
  12. #include <QMutexLocker>
  13. #include <atomic>
  14. #include "cppwebframework_global.h"
  15. CWF_BEGIN_NAMESPACE
  16. template <typename Key, typename T>
  17. /**
  18. * @brief The QMapThreadSafety class is a thread safe QMap.
  19. */
  20. class QMapThreadSafety
  21. {
  22. mutable QMap<Key, T> m_map;
  23. mutable QMutex mutex;
  24. public:
  25. typedef typename QMap<Key, T>::iterator iterator;
  26. typedef typename QMap<Key, T>::const_iterator const_iterator;
  27. QMapThreadSafety() = default;
  28. explicit QMapThreadSafety(std::initializer_list<std::pair<Key, T>> &list) : m_map(list){}
  29. explicit QMapThreadSafety(const QMap<Key, T> &other) : m_map(other){}
  30. explicit QMapThreadSafety(const std::map<Key, T> &other) : m_map(other){}
  31. explicit QMapThreadSafety(QMap<Key, T> &&other) : m_map(other){}
  32. /**
  33. * @brief This method retuns the begin iterator.
  34. * @return iterator
  35. */
  36. iterator begin() const
  37. {
  38. QMutexLocker locker(&mutex);
  39. return m_map.begin();
  40. }
  41. const_iterator cbegin() const
  42. {
  43. QMutexLocker locker(&mutex);
  44. return m_map.cbegin();
  45. }
  46. const_iterator cend() const
  47. {
  48. QMutexLocker locker(&mutex);
  49. return m_map.cend();
  50. }
  51. const_iterator constBegin() const
  52. {
  53. QMutexLocker locker(&mutex);
  54. return m_map.constBegin();
  55. }
  56. const_iterator constEnd() const
  57. {
  58. QMutexLocker locker(&mutex);
  59. return m_map.constEnd();
  60. }
  61. const_iterator constFind(const Key &key) const
  62. {
  63. QMutexLocker locker(&mutex);
  64. return m_map.constFind(key);
  65. }
  66. /**
  67. * @brief This method checks if the map contains and specific element given a specific key.
  68. * @param key : This represents the key that you want to find.
  69. * @return returns true if find the key and false if not find.
  70. */
  71. bool contains(const Key &key) const
  72. {
  73. QMutexLocker locker(&mutex);
  74. return m_map.contains(key);
  75. }
  76. int count(const Key &key) const
  77. {
  78. QMutexLocker locker(&mutex);
  79. return m_map.count(key);
  80. }
  81. int count() const
  82. {
  83. QMutexLocker locker(&mutex);
  84. return m_map.count();
  85. }
  86. bool empty() const
  87. {
  88. QMutexLocker locker(&mutex);
  89. return m_map.empty();
  90. }
  91. /**
  92. * @brief This method retuns the end iterator.
  93. * @return iterator
  94. */
  95. iterator end()
  96. {
  97. QMutexLocker locker(&mutex);
  98. return m_map.end();
  99. }
  100. QPair<iterator, iterator> equal_range(const Key &key)
  101. {
  102. QMutexLocker locker(&mutex);
  103. return m_map.equal_range(key);
  104. }
  105. iterator erase(iterator pos)
  106. {
  107. QMutexLocker locker(&mutex);
  108. return m_map.erase(pos);
  109. }
  110. iterator find(const Key &key)
  111. {
  112. QMutexLocker locker(&mutex);
  113. return m_map.find(key);
  114. }
  115. const_iterator find(const Key &key) const
  116. {
  117. QMutexLocker locker(&mutex);
  118. return m_map.find(key);
  119. }
  120. T & first()
  121. {
  122. QMutexLocker locker(&mutex);
  123. return m_map.first();
  124. }
  125. const T & first() const
  126. {
  127. QMutexLocker locker(&mutex);
  128. return m_map.first();
  129. }
  130. const Key & firstKey() const
  131. {
  132. QMutexLocker locker(&mutex);
  133. return m_map.firstKey();
  134. }
  135. /**
  136. * @brief This method inserts a new key and value in the map.
  137. * @param key : This represents the key that will be insert.
  138. * @param value : This represents the value that will be insert.
  139. */
  140. iterator insert(const Key &key, const T &value)
  141. {
  142. QMutexLocker locker(&mutex);
  143. return m_map.insert(key, value);
  144. }
  145. iterator insert(const_iterator pos, const Key &key, const T &value)
  146. {
  147. QMutexLocker locker(&mutex);
  148. return m_map.insert(pos, key, value);
  149. }
  150. iterator insertMulti(const Key &key, const T &value)
  151. {
  152. QMutexLocker locker(&mutex);
  153. return m_map.insertMulti(key, value);
  154. }
  155. bool isEmpty() const
  156. {
  157. QMutexLocker locker(&mutex);
  158. return m_map.isEmpty();
  159. }
  160. QList<Key> keys() const
  161. {
  162. QMutexLocker locker(&mutex);
  163. return m_map.keys();
  164. }
  165. QList<Key> keys(const T &value) const
  166. {
  167. QMutexLocker locker(&mutex);
  168. return m_map.keys(value);
  169. }
  170. T &last()
  171. {
  172. QMutexLocker locker(&mutex);
  173. return m_map.last();
  174. }
  175. const T &last() const
  176. {
  177. QMutexLocker locker(&mutex);
  178. return m_map.last();
  179. }
  180. iterator lowerBound(const Key &key)
  181. {
  182. QMutexLocker locker(&mutex);
  183. return m_map.lowerBound(key);
  184. }
  185. const_iterator lowerBound(const Key &key) const
  186. {
  187. QMutexLocker locker(&mutex);
  188. return m_map.lowerBound(key);
  189. }
  190. /**
  191. * @brief This method removes a specific element given a specific key.
  192. * @param key : This represents the key that will be insert.
  193. * @return int
  194. */
  195. int remove(const Key &key)
  196. {
  197. QMutexLocker locker(&mutex);
  198. return m_map.remove(key);
  199. }
  200. int size() const
  201. {
  202. QMutexLocker locker(&mutex);
  203. return m_map.size();
  204. }
  205. void swap(QMap<Key, T> & other)
  206. {
  207. QMutexLocker locker(&mutex);
  208. m_map.swap(other);
  209. }
  210. T take(const Key &key)
  211. {
  212. QMutexLocker locker(&mutex);
  213. return m_map.take(key);
  214. }
  215. std::map<Key, T> toStdMap() const
  216. {
  217. QMutexLocker locker(&mutex);
  218. return m_map.toStdMap();
  219. }
  220. QList<Key> uniqueKeys() const
  221. {
  222. QMutexLocker locker(&mutex);
  223. return m_map.uniqueKeys();
  224. }
  225. QMap<Key, T> &unite(const QMap<Key, T> & other)
  226. {
  227. QMutexLocker locker(&mutex);
  228. return m_map.unite(other);
  229. }
  230. iterator upperBound(const Key & key)
  231. {
  232. QMutexLocker locker(&mutex);
  233. return m_map.upperBound(key);
  234. }
  235. const_iterator upperBound(const Key & key) const
  236. {
  237. QMutexLocker locker(&mutex);
  238. return m_map.upperBound(key);
  239. }
  240. const T value(const Key & key, const T & defaultValue = T()) const
  241. {
  242. QMutexLocker locker(&mutex);
  243. return m_map.value(key, defaultValue);
  244. }
  245. QList<T> values() const
  246. {
  247. QMutexLocker locker(&mutex);
  248. return m_map.values();
  249. }
  250. QList<T> values(const Key & key) const
  251. {
  252. QMutexLocker locker(&mutex);
  253. return m_map.values(key);
  254. }
  255. bool operator!=(const QMap<Key, T> & other) const
  256. {
  257. QMutexLocker locker(&mutex);
  258. return m_map.operator !=(other);
  259. }
  260. QMap<Key, T> & operator=(const QMap<Key, T> & other)
  261. {
  262. QMutexLocker locker(&mutex);
  263. return m_map.operator =(other);
  264. }
  265. QMap<Key, T> & operator=(QMap<Key, T> && other)
  266. {
  267. QMutexLocker locker(&mutex);
  268. return m_map.operator =(other);
  269. }
  270. bool operator==(const QMap<Key, T> & other) const
  271. {
  272. QMutexLocker locker(&mutex);
  273. return m_map.operator ==(other);
  274. }
  275. T & operator[](const Key & key)
  276. {
  277. QMutexLocker locker(&mutex);
  278. return m_map.operator [](key);
  279. }
  280. /**
  281. * @brief This method is an overload of the operator [] and returns a value given a specific key.
  282. * @param key : This represents the key that you want to find.
  283. * @return T
  284. */
  285. const T operator [](const Key &key) const
  286. {
  287. QMutexLocker locker(&mutex);
  288. return m_map.operator [](key);
  289. }
  290. };
  291. CWF_END_NAMESPACE
  292. #endif // QMAPTHREADSAFETY_H