ROSE  0.11.96.11
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
Sawyer::Optional< Value >
Sawyer::Nothing
Represents no value.
Definition: Optional.h:32
Sawyer::Cached::Value
T Value
Type of stored value.
Definition: Cached.h:45
Sawyer::Cached::operator=
const Cached & operator=(const Value &x) const
Assign a new value.
Definition: Cached.h:104
Sawyer::Cached::get
const Value & get() const
Return the cached value.
Definition: Cached.h:79
Sawyer::Cached::getOptional
const Sawyer::Optional< Value > & getOptional() const
Return cached value or nothing.
Definition: Cached.h:90
Sawyer::Cached::isCached
bool isCached() const
Cached state.
Definition: Cached.h:62
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::Optional::get
const Value & get() const
Dereference to obtain value.
Definition: Optional.h:197
Sawyer::Cached::set
void set(const Value &x) const
Assign a new value.
Definition: Cached.h:101
Sawyer::Cached::get
Value & get()
Return the cached value.
Definition: Cached.h:82
Sawyer::Cached::clear
void clear() const
Remove cached value.
Definition: Cached.h:69
Sawyer::Cached
Implements cache data members.
Definition: Cached.h:42