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