ROSE  0.11.96.11
Public Types | Public Member Functions | List of all members
Sawyer::Cached< T > Class Template Reference

Description

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 { //typical accessor declaration
if (!bar_.isCached())
bar_ = expensiveComputation();
return bar_.get();
}

Definition at line 42 of file Cached.h.

#include <Cached.h>

Public Types

typedef T Value
 Type of stored value.
 

Public Member Functions

bool isCached () const
 Cached state. More...
 
void clear () const
 Remove cached value. More...
 
const Sawyer::Optional< Value > & getOptional () const
 Return cached value or nothing. More...
 
const Valueget () const
 Return the cached value. More...
 
Valueget ()
 Return the cached value. More...
 
void set (const Value &x) const
 Assign a new value. More...
 
const Cachedoperator= (const Value &x) const
 Assign a new value. More...
 

Member Function Documentation

◆ isCached()

template<typename T >
bool Sawyer::Cached< T >::isCached ( ) const
inline

Cached state.

Returns true when a value is cached, or false when nothing is cached.

Definition at line 62 of file Cached.h.

◆ clear()

template<typename T >
void Sawyer::Cached< T >::clear ( ) const
inline

Remove cached value.

Any value that might be cached is removed from the cache. If the cache is already empty then nothing happens.

Definition at line 69 of file Cached.h.

◆ get() [1/2]

template<typename T >
const Value& Sawyer::Cached< T >::get ( ) const
inline

Return the cached value.

If a value is cached then it is returned. This should not be called unless a cached value is present (see isCached). An alternative is to use getOptional.

Definition at line 79 of file Cached.h.

References Sawyer::Optional< T >::get().

Here is the call graph for this function:

◆ get() [2/2]

template<typename T >
Value& Sawyer::Cached< T >::get ( )
inline

Return the cached value.

If a value is cached then it is returned. This should not be called unless a cached value is present (see isCached). An alternative is to use getOptional.

Definition at line 82 of file Cached.h.

References Sawyer::Optional< T >::get().

Here is the call graph for this function:

◆ getOptional()

template<typename T >
const Sawyer::Optional<Value>& Sawyer::Cached< T >::getOptional ( ) const
inline

Return cached value or nothing.

If a value is cached then it is returned, otherwise nothing is returned.

Definition at line 90 of file Cached.h.

◆ set()

template<typename T >
void Sawyer::Cached< T >::set ( const Value x) const
inline

Assign a new value.

The specified value is cached. Note that if the cache is holding a pointer to a value then caching a null pointer is different than caching nothing. Setting the value to an instance of Nothing has the same effect as calling clear.

Definition at line 101 of file Cached.h.

◆ operator=()

template<typename T >
const Cached& Sawyer::Cached< T >::operator= ( const Value x) const
inline

Assign a new value.

The specified value is cached. Note that if the cache is holding a pointer to a value then caching a null pointer is different than caching nothing. Setting the value to an instance of Nothing has the same effect as calling clear.

Definition at line 104 of file Cached.h.


The documentation for this class was generated from the following file: