Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::patterns::SimpleEventBus Class Referenceexport

Thread-safe event bus for publish/subscribe pattern. More...

Collaboration diagram for kcenon::common::patterns::SimpleEventBus:
Collaboration graph

Classes

struct  HandlerEntry
 

Public Member Functions

 SimpleEventBus ()
 
 ~SimpleEventBus ()
 
void start ()
 Start the event bus.
 
void stop ()
 Stop the event bus.
 
bool is_running () const
 Check if the event bus is running.
 
template<typename Event >
uint64_t subscribe (std::function< void(const Event &)> handler)
 Subscribe to events of a specific type.
 
void unsubscribe (uint64_t subscription_id)
 Unsubscribe from events.
 
template<typename Event >
void publish (const Event &event)
 Publish an event to all subscribers.
 
template<typename Event >
size_t subscriber_count () const
 Get the number of subscribers for an event type.
 
void clear ()
 Clear all subscriptions.
 

Private Attributes

std::shared_mutex mutex_
 
std::atomic< bool > running_
 
std::atomic< uint64_t > next_id_
 
std::unordered_map< std::type_index, std::vector< HandlerEntry > > handlers_
 

Detailed Description

Thread-safe event bus for publish/subscribe pattern.

Allows components to communicate through events without direct coupling. Supports type-safe event publishing and subscription.

Definition at line 45 of file patterns.cppm.

Constructor & Destructor Documentation

◆ SimpleEventBus()

kcenon::common::patterns::SimpleEventBus::SimpleEventBus ( )
inlineexport

Definition at line 47 of file patterns.cppm.

47: running_(false), next_id_(1) {}

◆ ~SimpleEventBus()

kcenon::common::patterns::SimpleEventBus::~SimpleEventBus ( )
inlineexport

Definition at line 49 of file patterns.cppm.

49 {
50 stop();
51 }

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ clear()

void kcenon::common::patterns::SimpleEventBus::clear ( )
inlineexport

Clear all subscriptions.

Definition at line 146 of file patterns.cppm.

146 {
147 std::unique_lock lock(mutex_);
148 handlers_.clear();
149 }
std::unordered_map< std::type_index, std::vector< HandlerEntry > > handlers_

References handlers_, and mutex_.

◆ is_running()

bool kcenon::common::patterns::SimpleEventBus::is_running ( ) const
inlineexport

Check if the event bus is running.

Definition at line 70 of file patterns.cppm.

70 {
71 return running_.load(std::memory_order_acquire);
72 }

References running_.

Referenced by publish().

Here is the caller graph for this function:

◆ publish()

template<typename Event >
void kcenon::common::patterns::SimpleEventBus::publish ( const Event & event)
inlineexport

Publish an event to all subscribers.

Template Parameters
EventEvent type
Parameters
eventEvent to publish

Definition at line 116 of file patterns.cppm.

116 {
117 if (!is_running()) return;
118
119 std::shared_lock lock(mutex_);
120 auto type = std::type_index(typeid(Event));
121 auto it = handlers_.find(type);
122 if (it != handlers_.end()) {
123 for (const auto& entry : it->second) {
124 entry.handler(&event);
125 }
126 }
127 }
bool is_running() const
Check if the event bus is running.
Definition patterns.cppm:70

References handlers_, is_running(), and mutex_.

Here is the call graph for this function:

◆ start()

void kcenon::common::patterns::SimpleEventBus::start ( )
inlineexport

Start the event bus.

Definition at line 56 of file patterns.cppm.

56 {
57 running_.store(true, std::memory_order_release);
58 }

References running_.

◆ stop()

void kcenon::common::patterns::SimpleEventBus::stop ( )
inlineexport

Stop the event bus.

Definition at line 63 of file patterns.cppm.

63 {
64 running_.store(false, std::memory_order_release);
65 }

References running_.

Referenced by ~SimpleEventBus().

Here is the caller graph for this function:

◆ subscribe()

template<typename Event >
uint64_t kcenon::common::patterns::SimpleEventBus::subscribe ( std::function< void(const Event &)> handler)
inlineexport

Subscribe to events of a specific type.

Template Parameters
EventEvent type to subscribe to
Parameters
handlerHandler function to call when event is published
Returns
Subscription ID for unsubscribing

Definition at line 81 of file patterns.cppm.

81 {
82 std::unique_lock lock(mutex_);
83 uint64_t id = next_id_++;
84 auto type = std::type_index(typeid(Event));
85
86 auto& handlers = handlers_[type];
87 handlers.push_back({id, [handler](const void* event) {
88 handler(*static_cast<const Event*>(event));
89 }});
90
91 return id;
92 }

References handlers_, mutex_, and next_id_.

◆ subscriber_count()

template<typename Event >
size_t kcenon::common::patterns::SimpleEventBus::subscriber_count ( ) const
inlineexport

Get the number of subscribers for an event type.

Definition at line 133 of file patterns.cppm.

133 {
134 std::shared_lock lock(mutex_);
135 auto type = std::type_index(typeid(Event));
136 auto it = handlers_.find(type);
137 if (it != handlers_.end()) {
138 return it->second.size();
139 }
140 return 0;
141 }

References handlers_, and mutex_.

◆ unsubscribe()

void kcenon::common::patterns::SimpleEventBus::unsubscribe ( uint64_t subscription_id)
inlineexport

Unsubscribe from events.

Parameters
subscription_idID returned from subscribe

Definition at line 98 of file patterns.cppm.

98 {
99 std::unique_lock lock(mutex_);
100 for (auto& [type, handlers] : handlers_) {
101 handlers.erase(
102 std::remove_if(handlers.begin(), handlers.end(),
103 [subscription_id](const auto& entry) {
104 return entry.id == subscription_id;
105 }),
106 handlers.end());
107 }
108 }
uint64_t subscription_id
Type alias for subscription ID.
Definition event_bus.h:105

References handlers_, and mutex_.

Member Data Documentation

◆ handlers_

std::unordered_map<std::type_index, std::vector<HandlerEntry> > kcenon::common::patterns::SimpleEventBus::handlers_
exportprivate

Definition at line 160 of file patterns.cppm.

Referenced by clear(), publish(), subscribe(), subscriber_count(), and unsubscribe().

◆ mutex_

std::shared_mutex kcenon::common::patterns::SimpleEventBus::mutex_
mutableexportprivate

Definition at line 157 of file patterns.cppm.

Referenced by clear(), publish(), subscribe(), subscriber_count(), and unsubscribe().

◆ next_id_

std::atomic<uint64_t> kcenon::common::patterns::SimpleEventBus::next_id_
exportprivate

Definition at line 159 of file patterns.cppm.

Referenced by subscribe().

◆ running_

std::atomic<bool> kcenon::common::patterns::SimpleEventBus::running_
exportprivate

Definition at line 158 of file patterns.cppm.

Referenced by is_running(), start(), and stop().


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