CelestialCalendar 1.0.0 · unreleased development @ 67a7c4c79a4a4a6aa7e703c9e6f0039ae34bee82
Loading...
Searching...
No Matches
util::cache Namespace Reference

Classes

struct  TupleHash

Concepts

concept  CacheKeyElement
 What one element of a cache key must support: equality, hashing, and being copyable — the key is copied into a tuple and again into the map. Element-wise on purpose: TupleHash's operator() takes any tuple and fails inside its body, so constraining it would constrain nothing.
concept  FunctionConvertible
 Checks if a type can be converted to std::function.

Functions

template<typename RetType, typename... Args>
requires std::is_object_v<RetType> and std::copy_constructible<RetType> and (... and CacheKeyElement<std::decay_t<Args>>)
auto make_cached (const std::function< RetType(Args...)> &func)
 A wrapper that caches the result of a function.
auto cache_func (const FunctionConvertible auto &func)
 A wrapper that caches the result of a function.

Function Documentation

◆ cache_func()

auto util::cache::cache_func ( const FunctionConvertible auto & func)
inlinenodiscard

A wrapper that caches the result of a function.

Parameters
funcThe function to cache.
Returns
The cached closure.

◆ make_cached()

template<typename RetType, typename... Args>
requires std::is_object_v<RetType> and std::copy_constructible<RetType> and (... and CacheKeyElement<std::decay_t<Args>>)
auto util::cache::make_cached ( const std::function< RetType(Args...)> & func)
inlinenodiscard

A wrapper that caches the result of a function.

Parameters
funcThe function to cache. Must be pure — same arguments ⇒ same result — and have a single, non-generic call signature (deduced through std::function).
Returns
The cached closure (copies share one cache, #78; not assignable). Thread-safe.
Note
The constraints state what the unordered_map below already enforces, so misuse is rejected here instead of inside its instantiation. copy_constructible also rules out void; is_object_v rules out reference returns — the hit path forms const RetType*.
The cache never erases and never evicts — an entry lives as long as its shared state. The hit path's out-of-lock copy depends on that: references into an unordered_map survive rehash, only erase would invalidate them. No LRU and no clear, by the same invariant. The key space is bounded only by the caller's contract.