callonce  1.0.0
Header-only memoized call-once wrapper for any C++ callable
memoized_invoke.hh File Reference

Header-only, memoized call-once wrapper for any C++ callable. More...

#include <atomic>
#include <cstdint>
#include <functional>
#include <optional>
#include <thread>
#include <tuple>
#include <type_traits>
#include <utility>
+ Include dependency graph for memoized_invoke.hh:

Classes

struct  fedem::utility::detail::return_storage< T >
 
struct  fedem::utility::detail::return_storage< void >
 
class  fedem::utility::single_threaded
 Execution policy with no synchronisation overhead. The CTAD default. More...
 
class  fedem::utility::lock_free
 Thread-safe, lock-free execution policy. More...
 
class  fedem::utility::memoized_invoke< ExecutionPolicy, F, Args >
 Wraps a callable + argument set, runs it at most once, caches the result. More...
 
struct  fedem::utility::memoized_invoke< ExecutionPolicy, F, Args >::execution_guard
 

Namespaces

 fedem
 Project root namespace.
 
 fedem::utility
 Small, self-contained utility vocabulary types.
 
 fedem::utility::detail
 Implementation details. Not part of the public API; do not use directly.
 

Enumerations

enum class  fedem::utility::detail::execution_state : std::uint8_t { fedem::utility::detail::not_started , fedem::utility::detail::running , fedem::utility::detail::done }
 

Functions

template<typename F >
 fedem::utility::memoized_invoke (F) -> memoized_invoke< single_threaded, F >
 CTAD guide: a nullary callable deduces memoized_invoke<single_threaded, F>. More...
 
template<typename F , typename... Args>
 fedem::utility::requires (sizeof...(Args) > 0 &&std::is_invocable_v< F, Args... >) memoized_invoke(F
 CTAD guide: a callable + arguments deduces single_threaded with decayed Args. More...
 
template<typename ExecutionPolicy , typename F , typename... Args>
requires std::is_invocable_v< F, Args... > auto fedem::utility::make_memoized (F &&func, Args &&... args)
 Build a memoized_invoke with an explicit execution policy. More...
 

Detailed Description

Header-only, memoized call-once wrapper for any C++ callable.

The single public type is fedem::utility::memoized_invoke — a class template that owns a callable together with a fixed argument set, invokes it at most once, and caches the return value. Later calls hand back the cached value without re-executing. Calling with a different argument set invalidates the cache and re-executes.

Synchronisation is a policy parameter: fedem::utility::single_threaded (zero overhead, the CTAD default) or fedem::utility::lock_free (thread-safe via an atomic CAS plus C++20 atomic::wait/notify_all). fedem::utility::make_memoized is the factory used to pick a non-default policy.

See also
https://callonce.fedem.eu/reference/overview