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