sdi_toolBox
Private Member Functions | List of all members
Node Class Reference

#include <node.h>

Inheritance diagram for Node:
Inheritance graph
[legend]
Collaboration diagram for Node:
Collaboration graph
[legend]

Detailed Description

Concrete subscriber node in the event bus system.

Node is the concrete implementation of INode. It represents a participant in the event bus that can subscribe to specific message types or to broadcast mode, emit and post messages through the bus, and consume received messages from its internal FIFO queue.

Each Node holds a reference to the Bus it belongs to. Subscriptions and message transmissions are delegated to the bus. Incoming messages are stored in an internal thread-safe queue and can be retrieved via popMessage().

The Node also supports synchronous waiting: a thread can block on syncWaitForMessage() until at least one message is available in the queue.

On destruction, the Node automatically unsubscribes from all event types and broadcast mode, preventing dangling pointers in the bus routing table.

Note
Node is non-copyable and non-movable.
A Bus reference must be provided at construction time.
The Node does not take ownership of the Bus.
Example usage:
node.subscribe(MY_EVENT_TYPE);
node.emit<MyMessage>(42);
node.syncWaitForMessage();
auto msg = std::dynamic_pointer_cast<MyMessage>(node.popMessage());
Central message dispatcher for the event bus system.
Definition bus.h:63
Concrete subscriber node in the event bus system.
Definition node.h:64
See also
Bus
INode
Message

Definition at line 63 of file node.h.

Public Member Functions

Construction & Destruction
 Node ()=delete
 Default constructor - deleted. A Bus reference must be provided.
 
virtual ~Node ()
 Destructor.
 
 Node (const Node &obj)=delete
 Copy constructor - deleted.
 
 Node (Node &&obj) noexcept=delete
 Move constructor - deleted.
 
Nodeoperator= (const Node &obj)=delete
 Copy assignment operator - deleted.
 
Nodeoperator= (Node &&obj) noexcept=delete
 Move assignment operator - deleted.
 
 Node (Bus &bus)
 Construct a Node attached to the given Bus.
 
Synchronization
void syncWaitForMessage ()
 Block the calling thread until a message is received.
 
Subscription Management
void subscribe (MessageTypeID eventType)
 Subscribe this node to a specific message type.
 
void unsubscribe (MessageTypeID eventType)
 Unsubscribe this node from a specific message type.
 
void unsubscribeFromAll ()
 Unsubscribe this node from all message types and broadcast mode.
 
Message Transmission
template<class T , class... Args>
bool emit (Args &&...args)
 Construct and emit a message of type T through the bus.
 
bool post (const std::shared_ptr< Message > &message) const
 Post an already constructed message through the bus.
 
Message queue management
size_t getMessageCount () const
 Get the number of messages currently in the node's queue.
 
std::shared_ptr< MessagepopMessage ()
 Remove and return the front message from the node's queue (FIFO).
 
- Public Member Functions inherited from INode
 INode ()=default
 Default constructor.
 
virtual ~INode ()=default
 Default destructor.
 
 INode (const INode &obj)=delete
 Copy constructor - deleted.
 
 INode (INode &&obj) noexcept=delete
 Move constructor - deleted.
 
INodeoperator= (const INode &obj)=delete
 Copy assignment operator - deleted.
 
INodeoperator= (INode &&obj) noexcept=delete
 Move assignment operator - deleted.
 

Private Member Functions

void append (const std::shared_ptr< Message > &message) override
 Append a message to the node's internal queue.
 

Constructor & Destructor Documentation

◆ Node() [1/4]

Node ( )
delete

Default constructor - deleted. A Bus reference must be provided.

◆ ~Node()

~Node ( )
inlinevirtual

Destructor.

Automatically unsubscribes the node from all specific event types and broadcast mode via unsubscribeFromAll(), preventing dangling pointers in the bus routing table. Also notifies any thread blocked in syncWaitForMessage() to unblock it gracefully.

Definition at line 276 of file node.h.

◆ Node() [2/4]

Node ( const Node obj)
delete

Copy constructor - deleted.

Node is non-copyable.

◆ Node() [3/4]

