AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
anch::events::EventBus< T > Class Template Reference

A class which manage global events firing and events QoS. More...

#include <eventBus.hpp>

Inheritance diagram for anch::events::EventBus< T >:
Collaboration diagram for anch::events::EventBus< T >:

Public Member Functions

 EventBus ()
 EventBus (const EventBus &)=delete
 ~EventBus ()
EventBus & operator= (const EventBus &)=delete
bool addObserver (anch::events::Observer< T > &observer) noexcept
void removeObserver (anch::events::Observer< T > &observer) noexcept
void fireEvent (const T &event, const std::map< std::string, std::string > &headers={}) noexcept
void fireEvent (const anch::events::Event< T > &event) noexcept
void release ()
Public Member Functions inherited from anch::Singleton< EventBus< T > >
Singleton & operator= (const Singleton &)=delete

Static Public Member Functions

static bool AddObserver (anch::events::Observer< T > &observer) noexcept
static void RemoveObserver (anch::events::Observer< T > &observer) noexcept
static void FireEvent (const T &event, const std::map< std::string, std::string > &headers={}) noexcept
static void FireEvent (const anch::events::Event< T > &event) noexcept
Static Public Member Functions inherited from anch::Singleton< EventBus< T > >
static T & getInstance ()

Friends

class anch::Singleton< EventBus< T > >

Additional Inherited Members

Protected Member Functions inherited from anch::Singleton< EventBus< T > >
 Singleton () noexcept=default
 ~Singleton ()=default

Detailed Description

template<typename T>
class anch::events::EventBus< T >

A class which manage global events firing and events QoS.

Events are queued and notified asynchronously, in their firing order, by a dedicated thread (started on first event). fireEvent does not wait for observers: observers can fire events from their handle method.
Observers can add or remove observers from their handle method. When removeObserver returns, the observer will not be notified anymore and can be destroyed.

getInstance returns the global EventBus. Other buses can be created as plain objects.
The destructor notifies the queued events and stops the thread: it must not be called from an observer. The global bus is destroyed at program exit: its observers must not use already destroyed static objects.

Since
0.1
Author
Vincent Lachenal

Constructor & Destructor Documentation

◆ EventBus() [1/2]

template<typename T>
anch::events::EventBus< T >::EventBus ( )

EventBus default constructor.
This constructor is public to allow to declare other buses than the global one.

◆ EventBus() [2/2]

template<typename T>
anch::events::EventBus< T >::EventBus ( const EventBus< T > & )
delete

Prohibits EventBus copy constructor

Here is the call graph for this function:

◆ ~EventBus()

template<typename T>
anch::events::EventBus< T >::~EventBus ( )

EventBus destructor.
Notify the queued events and stop the bus thread.

Member Function Documentation

◆ AddObserver()

template<typename T>
bool anch::events::EventBus< T >::AddObserver ( anch::events::Observer< T > & observer)
staticnoexcept

Retrieve EventBus instance and register Observer to EventBus

Parameters
observerthe Observer to register
Returns
true when observer has been added, false otherwise (when observer has already been registered)

◆ addObserver()

template<typename T>
bool anch::events::EventBus< T >::addObserver ( anch::events::Observer< T > & observer)
noexcept

Register Observer to EventBus

Parameters
observerthe Observer to register
Returns
true when observer has been added, false otherwise (when observer has already been registered)

◆ FireEvent() [1/2]

template<typename T>
void anch::events::EventBus< T >::FireEvent ( const anch::events::Event< T > & event)
staticnoexcept

Retrieve EventBus instance and notify all observer that an event has been fired

Parameters
eventthe event which has been fired

◆ FireEvent() [2/2]

template<typename T>
void anch::events::EventBus< T >::FireEvent ( const T & event,
const std::map< std::string, std::string > & headers = {} )
staticnoexcept

Retrieve EventBus instance and notify all observer that an event has been fired

Parameters
eventthe event which has been fired
headersthe event's context

◆ fireEvent() [1/2]

template<typename T>
void anch::events::EventBus< T >::fireEvent ( const anch::events::Event< T > & event)
noexcept

Notify all observer that an event has been fired.
The event is queued: this method does not wait for observers.

Parameters
eventthe event which has been fired

◆ fireEvent() [2/2]

template<typename T>
void anch::events::EventBus< T >::fireEvent ( const T & event,
const std::map< std::string, std::string > & headers = {} )
noexcept

Notify all observer that an event has been fired.
The event is queued: this method does not wait for observers.

Parameters
eventthe event which has been fired
headersthe event's context

◆ operator=()

template<typename T>
EventBus & anch::events::EventBus< T >::operator= ( const EventBus< T > & )
delete

Prohibits EventBus copy assignment

Here is the call graph for this function:

◆ release()

template<typename T>
void anch::events::EventBus< T >::release ( )

Delete this .
Only for buses allocated with new (never for the global bus returned by getInstance).

◆ RemoveObserver()

template<typename T>
void anch::events::EventBus< T >::RemoveObserver ( anch::events::Observer< T > & observer)
staticnoexcept

Retrieve EventBus instance and remove observer for notifications

Parameters
observerthe Observer to remove

◆ removeObserver()

template<typename T>
void anch::events::EventBus< T >::removeObserver ( anch::events::Observer< T > & observer)
noexcept

Remove observer for notifications.
Wait for the running notification (when not called from an observer of this bus).

Parameters
observerthe Observer to remove

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