Skip to content

Engine → Core → delegate.h

delegate

Syntax

template <typename Return, typename... Args> class delegate<Return(Args...)>

Constructors

AccessSignatureDescription
publicconstexpr delegate() noexcept = default;

Variables

AccessNameTypeDefaultDescription
publicCAN_BINDtemplate <typename Callable> static constexpr boolsizeof(Callable) <= STORAGE_BYTES && alignof(Callable) <= alignof(void *) && std::is_trivially_copyable_v<Callable> && std::is_trivially_destructible_v<Callable> && std::is_invocable_r_v<Return, const Callable &, Args...>True when a callable type can be bound: trivially copyable, pointer-aligned, fits the inline storage and is invocable as const with the delegate's signature.
privatemInvokeinvoker_typenullptr
publicSTORAGE_BYTESstatic constexpr usize2 * sizeof(void *)Bytes of inline storage for the bound target.
privatevoidalignas( *) std::array<std::byte, STORAGE_BYTES> mStorage{}

Functions

Public

bind

template <auto Function> requires std::is_pointer_v<decltype(Function)> && std::is_function_v<std::remove_pointer_t<decltype(Function)>> && std::is_invocable_r_v<Return, decltype(Function), Args...> [[nodiscard]] static delegate bind() noexcept;

Binds a free function known at compile time.

template <auto Method, typename Class> requires std::is_member_function_pointer_v<decltype(Method)> && std::is_invocable_r_v<Return, decltype(Method), Class *, Args...> [[nodiscard]] static delegate bind(
    Class *instance) noexcept;

Binds a member function to an instance: delegate<void(int)>::bind<&foo::bar>(&foo). Const member functions accept a pointer to const. The instance is not owned.

[[nodiscard]] static delegate bind(
    function_pointer function) noexcept;

Binds a function pointer chosen at runtime. A null pointer yields an unbound delegate.

template <typename Callable> requires (!std::is_same_v<std::remove_cvref_t<Callable>, delegate>) && (!std::is_same_v<std::remove_cvref_t<Callable>, function_pointer>) [[nodiscard]] static delegate bind(
    Callable callable) noexcept;

Binds a stateless or small trivially copyable callable (see CAN_BIND) by copying it into the inline storage.

is_bound

[[nodiscard]] constexpr bool is_bound() const noexcept;

True when a target is bound.

reset

constexpr void reset() noexcept;

Unbinds the delegate.

Private

invoke_callable

template <typename Callable> static Return invoke_callable(
    const void *storage,
    Args... args);

invoke_function_pointer

static Return invoke_function_pointer(
    const void *storage,
    Args... args);

invoke_member

template <auto Method, typename Class> static Return invoke_member(
    const void *storage,
    Args... args);

invoke_static

template <auto Function> static Return invoke_static(
    [[maybe_unused]] const void *storage,
    Args... args);

Operator Overloads

Public

operator bool

[[nodiscard]] constexpr explicit operator bool() const noexcept;

Same as is_bound().

operator()

Return operator()(
    Args... args) const;

Invokes the bound target. Calling an unbound delegate asserts and returns a default-constructed value (or nothing for void).

operator==

[[nodiscard]] bool operator==(
    const delegate &other) const noexcept;

Two delegates are equal when they invoke the same target on the same instance (or the same captured state). Unbound delegates are equal to each other. The comparison is of the invoker and the raw inline storage, which is exact for free-function, member, function-pointer and stateless-lambda bindings. A capturing lambda is copied whole, padding included, so its equality is exact only when the closure has no padding (std::has_unique_object_representations_v); otherwise two bindings with equal captures (a bool next to a pointer, say) may compare unequal.