29#include <unordered_map>
34#if KCENON_HAS_COMMON_SYSTEM
35#include <kcenon/common/concepts/event.h>
38namespace kcenon {
namespace monitoring {
52concept EventType = std::is_class_v<T> && std::is_copy_constructible_v<T>;
61template <
typename H,
typename E>
63 std::invocable<H, const E&> && std::is_void_v<std::invoke_result_t<H, const E&>>;
72template <
typename F,
typename E>
74 std::invocable<F, const E&> && std::convertible_to<std::invoke_result_t<F, const E&>,
bool>;
134 :
timestamp_(std::chrono::steady_clock::now()),
139 static std::atomic<uint64_t>
counter{0};
140 return counter.fetch_add(1, std::memory_order_relaxed);
162template<
typename EventType>
181 static std::atomic<uint64_t>
counter{0};
182 return counter.fetch_add(1, std::memory_order_relaxed);
265 template <concepts::EventType E>
281 template <concepts::EventType E>
282 common::Result<subscription_token>
subscribe_event(std::function<
void(
const E&)> handler,
286 std::type_index(
typeid(E)),
287 [wrapped_handler](
const std::any& event) { wrapped_handler(std::any_cast<E>(event)); },
288 wrapped_handler.get_id(), priority);
302 template <concepts::EventType E, concepts::EventHandler<E> H>
305 return subscribe_event(std::function<
void(
const E&)>(std::forward<H>(handler)), priority);
320 template <concepts::EventType E>
330 template <concepts::EventType E>
345 virtual common::VoidResult
start() = 0;
351 virtual common::VoidResult
stop() = 0;
368 std::type_index event_type,
372 std::type_index event_type,
373 std::function<
void(
const std::any&)> handler,
378 std::type_index event_type) = 0;
381 std::type_index event_type)
const = 0;
428 virtual common::VoidResult
set_event_bus(std::shared_ptr<interface_event_bus> bus) = 0;
Priority levels for event processing.
Base class for all events in the system.
std::chrono::steady_clock::time_point timestamp_
virtual std::string get_type_name() const =0
Get the event type name.
uint64_t get_id() const
Get unique event ID.
virtual ~event_base()=default
std::chrono::steady_clock::time_point get_timestamp() const
Get timestamp when event was created.
static uint64_t generate_id()
Type-safe event handler wrapper.
std::function< void(const EventType &)> handler_func
event_priority get_priority() const
void operator()(const EventType &event) const
event_handler(handler_func handler, event_priority priority=event_priority::normal)
static uint64_t generate_id()
Pure virtual interface for event bus implementation.
size_t get_subscriber_count() const
Get the number of subscribers for an event type.
virtual common::Result< subscription_token > subscribe_event_impl(std::type_index event_type, std::function< void(const std::any &)> handler, uint64_t handler_id, event_priority priority)=0
virtual bool is_active() const =0
Check if event bus is active.
virtual common::VoidResult unsubscribe_event(const subscription_token &token)=0
Unsubscribe from events using subscription token.
virtual common::VoidResult start()=0
Start the event bus.
virtual common::VoidResult publish_event_impl(std::type_index event_type, std::any event)=0
virtual common::VoidResult process_pending_events()=0
Process all pending events synchronously.
common::VoidResult publish_event(const E &event)
Publish an event to all subscribers.
virtual size_t get_subscriber_count_impl(std::type_index event_type) const =0
common::Result< subscription_token > subscribe_event(H &&handler, event_priority priority=event_priority::normal)
Subscribe to events with a callable handler.
common::VoidResult clear_subscriptions()
Clear all subscriptions for a specific event type.
virtual common::VoidResult clear_subscriptions_impl(std::type_index event_type)=0
virtual size_t get_pending_event_count() const =0
Get pending event count.
common::Result< subscription_token > subscribe_event(std::function< void(const E &)> handler, event_priority priority=event_priority::normal)
Subscribe to events of a specific type.
virtual common::VoidResult stop()=0
Stop the event bus.
virtual ~interface_event_bus()=default
Interface for components that publish events.
virtual std::shared_ptr< interface_event_bus > get_event_bus() const =0
Get the current event bus.
virtual ~interface_event_publisher()=default
virtual common::VoidResult set_event_bus(std::shared_ptr< interface_event_bus > bus)=0
Set the event bus for publishing.
Interface for components that subscribe to events.
virtual ~interface_event_subscriber()=default
virtual common::VoidResult subscribe_to_events(std::shared_ptr< interface_event_bus > bus)=0
Subscribe to required events.
virtual common::VoidResult unsubscribe_from_events()=0
Unsubscribe from all events.
virtual std::vector< subscription_token > get_subscriptions() const =0
Get subscription tokens.
Token for managing event subscriptions.
std::type_index get_event_type() const
uint64_t get_handler_id() const
subscription_token(std::type_index event_type, uint64_t handler_id)
std::type_index event_type_
A callable that filters events based on criteria.
A callable that can handle events of a specific type.
A type that can be used as an event in the event bus.
Unified feature flags header for monitoring_system.
@ counter
Monotonically increasing counter.
Result pattern type definitions for monitoring system.