|
Common System 0.2.0
Common interfaces and patterns for system integration
|
Simple synchronous event bus for testing when monitoring is disabled. More...
#include <event_bus.h>

Classes | |
| struct | subscription_info |
Public Types | |
| using | error_callback_t |
| Type for error callback function. | |
Public Member Functions | |
| template<concepts::EventType EventType> | |
| void | publish (const EventType &evt, event_priority=event_priority::normal) |
| Publish an event to all subscribed handlers. | |
| void | publish (event &&evt, event_priority=event_priority::normal) |
| template<concepts::EventType EventType, concepts::EventHandler< EventType > HandlerFunc> | |
| uint64_t | subscribe (HandlerFunc &&func) |
| Subscribe to events of a specific type. | |
| uint64_t | subscribe (std::function< void(const event &)> &&func) |
| template<concepts::EventType EventType, concepts::EventHandler< EventType > HandlerFunc, concepts::EventFilter< EventType > FilterFunc> | |
| uint64_t | subscribe_filtered (HandlerFunc &&func, FilterFunc &&filter) |
| Subscribe to events with a filter function. | |
| uint64_t | subscribe_filtered (std::function< void(const event &)> &&func, std::function< bool(const event &)> &&filter) |
| void | unsubscribe (uint64_t id) |
| void | start () |
| void | stop () |
| bool | is_running () const |
| void | set_error_callback (error_callback_t callback) |
| Set the error callback for handler exceptions and type mismatches. | |
| void | clear_error_callback () |
| Clear the error callback. | |
Static Public Member Functions | |
| static simple_event_bus & | instance () |
| Get the singleton instance (thread-safe via C++11 magic statics) | |
Private Attributes | |
| std::mutex | mutex_ |
| std::unordered_multimap< size_t, subscription_info > | handlers_ |
| std::atomic< uint64_t > | next_id_ {1} |
| std::atomic< bool > | running_ {true} |
| error_callback_t | error_callback_ |
Simple synchronous event bus for testing when monitoring is disabled.
Thread-safety: This class is thread-safe using a mutex to protect subscription management and event dispatch.
Note: This implementation avoids RTTI by using template-based type IDs.
Definition at line 150 of file event_bus.h.
Type for error callback function.
This callback is invoked when an exception occurs in an event handler or when a type mismatch is detected. The callback receives:
Definition at line 161 of file event_bus.h.
|
inline |
Clear the error callback.
Definition at line 415 of file event_bus.h.
References error_callback_, and mutex_.
|
inlinestatic |
Get the singleton instance (thread-safe via C++11 magic statics)
Definition at line 424 of file event_bus.h.
References instance().
Referenced by kcenon::common::get_event_bus(), and instance().


|
inline |
|
inline |
Publish an event to all subscribed handlers.
| EventType | The event type (must satisfy concepts::EventType) |
| evt | The event to publish |
| priority | Event priority (default: normal) |
Definition at line 174 of file event_bus.h.
References error_callback_, handlers_, kcenon::common::detail::event_type_id< T >::id(), and mutex_.

|
inline |
Definition at line 225 of file event_bus.h.
References error_callback_, handlers_, kcenon::common::detail::event_type_id< T >::id(), and mutex_.

|
inline |
Set the error callback for handler exceptions and type mismatches.
This callback will be invoked whenever:
The error callback is useful for logging and debugging handler issues without stopping the processing of other handlers.
| callback | The callback function to invoke on errors |
Example usage:
Definition at line 407 of file event_bus.h.
References error_callback_, and mutex_.
|
inline |
|
inline |
|
inline |
Subscribe to events of a specific type.
| EventType | The event type (must satisfy concepts::EventType) |
| HandlerFunc | The handler function type (must satisfy concepts::EventHandler) |
| func | The handler function to call when the event is published |
Definition at line 281 of file event_bus.h.
References handlers_, kcenon::common::detail::event_type_id< T >::id(), mutex_, and next_id_.
Referenced by subscribe_filtered(), and subscribe_filtered().


|
inline |
Definition at line 302 of file event_bus.h.
References handlers_, kcenon::common::detail::event_type_id< T >::id(), mutex_, and next_id_.

|
inline |
Subscribe to events with a filter function.
| EventType | The event type (must satisfy concepts::EventType) |
| HandlerFunc | The handler function type (must satisfy concepts::EventHandler) |
| FilterFunc | The filter function type (must satisfy concepts::EventFilter) |
| func | The handler function to call when the event passes the filter |
| filter | The filter function to determine if the handler should be called |
Definition at line 337 of file event_bus.h.
References subscribe().

|
inline |
Definition at line 353 of file event_bus.h.
References subscribe().

|
inline |
Definition at line 369 of file event_bus.h.
|
private |
Definition at line 440 of file event_bus.h.
Referenced by clear_error_callback(), publish(), publish(), and set_error_callback().
|
private |
Definition at line 437 of file event_bus.h.
Referenced by publish(), publish(), subscribe(), subscribe(), and unsubscribe().
|
mutableprivate |
Definition at line 436 of file event_bus.h.
Referenced by clear_error_callback(), publish(), publish(), set_error_callback(), subscribe(), subscribe(), and unsubscribe().
|
private |
|
private |
Definition at line 439 of file event_bus.h.
Referenced by is_running(), start(), and stop().