Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
fixed_function.hpp File Reference

FixedFunction<Signature, Capacity> - a move-only callable wrapper with inline storage that never touches the heap. More...

#include <cstddef>
#include <new>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Classes

class  pulp::runtime::FixedFunction< R(Args...), Capacity, Alignment >

Namespaces

namespace  pulp
namespace  pulp::runtime

Detailed Description

FixedFunction<Signature, Capacity> - a move-only callable wrapper with inline storage that never touches the heap.

std::function may allocate when it is constructed or copied and requires a copyable target, so it does not belong on a realtime path. FixedFunction stores the target inside the object. A target that does not fit is a compile error, not a silent allocation, so a realtime queue of callables stays allocation-free by construction.

FixedFunction<void(int), 32> fn = [gain = &gain_](int v) { gain->store(v); };
fn(7);

Contract:

  • Construction, move, call and destruction never allocate, lock or throw.
  • The target must fit Capacity bytes, need no stricter alignment than Alignment, and be nothrow move-constructible. Each is a static_assert.
  • Move-only. Move-only targets (a lambda owning a unique_ptr) are allowed.
  • Invoking an empty FixedFunction is undefined behaviour. Test with explicit operator bool first when it can be empty.
  • Not thread-safe: one thread at a time may call, move or destroy an object, like any non-atomic value. Hand it between threads through a queue.

The target is destroyed with the wrapper, so destroying a FixedFunction on the audio thread runs the target's destructor there. Capture trivially destructible state (pointers, numbers) for objects that live on the audio thread.