18#include <unordered_map>
42 std::chrono::steady_clock::time_point
timestamp()
const {
47 std::chrono::steady_clock::time_point
timestamp_{std::chrono::steady_clock::now()};
85 return bus_ !=
nullptr;
100 other.bus_ =
nullptr;
115 std::type_index
type_{
typeid(void)};
123 explicit event_bus(std::shared_ptr<thread_pool> pool =
nullptr)
138 template<
typename Event>
140 auto type = std::type_index(
typeid(Event));
143 std::vector<handler_func> handlers_copy;
145 std::lock_guard<std::mutex> lock(
mutex_);
148 for (
const auto& [
id, handler] : it->second) {
149 handlers_copy.push_back(handler);
157 for (
const auto& handler : handlers_copy) {
159 handler(std::any(event));
173 template<
typename Event>
175 auto type = std::type_index(
typeid(Event));
177 std::lock_guard<std::mutex> lock(
mutex_);
180 for (
const auto& [
id, handler] : it->second) {
182 handler(std::any(event));
196 template<
typename Event>
198 auto type = std::type_index(
typeid(Event));
199 auto wrapped_handler = [handler](
const std::any& any_event) {
201 const auto&
event = std::any_cast<const Event&>(any_event);
203 }
catch (
const std::bad_any_cast&) {
208 std::lock_guard<std::mutex> lock(
mutex_);
219 template<
typename Event>
221 auto type = std::type_index(
typeid(Event));
222 std::lock_guard<std::mutex> lock(
mutex_);
230 std::lock_guard<std::mutex> lock(
mutex_);
239 template<
typename Event>
241 auto type = std::type_index(
typeid(Event));
242 std::lock_guard<std::mutex> lock(
mutex_);
245 return it->second.size();
264 std::lock_guard<std::mutex> lock(
mutex_);
267 it->second.erase(
id);
268 if (it->second.empty()) {
275 std::unordered_map<std::type_index, std::unordered_map<std::size_t, handler_func>>
handlers_;
292 return "SystemStartupEvent";
307 return "SystemShutdownEvent";
325 return "ConfigChangedEvent";
343 return "PerformanceAlertEvent";
Event base class for type safety.
virtual std::string type_name() const =0
Get event type name.
std::chrono::steady_clock::time_point timestamp_
std::chrono::steady_clock::time_point timestamp() const
Get event timestamp.
virtual ~event_base()=default
Subscription handle for managing subscriptions.
subscription(subscription &&other) noexcept
bool is_active() const
Check if subscription is active.
subscription & operator=(const subscription &)=delete
void unsubscribe()
Unsubscribe from events.
subscription(const subscription &)=delete
subscription(event_bus *bus, std::type_index type, std::size_t id)
subscription & operator=(subscription &&other) noexcept
Event Bus for publish-subscribe pattern.
void clear_all_subscriptions()
Clear all subscriptions.
void clear_subscriptions()
Clear all subscriptions for a specific event type.
void publish(const Event &event)
Publish an event asynchronously.
static event_bus & instance()
Get singleton instance.
event_bus(std::shared_ptr< thread_pool > pool=nullptr)
Constructor.
subscription subscribe(std::function< void(const Event &)> handler)
Subscribe to events of a specific type.
std::function< void(const std::any &)> handler_func
Handler function type.
std::unordered_map< std::type_index, std::unordered_map< std::size_t, handler_func > > handlers_
std::size_t subscriber_count() const
Get the number of subscribers for an event type.
void unsubscribe(std::type_index type, std::size_t id)
Unsubscribe a specific handler.
void publish_sync(const Event &event)
Publish an event synchronously.
std::size_t next_handler_id_
std::shared_ptr< thread_pool > thread_pool_
Fluent builder for creating and configuring thread pools.
thread_pool_builder & with_workers(std::size_t count)
Sets the number of worker threads.
std::shared_ptr< thread_pool > build_and_start()
Builds the pool and starts it immediately.
Core thread pool implementation with work stealing and auto-scaling.
Core threading foundation of the thread system library.
Configuration changed event.
config_changed_event(std::string path, std::any old_val, std::any new_val)
std::string type_name() const override
Get event type name.
system_shutdown_event(std::string name, std::string reason_msg="")
std::string type_name() const override
Get event type name.
system_startup_event(std::string name)
std::string type_name() const override
Get event type name.
Fluent builder for creating and configuring thread pools.