MayaFlux 1.0.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 * Detaches the event, then requests its termination, so its completion
22 * callback fires. Replacement by name in add_event goes through this.
23 */
24 bool remove_event(const std::string& name);
25
26 /**
27 * @brief Get a named event
28 * @param name event name
29 * @return Shared pointer to event or nullptr
30 */
31 std::shared_ptr<Event> get_event(const std::string& name) const;
32
33 /**
34 * @brief Get all managed events
35 */
36 std::vector<std::shared_ptr<Event>> get_all_events() const { return m_events; }
37
38 /**
39 * @brief Cancels and removes a event from the manager
40 * @param event Shared pointer to the event to cancel
41 * @return True if the event was found and cancelled, false otherwise
42 *
43 * This method removes a event from the manager, preventing it from
44 * executing further. It's used to stop events that are no longer needed
45 * or to clean up before shutting down the engine. The event is detached
46 * from the manager before termination is requested.
47 */
48 bool cancel_event(const std::shared_ptr<Event>& event);
49
50 /**
51 * @brief Cancel a event by name
52 * @param name event name to cancel
53 * @return True if found and cancelled
54 */
55 bool cancel_event(const std::string& name);
56
57 /**
58 * @brief Update parameters of a named event
59 * @tparam Args Parameter types
60 * @param name event name
61 * @param args New parameters
62 * @return True if event found and updated
63 */
64 template <typename... Args>
65 bool update_event_params(const std::string& name, Args&&... args)
66 {
67 if (auto event = find_event_by_name(name); event && event->is_active()) {
68 event->update_params(std::forward<Args>(args)...);
69 return true;
70 }
71 return false;
72 }
73
74 /**
75 * @brief Get event state value by name and key
76 * @tparam T State value type
77 * @param name event name
78 * @param state_key State key
79 * @return Pointer to value or nullptr
80 */
81 template <typename T>
82 T* get_event_state(const std::string& name, const std::string& state_key) const
83 {
84 if (auto event = find_event_by_name(name); event && event->is_active()) {
85 return event->get_state<T>(state_key);
86 }
87 return nullptr;
88 }
89
90 /**
91 * @brief Create value accessor function for named event
92 * @tparam T Value type
93 * @param name event name
94 * @param state_key State key
95 * @return Function returning current value
96 */
97 template <typename T>
98 std::function<T()> create_value_accessor(const std::string& name, const std::string& state_key) const
99 {
100 return [this, name, state_key]() -> T {
101 if (auto value = get_event_state<T>(name, state_key)) {
102 return *value;
103 }
104 return T {};
105 };
106 }
107
108 /**
109 * @brief Generates a unique event ID for new events
110 * @return A unique event ID
111 */
112 uint64_t get_next_event_id() const;
113
114 /**
115 * @brief Check if the manager has any active events
116 * @return True if the manager has active events
117 */
118 bool has_active_events() const;
119
120 /**
121 * @brief Get all event names for debugging/inspection
122 * @return Vector of all event names
123 */
124 std::vector<std::string> get_event_names() const;
125
126 /**
127 * @brief Terminate and clear all events
128 *
129 * Empties both the event list and the name map, then requests termination
130 * of each event. Named lookups return null afterwards.
131 */
132 void terminate_all_events();
133
134private:
135 /**
136 * @brief Generate automatic name for a event based on its type
137 * @param event The event to name
138 * @return Generated name
139 */
140 std::string auto_generate_name(const std::shared_ptr<Event>& event) const;
141
142 /**
143 * @brief Find event entry by name
144 * @param name event name to find
145 * @return Event
146 */
147 std::shared_ptr<Event> find_event_by_name(const std::string& name);
148
149 /**
150 * @brief Find event entry by name (const version)
151 * @param name event name to find
152 * @return Const iterator to event entry or end()
153 */
154 std::shared_ptr<Event> find_event_by_name(const std::string& name) const;
155
156 /**
157 * @brief Clean up completed events
158 */
159 void cleanup_completed_events();
160
161 /**
162 * @brief event ID counter for unique identification
163 */
164 mutable std::atomic<uint64_t> m_next_event_id { 1 };
165
166 std::vector<std::shared_ptr<Event>> m_events;
167 std::unordered_map<std::string, std::shared_ptr<Event>> m_named_events;
168};
169
170}
glm::vec3 value
std::string name
Definition VKDevice.cpp:123
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::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.