ROSE  0.11.145.0
Cached.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_Cached_H
9 #define Sawyer_Cached_H
10 
11 #include <Sawyer/Sawyer.h>
12 #include <Sawyer/Optional.h>
13 #include <boost/serialization/access.hpp>
14 #include <boost/serialization/nvp.hpp>
15 
16 namespace Sawyer {
17 
41 template<typename T>
42 class Cached {
43 public:
45  typedef T Value;
46 
47 private:
48  mutable Sawyer::Optional<Value> value_;
49 
50 private:
51  friend class boost::serialization::access;
52 
53  template<class S>
54  void serialize(S &s, const unsigned /*version*/) {
55  s & BOOST_SERIALIZATION_NVP(value_);
56  }
57 
58 public:
62  bool isCached() const {
63  return bool(value_);
64  }
65 
69  void clear() const { // const is intended
70  value_ = Sawyer::Nothing();
71  }
72 
79  const Value& get() const {
80  return value_.get();
81  }
82  Value& get() {
83  return value_.get();
84  }
91  return value_;
92  }
93 
101  void set(const Value &x) const { // const is intentional
102  value_ = x;
103  }
104  const Cached& operator=(const Value &x) const { // const is intentional
105  value_ = x;
106  return *this;
107  }
109 };
110 
111 } // namespace
112 
113 #endif
const Sawyer::Optional< Value > & getOptional() const
Return cached value or nothing.
Definition: Cached.h:90
const Cached & operator=(const Value &x) const
Assign a new value.
Definition: Cached.h:104
Name space for the entire library.
Definition: FeasiblePath.h:767
T Value
Type of stored value.
Definition: Cached.h:45
void clear() const
Remove cached value.
Definition: Cached.h:69
void set(const Value &x) const
Assign a new value.
Definition: Cached.h:101
const Value & get() const
Dereference to obtain value.
Definition: Optional.h:197
bool isCached() const
Cached state.
Definition: Cached.h:62
Implements cache data members.
Definition: Cached.h:42
Represents no value.
Definition: Optional.h:32