VolViz
A volume visualization tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
AtomicWrapper.h
Go to the documentation of this file.
1 #ifndef VolViz_AtomicWrapper_h
2 #define VolViz_AtomicWrapper_h
3 
4 #include <mutex>
5 #include <functional>
6 
7 #pragma clang diagnostic ignored "-Wpadded"
8 
9 namespace VolViz {
10 
12 template <class T> class DefaultSetPolicy {
13 protected:
14  inline void set(T &dest, T const &src) const noexcept { dest = src; }
15  inline void set(T &dest, T &&src) const noexcept { dest = std::move(src); }
16 };
17 
21 template <class T> class SetAndNotifyPolicy : public DefaultSetPolicy<T> {
22 public:
25  std::function<void(T const &)> beforeAction;
26 
29  std::function<void(T const &)> afterAction;
30 
31 protected:
32  template <class Arg> inline void set(T &dest, Arg &&src) const noexcept {
34  DefaultSetPolicy<T>::set(dest, std::forward<Arg>(src));
35  if (afterAction) afterAction(dest);
36  }
37 };
38 
47 template <class T, template <class> class SetPolicy = DefaultSetPolicy>
48 class AtomicWrapper : public SetPolicy<T> {
49  using SetPol = SetPolicy<T>;
50 
51 public:
53  explicit AtomicWrapper(T const &obj) : obj_(obj) {}
55  explicit AtomicWrapper(T &&obj) : obj_(std::move(obj)) {}
56 
57  AtomicWrapper(AtomicWrapper const &) = delete;
58  AtomicWrapper &operator=(AtomicWrapper const &) = delete;
59 
61  operator T() const {
62  std::lock_guard<std::mutex> lock(mutex_);
63  auto cpy = obj_;
64  return cpy;
65  }
66 
68  AtomicWrapper &operator=(T const &rhs) {
69  std::lock_guard<std::mutex> lock(mutex_);
70 
71  SetPol::set(obj_, rhs);
72 
73  return *this;
74  }
75 
78  std::lock_guard<std::mutex> lock(mutex_);
79 
80  SetPol::set(obj_, std::move(rhs));
81 
82  return *this;
83  }
84 
85 private:
87  T obj_;
88 
90  mutable std::mutex mutex_;
91 };
92 
93 } // namespace VolViz
94 
95 #endif // VolViz_AtomicWrapper_h
Definition: AtomicWrapper.h:48
AtomicWrapper & operator=(AtomicWrapper const &)=delete
T obj_
Thre wrapped object.
Definition: AtomicWrapper.h:87
void set(T &dest, T const &src) const noexcept
Definition: AtomicWrapper.h:14
void set(T &dest, T &&src) const noexcept
Definition: AtomicWrapper.h:15
GLenum src
Definition: glad.h:3261
std::mutex mutex_
Mutex preventing simultaneous access.
Definition: AtomicWrapper.h:90
std::function< void(T const &)> afterAction
Definition: AtomicWrapper.h:29
std::function< void(T const &)> beforeAction
Definition: AtomicWrapper.h:25
Default set policy for AtomicWrapper class, justs sets dest to src.
Definition: AtomicWrapper.h:12
GLhandleARB obj
Definition: glad.h:8790
void set(T &dest, Arg &&src) const noexcept
Definition: AtomicWrapper.h:32
AtomicWrapper & operator=(T &&rhs)
Reassign the wrapped object in a thread safe manner (move version).
Definition: AtomicWrapper.h:77
Definition: AtomicWrapper.h:21
AtomicWrapper(T const &obj)
Initialized the wrapper with an object.
Definition: AtomicWrapper.h:53
AtomicWrapper(T &&obj)
Initialized the wrapper with an object (move constructor)
Definition: AtomicWrapper.h:55
AtomicWrapper & operator=(T const &rhs)
Reassign the wrapped object in a thread safe manner.
Definition: AtomicWrapper.h:68