sdi_toolBox
Friends | List of all members
Bus Class Referencefinal

#include <bus.h>

Detailed Description

Central message dispatcher for the event bus system.

The Bus class is the core component of the event bus architecture. It manages a routing table that maps message type identifiers to lists of subscribed nodes, and dispatches messages to the appropriate nodes when they are emitted or posted.

Nodes can subscribe to specific message types or to broadcast mode, in which case they receive all messages regardless of their type.

The Bus is thread-safe: all operations on the routing table are protected by an internal mutex.

Note
The Bus is non-copyable and non-movable.
The Bus does not take ownership of the nodes it manages.
Example usage:
node.subscribe(MY_EVENT_TYPE);
bus.emit<MyMessage>(arg1, arg2);
auto msg = std::dynamic_pointer_cast<MyMessage>(node.popMessage());
Central message dispatcher for the event bus system.
Definition bus.h:63
bool emit(Args &&...args)
Construct and emit a message of type T to the bus.
Definition bus.h:445
Concrete subscriber node in the event bus system.
Definition node.h:64
See also
Node
INode
Message

Definition at line 62 of file bus.h.

Public Member Functions

Construction & Destruction
 Bus ()
 Default constructor.
 
 ~Bus ()=default
 Default destructor.
 
 Bus (const Bus &obj)=delete
 Copy constructor - deleted.
 
 Bus (Bus &&obj) noexcept=delete
 Move constructor - deleted.
 
Busoperator= (const Bus &obj)=delete
 Copy assignment operator - deleted.
 
Busoperator= (Bus &&obj) noexcept=delete
 Move assignment operator - deleted.
 
Subscription Management
void clearAllSubscriptions ()
 Remove all subscriptions from the routing table.
 
void subscribe (INode *node, MessageTypeID eventType)
 Subscribe a node to a specific message type.
 
void unsubscribe (INode *node, MessageTypeID eventType)
 Unsubscribe a node from a specific message type.
 
void unsubscribeFromAll (INode *node)
 Unsubscribe a node from all message types and broadcast mode.
 
bool isSubscribed (const INode *node, MessageTypeID eventType) const
 Check whether a node is subscribed to a specific message type.
 
Broadcast management
void subscribeToBroadcast (INode *node)
 Subscribe a node to broadcast mode.
 
void unsubscribeFromBroadcast (INode *node)
 Unsubscribe a node from broadcast mode.
 
bool isSubscribedToBroadcast (INode *node) const
 Check whether a node is subscribed to broadcast mode.
 
Message transmission
template<class T , class... Args>
bool emit (Args &&...args)
 Construct and emit a message of type T to the bus.
 
bool post (const std::shared_ptr< Message > &message) const
 Post an already constructed message to the bus.
 

Friends

class Node
 Allow the Node class to access private members.
 

Constructor & Destructor Documentation

◆ Bus() [1/3]

Bus ( )
inline

Default constructor.

Initializes the Bus and records the construction timestamp used as the bus start reference time.

Definition at line 312 of file bus.h.

◆ ~Bus()

~Bus ( )
default

Default destructor.

◆ Bus() [2/3]

Bus ( const Bus obj)
delete

Copy constructor - deleted.

The Bus is non-copyable.

◆ Bus() [3/3]

Bus ( Bus &&  obj)
deletenoexcept

Move constructor - deleted.

The Bus is non-movable.

Member Function Documentation

◆ operator=() [1/2]

Bus & operator= ( const Bus obj)
delete

Copy assignment operator - deleted.

The Bus is non-copyable.

◆ operator=() [2/2]

Bus & operator= ( Bus &&  obj)
deletenoexcept

Move assignment operator - deleted.

The Bus is non-movable.

◆ clearAllSubscriptions()

void clearAllSubscriptions ( )
inline

Remove all subscriptions from the routing table.

Clears both the specific event subscriptions and the broadcast subscription list. After this call, no node will receive any message until it re-subscribes.

Note
This operation is thread-safe.

Definition at line 319 of file bus.h.

◆ subscribe()

void subscribe ( INode node,
MessageTypeID  eventType 
)
inline

Subscribe a node to a specific message type.

Registers the given node to receive messages of the specified type. If the node is already subscribed to this type, this call has no effect (no duplicates are created).

