Typed, enum-keyed message bus for structured inter-object communication. More...
#include <MessageManager.h>
Static Public Member Functions | |
| static void | clear () |
| Remove all subscriptions for all subscribers and all keys. | |
| static void | clearPending () |
| Discard all pending operations without executing them. | |
| static bool | isBusy () |
Return true if the manager is currently iterating. | |
| static void | processPending () |
| Flush all pending operations immediately. | |
| template<typename DataType, typename Enum> | |
| static void | publish (Enum event, const DataType &data) |
| Deliver a typed message to all matching subscribers. | |
| template<typename DataType, typename Enum> | |
| static void | subscribe (Enum event, void *subscriber, std::function< void(const DataType &)> callback) |
| Register a typed callback for a specific enum event. | |
| template<typename DataType, typename Enum> | |
| static void | unsubscribe (Enum event, void *subscriber) |
| Remove one subscriber's callback for a specific typed event. | |
| static void | unsubscribeAll (void *subscriber) |
| Remove all subscriptions for a given subscriber. | |
Static Protected Member Functions | |
| static void | beginBusy () |
| Signal that iteration has begun. | |
| static void | deferOrExecute (std::function< void()> operation) |
Execute operation now if safe, otherwise queue it. | |
| static void | endBusy () |
| Signal that iteration has ended; flush pending operations. | |
Static Protected Attributes | |
| static int | busyDepth |
| Iteration nesting depth. Operations are deferred while this is > 0. | |
| static std::vector< std::function< void()> > | pendingOperations |
| Queue of operations pending until the current iteration completes. | |
Typed, enum-keyed message bus for structured inter-object communication.
MessageManager is the engine behind the Messenger trait. It provides a completely separate communication channel from EventsManager:
EventsManager | MessageManager |
|---|---|
String keys ("click", "hover") | Enum keys (GameEvent::Started) |
| Fired by dispatchers based on SFML input | Fired explicitly by application code |
Delivers to Subscribable objects | Delivers to any void* subscriber |
| Used for input and UI lifecycle events | Used for application-level signals |
Each subscription is identified by a three-part key:
std::type_index of Enum)int)std::type_index of DataType)This means the same enum value can carry different payload types without collision, and different enum types are always distinct even if their integer values overlap.
MessageManager inherits DeferredOperationsManager<MessageManager>. Any unsubscribe or unsubscribeAll call made while publish() is iterating is automatically deferred and processed after the iteration completes. This makes it safe to unsubscribe from within a message callback.
Most user code interacts with MessageManager through the Messenger trait methods (sendMessage, onMessage, offMessage). Direct calls are only needed for framework-internal wiring.
Definition at line 69 of file MessageManager.h.
|
staticprotectedinherited |
Signal that iteration has begun.
Increments the busy-depth counter. Multiple nested calls are safe — operations remain deferred until the outermost endBusy() is reached.
|
static |
Remove all subscriptions for all subscribers and all keys.
Use during application teardown or full scene reset.
|
staticinherited |
Discard all pending operations without executing them.
Use during application shutdown or full reset when executing the queued operations would be unsafe (e.g., the objects they reference have already been destroyed).
|
staticprotectedinherited |
Execute operation now if safe, otherwise queue it.
If busyDepth > 0 the operation is appended to pendingOperations and will run when the current iteration completes. If the manager is idle the operation executes immediately.
| operation | The callable to execute or defer. |
|
staticprotectedinherited |
Signal that iteration has ended; flush pending operations.
Decrements the busy-depth counter. When the counter reaches zero, all operations in pendingOperations are executed in FIFO order and the queue is cleared.
|
staticinherited |
Return true if the manager is currently iterating.
Can be used by external code that needs to know whether a destructive operation will be deferred.
true when busyDepth > 0.
|
staticinherited |
Flush all pending operations immediately.
Normally called automatically by endBusy(). Provided as a public escape hatch for unusual teardown sequences.
isBusy() is true can cause iterator invalidation in the currently running loop. Only call manually when you are certain it is safe.
|
static |
Deliver a typed message to all matching subscribers.
Looks up all subscriptions matching the composite key for event and DataType, then invokes each callback with data. The manager marks itself busy for the duration; any unsubscriptions that arrive during delivery are deferred.
| DataType | Payload type. Must match the type used in the corresponding subscribe call. |
| Enum | Enum type identifying the message. |
| event | The specific enum value to publish. |
| data | Payload delivered to all matching callbacks. |
|
static |
Register a typed callback for a specific enum event.
The callback is stored against the composite key formed by Enum, event, and DataType. If subscriber has already registered a callback for this exact key, the new callback replaces the old one.
| DataType | Expected payload type. |
| Enum | Enum type identifying the message. |
| event | The specific enum value to subscribe to. |
| subscriber | Opaque pointer identifying the subscribing object (typically this). Used for targeted unsubscription. |
| callback | Called with a const reference to the payload when the message is published. |
|
static |
Remove one subscriber's callback for a specific typed event.
If called during a publish() iteration the removal is deferred.
| DataType | Payload type of the subscription to remove. |
| Enum | Enum type identifying the message. |
| event | The specific enum value to unsubscribe from. |
| subscriber | The subscribing object to remove. |
|
static |
Remove all subscriptions for a given subscriber.
Called automatically by Messenger's destructor. If called during a publish() iteration the removal is deferred, preventing use-after-free when an object destroys itself inside a callback.
| subscriber | The subscribing object to remove from all messages. |
|
inlinestaticprotectedinherited |
Iteration nesting depth. Operations are deferred while this is > 0.
Definition at line 69 of file DeferredOperationsManager.h.
|
inlinestaticprotectedinherited |
Queue of operations pending until the current iteration completes.
Definition at line 72 of file DeferredOperationsManager.h.