| The Gaudi Framework v29r0 (ff2e7097)

- 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 // =======================================================================
27 template <class T, class MutexType = std::mutex>
28 class LockedHandle
29 {
30 public:
31 LockedHandle( T* ptr, MutexType& mut ) : m_ptr( ptr ), m_mutex( &mut ) {}
32 LockedHandle( T* ptr, MutexType* mut ) : m_ptr( ptr ), m_mutex( mut ) {}
33 LockedHandle( T* ptr ) : m_ptr( ptr ), m_mutex( 0 ) {}
34 LockedHandle() : m_ptr( nullptr ), m_mutex( nullptr ) {}
35
36 void set( T* ptr, MutexType* mut )
37 {
38 m_ptr = ptr;
39 m_mutex = mut;
40 }
41
42 void setMutex( MutexType* mut ) { m_mutex = mut; }
43
44 T* get() const { return m_ptr; }
45
46 class Guard
47 {
48 Guard( const Guard& a ) = delete;
49
50 public:
51 Guard( Guard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
52
53 Guard( T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
54 ~Guard() { m_mutex->unlock(); }
55 T& operator*() { return *m_ptr; }
56 T* operator->() { return m_ptr; }
57
58 operator T&() { return *m_ptr; }
59
60 private:
61 T* m_ptr{nullptr};
62 MutexType* m_mutex{nullptr};
63 };
64
65 class ConstGuard
66 {
67 ConstGuard( const ConstGuard& a ) = delete;
68
69 public:
70 ConstGuard( ConstGuard&& a ) : m_ptr( a.m_ptr ), m_mutex( a.m_mutex ) { a.m_mutex = nullptr; }
71
72 ConstGuard( const T* ptr, MutexType& mutex ) : m_ptr( ptr ), m_mutex( &mutex ) { m_mutex->lock(); }
73 ~ConstGuard() { m_mutex->unlock(); }
74 const T& operator*() const { return *m_ptr; }
75 const T* operator->() const { return m_ptr; }
76
77 operator const T&() const { return *m_ptr; }
78
79 private:
80 const T* m_ptr;
81 MutexType* m_mutex;
82 };
83
85 Guard operator*() { return Guard( m_ptr, *m_mutex ); }
86
88 Guard operator->() { return Guard( m_ptr, *m_mutex ); }
89
91 ConstGuard operator*() const { return ConstGuard( m_ptr, *m_mutex ); }
92
94 ConstGuard operator->() const { return ConstGuard( m_ptr, *m_mutex ); }
95
96 operator bool() const { return m_ptr; }
97
98 private:
99 T* m_ptr;
100 mutable MutexType* m_mutex;
101 };
102
103 #endif
LockedHandle::operator->
Guard operator->()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:88
LockedHandle::ConstGuard::~ConstGuard
~ConstGuard()
Definition: LockedHandle.h:73
LockedHandle::ConstGuard::m_ptr
const T * m_ptr
Definition: LockedHandle.h:80
LockedHandle::ConstGuard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:81
LockedHandle::Guard::~Guard
~Guard()
Definition: LockedHandle.h:54
LockedHandle::m_ptr
T * m_ptr
Definition: LockedHandle.h:99
LockedHandle::Guard::Guard
Guard(const Guard &a)=delete
LockedHandle::Guard::Guard
Guard(T *ptr, MutexType &mutex)
Definition: LockedHandle.h:53
LockedHandle::ConstGuard::ConstGuard
ConstGuard(const T *ptr, MutexType &mutex)
Definition: LockedHandle.h:72
LockedHandle::LockedHandle
LockedHandle(T *ptr)
Definition: LockedHandle.h:33
LockedHandle::operator*
ConstGuard operator*() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:91
LockedHandle::Guard::operator->
T * operator->()
Definition: LockedHandle.h:56
LockedHandle::Guard::m_ptr
T * m_ptr
Definition: LockedHandle.h:61
LockedHandle::Guard::operator*
T & operator*()
Definition: LockedHandle.h:55
LockedHandle::ConstGuard::operator->
const T * operator->() const
Definition: LockedHandle.h:75
LockedHandle::Guard
Definition: LockedHandle.h:46
LockedHandle::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:100
LockedHandle
Provides automatic lock/unlock access to a class upon deref of ptr.
Definition: LockedHandle.h:28
LockedHandle::Guard::Guard
Guard(Guard &&a)
Definition: LockedHandle.h:51
LockedHandle::Guard::m_mutex
MutexType * m_mutex
Definition: LockedHandle.h:62
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType *mut)
Definition: LockedHandle.h:32
LockedHandle::operator*
Guard operator*()
Aquire and release the lock before and after the object is accessed.
Definition: LockedHandle.h:85
LockedHandle::ConstGuard
Definition: LockedHandle.h:65
LockedHandle::LockedHandle
LockedHandle(T *ptr, MutexType &mut)
Definition: LockedHandle.h:31
LockedHandle::ConstGuard::ConstGuard
ConstGuard(ConstGuard &&a)
Definition: LockedHandle.h:70
LockedHandle::ConstGuard::operator*
const T & operator*() const
Definition: LockedHandle.h:74
LockedHandle::LockedHandle
LockedHandle()
Definition: LockedHandle.h:34
LockedHandle::setMutex
void setMutex(MutexType *mut)
Definition: LockedHandle.h:42
LockedHandle::operator->
ConstGuard operator->() const
Aquire and release the lock before and after the const object is accessed.
Definition: LockedHandle.h:94