MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EventManager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Event.hpp"
4
5namespace MayaFlux::Vruta {
6
7class MAYAFLUX_API EventManager {
8public:
9 EventManager() = default;
10
11 /**
12 * @brief Add a event to the manager
13 * @param event Event to add
14 * @param name Optional name for the event (for event management)
15 */
16 void add_event(const std::shared_ptr<Event>& event, const std::string& name = "");
17
18 /**
19 * @brief Remove an event by name
20 */
21 bool remove_event(const std::string& name);
22
23 /**
24 * @brief Get a named event
25 * @param name event name
26 * @return Shared pointer to event or nullptr
27 */
28 std::shared_ptr<Event> get_event(const std::string& name) const;
29
30 /**
31 * @brief Get all managed events
32 */
33 std::vector<std::shared_ptr<Event>> get_all_events() const;
34
35 /**
36 * @brief Cancels and removes a event from the manager
37 * @param event Shared pointer to the event to cancel
38 * @return True if the event was found and cancelled, false otherwise
39 *
40 * This method removes a event from the manager, preventing it from
41 * executing further. It's used to stop events that are no longer needed
42 * or to clean up before shutting down the engine.
43 */
44 bool cancel_event(const std::shared_ptr<Event>& event);
45
46 /**
47 * @brief Cancel a event by name
48 * @param name event name to cancel
49 * @return True if found and cancelled
50 */
51 bool cancel_event(const std::string& name);
52
53 /**
54 * @brief Update parameters of a named event
55 * @tparam Args Parameter types
56 * @param name event name
57 * @param args New parameters
58 * @return True if event found and updated
59 */
60 template <typename... Args>
61 bool update_event_params(const std::string& name, Args&&... args)
62 {
63 if (auto event = find_event_by_name(name); event && event->is_active()) {
64 event->update_params(std::forward<Args>(args)...);
65 return true;
66 }
67 return false;
68 }
69
70 /**
71 * @brief Get event state value by name and key
72 * @tparam T State value type
73 * @param name event name
74 * @param state_key State key
75 * @return Pointer to value or nullptr
76 */
77 template <typename T>
78 T* get_event_state(const std::string& name, const std::string& state_key) const
79 {
80 if (auto event = find_event_by_name(name); event && event->is_active()) {
81 return event->get_state<T>(state_key);
82 }
83 return nullptr;
84 }
85
86 /**
87 * @brief Create value accessor function for named event
88 * @tparam T Value type
89 * @param name event name
90 * @param state_key State key
91 * @return Function returning current value
92 */
93 template <typename T>
94 std::function<T()> create_value_accessor(const std::string& name, const std::string& state_key) const
95 {
96 return [this, name, state_key]() -> T {
97 if (auto value = get_event_state<T>(name, state_key)) {
98 return *value;
99 }
100 return T {};
101 };
102 }
103
104 /**
105 * @brief Generates a unique event ID for new events
106 * @return A unique event ID
107 */
108 uint64_t get_next_event_id() const;
109
110 /**
111 * @brief Check if the manager has any active events
112 * @return True if the manager has active events
113 */
114 bool has_active_events() const;
115
116 /**
117 * @brief Get all event names for debugging/inspection
118 * @return Vector of all event names
119 */
120 std::vector<std::string> get_event_names() const;
121
122 /**
123 * @brief Terminate and clear all events
124 */
125 void terminate_all_events();
126
127private:
128 /**
129 * @brief Generate automatic name for a event based on its type
130 * @param event The event to name
131 * @return Generated name
132 */
133 std::string auto_generate_name(const std::shared_ptr<Event>& event) const;
134
135 /**
136 * @brief Find event entry by name
137 * @param name event name to find
138 * @return Event
139 */
140 std::shared_ptr<Event> find_event_by_name(const std::string& name);
141
142 /**
143 * @brief Find event entry by name (const version)
144 * @param name event name to find
145 * @return Const iterator to event entry or end()
146 */
147 std::shared_ptr<Event> find_event_by_name(const std::string& name) const;
148
149 /**
150 * @brief Clean up completed events
151 */
152 void cleanup_completed_events();
153
154 /**
155 * @brief event ID counter for unique identification
156 */
157 mutable std::atomic<uint64_t> m_next_event_id { 1 };
158
159 std::vector<std::shared_ptr<Event>> m_events;
160 std::unordered_map<std::string, std::shared_ptr<Event>> m_named_events;
161};
162
163}
T * get_event_state(const std::string &name, const std::string &state_key) const
Get event state value by name and key.
std::unordered_map< std::string, std::shared_ptr< Event > > m_named_events
bool update_event_params(const std::string &name, Args &&... args)
Update parameters of a named event.
std::vector< std::string > get_event_names() const
Get all event names for debugging/inspection.
std::function< T()> create_value_accessor(const std::string &name, const std::string &state_key) const
Create value accessor function for named event.
std::vector< std::shared_ptr< Event > > m_events
std::vector< std::shared_ptr< Event > > get_all_events() const
Get all managed events.