Parameters
nodePointer to the node to subscribe. Must not be nullptr.
eventTypeThe message type identifier to subscribe to.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 328 of file bus.h.

◆ unsubscribe()

void unsubscribe ( INode node,
MessageTypeID  eventType 
)
inline

Unsubscribe a node from a specific message type.

Removes the given node from the list of subscribers for the specified message type. If the node was not subscribed to this type, this call has no effect.

Parameters
nodePointer to the node to unsubscribe. Must not be nullptr.
eventTypeThe message type identifier to unsubscribe from.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 352 of file bus.h.

◆ unsubscribeFromAll()

void unsubscribeFromAll ( INode node)
inline

Unsubscribe a node from all message types and broadcast mode.

Removes the given node from all specific event subscription lists and from the broadcast subscription list. This is automatically called by the Node destructor to ensure no dangling pointers remain in the routing table.

Parameters
nodePointer to the node to unsubscribe. Must not be nullptr.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 370 of file bus.h.

◆ isSubscribed()

bool isSubscribed ( const INode node,
MessageTypeID  eventType 
) const
inline

Check whether a node is subscribed to a specific message type.

Parameters
nodePointer to the node to check. Must not be nullptr.
eventTypeThe message type identifier to check.
Returns
true if the node is subscribed to the given message type, false otherwise.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 387 of file bus.h.

◆ subscribeToBroadcast()

void subscribeToBroadcast ( INode node)
inline

Subscribe a node to broadcast mode.

A node in broadcast mode receives all messages posted to the bus, regardless of their type. A node can be subscribed to both broadcast mode and specific message types simultaneously, in which case it will receive the message twice for matching types.

Parameters
nodePointer to the node to subscribe. Must not be nullptr.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 405 of file bus.h.

◆ unsubscribeFromBroadcast()

void unsubscribeFromBroadcast ( INode node)
inline

Unsubscribe a node from broadcast mode.

Removes the given node from the broadcast subscription list. If the node was not subscribed to broadcast mode, this call has no effect.

Parameters
nodePointer to the node to unsubscribe. Must not be nullptr.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 418 of file bus.h.

◆ isSubscribedToBroadcast()

bool isSubscribedToBroadcast ( INode node) const
inline

Check whether a node is subscribed to broadcast mode.

Parameters
nodePointer to the node to check. Must not be nullptr.
Returns
true if the node is subscribed to broadcast mode, false otherwise.
Exceptions
std::runtime_errorif node is nullptr.
Note
This operation is thread-safe.

Definition at line 431 of file bus.h.

◆ emit()

template<class T , class... Args>
bool emit ( Args &&...  args)

Construct and emit a message of type T to the bus.

Creates a new message of type T by forwarding the provided arguments to its constructor, then posts it to the bus via post().

Template Parameters
TThe message type to emit. Must be derived from Message.
ArgsConstructor argument types for T.
Parameters
argsArguments forwarded to the constructor of T.
Returns
true if at least one subscriber received the message, false otherwise.
Note
Enforced at compile time: T must derive from Message.
This operation is thread-safe.
Example:
bus.emit<MyMessage>(arg1, arg2);

Definition at line 445 of file bus.h.

◆ post()

bool post ( const std::shared_ptr< Message > &  message) const
inline

Post an already constructed message to the bus.

Updates the message timestamp and dispatches it to all nodes subscribed to the message type, as well as all nodes in broadcast mode.

Parameters
messageShared pointer to the message to post. Must not be nullptr.
Returns
true if at least one specific subscriber received the message, false otherwise.
Note
This operation is thread-safe.
See also
emit()

Definition at line 457 of file bus.h.

Friends And Related Symbol Documentation

◆ Node

friend class Node
friend

Allow the Node class to access private members.

Definition at line 64 of file bus.h.

Member Data Documentation

◆ mtx

std::mutex mtx
mutable

Mutex for thread-safe access to the nodes map.

Definition at line 303 of file bus.h.

◆ nodeList

std::unordered_map<MessageTypeID, VectorNode> nodeList

Map of message type IDs to lists of subscribed nodes.

Definition at line 304 of file bus.h.

◆ broadcastNodeList

std::set<INode *> broadcastNodeList

Set of nodes subscribed to receive all messages (broadcast mode)

Definition at line 305 of file bus.h.