ROSE 0.11.145.192
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 {
};
Implements cache data members.
Definition Cached.h:40

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 40 of file Cached.h.

#include <Sawyer/Cached.h>

Inheritance diagram for Sawyer::Cached< T >:
Inheritance graph
[legend]

Public Types

typedef T Value
 Type of stored value.
 

Public Member Functions

 Cached ()
 Initialize to an empty value.
 
bool isCached () const
 Cached state.
 
void clear () const
 Remove cached value.
 
const Sawyer::Optional< Value > & getOptional () const
 Return cached value or nothing.
 
const Valueget () const
 Return the cached value.
 
Valueget ()
 Return the cached value.
 
const Valueoperator* () const
 Return the cached value.
 
Valueoperator* ()
 Return the cached value.
 
const Valueoperator-> () const
 Return pointer to cached value.
 
Valueoperator-> ()
 Return pointer to cached value.
 
void set (const Value &x) const
 Assign a new value.
 
const Cachedoperator= (const Value &x) const
 Assign a new value.
 

Member Typedef Documentation

◆ Value

template<typename T >
typedef T Sawyer::Cached< T >::Value

Type of stored value.

Definition at line 43 of file Cached.h.

Constructor & Destructor Documentation

◆ Cached()

template<typename T >
Sawyer::Cached< T >::Cached ( )
inline

Initialize to an empty value.

Definition at line 70 of file Cached.h.

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 75 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 82 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 92 of file Cached.h.

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

◆ 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 95 of file Cached.h.

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

◆ operator*() [1/2]

template<typename T >
const Value & Sawyer::Cached< T >::operator* ( ) 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 98 of file Cached.h.

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

◆ operator*() [2/2]

template<typename T >
Value & Sawyer::Cached< T >::operator* ( )
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 101 of file Cached.h.

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

◆ operator->() [1/2]

template<typename T >
const Value * Sawyer::Cached< T >::operator-> ( ) const
inline

Return pointer to cached value.

If a value is cached then its address is returned. This should not be called unless a cached value is present (see isCached).

Definition at line 112 of file Cached.h.

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

◆ operator->() [2/2]

template<typename T >
Value * Sawyer::Cached< T >::operator-> ( )
inline

Return pointer to cached value.

If a value is cached then its address is returned. This should not be called unless a cached value is present (see isCached).

Definition at line 115 of file Cached.h.

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

◆ 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 123 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 134 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 137 of file Cached.h.


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