ROSE 0.11.145.192
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://gitlab.com/charger7534/sawyer.git.
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
14namespace Sawyer {
15
39template<typename T>
40class Cached {
41public:
43 typedef T Value;
44
45private:
46 mutable Sawyer::Optional<Value> value_;
47
48#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
49private:
50 friend class boost::serialization::access;
51
52 template<class S>
53 void serialize(S &s, const unsigned /*version*/) {
54 s & BOOST_SERIALIZATION_NVP(value_);
55 }
56#endif
57
58#ifdef SAWYER_HAVE_CEREAL
59private:
60 friend class cereal::access;
61
62 template<class Archive>
63 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
64 archive(CEREAL_NVP(value_));
65 }
66#endif
67
68public:
70 Cached() {}
71
75 bool isCached() const {
76 return bool(value_);
77 }
78
82 void clear() const { // const is intended
83 value_ = Sawyer::Nothing();
84 }
85
92 const Value& get() const {
93 return value_.get();
94 }
96 return value_.get();
97 }
98 const Value& operator*() const {
99 return value_.get();
100 }
102 return value_.get();
103 }
112 const Value* operator->() const {
113 return &value_.get();
114 }
116 return &value_.get();
117 }
124 return value_;
125 }
126
134 void set(const Value &x) const { // const is intentional
135 value_ = x;
136 }
137 const Cached& operator=(const Value &x) const { // const is intentional
138 value_ = x;
139 return *this;
140 }
142};
143
144} // namespace
145
146#endif
Implements cache data members.
Definition Cached.h:40
bool isCached() const
Cached state.
Definition Cached.h:75
const Cached & operator=(const Value &x) const
Assign a new value.
Definition Cached.h:137
const Value & get() const
Return the cached value.
Definition Cached.h:92
T Value
Type of stored value.
Definition Cached.h:43
Value * operator->()
Return pointer to cached value.
Definition Cached.h:115
const Value & operator*() const
Return the cached value.
Definition Cached.h:98
const Value * operator->() const
Return pointer to cached value.
Definition Cached.h:112
void set(const Value &x) const
Assign a new value.
Definition Cached.h:134
Value & operator*()
Return the cached value.
Definition Cached.h:101
Value & get()
Return the cached value.
Definition Cached.h:95
const Sawyer::Optional< Value > & getOptional() const
Return cached value or nothing.
Definition Cached.h:123
void clear() const
Remove cached value.
Definition Cached.h:82
Cached()
Initialize to an empty value.
Definition Cached.h:70
Represents no value.
Definition Optional.h:36
Holds a value or nothing.
Definition Optional.h:56
const Value & get() const
Dereference to obtain value.
Definition Optional.h:229
Sawyer support library.