17 #ifndef COM_BORA_SOFTWARE__BALAU_INTERPROCESS__UNMANAGED_SHARED_MEMORY_OBJECT2 18 #define COM_BORA_SOFTWARE__BALAU_INTERPROCESS__UNMANAGED_SHARED_MEMORY_OBJECT2 25 #include <boost/interprocess/mapped_region.hpp> 26 #include <boost/interprocess/shared_memory_object.hpp> 54 : USharedMemoryObject(
CreateOnly,
"SMO_" +
UUID().asString(), params ...) {}
61 public:
template <
typename ... P>
63 : name(
std::move(name_)) {
64 boost::interprocess::shared_memory_object::remove(name.c_str());
66 sharedMemoryObject = boost::interprocess::shared_memory_object(
67 CreateOnly, name.c_str(), boost::interprocess::read_write
70 sharedMemoryObject.truncate(
sizeof(T));
71 mappedRegion = boost::interprocess::mapped_region(sharedMemoryObject, boost::interprocess::read_write);
72 object =
new (mappedRegion.get_address()) T(params ...);
82 public:
template <
typename ... P>
84 : name(
std::move(name_)) {
85 boost::interprocess::shared_memory_object::remove(name.c_str());
87 boost::interprocess::shared_memory_object shm_obj(
88 boost::interprocess::open_or_create, name.c_str(), boost::interprocess::read_write
91 boost::interprocess::offset_t size;
93 if (shm_obj.get_size(size)) {
94 shm_obj.truncate(
sizeof(T));
95 boost::interprocess::mapped_region region(shm_obj, boost::interprocess::read_write);
96 object =
new (region.get_address()) T(params ...);
106 : name(
std::move(name_)) {
107 boost::interprocess::shared_memory_object sharedMemoryObject(
108 boost::interprocess::open_only, name.c_str(), boost::interprocess::read_write
118 : name(
std::move(name_)) {
119 boost::interprocess::shared_memory_object shm_obj(
120 boost::interprocess::open_only, name.c_str(), boost::interprocess::read_only
130 : name(std::move(rhs.name))
131 , sharedMemoryObject(std::move(rhs.sharedMemoryObject))
132 , mappedRegion(std::move(rhs.mappedRegion))
133 , object(rhs.object) {
134 rhs.object =
nullptr;
138 boost::interprocess::shared_memory_object::remove(name.c_str());
145 name = std::move(rhs.name);
146 sharedMemoryObject = std::move(rhs.sharedMemoryObject);
147 mappedRegion = std::move(rhs.mappedRegion);
149 rhs.object =
nullptr;
153 public:
void remap() {
154 mappedRegion = boost::interprocess::mapped_region(sharedMemoryObject, boost::interprocess::read_write);
155 object = (T *) mappedRegion.get_address();
162 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
170 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
178 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
186 Assert::assertion(
object !=
nullptr,
"Attempt to dereference null object in shared memory object.");
192 private: std::string name;
193 private: boost::interprocess::shared_memory_object sharedMemoryObject;
194 private: boost::interprocess::mapped_region mappedRegion;
195 protected: T *
object =
nullptr;
200 #endif // COM_BORA_SOFTWARE__BALAU_INTERPROCESS__UNMANAGED_SHARED_MEMORY_OBJECT2 A shared memory object that uses the Boost interprocess library.
Definition: USharedMemoryObject.hpp:47
const CreateOnlySelector CreateOnly
Used to select an interprocess queue/object constructor that creates only.
USharedMemoryObject(boost::interprocess::open_or_create_t, std::string name_, const P &... params)
Create or open a shared memory object of type T with the supplied input arguments.
Definition: USharedMemoryObject.hpp:83
T * operator->()
Get the shared memory object pointer.
Definition: USharedMemoryObject.hpp:161
UUID class, using the Boost uuid implementation.
Interprocess functionality including interprocess containers.
Definition: MSharedMemoryObject.hpp:27
USharedMemoryObject & operator=(USharedMemoryObject &&rhs) noexcept
Assign a shared memory object by moving from a previously created one.
Definition: USharedMemoryObject.hpp:144
USharedMemoryObject(USharedMemoryObject &&rhs) noexcept
Create a shared memory object by moving from a previously created one.
Definition: USharedMemoryObject.hpp:129
USharedMemoryObject(const P &... params)
Create a shared memory object of type T with the supplied input arguments.
Definition: USharedMemoryObject.hpp:53
T & operator*()
Get a reference to the shared memory object.
Definition: USharedMemoryObject.hpp:177
USharedMemoryObject(boost::interprocess::create_only_t, std::string name_, const P &... params)
Create a shared memory object of type T with the supplied input arguments.
Definition: USharedMemoryObject.hpp:62
USharedMemoryObject(boost::interprocess::open_only_t, std::string name_)
Open a shared memory object of type T and with the specified name.
Definition: USharedMemoryObject.hpp:105
Balau exceptions for resources.
Assertion utilities for development purposes.
Interprocess shared memory utilities.
UUID class, using the Boost uuid implementation.
Definition: UUID.hpp:33
USharedMemoryObject(boost::interprocess::open_read_only_t, std::string name_)
Open read-only a shared memory object of type T and with the specified name.
Definition: USharedMemoryObject.hpp:117
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