sptk2 logo
SPTK Home Page
SynchronizedMap.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <functional>
30#include <map>
31#include <sptk5/sptk.h>
32
33namespace sptk {
34
45template<class K, class T>
47{
51 mutable std::mutex m_sync;
52
53 using Map = std::map<K, T>;
57 Map m_map;
58
59public:
68 using CallbackFunction = std::function<bool(const K& key, T& item)>;
69
75 virtual void insert(const K& key, const T& data)
76 {
77 std::scoped_lock lock(m_sync);
78 m_map.emplace(key, data);
79 }
80
89 virtual bool get(const K& key, T& item, bool remove = false)
90 {
91 std::scoped_lock lock(m_sync);
92 typename Map::iterator itor = m_map.find(key);
93 if (itor == m_map.end())
94 {
95 return false;
96 }
97 item = itor->second;
98 if (remove)
99 {
100 m_map.erase(itor);
101 }
102 return true;
103 }
104
111 virtual bool remove(const K& key)
112 {
113 std::scoped_lock lock(m_sync);
114 typename Map::iterator itor = m_map.find(key);
115 if (itor == m_map.end())
116 {
117 return false;
118 }
119 m_map.erase(itor);
120 return true;
121 }
122
126 bool empty() const
127 {
128 std::scoped_lock lock(m_sync);
129 return m_map.empty();
130 }
131
135 size_t size() const
136 {
137 std::scoped_lock lock(m_sync);
138 return m_map.size();
139 }
140
144 void clear()
145 {
146 std::scoped_lock lock(m_sync);
147 m_map.clear();
148 }
149
155 bool for_each(CallbackFunction callbackFunction)
156 {
157 std::scoped_lock lock(m_sync);
158 for (auto itor: m_map)
159 {
160 if (!callbackFunction(itor.first, itor.second))
161 {
162 return false;
163 }
164 }
165 return true;
166 }
167};
168
173} // namespace sptk
Definition: SynchronizedMap.h:47
virtual bool get(const K &key, T &item, bool remove=false)
Definition: SynchronizedMap.h:89
size_t size() const
Definition: SynchronizedMap.h:135
bool for_each(CallbackFunction callbackFunction)
Definition: SynchronizedMap.h:155
virtual bool remove(const K &key)
Definition: SynchronizedMap.h:111
bool empty() const
Definition: SynchronizedMap.h:126
virtual void insert(const K &key, const T &data)
Definition: SynchronizedMap.h:75
void clear()
Definition: SynchronizedMap.h:144
std::function< bool(const K &key, T &item)> CallbackFunction
Definition: SynchronizedMap.h:68

Fri Oct 14 2022 09:58:32: SPTK 5.4.1