17 #ifndef COM_BORA_SOFTWARE__BALAU_INTERPROCESS__MANAGED_SHARED_MEMORY_OBJECT 18 #define COM_BORA_SOFTWARE__BALAU_INTERPROCESS__MANAGED_SHARED_MEMORY_OBJECT 25 #include <boost/interprocess/managed_shared_memory.hpp> 56 : MSharedMemoryObject(
CreateOnly,
"SMO_" +
UUID().asString(), params ...) {}
63 public:
template <
typename ... P>
65 const std::string memoryName = name +
"_memory";
67 boost::interprocess::shared_memory_object::remove(memoryName.c_str());
69 segment = boost::interprocess::managed_shared_memory(
70 CreateOnly, memoryName.c_str(), metadataOverhead +
sizeof(T)
73 object = segment.construct<T>((name +
"_object").c_str())(params ...);
83 public:
template <
typename ... P>
85 const std::string memoryName = name +
"_memory";
87 boost::interprocess::shared_memory_object::remove(memoryName.c_str());
89 segment = boost::interprocess::managed_shared_memory(
90 boost::interprocess::open_or_create, memoryName.c_str(), metadataOverhead +
sizeof(T)
93 object = segment.construct<T>((name +
"_object").c_str())(params ...);
104 const std::string memoryName = name +
"_memory";
105 segment = boost::interprocess::managed_shared_memory(boost::interprocess::open_only, memoryName.c_str());
106 auto item = segment.find<T>((name +
"_object").c_str());
108 if (item.first ==
nullptr) {
125 const std::string memoryName = name +
"_memory";
126 segment = boost::interprocess::managed_shared_memory(boost::interprocess::open_read_only, memoryName.c_str());
127 auto item = segment.find<T>((name +
"_object").c_str());
129 if (item.first ==
nullptr) {
142 segment.destroy<T>((name +
"_memory").c_str());
143 boost::interprocess::shared_memory_object::remove((name +
"_object").c_str());
150 : name(std::move(rhs.name))
151 , segment(std::move(rhs.segment))
152 , object(rhs.object) {
153 rhs.object =
nullptr;
160 name = std::move(rhs.name);
161 segment = std::move(rhs.segment);
163 rhs.object =
nullptr;
171 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
179 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
187 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
195 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
202 private:
static const size_t metadataOverhead = 512;
204 private: std::string name;
205 private: boost::interprocess::managed_shared_memory segment;
206 private: T *
object =
nullptr;
211 #endif // COM_BORA_SOFTWARE__BALAU_INTERPROCESS__MANAGED_SHARED_MEMORY_OBJECT boost::interprocess::open_or_create_t OpenOrCreateSelector
Type of OpenOrCreate constructor selector.
Definition: SharedMemoryUtils.hpp:36
const CreateOnlySelector CreateOnly
Used to select an interprocess queue/object constructor that creates only.
MSharedMemoryObject(OpenOrCreateSelector, const std::string &name, const P &... params)
Create or open a shared memory object of type T with the supplied input arguments.
Definition: MSharedMemoryObject.hpp:84
boost::interprocess::create_only_t CreateOnlySelector
Type of CreateOnly constructor selector.
Definition: SharedMemoryUtils.hpp:31
#define ThrowBalauException(ExceptionClass,...)
Throw a Balau style exception, with implicit file and line number, and optional stacktrace.
Definition: BalauException.hpp:45
boost::interprocess::open_only_t OpenOnlySelector
Type of OpenOrCreate constructor selector.
Definition: SharedMemoryUtils.hpp:41
MSharedMemoryObject(MSharedMemoryObject &&rhs) noexcept
Create a shared memory object by moving from a previously created one.
Definition: MSharedMemoryObject.hpp:149
MSharedMemoryObject(OpenOnlySelector, const std::string &name)
Open a shared memory object of type T.
Definition: MSharedMemoryObject.hpp:103
UUID class, using the Boost uuid implementation.
Interprocess functionality including interprocess containers.
Definition: MSharedMemoryObject.hpp:27
MSharedMemoryObject(const P &... params)
Create a shared memory object of type T with the supplied input arguments.
Definition: MSharedMemoryObject.hpp:55
Thrown when a resource is not found.
Definition: ResourceExceptions.hpp:48
MSharedMemoryObject(CreateOnlySelector, const std::string &name, const P &... params)
Create a shared memory object of type T with the supplied input arguments.
Definition: MSharedMemoryObject.hpp:64
~MSharedMemoryObject()
Destroy the shared memory object.
Definition: MSharedMemoryObject.hpp:141
T * operator->()
Get the shared memory object pointer.
Definition: MSharedMemoryObject.hpp:170
Balau exceptions for resources.
A shared memory object that uses the Boost interprocess library.
Definition: MSharedMemoryObject.hpp:49
Assertion utilities for development purposes.
Interprocess shared memory utilities.
MSharedMemoryObject & operator=(MSharedMemoryObject &&rhs) noexcept
Assign a shared memory object by moving from a previously created one.
Definition: MSharedMemoryObject.hpp:159
UUID class, using the Boost uuid implementation.
Definition: UUID.hpp:33
MSharedMemoryObject(OpenReadOnlySelector, const std::string &name)
Open read-only a shared memory object of type T.
Definition: MSharedMemoryObject.hpp:124
boost::interprocess::open_read_only_t OpenReadOnlySelector
Type of OpenReadOnly constructor selector.
Definition: SharedMemoryUtils.hpp:46
static void assertion(bool test, StringFunctionT function)
If the bug test assertion fails, abort after logging the message supplied by the function.
Definition: Assert.hpp:49
T & operator*()
Get a reference to the shared memory object.
Definition: MSharedMemoryObject.hpp:186