36#include <initializer_list>
57 template <
typename Key,
typename Value>
79 std::map<Key, Value> local_instance;
81 for (
const auto& item : list)
83 local_instance[item.first] = item.second;
97 Value
at(
const Key& key)
const
99 std::map<Key, Value> local_instance;
108 return local_instance.at(key);
110 catch (
const std::out_of_range& ex)
124 std::map<Key, Value> local_instance;
131 return local_instance.count(key) == 1 ? local_instance.at(key) : default_value;
139 void insert(
const Key& key,
const Value& value)
144 local_instance[key] = value;
157 local_instance.erase(key);
166 std::size_t
count(
const Key& key)
const
190 void update_or_insert(
const Key& key, std::function<
void(Value&)> updater,
const Value& default_value)
195 if (local_instance.find(key) != local_instance.end())
197 updater(local_instance[key]);
201 local_instance[key] = default_value;
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"})
229 if (local_instance.find(key) == local_instance.end())
234 updater(local_instance[key]);
236 return local_instance.at(key);
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