Cbeam
Loading...
Searching...
No Matches
stable_interprocess_container.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/buffer.hpp> // for cbeam::container::buffer
29#include <cbeam/error/runtime_error.hpp> // for cbeam::error:runtime_error
30#include <cbeam/logging/log_manager.hpp> // for CBEAM_LOG
31#include <cbeam/memory/interprocess_shared_memory.hpp> // for cbeam::memory::interprocess_shared_memory
32#include <cbeam/serialization/traits.hpp> // for cbeam::serialization::traits, cbeam::serialization::serialized_object
33
34#include <cstring> // for memcpy, size_t, std::memcpy, std::size_t
35#include <string> // for std::allocator, std::operator+, std::string_literals::operator""s, std::char_traits, std::to_string, std::string, std::string_literals
36
37namespace cbeam::container
38{
70 template <typename T>
72 {
73 public:
93 stable_interprocess_container(const std::string& unique_identifier, std::size_t size)
94 : interprocess_shared_memory(unique_identifier, size)
95 {
96 }
97
102
109 virtual void clear()
110 {
111 auto lock = get_lock_guard(); // Locks the mutex to ensure exclusive access
112 serialize(T{}); // Resets the container by serializing an empty instance of T
113 }
114
122 virtual bool empty() const
123 {
124 auto lock = get_lock_guard(); // Locks the mutex for safe access
125 return deserialize().empty(); // Deserializes the container and checks if it's empty
126 }
127
135 virtual size_t size() const
136 {
137 auto lock = get_lock_guard();
138 return deserialize().size();
139 }
140
158 template <typename Func>
159 void foreach (Func func)
160 {
161 T local_instance;
162 {
163 auto lock = get_lock_guard();
164 local_instance = deserialize();
165 }
166
167 for (const auto& item : local_instance)
168 {
169 if (!func(item))
170 {
171 break;
172 }
173 }
174 }
175
176 protected:
184 T deserialize() const
185 {
187
188 if (it)
189 {
190 T result;
192 return result;
193 }
194 else
195 {
196 return {};
197 }
198 }
199
209 void serialize(const T& container)
210 {
211 using namespace std::string_literals;
212
215
216 if (buffer.size() > capacity())
217 {
218 std::string errorMessage = "cbeam::stable_interprocess_container::serialize: size of serialized container ("s + std::to_string(buffer.size())
219 + " bytes) exceeds shared memory size ("s + std::to_string(capacity()) + " bytes)."s;
220 CBEAM_LOG(errorMessage);
221 throw cbeam::error::runtime_error(errorMessage);
222 }
223 std::memcpy(data(), buffer.get(), buffer.size());
224 }
225 };
226}
Manages memory a byte buffer, offering dynamic appending. This class is designed for scenarios where ...
Definition buffer.hpp:50
virtual void * get() const noexcept
return a pointer to the managed memory block
Definition buffer.hpp:141
virtual std::size_t size() const noexcept
returns the size of the buffer in bytes
Definition buffer.hpp:112
virtual size_t size() const
Retrieves the size of the container.
Definition stable_interprocess_container.hpp:135
stable_interprocess_container & operator=(stable_interprocess_container &&)=delete
stable_interprocess_container(const stable_interprocess_container &)=delete
T deserialize() const
Deserializes the shared memory data into a container object.
Definition stable_interprocess_container.hpp:184
stable_interprocess_container(stable_interprocess_container &&)=delete
virtual void clear()
Clears the contents of the container.
Definition stable_interprocess_container.hpp:109
virtual bool empty() const
Checks if the container is empty.
Definition stable_interprocess_container.hpp:122
stable_interprocess_container(const std::string &unique_identifier, std::size_t size)
Constructs a stable_interprocess_container with a specific size and unique identifier.
Definition stable_interprocess_container.hpp:93
void serialize(const T &container)
Serializes a container object and stores it in shared memory.
Definition stable_interprocess_container.hpp:209
stable_interprocess_container & operator=(const stable_interprocess_container &)=delete
A Cbeam-specific runtime error that also acts like std::runtime_error.
Definition runtime_error.hpp:46
Provides a unified, platform-independent interface for managing shared memory segments.
Definition interprocess_shared_memory.hpp:61
lock_guard get_lock_guard() const
Acquires a lock_guard for mutex synchronization.
Definition interprocess_shared_memory.hpp:217
size_t capacity() const noexcept
Returns the size of the shared memory region.
Definition interprocess_shared_memory.hpp:202
void * data() const
Retrieves the starting address of the shared memory region.
Definition interprocess_shared_memory.hpp:189
interprocess_shared_memory(const std::string &unique_identifier, std::size_t size)
Constructor that initializes the shared memory segment.
Definition interprocess_shared_memory.hpp:88
#define CBEAM_LOG(s)
Logs a message using cbeam::logging::log_manager.
Definition log_manager.hpp:124
Offers advanced container types with unique approaches to stability and interprocess sharing....
Definition buffer.hpp:44
void * serialized_object
Represents a serialized value in memory.
Definition traits.hpp:42
static void serialize(const T &val, container::buffer &stream)
Required to serialize an object of type T into a shared_buffer stream.
Definition traits.hpp:63
static void deserialize(serialized_object &it, T &val)
Required to deserialize an object of type T from a serialized memory block, incrementing the iterator...
Definition traits.hpp:77