CppCommon  1.0.4.1
C++ Common Library
shared_type.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_SYSTEM_SHARED_TYPE_H
10 #define CPPCOMMON_SYSTEM_SHARED_TYPE_H
11 
12 #include "system/shared_memory.h"
13 
14 #include <memory>
15 #include <string>
16 
17 namespace CppCommon {
18 
20 
26 template <typename T>
28 {
29 public:
31 
34  explicit SharedType(const std::string& name);
35  SharedType(const SharedType<T>&) = delete;
37  ~SharedType() = default;
38 
41 
43  explicit operator bool() const { return (_shared.ptr() != nullptr); }
44 
46  T& operator*() { return ref(); }
48  const T& operator*() const { return ref(); }
49 
51  T* operator->() { return ptr(); }
53  const T* operator->() const { return ptr(); }
54 
56  const std::string& name() const noexcept { return _shared.name(); }
58  size_t size() const noexcept { return _shared.size(); }
59 
61  T* ptr() { return (T*)_shared.ptr(); }
63  const T* ptr() const { return (const T*)_shared.ptr(); }
64 
66  T& ref() { return *ptr(); }
68  const T& ref() const { return *ptr(); }
69 
71  bool owner() const { return _shared.owner(); }
72 
73 private:
74  SharedMemory _shared;
75 };
76 
79 } // namespace CppCommon
80 
81 #include "shared_type.inl"
82 
83 #endif // CPPCOMMON_SYSTEM_SHARED_TYPE_H
Shared memory manager.
Definition: shared_memory.h:29
void * ptr()
Get the shared memory block pointer.
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
const std::string & name() const noexcept
Get the shared memory block name.
Definition: shared_memory.h:48
Shared memory type.
Definition: shared_type.h:28
const std::string & name() const noexcept
Get the shared memory type name.
Definition: shared_type.h:56
const T & operator*() const
Dereference constant shared memory type.
Definition: shared_type.h:48
size_t size() const noexcept
Get the shared memory type size.
Definition: shared_type.h:58
const T * ptr() const
Get the constant shared memory type pointer.
Definition: shared_type.h:63
SharedType< T > & operator=(SharedType< T > &&)=delete
bool owner() const
Get the shared memory type owner flag (true if the new one was created, false if the existing one was...
Definition: shared_type.h:71
const T & ref() const
Get the constant shared memory type reference.
Definition: shared_type.h:68
T * operator->()
Dereference shared memory type member.
Definition: shared_type.h:51
SharedType< T > & operator=(const SharedType< T > &)=delete
SharedType(const std::string &name)
Create a new or open existing shared memory type with a given name.
Definition: shared_type.inl:12
SharedType(SharedType< T > &&)=delete
T * ptr()
Get the shared memory type pointer.
Definition: shared_type.h:61
T & operator*()
Dereference shared memory type.
Definition: shared_type.h:46
const T * operator->() const
Dereference constant shared memory type member.
Definition: shared_type.h:53
T & ref()
Get the shared memory type reference.
Definition: shared_type.h:66
SharedType(const SharedType< T > &)=delete
C++ Common project definitions.
Definition: token_bucket.h:15
Shared memory manager definition.
Shared memory type inline implementation.