template<typename T, size_t AllocatedSize = 4>
class toy::CallbacksPool< T, AllocatedSize >
Fixed-capacity array of function pointers for observer-style notification.
Stores up to AllocatedSize callbacks of type void (*)(T). No heap allocation: storage is inline in the object. add duplicates are ignored (same pointer not stored twice). remove swaps the last subscriber into the removed slot. call invokes subscribers in registration order.
- Template Parameters
-
| T | Callback argument type (passed by value to call()). |
| AllocatedSize | Maximum number of subscribers. Must be > 0 (default 4). |
Key Features
- Bounded storage: Capacity fixed at compile time;
add returns false when full (assert in debug).
- constexpr: Construction,
add, remove, reset, subscribersAmount, and call are usable in constant evaluation when the callbacks allow it.
- noexcept: All members are
noexcept.
- Duplicate prevention: Registering the same function pointer twice is a no-op; no duplicate entries.
Usage Example
void onEvent(int value) { (void)value; }
Fixed-capacity array of function pointers for observer-style notification.
Definition callbacks_pool.hpp:87
constexpr void reset() noexcept
Clears all callbacks from the pool.
Definition callbacks_pool.inl:63
constexpr bool add(void(*method)(T arg)) noexcept
Adds a callback to the pool.
Definition callbacks_pool.inl:33
constexpr bool remove(void(*method)(T arg)) noexcept
Removes a callback from the pool.
Definition callbacks_pool.inl:51
constexpr void call(T arg) const noexcept
Invokes all registered callbacks with the given parameter.
Definition callbacks_pool.inl:73
Umbrella header for the engine core module.
Performance Characteristics
- Construction / reset: O(1).
- add: O(n) in current subscriber count (linear scan for duplicate).
- remove: O(n) worst case (linear search).
- call: O(n) invocations for n subscribers.
- subscribersAmount: O(1).
Safety Guarantees
- Preconditions:
nullptr passed to add is asserted in debug; otherwise undefined behavior.
- Full pool:
add when full returns false; asserted in debug.
- Lifetime: The pool stores raw function pointers only; it does not manage callee lifetime.
- Note
- Registering the same function pointer twice does not increase the subscriber count; the second
add returns true without adding a duplicate.
- Warning
- Function pointers must remain valid for as long as they may be invoked through the pool.
template<typename T, size_t AllocatedSize>
Adds a callback to the pool.
This method registers a callback function in the pool. If the callback is already registered, the operation succeeds but does not create a duplicate entry. If the pool is full, the operation fails.
- Parameters
-
| method | A pointer to the callback function to register. |
- Returns
true if the callback was successfully added, false if the pool is full.
- Precondition
- The method must not be
nullptr.
- Note
- If the callback is already registered, the method returns
true without adding a duplicate.
-
If the pool is full, the method returns
false and asserts in debug mode.