CppCommon  1.0.4.1
C++ Common Library
locker.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_THREADS_LOCKER_H
10 #define CPPCOMMON_THREADS_LOCKER_H
11 
12 namespace CppCommon {
13 
15 
21 template <class T>
22 class Locker
23 {
24 public:
26 
29  explicit Locker(T& primitive) : _primitive(primitive) { _primitive.Lock(); }
30  Locker(const Locker&) = delete;
31  Locker(Locker&&) = delete;
32  ~Locker() { _primitive.Unlock(); }
33 
34  Locker& operator=(const Locker&) = delete;
35  Locker& operator=(Locker&&) = delete;
36 
37 private:
38  T& _primitive;
39 };
40 
42 
48 template <class T>
50 {
51 public:
53 
56  explicit ReadLocker(T& primitive) : _primitive(primitive) { _primitive.LockRead(); }
57  ReadLocker(const ReadLocker&) = delete;
58  ReadLocker(ReadLocker&&) = delete;
59  ~ReadLocker() { _primitive.UnlockRead(); }
60 
61  ReadLocker& operator=(const ReadLocker&) = delete;
63 
64 private:
65  T& _primitive;
66 };
67 
69 
75 template <class T>
77 {
78 public:
80 
83  explicit WriteLocker(T& primitive) : _primitive(primitive) { _primitive.LockWrite(); }
84  WriteLocker(const WriteLocker&) = delete;
85  WriteLocker(WriteLocker&&) = delete;
86  ~WriteLocker() { _primitive.UnlockWrite(); }
87 
88  WriteLocker& operator=(const WriteLocker&) = delete;
90 
91 private:
92  T& _primitive;
93 };
94 
95 } // namespace CppCommon
96 
97 #endif // CPPCOMMON_THREADS_LOCKER_H
Locker synchronization primitive.
Definition: locker.h:23
Locker & operator=(const Locker &)=delete
Locker(Locker &&)=delete
Locker(const Locker &)=delete
Locker & operator=(Locker &&)=delete
Locker(T &primitive)
Default class constructor.
Definition: locker.h:29
Read locker synchronization primitive.
Definition: locker.h:50
ReadLocker & operator=(ReadLocker &&)=delete
ReadLocker(const ReadLocker &)=delete
ReadLocker(T &primitive)
Default class constructor.
Definition: locker.h:56
ReadLocker & operator=(const ReadLocker &)=delete
ReadLocker(ReadLocker &&)=delete
Write locker synchronization primitive.
Definition: locker.h:77
WriteLocker(const WriteLocker &)=delete
WriteLocker & operator=(WriteLocker &&)=delete
WriteLocker(WriteLocker &&)=delete
WriteLocker & operator=(const WriteLocker &)=delete
WriteLocker(T &primitive)
Default class constructor.
Definition: locker.h:83
C++ Common project definitions.
Definition: token_bucket.h:15