|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Event Bus for publish-subscribe pattern. More...
#include <event_bus.h>

Classes | |
| class | subscription |
| Subscription handle for managing subscriptions. More... | |
Public Types | |
| using | handler_func = std::function<void(const std::any&)> |
| Handler function type. | |
Public Member Functions | |
| event_bus (std::shared_ptr< thread_pool > pool=nullptr) | |
| Constructor. | |
| template<typename Event > | |
| void | publish (const Event &event) |
| Publish an event asynchronously. | |
| template<typename Event > | |
| void | publish_sync (const Event &event) |
| Publish an event synchronously. | |
| template<typename Event > | |
| subscription | subscribe (std::function< void(const Event &)> handler) |
| Subscribe to events of a specific type. | |
| template<typename Event > | |
| void | clear_subscriptions () |
| Clear all subscriptions for a specific event type. | |
| void | clear_all_subscriptions () |
| Clear all subscriptions. | |
| template<typename Event > | |
| std::size_t | subscriber_count () const |
| Get the number of subscribers for an event type. | |
Static Public Member Functions | |
| static event_bus & | instance () |
| Get singleton instance. | |
Private Member Functions | |
| void | unsubscribe (std::type_index type, std::size_t id) |
| Unsubscribe a specific handler. | |
Private Attributes | |
| std::mutex | mutex_ |
| std::unordered_map< std::type_index, std::unordered_map< std::size_t, handler_func > > | handlers_ |
| std::shared_ptr< thread_pool > | thread_pool_ |
| std::size_t | next_handler_id_ {1} |
Event Bus for publish-subscribe pattern.
Provides asynchronous event distribution across system components
Definition at line 55 of file event_bus.h.
| using kcenon::thread::event_bus::handler_func = std::function<void(const std::any&)> |
Handler function type.
Definition at line 60 of file event_bus.h.
|
inlineexplicit |
Constructor.
| thread_pool | Optional thread pool for async processing |
Definition at line 123 of file event_bus.h.
References kcenon::thread::thread_pool_builder::build_and_start(), thread_pool_, and kcenon::thread::thread_pool_builder::with_workers().

|
inline |
Clear all subscriptions.
Definition at line 229 of file event_bus.h.
|
inline |
|
inlinestatic |
Get singleton instance.
Definition at line 254 of file event_bus.h.
References instance().
Referenced by instance().


|
inline |
Publish an event asynchronously.
| Event | Event type |
| event | Event to publish |
Definition at line 139 of file event_bus.h.
References handlers_, mutex_, and thread_pool_.
|
inline |
Publish an event synchronously.
| Event | Event type |
| event | Event to publish |
Definition at line 174 of file event_bus.h.
|
inlinenodiscard |
Subscribe to events of a specific type.
| Event | Event type to subscribe to |
| handler | Handler function |
Definition at line 197 of file event_bus.h.
References handlers_, mutex_, and next_handler_id_.
|
inline |
Get the number of subscribers for an event type.
| Event | Event type |
Definition at line 240 of file event_bus.h.
|
inlineprivate |
Unsubscribe a specific handler.
Definition at line 263 of file event_bus.h.
References handlers_, and mutex_.
Referenced by kcenon::thread::event_bus::subscription::unsubscribe().

|
private |
Definition at line 275 of file event_bus.h.
Referenced by clear_all_subscriptions(), clear_subscriptions(), publish(), publish_sync(), subscribe(), subscriber_count(), and unsubscribe().
|
mutableprivate |
Definition at line 274 of file event_bus.h.
Referenced by clear_all_subscriptions(), clear_subscriptions(), publish(), publish_sync(), subscribe(), subscriber_count(), and unsubscribe().
|
private |
|
private |
Definition at line 276 of file event_bus.h.
Referenced by event_bus(), and publish().