MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ExecutionContext.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <typeindex>
4
5namespace MayaFlux::Yantra {
6
7/**
8 * @enum OperationType
9 * @brief Operation categories for organization and discovery
10 */
11enum class OperationType : uint8_t {
13 SORTER,
16 CUSTOM
17};
18
19/**
20 * @enum ExecutionMode
21 * @brief Execution paradigms for operations
22 */
23enum class ExecutionMode : uint8_t {
24 SYNC, ///< Synchronous execution
25 ASYNC, ///< Asynchronous execution
26 PARALLEL, ///< Parallel with other operations
27 CHAINED, ///< Part of a sequential chain
28 DEPENDENCY ///< Part of dependency graph
29};
30
31/**
32 * @brief Callback type for pre/post operation hooks
33 */
34using OperationHookCallback = std::function<void(std::any&)>;
35
36/**
37 * @brief Callback type for custom reconstruction logic
38 */
39using ReconstructionCallback = std::function<std::any(std::vector<std::vector<double>>&, std::any&)>;
40
41/**
42 * @struct ExecutionContext
43 * @brief Context information controlling how a compute operation executes.
44 *
45 * ExecutionContext provides execution metadata, dependency hints, and hooks
46 * that influence how a Yantra operation is scheduled and run.
47 *
48 * The `execution_metadata` map allows arbitrary user-defined parameters
49 * to be passed into operations. All reads should be performed using the
50 * provided accessors (`get`, `get_or`, `get_or_throw`) which internally
51 * use `safe_any_cast` to provide robust type conversion and diagnostics.
52 *
53 * Typical usage:
54 *
55 * @code
56 * ExecutionContext ctx;
57 *
58 * ctx.set("grain_size", 1024)
59 * .set("hop_size", 512)
60 * .depends_on<MyAnalyzer>();
61 *
62 * auto grain = ctx.get_or<uint32_t>("grain_size", 512);
63 * @endcode
64 */
65struct MAYAFLUX_API ExecutionContext {
66
67 /**
68 * @brief Execution mode controlling scheduling behavior.
69 */
70 ExecutionMode mode = ExecutionMode::SYNC;
71
72 /**
73 * @brief Optional thread pool for asynchronous or parallel execution.
74 */
75 std::shared_ptr<std::thread> thread_pool = nullptr;
76
77 /**
78 * @brief Operation dependencies required before execution.
79 *
80 * Stores type identifiers for operations that must complete before
81 * this context's operation may run.
82 */
83 std::vector<std::type_index> dependencies;
84
85 /**
86 * @brief Optional timeout for operation execution.
87 */
88 std::chrono::milliseconds timeout { 0 };
89
90 /**
91 * @brief Arbitrary metadata parameters used by operations.
92 *
93 * This key/value store carries runtime configuration such as
94 * algorithm parameters, flags, thresholds, or domain-specific values.
95 *
96 * Values are stored as `std::any` and should be retrieved via
97 * `get()` or `get_or()` to ensure safe casting.
98 */
99 std::unordered_map<std::string, std::any> execution_metadata;
100
101 /**
102 * @brief Optional callback invoked before operation execution.
103 */
104 OperationHookCallback pre_execution_hook = nullptr;
105
106 /**
107 * @brief Optional callback invoked after operation execution.
108 */
109 OperationHookCallback post_execution_hook = nullptr;
110
111 /**
112 * @brief Optional callback used for custom reconstruction of results.
113 */
114 ReconstructionCallback reconstruction_callback = nullptr;
115
116 //=====================================================================
117 // Metadata helpers
118 //=====================================================================
119
120 /**
121 * @brief Insert or update metadata value.
122 *
123 * Adds or replaces a value in the metadata store.
124 *
125 * @tparam T Value type
126 * @param key Metadata key
127 * @param value Value to store
128 * @return Reference to this context for fluent chaining
129 */
130 template <typename T>
131 ExecutionContext& set(std::string key, T&& value)
132 {
133 execution_metadata[std::move(key)] = std::forward<T>(value);
134 return *this;
135 }
136
137 /**
138 * @brief Retrieve metadata value using safe casting.
139 *
140 * Uses `safe_any_cast` internally, allowing numeric conversions
141 * and providing detailed error reporting.
142 *
143 * @tparam T Expected type
144 * @param key Metadata key
145 * @return CastResult containing the value or error details
146 */
147 template <typename T>
148 CastResult<T> get(const std::string& key) const
149 {
150 auto it = execution_metadata.find(key);
151
152 if (it == execution_metadata.end()) {
153 CastResult<T> result;
154 result.error = "ExecutionContext missing key: " + key;
155 return result;
156 }
157
158 return safe_any_cast<T>(it->second);
159 }
160
161 /**
162 * @brief Retrieve metadata value or return a default.
163 *
164 * @tparam T Expected type
165 * @param key Metadata key
166 * @param default_value Value returned if key missing or conversion fails
167 * @return Retrieved or default value
168 */
169 template <typename T>
170 T get_or(const std::string& key, const T& default_value) const
171 {
172 auto it = execution_metadata.find(key);
173
174 if (it == execution_metadata.end())
175 return default_value;
176
177 return safe_any_cast<T>(it->second).value_or(default_value);
178 }
179
180 /**
181 * @brief Retrieve metadata value or throw if unavailable.
182 *
183 * Uses `safe_any_cast_or_throw`.
184 *
185 * @tparam T Expected type
186 * @param key Metadata key
187 * @return Retrieved value
188 *
189 * @throws std::runtime_error if key missing or conversion fails
190 */
191 template <typename T>
192 T get_or_throw(const std::string& key) const
193 {
194 auto it = execution_metadata.find(key);
195
196 if (it == execution_metadata.end())
197 throw std::runtime_error("ExecutionContext missing key: " + key);
198
199 return safe_any_cast_or_throw<T>(it->second);
200 }
201
202 /**
203 * @brief Check whether a metadata key exists.
204 *
205 * @param key Metadata key
206 * @return True if key is present
207 */
208 bool contains(const std::string& key) const
209 {
210 return execution_metadata.contains(key);
211 }
212
213 //=====================================================================
214 // Dependency helpers
215 //=====================================================================
216
217 /**
218 * @brief Register dependency on a specific operation type.
219 *
220 * @tparam T Operation type
221 * @return Reference to this context for fluent chaining
222 */
223 template <typename T>
225 {
226 dependencies.emplace_back(typeid(T));
227 return *this;
228 }
229
230 //=====================================================================
231 // Hook helpers
232 //=====================================================================
233
234 /**
235 * @brief Set pre-execution hook.
236 *
237 * @param cb Callback invoked before operation execution
238 * @return Reference to this context for fluent chaining
239 */
241 {
242 pre_execution_hook = std::move(cb);
243 return *this;
244 }
245
246 /**
247 * @brief Set post-execution hook.
248 *
249 * @param cb Callback invoked after operation execution
250 * @return Reference to this context for fluent chaining
251 */
253 {
254 post_execution_hook = std::move(cb);
255 return *this;
256 }
257
258 /**
259 * @brief Set reconstruction callback.
260 *
261 * @param cb Reconstruction logic for output data
262 * @return Reference to this context for fluent chaining
263 */
265 {
266 reconstruction_callback = std::move(cb);
267 return *this;
268 }
269
270 /**
271 * @brief Set execution timeout.
272 *
273 * @param duration Maximum allowed runtime
274 * @return Reference to this context for fluent chaining
275 */
276 ExecutionContext& with_timeout(std::chrono::milliseconds duration)
277 {
278 timeout = duration;
279 return *this;
280 }
281
282 /**
283 * @brief Set execution mode.
284 *
285 * @param m Desired execution mode
286 * @return Reference to this context for fluent chaining
287 */
289 {
290 mode = m;
291 return *this;
292 }
293};
294
295}
std::optional< double > duration
@ CUSTOM
User-defined analysis types.
std::function< void(std::any &)> OperationHookCallback
Callback type for pre/post operation hooks.
OperationType
Operation categories for organization and discovery.
ExecutionMode
Execution paradigms for operations.
@ SYNC
Synchronous execution.
@ CHAINED
Part of a sequential chain.
@ DEPENDENCY
Part of dependency graph.
@ ASYNC
Asynchronous execution.
@ PARALLEL
Parallel with other operations.
std::function< std::any(std::vector< std::vector< double > > &, std::any &)> ReconstructionCallback
Callback type for custom reconstruction logic.
ExecutionContext & on_reconstruct(ReconstructionCallback cb)
Set reconstruction callback.
std::vector< std::type_index > dependencies
Operation dependencies required before execution.
CastResult< T > get(const std::string &key) const
Retrieve metadata value using safe casting.
ExecutionContext & on_pre(OperationHookCallback cb)
Set pre-execution hook.
ExecutionContext & set_mode(ExecutionMode m)
Set execution mode.
ExecutionContext & with_timeout(std::chrono::milliseconds duration)
Set execution timeout.
bool contains(const std::string &key) const
Check whether a metadata key exists.
T get_or_throw(const std::string &key) const
Retrieve metadata value or throw if unavailable.
std::unordered_map< std::string, std::any > execution_metadata
Arbitrary metadata parameters used by operations.
ExecutionContext & on_post(OperationHookCallback cb)
Set post-execution hook.
T get_or(const std::string &key, const T &default_value) const
Retrieve metadata value or return a default.
ExecutionContext & depends_on()
Register dependency on a specific operation type.
ExecutionContext & set(std::string key, T &&value)
Insert or update metadata value.
Context information controlling how a compute operation executes.