map.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #include <map>
  3. #include <mutex>
  4. #include <functional>
  5. namespace ylib
  6. {
  7. template <typename KEY, typename VAL>
  8. class map :private std::map<KEY, VAL>
  9. {
  10. public:
  11. map() {}
  12. ~map() {}
  13. const std::map<KEY, VAL> to_stl()
  14. {
  15. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  16. return *this;
  17. }
  18. bool add(const KEY& key_, const VAL val, bool lock = true)
  19. {
  20. if (lock)
  21. this->m_mutex.lock();
  22. typename std::map<KEY, VAL>::iterator iter = ::std::map<KEY, VAL>::find(key_);
  23. if (iter != ::std::map<KEY, VAL>::end()) {
  24. if (lock)
  25. this->m_mutex.unlock();
  26. return false;
  27. }
  28. ::std::map<KEY, VAL>::insert(std::pair<KEY, VAL>(key_, val));
  29. if (lock)
  30. this->m_mutex.unlock();
  31. return true;
  32. }
  33. bool exist(const KEY& key_, bool lock = true)
  34. {
  35. if (lock)
  36. this->m_mutex.lock();
  37. auto iter = ::std::map<KEY, VAL>::find(key_);
  38. bool ret = iter != ::std::map<KEY, VAL>::end();
  39. if (lock)
  40. this->m_mutex.unlock();
  41. return ret;
  42. }
  43. bool set(const KEY& key_, VAL val, bool insert = false)
  44. {
  45. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  46. typename std::map<KEY, VAL>::iterator iter = ::std::map<KEY, VAL>::find(key_);
  47. if (iter == ::std::map<KEY, VAL>::end())
  48. {
  49. if (insert == true)
  50. {
  51. ::std::map<KEY, VAL>::insert(std::pair<KEY, VAL>(key_, val));
  52. return true;
  53. }
  54. else
  55. {
  56. return false;
  57. }
  58. }
  59. else
  60. {
  61. iter->second = val;
  62. return true;
  63. }
  64. }
  65. bool get(const KEY& key_, VAL& val, bool lock = true)
  66. {
  67. if (lock)
  68. this->m_mutex.lock();
  69. typename std::map<KEY, VAL>::iterator iter = ::std::map<KEY, VAL>::find(key_);
  70. if (iter == ::std::map<KEY, VAL>::end()) {
  71. if (lock)
  72. this->m_mutex.unlock();
  73. return false;
  74. }
  75. val = iter->second;
  76. if (lock)
  77. this->m_mutex.unlock();
  78. return true;
  79. }
  80. bool del(const KEY& key_, bool locked = true)
  81. {
  82. if (locked)
  83. m_mutex.lock();
  84. typename std::map<KEY, VAL>::iterator iter = ::std::map<KEY, VAL>::find(key_);
  85. if (iter == ::std::map<KEY, VAL>::end()) {
  86. if (locked)
  87. m_mutex.unlock();
  88. return false;
  89. }
  90. ::std::map<KEY, VAL>::erase(iter);
  91. if (locked)
  92. m_mutex.unlock();
  93. return true;
  94. }
  95. void clear()
  96. {
  97. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  98. //::std::map<KEY,VAL>::swap();
  99. ::std::map<KEY, VAL>::clear();
  100. }
  101. size_t size()
  102. {
  103. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  104. return ::std::map<KEY, VAL>::size();
  105. }
  106. void lock()
  107. {
  108. this->m_mutex.lock();
  109. }
  110. void unlock()
  111. {
  112. this->m_mutex.unlock();
  113. }
  114. VAL operator[](const KEY& key)
  115. {
  116. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  117. VAL val;
  118. get(key, val, false);
  119. return val;
  120. }
  121. bool find(std::function<bool(const KEY& key, const VAL& value)> delegate)
  122. {
  123. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  124. for_iter(iter, (*this))
  125. {
  126. if (delegate(iter->first, iter->second))
  127. return true;
  128. }
  129. return false;
  130. }
  131. void loop(std::function<void(const KEY& key, const VAL& value)> callback)
  132. {
  133. std::unique_lock<std::mutex> __guard_lock__(m_mutex);
  134. for_iter(iter, (*this))
  135. {
  136. callback(iter->first, iter->second);
  137. }
  138. }
  139. void loop2(std::function<bool(const KEY& key, VAL& value)> callback, bool lock = true)
  140. {
  141. if (lock)
  142. m_mutex.lock();
  143. for (auto iter = this->begin(); iter != this->end(); ++iter)
  144. {
  145. if (callback(iter->first, iter->second) == false)
  146. {
  147. if (lock)
  148. m_mutex.unlock();
  149. return;
  150. }
  151. }
  152. if (lock)
  153. m_mutex.unlock();
  154. }
  155. std::map<KEY, VAL>* parent()
  156. {
  157. return this;
  158. }
  159. public:
  160. std::mutex m_mutex;
  161. };
  162. }