Loading...
Searching...
No Matches
ml::Hookable Struct Reference

Trait that layers a hook system on top of ml::Messenger. More...

#include <Malena/Traits/Communication/Hookable.h>

Inheritance diagram for ml::Hookable:
[legend]

Public Member Functions

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.

Detailed Description

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.

Constructor & Destructor Documentation

◆ ~Hookable()

virtual ml::Hookable::~Hookable ( )
virtualdefault

Member Function Documentation

◆ 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
DataTypeThe payload type of the subscription to remove.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to unsubscribe from.

◆ onHook()

template<typename Enum, typename Callback>
void ml::Hookable::onHook ( Enum hook,
Callback && cb )
inline

Definition at line 52 of file Hookable.h.

◆ 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
DataTypeThe expected payload type.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to listen for.
callbackFunction called with a const reference to the payload when the event fires.

◆ sendHook()

template<typename Enum>
void ml::Hookable::sendHook ( Enum hook)
inline

Definition at line 47 of file Hookable.h.

◆ 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
DataTypeThe payload type. Must match the type used in the corresponding onMessage call.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to send.
dataThe payload to deliver to subscribers.

The documentation for this struct was generated from the following file: