Trait that layers a hook system on top of ml::Messenger.
More...
#include <Malena/Traits/Communication/Hookable.h>
|
| virtual | ~Hookable ()=default |
| 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 Enum, typename Callback> |
| void | onHook (Enum hook, Callback &&cb) |
| 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 Enum> |
| void | sendHook (Enum hook) |
| template<typename DataType, typename Enum> |
| void | sendMessage (Enum event, const DataType &data) |
| | Publish a typed message to all current subscribers.
|
Trait that layers a hook system on top of ml::Messenger.
Inherit Hookable to gain sendHook / onHook / sharedAs on any class that participates in the message bus.
- sendHook(enum) — broadcast this object as a shared hook payload.
- onHook(enum, callback) — subscribe; callback receives T& or derived T& (dynamic_cast applied automatically).
- sharedAs<T>() — get a shared_ptr<T> to this (for lifetime capture).
Classes that call sendHook must be managed by a shared_ptr. Classes that only call onHook (e.g. AppShell) do not need one.
Definition at line 42 of file Hookable.h.
◆ ~Hookable()
| virtual ml::Hookable::~Hookable |
( |
| ) |
|
|
virtualdefault |
◆ offAllMessages()
| void ml::Messenger::offAllMessages |
( |
| ) |
|
|
inherited |
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.
◆ offMessage()
template<typename DataType, typename Enum>
| void ml::Messenger::offMessage |
( |
Enum | event | ) |
|
|
inherited |
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.
- Template Parameters
-
| DataType | The payload type of the subscription to remove. |
| Enum | The enum type identifying the event. |
- Parameters
-
| event | The specific event to unsubscribe from. |
◆ onHook()
template<typename Enum, typename
Callback>
| void ml::Hookable::onHook |
( |
Enum | hook, |
|
|
Callback && | cb ) |
|
inline |
◆ onMessage()
template<typename DataType, typename Enum>
| void ml::Messenger::onMessage |
( |
Enum | event, |
|
|
std::function< void(const DataType &)> | callback ) |
|
inherited |
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.
- Template Parameters
-
| DataType | The expected payload type. |
| Enum | The enum type identifying the event. |
- Parameters
-
| event | The specific event to listen for. |
| callback | Function called with a const reference to the payload when the event fires. |
◆ sendHook()
template<typename Enum>
| void ml::Hookable::sendHook |
( |
Enum | hook | ) |
|
|
inline |
◆ sendMessage()
template<typename DataType, typename Enum>
| void ml::Messenger::sendMessage |
( |
Enum | event, |
|
|
const DataType & | data ) |
|
inherited |
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.
- Template Parameters
-
| DataType | The payload type. Must match the type used in the corresponding onMessage call. |
| Enum | The enum type identifying the event. |
- Parameters
-
| event | The specific event to send. |
| data | The payload to deliver to subscribers. |
The documentation for this struct was generated from the following file: