MSharedMemoryObject.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__MANAGED_SHARED_MEMORY_OBJECT
18 #define COM_BORA_SOFTWARE__BALAU_INTERPROCESS__MANAGED_SHARED_MEMORY_OBJECT
19 
22 #include <Balau/Dev/Assert.hpp>
23 #include <Balau/Type/UUID.hpp>
24 
25 #include <boost/interprocess/managed_shared_memory.hpp>
26 
28 
49 template <typename T> class MSharedMemoryObject {
55  public: template <typename ... P> explicit MSharedMemoryObject(const P & ... params)
56  : MSharedMemoryObject(CreateOnly, "SMO_" + UUID().asString(), params ...) {}
57 
63  public: template <typename ... P>
64  MSharedMemoryObject(CreateOnlySelector, const std::string & name, const P & ... params) {
65  const std::string memoryName = name + "_memory";
66 
67  boost::interprocess::shared_memory_object::remove(memoryName.c_str());
68 
69  segment = boost::interprocess::managed_shared_memory(
70  CreateOnly, memoryName.c_str(), metadataOverhead + sizeof(T)
71  );
72 
73  object = segment.construct<T>((name + "_object").c_str())(params ...);
74  }
75 
83  public: template <typename ... P>
84  MSharedMemoryObject(OpenOrCreateSelector, const std::string & name, const P & ... params) {
85  const std::string memoryName = name + "_memory";
86 
87  boost::interprocess::shared_memory_object::remove(memoryName.c_str());
88 
89  segment = boost::interprocess::managed_shared_memory(
90  boost::interprocess::open_or_create, memoryName.c_str(), metadataOverhead + sizeof(T)
91  );
92 
93  object = segment.construct<T>((name + "_object").c_str())(params ...);
94  }
95 
103  public: MSharedMemoryObject(OpenOnlySelector, const std::string & name) {
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());
107 
108  if (item.first == nullptr) {
110  Exception::NotFoundException, "The shared memory object with name prefix " + name + " was not found."
111  );
112  }
113 
114  object = item.first;
115  }
116 
124  public: MSharedMemoryObject(OpenReadOnlySelector, const std::string & name) {
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());
128 
129  if (item.first == nullptr) {
131  Exception::NotFoundException, "The shared memory object with name prefix " + name + " was not found."
132  );
133  }
134 
135  object = item.first;
136  }
137 
142  segment.destroy<T>((name + "_memory").c_str());
143  boost::interprocess::shared_memory_object::remove((name + "_object").c_str());
144  }
145 
149  public: MSharedMemoryObject(MSharedMemoryObject && rhs) noexcept
150  : name(std::move(rhs.name))
151  , segment(std::move(rhs.segment))
152  , object(rhs.object) {
153  rhs.object = nullptr;
154  }
155 
160  name = std::move(rhs.name);
161  segment = std::move(rhs.segment);
162  object = rhs.object;
163  rhs.object = nullptr;
164  return *this;
165  }
166 
170  public: T * operator -> () {
171  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
172  return object;
173  }
174 
178  public: const T * operator -> () const {
179  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
180  return object;
181  }
182 
186  public: T & operator * () {
187  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
188  return *object;
189  }
190 
194  public: const T & operator * () const {
195  Assert::assertion(object != nullptr, "Attempt to dereference null object in shared memory object.");
196  return *object;
197  }
198 
200 
201  // Reserved memory for metadata/index used in the allocated block.
202  private: static const size_t metadataOverhead = 512;
203 
204  private: std::string name;
205  private: boost::interprocess::managed_shared_memory segment;
206  private: T * object = nullptr;
207 };
208 
209 } // namespace Balau::Interprocess
210 
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