template<typename T>
class Sawyer::Cached< T >
Implements cache data members.
A cache data member either stores a value or stores no value. When its value is present, it can be obtained, and when absent it has no value (not even a default constructed value). The value of a cache data member can be modified even when its object is const. For instance, an object that caches an expensive-to-compute value could be declared thusly:
struct Foo {
Cached<Bar> bar_;
};
When a Foo object is constructed the bar_
member will contain nothing (not even a default-constructed object). Here's a sample implementation that returns the value, performing the expensive calculation and caching the result if necessary. The method is declared to take a const this
pointer since it is conceptually a data member accessor:
const Bar& Foo::bar() const {
if (!bar_.isCached())
bar_ = expensiveComputation();
return bar_.get();
}
Definition at line 42 of file Cached.h.