Raven Engine v0.1
A modern 3D Game Engine
Loading...
Searching...
No Matches
Raven::function_cache< Callable > Class Template Reference

Memoization wrapper for a stateless callable. More...

#include <FunctionCache.h>

Public Member Functions

 function_cache (Callable f)
 Constructs a function_cache wrapping the given callable.
void Clear () noexcept
 Removes all entries from the cache.
usize Size () const noexcept
 Returns the number of entries currently in the cache.
template<typename Action>
void Remove (Action &&action) noexcept
 Invokes an action on every cached value, then clears the cache.
template<typename Type, typename Action>
void RemoveIf (Type value, Action &&action) noexcept
 Invokes an action on, then removes, all cached entries whose key contains a matching value.
template<typename Type>
void RemoveIf (Type value) noexcept
 Removes all cached entries whose key contains a matching value, without invoking an action.
template<typename... CallArgs>
decltype(auto) operator() (CallArgs &&... args)
 Calls the wrapped callable with the given arguments, returning a cached result if available.

Static Public Member Functions

static const auto & GetCache () noexcept
 Returns a read-only reference to the underlying cache map.

Detailed Description

template<typename Callable>
class Raven::function_cache< Callable >

Memoization wrapper for a stateless callable.

Wraps a callable and caches results in a static std::flat_map keyed by argument tuple. On each call, the cache is checked first; on a miss the callable is invoked and the result is stored before being returned.

The cache is static, so it is shared across all instances wrapping the same callable type. Use Clear() or the Remove / RemoveIf family to invalidate entries when the underlying data changes.

Template Parameters
CallableA stateless callable type (empty type or function pointer).
Note
Does not support void or bool return types.
Does not support callables that mutate arguments through references or pointers.
Warning
Generates roughly 150 lines of assembly per instantiation.

Constructor & Destructor Documentation

◆ function_cache()

template<typename Callable>
Raven::function_cache< Callable >::function_cache ( Callable f)
inlineexplicit

Constructs a function_cache wrapping the given callable.

Parameters
fThe callable to wrap. Must be stateless (empty type or function pointer).

Member Function Documentation

◆ Clear()

template<typename Callable>
void Raven::function_cache< Callable >::Clear ( )
inlinenoexcept

Removes all entries from the cache.

◆ GetCache()

template<typename Callable>
const auto & Raven::function_cache< Callable >::GetCache ( )
inlinestaticnoexcept

Returns a read-only reference to the underlying cache map.

Returns
Const reference to the internal std::flat_map.

◆ operator()()

template<typename Callable>
template<typename... CallArgs>
decltype(auto) Raven::function_cache< Callable >::operator() ( CallArgs &&... args)
inline

Calls the wrapped callable with the given arguments, returning a cached result if available.

On a cache hit the stored result is returned directly. On a miss the callable is invoked, the result is inserted into the cache, and a reference to the stored value is returned.

Template Parameters
CallArgsThe deduced argument types for this call.
Parameters
argsArguments forwarded to the wrapped callable.
Returns
A reference to the cached result for the given arguments.

◆ Remove()

template<typename Callable>
template<typename Action>
void Raven::function_cache< Callable >::Remove ( Action && action)
inlinenoexcept

Invokes an action on every cached value, then clears the cache.

Useful for performing cleanup (e.g. releasing GPU resources) on all cached results before invalidating them.

Template Parameters
ActionA callable accepting a Return& value.
Parameters
actionThe action to invoke on each cached value.

◆ RemoveIf() [1/2]

template<typename Callable>
template<typename Type>
void Raven::function_cache< Callable >::RemoveIf ( Type value)
inlinenoexcept

Removes all cached entries whose key contains a matching value, without invoking an action.

Convenience overload of RemoveIf(Type, Action) with a no-op action.

Template Parameters
TypeThe argument type to match against in the cache key.
Parameters
valueThe value to match.

◆ RemoveIf() [2/2]

template<typename Callable>
template<typename Type, typename Action>
void Raven::function_cache< Callable >::RemoveIf ( Type value,
Action && action )
inlinenoexcept

Invokes an action on, then removes, all cached entries whose key contains a matching value.

If Type does not appear in the callable's parameter list this is a no-op. The action receives a mutable reference to each matching cached result before its entry is erased.

Template Parameters
TypeThe argument type to match against in the cache key.
ActionA callable accepting a Return& value.
Parameters
valueThe value to match.
actionThe action to invoke on each matched cached value.

◆ Size()

template<typename Callable>
usize Raven::function_cache< Callable >::Size ( ) const
inlinenoexcept

Returns the number of entries currently in the cache.

Returns
Number of cached key-value pairs.

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