Loading...
Searching...
No Matches
ml::Messenger Class Reference

Trait that adds typed, enum-keyed message sending and receiving to any class. More...

#include <Messenger.h>

Inheritance diagram for ml::Messenger:

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.

Detailed Description

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:

What it provides

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.

Usage

enum class GameEvent { Started, ScoreChanged, Stopped };
// Subscribe — callback receives a const T& payload
onMessage<int>(GameEvent::ScoreChanged, [](const int& score) {
updateScoreDisplay(score);
});
// Send — all subscribers for this key are notified immediately
sendMessage<int>(GameEvent::ScoreChanged, 42);
// Unsubscribe from one key
offMessage<int>(GameEvent::ScoreChanged);
// Unsubscribe from everything this object has registered
void offAllMessages()
Unsubscribe from all messages registered by this object.
void offMessage(Enum event)
Unsubscribe from a specific typed message.
void sendMessage(Enum event, const DataType &data)
Publish a typed message to all current subscribers.
void onMessage(Enum event, std::function< void(const DataType &)> callback)
Register a callback to receive a typed message.

Lifetime safety

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.

See also
MessageManager, Plugin, PluginWith

Definition at line 63 of file Messenger.h.

Constructor & Destructor Documentation

◆ ~Messenger()

virtual ml::Messenger::~Messenger ( )
virtual

◆ Messenger()

ml::Messenger::Messenger ( )
default

Member Function Documentation

◆ offAllMessages()

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.

◆ offMessage()

template<typename DataType, typename Enum>
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.

Template Parameters
DataTypeThe payload type of the subscription to remove.
EnumThe enum type identifying the event.
Parameters
eventThe specific event to unsubscribe from.

◆ onMessage()

template<typename DataType, typename Enum>
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.

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.

◆ sendMessage()

template<typename DataType, typename Enum>
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.

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 class was generated from the following file: