ToyGine2 26.2.0
Game Engine for retro consoles
Loading...
Searching...
No Matches
toy::CallbacksPool< T, AllocatedSize > Class Template Reference

Fixed-capacity array of function pointers for observer-style notification. More...

Classes

struct  StaticCallback
 Internal structure to store a callback function pointer. More...

Public Member Functions

constexpr bool add (void(*method)(T arg)) noexcept
 Adds a callback to the pool.
constexpr bool remove (void(*method)(T arg)) noexcept
 Removes a callback from the pool.
constexpr void reset () noexcept
 Clears all callbacks from the pool.
constexpr size_t subscribersAmount () const noexcept
 Returns the number of registered callbacks.
constexpr void call (T arg) const noexcept
 Invokes all registered callbacks with the given parameter.

Private Attributes

array< StaticCallback, AllocatedSize > _callbacks
 Fixed-size array storing registered callbacks.
size_t _subscribersCount {0}
 Active callbacks count.

Detailed Description

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
TCallback argument type (passed by value to call()).
AllocatedSizeMaximum 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

#include "core.hpp"
void onEvent(int value) { (void)value; }
pool.add(onEvent);
pool.call(42);
pool.remove(onEvent);
pool.reset();
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.

Member Function Documentation

◆ add()

template<typename T, size_t AllocatedSize>
bool toy::CallbacksPool< T, AllocatedSize >::add ( void(* method )(T arg))
constexprnoexcept

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
methodA 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.

◆ call()

template<typename T, size_t AllocatedSize>
void toy::CallbacksPool< T, AllocatedSize >::call ( T arg) const
constexprnoexcept

Invokes all registered callbacks with the given parameter.

This method calls all registered callback functions with the specified parameter value. Callbacks are invoked in the order they were added to the pool.

Parameters
argThe parameter value to pass to all registered callbacks.

◆ remove()

template<typename T, size_t AllocatedSize>
bool toy::CallbacksPool< T, AllocatedSize >::remove ( void(* method )(T arg))
constexprnoexcept

Removes a callback from the pool.

This method unregisters a callback function from the pool. If the callback is not found, the operation has no effect.

Parameters
methodA pointer to the callback function to remove.
Returns
true if the callback was found and removed, false otherwise.
Note
Removing a non-existent callback is safe and returns false.

◆ reset()

template<typename T, size_t AllocatedSize>
void toy::CallbacksPool< T, AllocatedSize >::reset ( )
constexprnoexcept

Clears all callbacks from the pool.

This method removes all registered callbacks, resetting the pool to an empty state.

Postcondition
The pool is empty (no callbacks registered).
All callback slots are available for new subscriptions.

◆ subscribersAmount()

template<typename T, size_t AllocatedSize>
size_t toy::CallbacksPool< T, AllocatedSize >::subscribersAmount ( ) const
nodiscardconstexprnoexcept

Returns the number of registered callbacks.

This method returns the current number of active callbacks in the pool.

Returns
The number of registered callbacks.
Note
Matches the number of slots filled by successful add operations (excluding duplicate adds).

Member Data Documentation

◆ _callbacks

template<typename T, size_t AllocatedSize = 4>
array<StaticCallback, AllocatedSize> toy::CallbacksPool< T, AllocatedSize >::_callbacks
private

Fixed-size array storing registered callbacks.

◆ _subscribersCount

template<typename T, size_t AllocatedSize = 4>
size_t toy::CallbacksPool< T, AllocatedSize >::_subscribersCount {0}
private

Active callbacks count.


The documentation for this class was generated from the following files: