USharedMemoryObject.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_INTERPROCESS__UNMANAGED_SHARED_MEMORY_OBJECT2
18 #define COM_BORA_SOFTWARE__BALAU_INTERPROCESS__UNMANAGED_SHARED_MEMORY_OBJECT2
19 
22 #include <Balau/Dev/Assert.hpp>
23 #include <Balau/Type/UUID.hpp>
24 
25 #include <boost/interprocess/mapped_region.hpp>
26 #include <boost/interprocess/shared_memory_object.hpp>
27 
28 namespace Balau::Interprocess {
29 
47 template <typename T> class USharedMemoryObject {
53  public: template <typename ... P> explicit USharedMemoryObject(const P & ... params)
54  : USharedMemoryObject(CreateOnly, "SMO_" + UUID().asString(), params ...) {}
55 
61  public: template <typename ... P>
62  USharedMemoryObject(boost::interprocess::create_only_t, std::string name_, const P & ... params)
63  : name(std::move(name_)) {
64  boost::interprocess::shared_memory_object::remove(name.c_str());
65 
66  sharedMemoryObject = boost::interprocess::shared_memory_object(
67  CreateOnly, name.c_str(), boost::interprocess::read_write
68  );
69 
70  sharedMemoryObject.truncate(sizeof(T));
71  mappedRegion = boost::interprocess::mapped_region(sharedMemoryObject, boost::interprocess::read_write);
72  object = new (mappedRegion.get_address()) T(params ...);
73  }
74 
82  public: template <typename ... P>
83  USharedMemoryObject(boost::interprocess::open_or_create_t, std::string name_, const P & ... params)
84  : name(std::move(name_)) {
85  boost::interprocess::shared_memory_object::remove(name.c_str());
86 
87  boost::interprocess::shared_memory_object shm_obj(
88  boost::interprocess::open_or_create, name.c_str(), boost::interprocess::read_write
89  );
90 
91  boost::interprocess::offset_t size;
92 
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 ...);
97  } else {
98  remap();
99  }
100  }
101 
105  public: USharedMemoryObject(boost::interprocess::open_only_t, std::string name_)
106  : name(std::move(name_)) {
107  boost::interprocess::shared_memory_object sharedMemoryObject(
108  boost::interprocess::open_only, name.c_str(), boost::interprocess::read_write
109  );
110 
111  remap();
112  }
113 
117  public: USharedMemoryObject(boost::interprocess::open_read_only_t, std::string name_)
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
121  );
122 
123  remap();
124  }
125 
129  public: USharedMemoryObject(USharedMemoryObject && rhs) noexcept
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;
135  }
136 
137  public: ~USharedMemoryObject() {
138  boost::interprocess::shared_memory_object::remove(name.c_str());
139  }
140 
145  name = std::move(rhs.name);
146  sharedMemoryObject = std::move(rhs.sharedMemoryObject);
147  mappedRegion = std::move(rhs.mappedRegion);
148  object = rhs.object;
149  rhs.object = nullptr;
150  return *this;
151  }
152 
153  public: void remap() {
154  mappedRegion = boost::interprocess::mapped_region(sharedMemoryObject, boost::interprocess::read_write);
155  object = (T *) mappedRegion.get_address();
156  }
157 
161  public: T * operator -> () {
162  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
163  return object;
164  }
165 
169  public: const T * operator -> () const {
170  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
171  return object;
172  }
173 
177  public: T & operator * () {
178  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
179  return *object;
180  }
181 
185  public: const T & operator * () const {
186  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
187  return *object;
188  }
189 
191 
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;
196 };
197 
198 } // namespace Balau::Interprocess
199 
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.
STL namespace.
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