Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::CachedData< TType > Class Template Reference

Lazily generates and caches a value with optional custom destructor. More...

#include <spk_cached_data.hpp>

Collaboration diagram for spk::CachedData< TType >:
Collaboration graph

Public Types

using value_type = TType
 Stored value type.
using Generator = std::function<TType()>
 Callable used to lazily produce the cached value.
using Destructor = std::function<void(TType &)>
 Optional cleanup function invoked when the cache is cleared.

Public Member Functions

 CachedData (Generator p_generator, Destructor p_destructor=nullptr)
 Builds a cache with a generator and optional destructor.
 ~CachedData ()
 Destroys any cached value using the destructor callback if set.
 CachedData (const CachedData &)=delete
CachedDataoperator= (const CachedData &)=delete
 CachedData (CachedData &&p_other)=delete
CachedDataoperator= (CachedData &&p_other)=delete
TType & get ()
 Retrieves the cached value, generating it on first access.
const TType & get () const
 Retrieves the cached value, generating it on first access.
TType & operator* ()
 Dereferences to the cached value, generating if needed.
const TType & operator* () const
 Dereferences to the cached value, generating if needed.
TType * operator-> ()
 Member access to the cached value, generating if needed.
const TType * operator-> () const
 Member access to the cached value, generating if needed.
 operator TType & ()
 Implicitly converts to a mutable reference.
 operator const TType & () const
 Implicitly converts to a const reference.
void release () const
 Releases the cached value by invoking destructor if provided.
void configure (Generator p_generator, Destructor p_destructor=nullptr)
 Replaces the generator/destructor pair and clears any cached value.
void set (const TType &p_value)
 Stores a value directly, bypassing the generator.
void set (TType &&p_value)
 Stores a value directly, bypassing the generator (move overload).
std::optional< TType > take ()
 Moves the cached value out of the cache without invoking the destructor.
bool isCached () const
 Checks if a value is currently cached.
TType & refresh ()
 Drops the cached value and regenerates it.

Detailed Description

template<typename TType>
class spk::CachedData< TType >

Lazily generates and caches a value with optional custom destructor.

Template Parameters
TTypecached value type (non-void).
spk::CachedData<std::string> blob([]{ return loadLarge(); });
auto &value = blob.get();
blob.release(); // free
Lazily generates and caches a value with optional custom destructor.
Definition spk_cached_data.hpp:26
TType & get()
Retrieves the cached value, generating it on first access.
Definition spk_cached_data.hpp:116

Constructor & Destructor Documentation

◆ CachedData()

template<typename TType>
spk::CachedData< TType >::CachedData ( Generator p_generator,
Destructor p_destructor = nullptr )
inlineexplicit

Builds a cache with a generator and optional destructor.

Parameters
p_generatorFunction to produce the value on demand.
p_destructorOptional cleanup function for the value.

Member Function Documentation

◆ configure()

template<typename TType>
void spk::CachedData< TType >::configure ( Generator p_generator,
Destructor p_destructor = nullptr )
inline

Replaces the generator/destructor pair and clears any cached value.

Parameters
p_generatorNew generator used to rebuild the value on demand.
p_destructorOptional destructor for the cached value.

◆ get() [1/2]

template<typename TType>
TType & spk::CachedData< TType >::get ( )
inline

Retrieves the cached value, generating it on first access.

Returns
Mutable reference to the cached value.
Here is the caller graph for this function:

◆ get() [2/2]

template<typename TType>
const TType & spk::CachedData< TType >::get ( ) const
inline

Retrieves the cached value, generating it on first access.

Returns
Constant reference to the cached value.

◆ isCached()

template<typename TType>
bool spk::CachedData< TType >::isCached ( ) const
inline

Checks if a value is currently cached.

Returns
true when data exists.

◆ operator const TType &()

template<typename TType>
spk::CachedData< TType >::operator const TType & ( ) const
inline

Implicitly converts to a const reference.

Returns
Const reference to the value.

◆ operator TType &()

template<typename TType>
spk::CachedData< TType >::operator TType & ( )
inline

Implicitly converts to a mutable reference.

Returns
Reference to the value.

◆ operator*() [1/2]

template<typename TType>
TType & spk::CachedData< TType >::operator* ( )
inline

Dereferences to the cached value, generating if needed.

Returns
Mutable reference to the value.

◆ operator*() [2/2]

template<typename TType>
const TType & spk::CachedData< TType >::operator* ( ) const
inline

Dereferences to the cached value, generating if needed.

Returns
Const reference to the value.

◆ operator->() [1/2]

template<typename TType>
TType * spk::CachedData< TType >::operator-> ( )
inline

Member access to the cached value, generating if needed.

Returns
Pointer to the value.

◆ operator->() [2/2]

template<typename TType>
const TType * spk::CachedData< TType >::operator-> ( ) const
inline

Member access to the cached value, generating if needed.

Returns
Const pointer to the value.

◆ refresh()

template<typename TType>
TType & spk::CachedData< TType >::refresh ( )
inline

Drops the cached value and regenerates it.

Returns
Reference to the new value.

◆ set() [1/2]

template<typename TType>
void spk::CachedData< TType >::set ( const TType & p_value)
inline

Stores a value directly, bypassing the generator.

Parameters
p_valueValue to cache.

◆ set() [2/2]

template<typename TType>
void spk::CachedData< TType >::set ( TType && p_value)
inline

Stores a value directly, bypassing the generator (move overload).

Parameters
p_valueValue to cache.

◆ take()

template<typename TType>
std::optional< TType > spk::CachedData< TType >::take ( )
inline

Moves the cached value out of the cache without invoking the destructor.

Returns
Optional containing the moved value if it was cached.

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