|
| | 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.
|
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
-
| Callable | A 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.
template<typename Callable>
template<typename... CallArgs>
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
-
| CallArgs | The deduced argument types for this call. |
- Parameters
-
| args | Arguments forwarded to the wrapped callable. |
- Returns
- A reference to the cached result for the given arguments.
template<typename Callable>
template<typename Action>
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
-
| Action | A callable accepting a Return& value. |
- Parameters
-
| action | The action to invoke on each cached value. |
template<typename Callable>
template<typename Type, typename Action>
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
-
| Type | The argument type to match against in the cache key. |
| Action | A callable accepting a Return& value. |
- Parameters
-
| value | The value to match. |
| action | The action to invoke on each matched cached value. |