SVCS - English Documentation
Loading...
Searching...
No Matches
svcs::services::EventBus Class Reference

A thread-safe implementation of ISubject for centralized event distribution. More...

#include <EventBus.hxx>

Inheritance diagram for svcs::services::EventBus:
svcs::services::ISubject

Public Member Functions

 ~EventBus () override=default
 Default virtual destructor.
void attach (std::shared_ptr< IObserver > observer) override
 Registers a new Observer.
void detach (std::shared_ptr< IObserver > observer_to_remove) override
 Deregisters a specific Observer.
void notify (const Event &event) const override
 Sends an event to all active Observers.
Public Member Functions inherited from svcs::services::ISubject
virtual ~ISubject ()=default
 Virtual destructor.

Additional Inherited Members

Protected Attributes inherited from svcs::services::ISubject
std::vector< IObserver * > observers
 Collection of registered observers.

Detailed Description

A thread-safe implementation of ISubject for centralized event distribution.

This class uses std::weak_ptr to store observers, preventing circular dependencies and ensuring automatic cleanup of destroyed observers. It inherits from std::enable_shared_from_this to ensure that valid shared pointers to the bus can be created even inside its member functions.

Member Function Documentation

◆ attach()

void svcs::services::EventBus::attach ( std::shared_ptr< IObserver > observer)
inlineoverridevirtual

Registers a new Observer.

The observer's shared pointer is stored as a weak pointer internally.

Parameters
observerThe smart pointer to the Observer to be registered.

Implements svcs::services::ISubject.

◆ detach()

void svcs::services::EventBus::detach ( std::shared_ptr< IObserver > observer_to_remove)
inlineoverridevirtual

Deregisters a specific Observer.

Compares the provided shared_ptr against all stored weak_ptr objects.

Parameters
observer_to_removeThe smart pointer to the Observer to be removed.

Implements svcs::services::ISubject.

◆ notify()

void svcs::services::EventBus::notify ( const Event & event) const
inlineoverridevirtual

Sends an event to all active Observers.

Iterates over the list, safely locking each weak pointer before calling update(). Invalid (expired) weak pointers are effectively ignored.

Parameters
eventThe constant reference to the Event to be published.

Implements svcs::services::ISubject.


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