MayaFlux 0.3.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Chain.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::Vruta {
4class TaskScheduler;
5class SoundRoutine;
6}
7
8namespace MayaFlux::Kriya {
9
10/**
11 * @class EventChain
12 * @brief A sequential chain of timed events with precise temporal control
13 *
14 * The EventChain class provides a way to schedule a sequence of events to occur
15 * at specific time intervals. It's designed for creating temporal sequences of
16 * actions with sample-accurate timing, which is essential for deterministic computational flows.
17 *
18 * This approach is inspired by reactive programming and temporal logic systems
19 * where precise sequencing of operations is critical. It allows for creating complex
20 * temporal behaviors with a simple, declarative API.
21 *
22 * Example usage:
23 * ```cpp
24 * // Create an event chain
25 * EventChain chain(*scheduler);
26 *
27 * // Add events with specific delays
28 * chain.then([]() { process_initial_state(); }) // Immediate
29 * .then([]() { transform_data(); }, 0.5) // After 0.5 seconds
30 * .then([]() { apply_filter(); }, 0.25) // After another 0.25 seconds
31 * .then([]() { finalize_output(); }, 0.25); // After another 0.25 seconds
32 *
33 * // Start the chain
34 * chain.start();
35 * ```
36 *
37 * The EventChain is particularly useful for creating precisely timed computational sequences,
38 * state transitions, or any series of time-based events that need to occur in a specific
39 * order with deterministic timing.
40 */
41class MAYAFLUX_API EventChain {
42public:
43 /**
44 * @brief Constructs an EventChain with an explicit scheduler
45 * @param scheduler The TaskScheduler to use for timing
46 * @param name Optional name for the event chain (useful for debugging)
47 *
48 * Creates a new EventChain that will use the provided scheduler for timing
49 * operations. This allows for more control over which scheduler is used,
50 * which is useful in contexts where multiple processing engines might exist.
51 */
52 EventChain(Vruta::TaskScheduler& scheduler, std::string name = "");
53
54 /**
55 * @brief Adds an event to the chain with a specified delay
56 * @param action Function to execute when the event occurs
57 * @param delay_seconds Time to wait before executing this event (in seconds)
58 * @return Reference to this EventChain for method chaining
59 *
60 * This method adds an event to the chain, to be executed after the specified
61 * delay from the previous event. The first event's delay is measured from
62 * when start() is called.
63 *
64 * The method returns a reference to the EventChain itself, allowing for a
65 * fluent, declarative API style.
66 */
67 EventChain& then(std::function<void()> action, double delay_seconds = 0.F);
68
69 /**
70 * @brief Repeat the last event N times
71 * @param count Number of times to repeat
72 * @return Reference to this EventChain for method chaining
73 */
74 EventChain& repeat(size_t count);
75
76 /**
77 * @brief Repeat entire chain N times
78 * @param count Number of times to repeat the entire sequence
79 * @return Reference to this EventChain for method chaining
80 */
81 EventChain& times(size_t count);
82
83 /**
84 * @brief Add a wait/delay without an action
85 * @param delay_seconds Time to wait in seconds
86 * @return Reference to this EventChain for method chaining
87 */
88 EventChain& wait(double delay_seconds);
89
90 /**
91 * @brief Convenience method for repeating an action at regular intervals
92 * @param interval_seconds Time between each execution
93 * @param action Function to execute
94 * @return Reference to this EventChain for method chaining
95 */
96 EventChain& every(double interval_seconds, std::function<void()> action);
97
98 /**
99 * @brief Starts executing the event chain
100 *
101 * This method begins the execution of the event chain, scheduling each event
102 * to occur at its specified time. The events are executed in the order they
103 * were added, with the specified delays between them.
104 *
105 * The timing is sample-accurate, ensuring that each event occurs at precisely
106 * the right moment in the computational timeline.
107 */
108 void start();
109
110 /**
111 * @brief Cancels the event chain if it's currently executing
112 *
113 * Terminates the underlying coroutine, preventing any remaining
114 * events from executing. Safe to call even if chain has completed.
115 */
116 void cancel();
117
118 /**
119 * @brief Checks if the event chain is currently active
120 * @return True if chain is executing, false if completed or not started
121 */
122 [[nodiscard]] bool is_active() const;
123
124 /**
125 * @brief Sets a callback to execute when the chain stops executing
126 * @param callback Function to call after chain completes or is cancelled
127 *
128 * The callback fires regardless of how the chain stops:
129 * - After final event completes normally
130 * - After cancel() is called
131 * - After an exception in an action (action exceptions are caught)
132 *
133 * Use this for cleanup that must happen regardless of completion reason.
134 * For actions that should only run on successful completion, use .then()
135 */
136 EventChain& on_complete(std::function<void()> callback);
137
138 /**
139 * @brief Gets the name of the event chain
140 * @return Name of the event chain
141 *
142 * The name can be used for debugging or management purposes, especially when
143 * multiple chains are active. If no name was set, this will return an empty string.
144 */
145 [[nodiscard]] const std::string& name() const { return m_name; }
146
147 /**
148 * @brief Gets the number of events in the chain
149 * @return Number of events
150 */
151 [[nodiscard]] size_t event_count() const { return m_events.size(); }
152
153 /**
154 * @brief Gets the repeat count for the entire chain
155 * @return Number of times the chain will repeat
156 */
157 [[nodiscard]] size_t repeat_count() const { return m_repeat_count; }
158
159private:
160 /**
161 * @brief Structure representing a timed event in the chain
162 *
163 * This structure encapsulates an action to perform and the delay before
164 * performing it, relative to the previous event in the chain.
165 */
166 struct TimedEvent {
167 std::function<void()> action; ///< Function to execute
168 double delay_seconds; ///< Delay before execution
169 };
170
171 /**
172 * @brief Collection of events in this chain
173 *
174 * This vector contains all the events that have been added to the chain,
175 * in the order they will be executed.
176 */
177 std::vector<TimedEvent> m_events;
178
179 /**
180 * @brief Reference to the scheduler that manages timing
181 *
182 * The scheduler provides the timing infrastructure needed for
183 * precise execution of events in the chain.
184 */
186
187 /**
188 * @brief The underlying computational routine that implements the chain
189 *
190 * This coroutine handles the actual timing and execution of events
191 * in the chain. It's created when start() is called.
192 */
193 std::shared_ptr<Vruta::SoundRoutine> m_routine;
194
195 /**
196 * @brief Optional callback to execute when the chain completes
197 *
198 * This function is called after the final event in the chain has executed.
199 * It can be used for cleanup, triggering subsequent actions, or any other
200 * behavior that should occur after the chain finishes.
201 */
202 std::function<void()> m_on_complete;
203
204 /**
205 * @brief Optional name for the event chain
206 *
207 * This name can be used for debugging or management purposes, especially when
208 * multiple chains are active. If no name is provided, it will be an empty string.
209 */
210 std::string m_name;
211
212 bool m_on_complete_fired {}; ///< Flag to ensure on_complete is only fired once
213
214 /**
215 * @brief Internal method to safely fire the on_complete callback
216 */
217 void fire_on_complete();
218
219 size_t m_repeat_count { 1 }; ///< Number of times to repeat the entire chain
220
221 uint64_t m_default_rate { 48000 };
222};
223
224}
Eigen::Index count
Vruta::TaskScheduler & m_Scheduler
Reference to the scheduler that manages timing.
Definition Chain.hpp:185
const std::string & name() const
Gets the name of the event chain.
Definition Chain.hpp:145
std::function< void()> m_on_complete
Optional callback to execute when the chain completes.
Definition Chain.hpp:202
std::vector< TimedEvent > m_events
Collection of events in this chain.
Definition Chain.hpp:177
size_t repeat_count() const
Gets the repeat count for the entire chain.
Definition Chain.hpp:157
std::string m_name
Optional name for the event chain.
Definition Chain.hpp:210
size_t event_count() const
Gets the number of events in the chain.
Definition Chain.hpp:151
std::shared_ptr< Vruta::SoundRoutine > m_routine
The underlying computational routine that implements the chain.
Definition Chain.hpp:193
A sequential chain of timed events with precise temporal control.
Definition Chain.hpp:41
Token-based multimodal task scheduling system for unified coroutine processing.
Definition Scheduler.hpp:51
double delay_seconds
Delay before execution.
Definition Chain.hpp:168
std::function< void()> action
Function to execute.
Definition Chain.hpp:167
Structure representing a timed event in the chain.
Definition Chain.hpp:166