Cbeam
Loading...
Searching...
No Matches
stable_interprocess_map.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
28#include <cbeam/container/stable_interprocess_container.hpp> // for cbeam::container::stable_interprocess_container
29#include <cbeam/error/out_of_range.hpp> // for cbeam::error:out_of_range
30#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
31#include <cbeam/serialization/map.hpp> // IWYU pragma: keep (required for serialization of the template type of stable_interprocess_container, which is a std::map)
32
33#include <cstddef> // for std::size_t
34
35#include <functional> // for std::function
36#include <initializer_list> // for std::initializer_list
37#include <map> // for std::allocator, std::map
38#include <string> // for std::string
39#include <utility> // for std::pair
40
41namespace cbeam::container
42{
57 template <typename Key, typename Value>
58 class stable_interprocess_map : public stable_interprocess_container<std::map<Key, Value>>
59 {
60 public:
67 stable_interprocess_map(const std::string& unique_identifier, std::size_t size)
68 : stable_interprocess_container<std::map<Key, Value>>(unique_identifier, size)
69 {
70 }
71
77 stable_interprocess_map& operator=(const std::initializer_list<std::pair<const Key, Value>>& list)
78 {
79 std::map<Key, Value> local_instance;
80
81 for (const auto& item : list)
82 {
83 local_instance[item.first] = item.second;
84 }
85
86 auto lock = this->get_lock_guard();
87 this->serialize(local_instance);
88 return *this;
89 }
90
97 Value at(const Key& key) const
98 {
99 std::map<Key, Value> local_instance;
100 {
101 auto lock = this->get_lock_guard();
102
103 local_instance = this->deserialize();
104 }
105
106 try
107 {
108 return local_instance.at(key);
109 }
110 catch (const std::out_of_range& ex)
111 {
112 throw cbeam::error::out_of_range(ex.what());
113 }
114 }
115
122 Value at_or_default(const Key& key, const Value& default_value) const
123 {
124 std::map<Key, Value> local_instance;
125 {
126 auto lock = this->get_lock_guard();
127
128 local_instance = this->deserialize();
129 }
130
131 return local_instance.count(key) == 1 ? local_instance.at(key) : default_value;
132 }
133
139 void insert(const Key& key, const Value& value)
140 {
141 auto lock = this->get_lock_guard();
142
143 auto local_instance = this->deserialize();
144 local_instance[key] = value;
145 this->serialize(local_instance);
146 }
147
152 void erase(const Key& key)
153 {
154 auto lock = this->get_lock_guard();
155
156 auto local_instance = this->deserialize();
157 local_instance.erase(key);
158 this->serialize(local_instance);
159 }
160
166 std::size_t count(const Key& key) const
167 {
168 auto lock = this->get_lock_guard();
169
170 return this->deserialize().count(key);
171 }
172
190 void update_or_insert(const Key& key, std::function<void(Value&)> updater, const Value& default_value)
191 {
192 auto lock = this->get_lock_guard();
193 auto local_instance = this->deserialize();
194
195 if (local_instance.find(key) != local_instance.end())
196 {
197 updater(local_instance[key]);
198 }
199 else
200 {
201 local_instance[key] = default_value;
202 }
203
204 this->serialize(local_instance);
205 }
206
224 const Value update(const Key& key, std::function<void(Value&)> updater, const std::string& error_string = {"cbeam::stable_interprocess_map::update: key not found"})
225 {
226 auto lock = this->get_lock_guard();
227 auto local_instance = this->deserialize();
228
229 if (local_instance.find(key) == local_instance.end())
230 {
231 throw cbeam::error::runtime_error(error_string);
232 }
233
234 updater(local_instance[key]);
235 this->serialize(local_instance);
236 return local_instance.at(key);
237 }
238 };
239}
virtual size_t size() const
Definition stable_interprocess_container.hpp:135
std::map< Key, Value > deserialize() const
Definition stable_interprocess_container.hpp:184
stable_interprocess_container(const std::string &unique_identifier, std::size_t size)
Definition stable_interprocess_container.hpp:93
void serialize(const std::map< Key, Value > &container)
Definition stable_interprocess_container.hpp:209
void erase(const Key &key)
Erases the element associated with a specific key.
Definition stable_interprocess_map.hpp:152
std::size_t count(const Key &key) const
Counts the number of elements with a specific key.
Definition stable_interprocess_map.hpp:166
void insert(const Key &key, const Value &value)
Inserts a key-value pair into the map.
Definition stable_interprocess_map.hpp:139
Value at(const Key &key) const
Retrieves the value associated with a specific key.
Definition stable_interprocess_map.hpp:97
void update_or_insert(const Key &key, std::function< void(Value &)> updater, const Value &default_value)
Definition stable_interprocess_map.hpp:190
Value at_or_default(const Key &key, const Value &default_value) const
Retrieves the value associated with a key, or a default value if the key is not found.
Definition stable_interprocess_map.hpp:122
const Value update(const Key &key, std::function< void(Value &)> updater, const std::string &error_string={"cbeam::stable_interprocess_map::update: key not found"})
Definition stable_interprocess_map.hpp:224
stable_interprocess_map & operator=(const std::initializer_list< std::pair< const Key, Value > > &list)
Assigns key-value pairs to the map from an initializer list.
Definition stable_interprocess_map.hpp:77
stable_interprocess_map(const std::string &unique_identifier, std::size_t size)
Constructs a stable_interprocess_map with a unique identifier and fixed size.
Definition stable_interprocess_map.hpp:67
A Cbeam-specific out_of_range error that also behaves like std::out_of_range.
Definition out_of_range.hpp:50
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
lock_guard get_lock_guard() const
Acquires a lock_guard for mutex synchronization.
Definition interprocess_shared_memory.hpp:217
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44