Node ( Node &&  obj)
deletenoexcept

Move constructor - deleted.

Node is non-movable.

◆ Node() [4/4]

Node ( Bus bus)
inlineexplicit

Construct a Node attached to the given Bus.

Parameters
busReference to the Bus this node belongs to. The bus must outlive the node.

Definition at line 269 of file node.h.

Member Function Documentation

◆ operator=() [1/2]

Node & operator= ( const Node obj)
delete

Copy assignment operator - deleted.

Node is non-copyable.

◆ operator=() [2/2]

Node & operator= ( Node &&  obj)
deletenoexcept

Move assignment operator - deleted.

Node is non-movable.

◆ syncWaitForMessage()

void syncWaitForMessage ( )
inline

Block the calling thread until a message is received.

Suspends the calling thread using an atomic wait until at least one message has been appended to the node's internal queue by the Bus. This method is intended for synchronous event-driven patterns where a thread should idle until work is available.

Note
Returns immediately if a message is already pending in the queue at the time of the call.
If the Node is destroyed while a thread is blocked here, the destructor triggers a notification to unblock the waiting thread gracefully.
Warning
The caller is responsible for checking the queue after this call returns, as the notification may also be triggered by the destructor with an empty queue.

Definition at line 286 of file node.h.

◆ subscribe()

void subscribe ( MessageTypeID  eventType)
inline

Subscribe this node to a specific message type.

Delegates to Bus::subscribe(). The node will receive all messages of the given type posted to the bus. Duplicate subscriptions are ignored.

Parameters
eventTypeThe message type identifier to subscribe to.
See also
Bus::subscribe()

Definition at line 295 of file node.h.

◆ unsubscribe()

void unsubscribe ( MessageTypeID  eventType)
inline

Unsubscribe this node from a specific message type.

Delegates to Bus::unsubscribe(). If the node was not subscribed to the given type, this call has no effect.

Parameters
eventTypeThe message type identifier to unsubscribe from.
See also
Bus::unsubscribe()

Definition at line 301 of file node.h.

◆ unsubscribeFromAll()

void unsubscribeFromAll ( )
inline

Unsubscribe this node from all message types and broadcast mode.

Delegates to Bus::unsubscribeFromAll(). After this call, the node will no longer receive any messages until it re-subscribes.

See also
Bus::unsubscribeFromAll()

Definition at line 307 of file node.h.

◆ emit()

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

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

Forwards the call to Bus::emit(). Creates a new message of type T using the provided arguments and posts it to the bus.

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.
See also
Bus::emit()

Definition at line 314 of file node.h.

◆ post()

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

Post an already constructed message through the bus.

Forwards the call to Bus::post().

Parameters
messageShared pointer to the message to post. Must not be nullptr.
Returns
true if at least one subscriber received the message, false otherwise.
See also
Bus::post()

Definition at line 320 of file node.h.

◆ getMessageCount()

size_t getMessageCount ( ) const
inline

Get the number of messages currently in the node's queue.

Returns
The number of pending messages waiting to be consumed.
Note
This operation is thread-safe.

Definition at line 326 of file node.h.

◆ popMessage()

std::shared_ptr< Message > popMessage ( )
inline

Remove and return the front message from the node's queue (FIFO).

Retrieves the oldest message in the queue and removes it. If the queue is empty, returns nullptr.

Returns
A shared pointer to the front Message, or nullptr if the queue is empty.
Note
This operation is thread-safe.

Definition at line 334 of file node.h.

◆ append()

void append ( const std::shared_ptr< Message > &  message)
inlineoverrideprivatevirtual

Append a message to the node's internal queue.

Called exclusively by Bus when dispatching a message to this node. Pushes the message onto the queue and notifies any thread waiting in syncWaitForMessage().

Parameters
messageShared pointer to the message being delivered.

Implements INode.

Definition at line 348 of file node.h.

Member Data Documentation

◆ mtx

std::mutex mtx
mutable

Mutex for thread-safe access to the message queue.

Definition at line 262 of file node.h.

◆ messageQueue

std::queue<std::shared_ptr<Message> > messageQueue

Queue of messages received by the node.

Definition at line 263 of file node.h.