libDwm-0.9.45
DwmIpv4AddrMap.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2021
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMIPV4ADDRMAP_HH_
43#define _DWMIPV4ADDRMAP_HH_
44
45#include <cassert>
46#include <mutex>
47#include <shared_mutex>
48#include <unordered_map>
49
50#include "DwmIpv4Address.hh"
51
52namespace Dwm {
53
54 //--------------------------------------------------------------------------
56 //--------------------------------------------------------------------------
57 template <typename T>
58 class Ipv4AddrMap
59 {
60 public:
61 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
64 using MapType = std::unordered_map<ipv4addr_t,T>;
65
66 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
69 Ipv4AddrMap()
70 : _mtx(), _map()
71 {
72 _map.max_load_factor(0.15);
73 }
74
75 //------------------------------------------------------------------------
77 //------------------------------------------------------------------------
78 void Add(const Ipv4Address & addr, const T & value)
79 {
80 std::unique_lock lck(_mtx);
81 return Add(lck, addr, value);
82 }
83
84 //------------------------------------------------------------------------
86 //------------------------------------------------------------------------
87 void Add(std::unique_lock<std::shared_mutex> & lck,
88 const Ipv4Address & addr, const T & value)
89 {
90 assert((lck.mutex() == &_mtx) && lck.owns_lock());
91 _map[addr.Raw()] = value;
92 return;
93 }
94
95 //------------------------------------------------------------------------
97 //------------------------------------------------------------------------
98 bool Find(const Ipv4Address & addr, T & value) const
99 {
100 std::shared_lock lck(_mtx);
101 return Find(lck, addr, value);
102 }
103
104 //------------------------------------------------------------------------
106 //------------------------------------------------------------------------
107 bool Find(std::shared_lock<std::shared_mutex> & lck,
108 const Ipv4Address & addr, T & value) const
109 {
110 assert((lck.mutex() == &_mtx) && lck.owns_lock());
111 auto iter = _map.find(addr.Raw());
112 if (iter != _map.end()) {
113 value = iter->second;
114 return true;
115 }
116 return false;
117 }
118
119 //------------------------------------------------------------------------
121 //------------------------------------------------------------------------
122 bool Find(std::unique_lock<std::shared_mutex> & lck,
123 const Ipv4Address & addr, T & value)
124 {
125 assert((lck.mutex() == &_mtx) && lck.owns_lock());
126 auto iter = _map.find(addr.Raw());
127 if (iter != _map.end()) {
128 value = iter->second;
129 return true;
130 }
131 return false;
132 }
133
134 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 bool Remove(const Ipv4Address & addr)
138 {
139 std::unique_lock lck(_mtx);
140 return Remove(lck, addr);
141 }
142
143 //------------------------------------------------------------------------
145 //------------------------------------------------------------------------
146 bool Remove(std::unique_lock<std::shared_mutex> & lck,
147 const Ipv4Address & addr)
148 {
149 assert((lck.mutex() == &_mtx) && lck.owns_lock());
150 bool rc = false;
151 auto it = _map.find(addr.Raw());
152 if (it != _map.end()) {
153 _map.erase(it);
154 rc = true;
155 }
156 return rc;
157 }
158
159 //------------------------------------------------------------------------
161 //------------------------------------------------------------------------
162 bool Empty() const
163 {
164 std::shared_lock lck(_mtx);
165 return Empty(lck);
166 }
167
168 //------------------------------------------------------------------------
170 //------------------------------------------------------------------------
171 bool Empty(std::shared_lock<std::shared_mutex> & lck) const
172 {
173 assert((lck.mutex() == &_mtx) && lck.owns_lock());
174 return _map.empty();
175 }
176
177 //------------------------------------------------------------------------
179 //------------------------------------------------------------------------
180 bool Empty(std::unique_lock<std::shared_mutex> & lck)
181 {
182 assert((lck.mutex() == &_mtx) && lck.owns_lock());
183 return _map.empty();
184 }
185
186 //------------------------------------------------------------------------
188 //------------------------------------------------------------------------
189 const MapType & Map(std::shared_lock<std::shared_mutex> & lck) const
190 {
191 assert((lck.mutex() == &_mtx) && lck.owns_lock());
192 return _map;
193 }
194
195 //------------------------------------------------------------------------
197 //------------------------------------------------------------------------
198 MapType & Map(std::unique_lock<std::shared_mutex> & lck)
199 {
200 assert((lck.mutex() == &_mtx) && lck.owns_lock());
201 return _map;;
202 }
203
204 //------------------------------------------------------------------------
206 //------------------------------------------------------------------------
207 void Clear()
208 {
209 std::unique_lock lck(_mtx);
210 _map.clear();
211 return;
212 }
213
214 //------------------------------------------------------------------------
216 //------------------------------------------------------------------------
217 void Clear(std::unique_lock<std::shared_mutex> & lck)
218 {
219 assert((lck.mutex() == &_mtx) && lck.owns_lock());
220 _map.clear();
221 return;
222 }
223
224 //------------------------------------------------------------------------
226 //------------------------------------------------------------------------
227 std::shared_lock<std::shared_mutex> SharedLock() const
228 {
229 return std::shared_lock(_mtx);
230 }
231
232 //------------------------------------------------------------------------
234 //------------------------------------------------------------------------
235 std::unique_lock<std::shared_mutex> UniqueLock() const
236 {
237 return std::unique_lock(_mtx);
238 }
239
240 private:
241 mutable std::shared_mutex _mtx;
242 MapType _map;
243 };
244
245} // namespace Dwm
246
247#endif // _DWMIPV4ADDRMAP_HH_
Dwm::Ipv4Address class definition.