| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

LockedHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_LOCKEDHANDLE
2 #define GAUDIKERNEL_LOCKEDHANDLE 1
3
4 #include <mutex>
5
6 // =======================================================================
29 template <class T, class MutexType=std::mutex>
30 class LockedHandle {
31 public:
32
33 LockedHandle(T* ptr, MutexType& mut) : m_ptr(ptr), m_mutex(&mut) {}
34 LockedHandle(T* ptr, MutexType* mut) : m_ptr(ptr), m_mutex(mut) {}
35 LockedHandle(T* ptr) : m_ptr(ptr), m_mutex(0) {}
36 LockedHandle() : m_ptr(nullptr), m_mutex(nullptr) {}
37
38 void set(T* ptr, MutexType* mut) {
39 m_ptr = ptr;
40 m_mutex = mut;
41 }
42
43 void setMutex(MutexType* mut) { m_mutex = mut; }
44
45 T* get() const { return m_ptr; }
46
47 class Guard {
48 Guard(const Guard &a) = delete;
49 public:
50 Guard(Guard &&a)
51 : m_ptr(a.m_ptr),
52 m_mutex(a.m_mutex)
53 {
54 a.m_mutex=nullptr;
55 }
56
57 Guard(T *ptr,MutexType &mutex)
58 : m_ptr(ptr),
59 m_mutex(&mutex)
60 {
61 m_mutex->lock();
62 }
63 ~Guard() {
64 m_mutex->unlock();
65 }
66 T &operator*() { return *m_ptr; }
67 T *operator->() { return m_ptr; }
68
69 operator T&() {
70 return *m_ptr;
71 }
72
73 private:
74 T *m_ptr {nullptr};
75 MutexType *m_mutex {nullptr};
76 };
77
78 class ConstGuard {
79 ConstGuard(const ConstGuard &a) = delete;
80 public:
81 ConstGuard(ConstGuard &&a)
82 : m_ptr(a.m_ptr),
83 m_mutex(a.m_mutex)
84 {
85 a.m_mutex=nullptr;
86 }
87
88 ConstGuard(const T *ptr,MutexType &mutex)
89 : m_ptr(ptr),
90 m_mutex(&mutex)
91 {
92 m_mutex->lock();
93 }
94 ~ConstGuard() {
95 m_mutex->unlock();
96 }
97 const T &operator*() const { return *m_ptr; }
98 const T *operator->() const { return m_ptr; }
99
100 operator const T&() const {
101 return *m_ptr;
102 }
103
104 private:
105 const T *m_ptr;
106 MutexType *m_mutex;
107 };
108
109
111 Guard operator*() { return Guard(m_ptr,*m_mutex); }
112
114 Guard operator->() { return Guard(m_ptr,*m_mutex); }
115
117 ConstGuard operator*() const { return ConstGuard(m_ptr,*m_mutex); }
118
120 ConstGuard operator->() const { return ConstGuard(m_ptr,*m_mutex); }
121
122 operator bool() const {
123 return m_ptr;
124 }
125 private:
126
127 T *m_ptr;
128 mutable MutexType *m_mutex;
129 };
130
131
132
133 #endif
LockedHandle::operator->
Guard operator->()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:114
LockedHandle::ConstGuard::~ConstGuard
~ConstGuard()
Definition: LockedHandle.h:94
LockedHandle::ConstGuard::m_ptr
const T * m_ptr
Definition: LockedHandle.h:105
LockedHandle::ConstGuard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:106
LockedHandle::Guard::~Guard
~Guard()
Definition: LockedHandle.h:63
LockedHandle::m_ptr
T * m_ptr
Definition: LockedHandle.h:127
LockedHandle::Guard::Guard
Guard(const Guard &a)=delete
LockedHandle::Guard::Guard
Guard(T *ptr, MutexType &mutex)
Definition: LockedHandle.h:57
LockedHandle::ConstGuard::ConstGuard
ConstGuard(const T *ptr, MutexType &mutex)
Definition: LockedHandle.h:88
LockedHandle::LockedHandle
LockedHandle(T *ptr)
Definition: LockedHandle.h:35
LockedHandle::operator*
ConstGuard operator*() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:117
LockedHandle::Guard::operator->
T * operator->()
Definition: LockedHandle.h:67
LockedHandle::Guard::m_ptr
T * m_ptr
Definition: LockedHandle.h:74
LockedHandle::Guard::operator*
T & operator*()
Definition: LockedHandle.h:66
LockedHandle::ConstGuard::operator->
const T * operator->() const
Definition: LockedHandle.h:98
LockedHandle::Guard
Definition: LockedHandle.h:47
LockedHandle::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:128
LockedHandle
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:30
LockedHandle::Guard::Guard
Guard(Guard &&a)
Definition: LockedHandle.h:50
LockedHandle::Guard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:75
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType *mut)
Definition: LockedHandle.h:34
LockedHandle::operator*
Guard operator*()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:111
LockedHandle::ConstGuard
Definition: LockedHandle.h:78
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType &mut)
Definition: LockedHandle.h:33
LockedHandle::ConstGuard::ConstGuard
ConstGuard(ConstGuard &&a)
Definition: LockedHandle.h:81
LockedHandle::ConstGuard::operator*
const T & operator*() const
Definition: LockedHandle.h:97
LockedHandle::LockedHandle
LockedHandle()
Definition: LockedHandle.h:36
LockedHandle::setMutex
void setMutex(MutexType *mut)
Definition: LockedHandle.h:43
LockedHandle::operator->
ConstGuard operator->() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:120