|
sdi_toolBox
|
#include <bus.h>
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.
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. | |
| Bus & | operator= (const Bus &obj)=delete |
| Copy assignment operator - deleted. | |
| Bus & | operator= (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. | |
|
inline |
|
default |
Default destructor.
Copy assignment operator - deleted.
The Bus is non-copyable.
Move assignment operator - deleted.
The Bus is non-movable.
|
inline |
|
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).
| node | Pointer to the node to subscribe. Must not be nullptr. |
| eventType | The message type identifier to subscribe to. |
| std::runtime_error | if node is nullptr. |
|
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.
| node | Pointer to the node to unsubscribe. Must not be nullptr. |
| eventType | The message type identifier to unsubscribe from. |
| std::runtime_error | if node is nullptr. |
|
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.
| node | Pointer to the node to unsubscribe. Must not be nullptr. |
| std::runtime_error | if node is nullptr. |
|
inline |
Check whether a node is subscribed to a specific message type.
| node | Pointer to the node to check. Must not be nullptr. |
| eventType | The message type identifier to check. |
true if the node is subscribed to the given message type, false otherwise.| std::runtime_error | if node is nullptr. |
|
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.
| node | Pointer to the node to subscribe. Must not be nullptr. |
| std::runtime_error | if node is nullptr. |
|
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.
| node | Pointer to the node to unsubscribe. Must not be nullptr. |
| std::runtime_error | if node is nullptr. |
|
inline |
Check whether a node is subscribed to broadcast mode.
| node | Pointer to the node to check. Must not be nullptr. |
true if the node is subscribed to broadcast mode, false otherwise.| std::runtime_error | if node is nullptr. |
| 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().
| T | The message type to emit. Must be derived from Message. |
| Args | Constructor argument types for T. |
| args | Arguments forwarded to the constructor of T. |
true if at least one subscriber received the message, false otherwise.T must derive from Message.
|
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.
| message | Shared pointer to the message to post. Must not be nullptr. |
true if at least one specific subscriber received the message, false otherwise.
|
friend |
|
mutable |
| std::unordered_map<MessageTypeID, VectorNode> nodeList |