MayaFlux 0.1.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EventBus.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace Lila {
4
5/**
6 * @enum EventType
7 * @brief Enumerates all event types supported by the Lila event system.
8 *
9 * These events represent key lifecycle and runtime actions in the live coding environment,
10 * such as client connections, code evaluation, symbol definition, and server state changes.
11 */
12enum class EventType : uint8_t {
13 ClientConnected, ///< A client has connected to the server
14 ClientDisconnected, ///< A client has disconnected from the server
15 EvalStart, ///< Code evaluation has started
16 EvalSuccess, ///< Code evaluation succeeded
17 EvalError, ///< Code evaluation failed
18 SymbolDefined, ///< A new symbol was defined in the interpreter
19 ServerStart, ///< The server has started
20 ServerStop ///< The server has stopped
21};
22
23/**
24 * @brief Converts an EventType to its string representation.
25 * @param type EventType value
26 * @return String view of the event name
27 */
28constexpr std::string_view to_string(EventType type)
29{
30 switch (type) {
31 case EventType::ClientConnected:
32 return "client_connected";
33 case EventType::ClientDisconnected:
34 return "client_disconnected";
35 case EventType::EvalStart:
36 return "eval_start";
37 case EventType::EvalSuccess:
38 return "eval_success";
39 case EventType::EvalError:
40 return "eval_error";
41 case EventType::SymbolDefined:
42 return "symbol_defined";
43 case EventType::ServerStart:
44 return "server_start";
45 case EventType::ServerStop:
46 return "server_stop";
47 default:
48 return "unknown";
49 }
50}
51
52/**
53 * @struct ClientInfo
54 * @brief Information about a connected client.
55 *
56 * Used for connection/disconnection events.
57 */
58struct ClientInfo {
59 int fd; ///< Client socket file descriptor
60 std::string session_id; ///< Session identifier (if set)
61 std::chrono::system_clock::time_point connected_at; ///< Connection timestamp
62
63 auto operator<=>(const ClientInfo&) const = default;
64};
65
66/**
67 * @struct EvalEvent
68 * @brief Data for code evaluation events.
69 *
70 * Used for EvalStart events.
71 */
72struct EvalEvent {
73 std::string code_snippet; ///< The code being evaluated
74 std::optional<std::string> session_id; ///< Optional session ID
75 std::chrono::system_clock::time_point timestamp; ///< Event timestamp
76};
77
78/**
79 * @struct ErrorEvent
80 * @brief Data for error events.
81 *
82 * Used for EvalError events.
83 */
84struct ErrorEvent {
85 std::string message; ///< Error message
86 std::optional<std::string> details; ///< Optional error details
87 std::chrono::system_clock::time_point timestamp; ///< Event timestamp
88};
89
90/**
91 * @struct SymbolEvent
92 * @brief Data for symbol definition events.
93 *
94 * Used for SymbolDefined events.
95 */
97 std::string name; ///< Symbol name
98 std::optional<std::string> type; ///< Optional symbol type
99 std::optional<uintptr_t> address; ///< Optional symbol address
100};
101
102/**
103 * @brief Variant type for event data.
104 *
105 * Associates each EventType with its corresponding data structure.
106 */
107using EventData = std::variant<
108 ClientInfo, ///< For ClientConnected, ClientDisconnected
109 EvalEvent, ///< For EvalStart
110 std::monostate, ///< For EvalSuccess, ServerStart, ServerStop
111 ErrorEvent, ///< For EvalError
112 SymbolEvent ///< For SymbolDefined
113 >;
114
115/**
116 * @struct StreamEvent
117 * @brief Represents a published event in the system.
118 *
119 * Contains the event type, associated data, and timestamp.
120 */
122 EventType type; ///< Type of event
123 EventData data; ///< Event data
124 std::chrono::system_clock::time_point timestamp; ///< Event timestamp
125
126 StreamEvent(EventType t, EventData d = std::monostate {});
127};
128
129/**
130 * @class Subscription
131 * @brief Abstract base class for event subscriptions.
132 *
133 * Implement this interface to receive events from the EventBus.
134 */
136public:
137 virtual ~Subscription() = default;
138 /**
139 * @brief Called when an event is published.
140 * @param event The published event
141 */
142 virtual void on_event(const StreamEvent& event) = 0;
143};
144
145/**
146 * @concept EventHandler
147 * @brief Concept for event handler functions.
148 *
149 * Any callable that takes a const StreamEvent& and returns void.
150 */
151template <typename T>
152concept EventHandler = requires(T handler, const StreamEvent& event) {
153 { handler(event) } -> std::same_as<void>;
154};
155
156/**
157 * @class EventBus
158 * @brief Thread-safe event subscription and publishing system for Lila.
159 *
160 * The EventBus allows components to subscribe to specific event types and receive notifications
161 * when those events are published. Supports both Subscription objects and simple handler functions.
162 *
163 * ## Usage Example
164 * ```cpp
165 * // Subscribe with a handler function
166 * bus.subscribe(EventType::EvalError, [](const StreamEvent& event) {
167 * // Handle error event
168 * });
169 *
170 * // Subscribe with a Subscription object
171 * class MySub : public Lila::Subscription { ... };
172 * bus.subscribe(EventType::ClientConnected, std::make_shared<MySub>());
173 *
174 * // Publish an event
175 * bus.publish(StreamEvent{EventType::EvalSuccess});
176 * ```
177 *
178 * Thread safety: All subscription and publishing operations are protected by a mutex.
179 */
180class EventBus {
181private:
182 std::unordered_map<EventType, std::vector<std::weak_ptr<Subscription>>> m_subscribers; ///< Subscribers by event type
183 std::mutex m_mutex; ///< Mutex for thread safety
184
185public:
186 /**
187 * @brief Subscribe to an event type with a Subscription object.
188 * @param type EventType to subscribe to
189 * @param subscriber Shared pointer to Subscription
190 */
191 void subscribe(EventType type, const std::shared_ptr<Subscription>& subscriber);
192
193 /**
194 * @brief Subscribe to an event type with a handler function.
195 * @tparam Handler Callable matching EventHandler concept
196 * @param type EventType to subscribe to
197 * @param handler Handler function
198 */
199 template <EventHandler Handler>
200 void subscribe(EventType type, Handler&& handler)
201 {
202 struct HandlerSubscription : Subscription {
203 Handler m_handler;
204 HandlerSubscription(Handler&& h)
205 : m_handler(std::forward<Handler>(h))
206 {
207 }
208 void on_event(const StreamEvent& event) override { m_handler(event); }
209 };
210
211 subscribe(type, std::make_shared<HandlerSubscription>(std::forward<Handler>(handler)));
212 }
213
214 /**
215 * @brief Publish an event to all subscribers of its type.
216 * @param event The event to publish
217 */
218 void publish(const StreamEvent& event);
219};
220
221} // namespace Lila
std::mutex m_mutex
Mutex for thread safety.
Definition EventBus.hpp:183
std::unordered_map< EventType, std::vector< std::weak_ptr< Subscription > > > m_subscribers
Subscribers by event type.
Definition EventBus.hpp:182
void subscribe(EventType type, const std::shared_ptr< Subscription > &subscriber)
Subscribe to an event type with a Subscription object.
Definition EventBus.cpp:12
void subscribe(EventType type, Handler &&handler)
Subscribe to an event type with a handler function.
Definition EventBus.hpp:200
void publish(const StreamEvent &event)
Publish an event to all subscribers of its type.
Definition EventBus.cpp:18
Thread-safe event subscription and publishing system for Lila.
Definition EventBus.hpp:180
virtual ~Subscription()=default
virtual void on_event(const StreamEvent &event)=0
Called when an event is published.
Abstract base class for event subscriptions.
Definition EventBus.hpp:135
std::variant< ClientInfo, EvalEvent, std::monostate, ErrorEvent, SymbolEvent > EventData
Variant type for event data.
Definition EventBus.hpp:113
EventType
Enumerates all event types supported by the Lila event system.
Definition EventBus.hpp:12
Concept for event handler functions.
Definition EventBus.hpp:152
constexpr std::string_view to_string(EventType type)
Converts an EventType to its string representation.
Definition EventBus.hpp:28
std::string session_id
Session identifier (if set)
Definition EventBus.hpp:60
int fd
Client socket file descriptor.
Definition EventBus.hpp:59
std::chrono::system_clock::time_point connected_at
Connection timestamp.
Definition EventBus.hpp:61
auto operator<=>(const ClientInfo &) const =default
Information about a connected client.
Definition EventBus.hpp:58
std::optional< std::string > details
Optional error details.
Definition EventBus.hpp:86
std::chrono::system_clock::time_point timestamp
Event timestamp.
Definition EventBus.hpp:87
std::string message
Error message.
Definition EventBus.hpp:85
Data for error events.
Definition EventBus.hpp:84
std::optional< std::string > session_id
Optional session ID.
Definition EventBus.hpp:74
std::chrono::system_clock::time_point timestamp
Event timestamp.
Definition EventBus.hpp:75
std::string code_snippet
The code being evaluated.
Definition EventBus.hpp:73
Data for code evaluation events.
Definition EventBus.hpp:72
EventType type
Type of event.
Definition EventBus.hpp:122
EventData data
Event data.
Definition EventBus.hpp:123
std::chrono::system_clock::time_point timestamp
Event timestamp.
Definition EventBus.hpp:124
Represents a published event in the system.
Definition EventBus.hpp:121
std::optional< uintptr_t > address
Optional symbol address.
Definition EventBus.hpp:99
std::optional< std::string > type
Optional symbol type.
Definition EventBus.hpp:98
std::string name
Symbol name.
Definition EventBus.hpp:97
Data for symbol definition events.
Definition EventBus.hpp:96