Trait that adds typed, enum-keyed message sending and receiving to any class. More...
#include <Messenger.h>
Public Member Functions | |
| Messenger ()=default | |
| virtual | ~Messenger () |
| void | offAllMessages () |
| Unsubscribe from all messages registered by this object. | |
| template<typename DataType, typename Enum> | |
| void | offMessage (Enum event) |
| Unsubscribe from a specific typed message. | |
| template<typename DataType, typename Enum> | |
| void | onMessage (Enum event, std::function< void(const DataType &)> callback) |
| Register a callback to receive a typed message. | |
| template<typename DataType, typename Enum> | |
| void | sendMessage (Enum event, const DataType &data) |
| Publish a typed message to all current subscribers. | |
Trait that adds typed, enum-keyed message sending and receiving to any class.
Messenger is a self-contained trait — it has no dependency on Subscribable, Core, or any other framework base class. Any class can gain message-bus participation simply by inheriting it:
ml::Plugin inherits Messenger directly, so every plugin is a first-class message-bus participant out of the box.ComponentWith<MyManifest, ml::Messenger>.Messenger directly.A thin, per-object wrapper over MessageManager. Each Messenger instance tracks which messages it has subscribed to, so that offAllMessages() (called automatically by the destructor) only removes this object's subscriptions — never anyone else's.
Messages are keyed by a user-defined enum class value and carry a strongly-typed payload. The sender and receiver only need to share the enum header — neither needs to know about the other's class.
The destructor calls offAllMessages() automatically. Subscriptions are always cleaned up when the owning object is destroyed, with no manual teardown required. It is also safe to call offMessage or offAllMessages from inside a callback — removals are deferred by MessageManager until the current delivery pass completes.
Definition at line 63 of file Messenger.h.
|
virtual |
|
default |
| void ml::Messenger::offAllMessages | ( | ) |
Unsubscribe from all messages registered by this object.
Called automatically by the destructor. Only call manually if you need to reset all subscriptions while the object is still alive.
| void ml::Messenger::offMessage | ( | Enum | event | ) |
Unsubscribe from a specific typed message.
After this call, the callback previously registered for event and DataType will no longer be invoked. Safe to call from within the callback itself.
| DataType | The payload type of the subscription to remove. |
| Enum | The enum type identifying the event. |
| event | The specific event to unsubscribe from. |
| void ml::Messenger::onMessage | ( | Enum | event, |
| std::function< void(const DataType &)> | callback ) |
Register a callback to receive a typed message.
The callback is invoked each time sendMessage is called with the same event and DataType. Multiple calls with the same event replace the previous callback for that event.
| DataType | The expected payload type. |
| Enum | The enum type identifying the event. |
| event | The specific event to listen for. |
| callback | Function called with a const reference to the payload when the event fires. |
| void ml::Messenger::sendMessage | ( | Enum | event, |
| const DataType & | data ) |
Publish a typed message to all current subscribers.
All objects that have called onMessage for the same event and DataType will have their callbacks invoked with data. Delivery is immediate unless MessageManager is currently iterating, in which case it is deferred.
| DataType | The payload type. Must match the type used in the corresponding onMessage call. |
| Enum | The enum type identifying the event. |
| event | The specific event to send. |
| data | The payload to deliver to subscribers. |