21template <
typename EventType, std::
size_t InlineSize = 32>
23 using StubFn = void (*)(
void* instance,
const EventType& event);
24 using DestroyFn = void (*)(
void* instance)
noexcept;
25 using MoveFn = void (*)(
void* destStorage,
void*& destInstance,
void*& srcInstance)
noexcept;
35 typename Decayed = std::decay_t<Handler>,
36 typename = std::enable_if_t<
38 std::is_invocable_r_v<
void, Decayed&, const EventType&>
44 std::is_nothrow_destructible_v<Decayed>,
45 "Handler destructor must be noexcept"
48 sizeof(Decayed) <= InlineSize,
49 "Handler size exceeds FastDelegate inline storage size! Reduce captured state or increase InlineSize."
52 alignof(Decayed) <=
alignof(std::max_align_t),
53 "Handler alignment requirement exceeds inline storage alignment."
56 std::is_nothrow_move_constructible_v<Decayed>,
57 "Handler must be nothrow move constructible."
60 void* storage =
static_cast<void*
>(_inlineStorage);
61 ::new (storage) Decayed(std::forward<Handler>(handler));
64 _destroy = [](
void* instance)
noexcept {
65 reinterpret_cast<Decayed*
>(instance)->~Decayed();
68 _move = [](
void* destStorage,
void*& destInstance,
void*& srcInstance)
noexcept {
69 auto* src =
reinterpret_cast<Decayed*
>(srcInstance);
70 ::new (destStorage) Decayed(std::move(*src));
72 destInstance = destStorage;
73 srcInstance =
nullptr;
76 _stub = [](
void* instance,
const EventType& event) {
77 (*
reinterpret_cast<Decayed*
>(instance))(event);
84 void (*stub)(
void* instance,
const EventType& event),
85 void (*moveFn)(
void* destStorage,
void*& destInstance,
void*& srcInstance)
noexcept,
86 void (*destroyFn)(
void* instance)
noexcept
89 void* storage =
static_cast<void*
>(_inlineStorage);
94 _move(storage, _instance, srcObj);
105 moveFrom(std::move(rhs));
112 moveFrom(std::move(rhs));
123 void invoke(
const EventType& event)
const
125 assert(_stub &&
"Invoking an empty EventHandlerDelegate");
131 _stub(_instance, event);
141 explicit operator bool() const noexcept
143 return _stub !=
nullptr;
163 _destroy = rhs._destroy;
168 static_cast<void*
>(_inlineStorage),
177 rhs._destroy =
nullptr;
182 alignas(std::max_align_t) std::byte _inlineStorage[InlineSize > 0 ? InlineSize : 1]{};
184 void* _instance =
nullptr;
185 StubFn _stub =
nullptr;
186 DestroyFn _destroy =
nullptr;
187 MoveFn _move =
nullptr;
191template <
typename EventType, std::
size_t InlineSize = 32>
Lightweight non-allocating delegate wrapper with 32-byte Small Buffer Optimization (SBO)....
Definition FastDelegate.hpp:22
void invoke(const EventType &event) const
Invoke wrapped handler directly with concrete event.
Definition FastDelegate.hpp:123
void operator()(const EventType &event) const
Function call operator for callable ergonomics.
Definition FastDelegate.hpp:135
~EventHandlerDelegate()
Definition FastDelegate.hpp:98
EventHandlerDelegate & operator=(EventHandlerDelegate &&rhs) noexcept
Definition FastDelegate.hpp:108
EventHandlerDelegate & operator=(const EventHandlerDelegate &)=delete
void reset() noexcept
Reset delegate to empty state.
Definition FastDelegate.hpp:147
EventHandlerDelegate(void *srcObj, void(*stub)(void *instance, const EventType &event), void(*moveFn)(void *destStorage, void *&destInstance, void *&srcInstance) noexcept, void(*destroyFn)(void *instance) noexcept) noexcept
Construct delegate from type-erased thunk operations.
Definition FastDelegate.hpp:82
EventHandlerDelegate(EventHandlerDelegate &&rhs) noexcept
Definition FastDelegate.hpp:103
EventHandlerDelegate() noexcept=default
EventHandlerDelegate(const EventHandlerDelegate &)=delete
Definition Application.hpp:16