CppCommon  1.0.4.1
C++ Common Library
shared_memory.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_SYSTEM_SHARED_MEMORY_H
10 #define CPPCOMMON_SYSTEM_SHARED_MEMORY_H
11 
12 #include "errors/exceptions.h"
13 
14 #include <memory>
15 #include <string>
16 
17 namespace CppCommon {
18 
20 
29 {
30 public:
32 
36  explicit SharedMemory(const std::string& name, size_t size);
37  SharedMemory(const SharedMemory&) = delete;
38  SharedMemory(SharedMemory&& shmem) = delete;
39  ~SharedMemory();
40 
41  SharedMemory& operator=(const SharedMemory&) = delete;
42  SharedMemory& operator=(SharedMemory&& shmem) = delete;
43 
45  explicit operator bool() const { return (ptr() != nullptr); }
46 
48  const std::string& name() const noexcept { return _name; }
50  size_t size() const noexcept { return _size; }
51 
53  void* ptr();
55  const void* ptr() const;
56 
58  bool owner() const;
59 
60 private:
61  class Impl;
62 
63  Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
64  const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
65 
66  static const size_t StorageSize = 64;
67  static const size_t StorageAlign = 8;
68  alignas(StorageAlign) std::byte _storage[StorageSize];
69 
70  std::string _name;
71  size_t _size;
72 };
73 
76 } // namespace CppCommon
77 
78 #endif // CPPCOMMON_SYSTEM_SHARED_MEMORY_H
Shared memory manager.
Definition: shared_memory.h:29
void * ptr()
Get the shared memory block pointer.
SharedMemory & operator=(const SharedMemory &)=delete
bool owner() const
Get the shared memory owner flag (true if the new one was created, false if the existing one was open...
size_t size() const noexcept
Get the shared memory block size.
Definition: shared_memory.h:50
SharedMemory(const std::string &name, size_t size)
Create a new or open existing block of shared memory with a given name and size.
const std::string & name() const noexcept
Get the shared memory block name.
Definition: shared_memory.h:48
SharedMemory & operator=(SharedMemory &&shmem)=delete
SharedMemory(SharedMemory &&shmem)=delete
SharedMemory(const SharedMemory &)=delete
Exceptions definition.
C++ Common project definitions.
Definition: token_bucket.h:15