VolViz
A volume visualization tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
AtomicCache.h
Go to the documentation of this file.
1 #ifndef VolViz_AtomicCache_h
2 #define VolViz_AtomicCache_h
3 
4 #include <gsl/gsl>
5 
6 #include <atomic>
7 
8 #pragma clang diagnostic ignored "-Wpadded"
9 
10 namespace VolViz {
11 
22 template <class T> class AtomicCache {
23 public:
28  template <class FetchOp>
29  AtomicCache(FetchOp fetchOperation)
30  : fetchOperation_(fetchOperation) {
31  Expects(fetchOperation_);
32  dirtyFlag_.clear();
33  }
34 
38  inline void markAsDirty() noexcept { dirtyFlag_.clear(); }
39 
43  inline operator T const &() const noexcept {
44  if (dirtyFlag_.test_and_set()) return value_;
45 
46  Expects(fetchOperation_);
47  value_ = std::move(fetchOperation_());
48  return value_;
49  }
50 
51 private:
53  mutable T value_;
54 
56  mutable std::atomic_flag dirtyFlag_;
57 
59  std::function<T()> const fetchOperation_;
60 };
61 
62 } // namespace VolViz
63 
64 #endif // VolViz_AtomicCache_h
void markAsDirty() noexcept
Definition: AtomicCache.h:38
T value_
The caced value.
Definition: AtomicCache.h:53
std::atomic_flag dirtyFlag_
The atomic dirty flag.
Definition: AtomicCache.h:56
std::function< T()> const fetchOperation_
The fetch operation.
Definition: AtomicCache.h:59
Definition: AtomicCache.h:22
AtomicCache(FetchOp fetchOperation)
Definition: AtomicCache.h:29