MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GetPromise.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Kriya {
6
7/**
8 * @struct GetPromiseBase
9 * @brief Templated awaitable for accessing a coroutine's promise object
10 *
11 * This template allows coroutines to access their own promise object in a
12 * type-safe, domain-agnostic way. Each domain (audio, graphics, complex, event)
13 * can instantiate this template with their specific promise type.
14 *
15 * @tparam PromiseType The promise type to access (audio_promise, graphics_promise, etc.)
16 */
17template <typename PromiseType>
18struct MAYAFLUX_API GetPromiseBase {
19 using promise_handle = PromiseType;
20
21 /**
22 * @brief Pointer to store the promise object
23 *
24 * This field is set during await_suspend and returned by
25 * await_resume, providing the coroutine with access to its
26 * own promise object.
27 */
28 promise_handle* promise_ptr = nullptr;
29
30 GetPromiseBase() = default;
31
32 [[nodiscard]] inline bool await_ready() const noexcept
33 {
34 return false;
35 }
36
37 void await_suspend(std::coroutine_handle<promise_handle> h) noexcept
38 {
39 promise_ptr = &h.promise();
40 h.promise().active_delay_context = Vruta::DelayContext::AWAIT;
41 }
42
43 [[nodiscard]] promise_handle& await_resume() const noexcept
44 {
45 return *promise_ptr;
46 }
47};
48
49/**
50 * @brief Audio domain promise accessor
51 *
52 * Usage in SoundRoutine:
53 * ```cpp
54 * auto routine = []() -> SoundRoutine {
55 * auto& promise = co_await GetPromise{};
56 * // or explicitly: co_await GetAudioPromise{};
57 * // promise is audio_promise&
58 * };
59 * ```
60 */
62
63/**
64 * @brief Graphics domain promise accessor
65 *
66 * Usage in GraphicsRoutine:
67 * ```cpp
68 * auto routine = []() -> GraphicsRoutine {
69 * auto& promise = co_await GetGraphicsPromise{};
70 * // promise is graphics_promise&
71 * };
72 * ```
73 */
75
76/**
77 * @brief Promise accessor specialization for cross_promise.
78 *
79 * Identical to the primary template except the AWAIT context is published with
80 * release ordering, since cross_promise::active_delay_context is atomic and
81 * read concurrently by the sample-clock and frame-clock pumps.
82 */
83template <>
84struct MAYAFLUX_API GetPromiseBase<Vruta::cross_promise> {
86
87 promise_handle* promise_ptr = nullptr;
88
89 GetPromiseBase() = default;
90
91 [[nodiscard]] inline bool await_ready() const noexcept { return false; }
92
93 void await_suspend(std::coroutine_handle<promise_handle> h) noexcept
94 {
95 promise_ptr = &h.promise();
96 h.promise().active_delay_context.store(
97 Vruta::DelayContext::AWAIT, std::memory_order_release);
98 }
99
100 [[nodiscard]] promise_handle& await_resume() const noexcept
101 {
102 return *promise_ptr;
103 }
104};
105
107
108/**
109 * @struct GetEventPromise
110 * @brief Event-domain promise accessor with optional NetworkSource ownership transfer.
111 *
112 * Extends GetPromiseBase for the event domain. When constructed with a
113 * NetworkSource, deposits it into the promise's owned_sources on
114 * await_resume, binding the source lifetime to the coroutine frame.
115 *
116 * Users familiar with coroutine mechanics are expected to use this as the
117 * designated entry point for resource ownership in Event coroutines.
118 *
119 * @code
120 * auto handler = []() -> Vruta::Event {
121 * auto source = std::make_shared<Vruta::NetworkSource>(
122 * Core::EndpointInfo{ .transport = Core::NetworkTransport::UDP, .local_port = 8000 });
123 * co_await Kriya::GetEventPromise{ source };
124 * while (true) {
125 * auto msg = co_await source->next_message();
126 * }
127 * };
128 * @endcode
129 */
130struct MAYAFLUX_API GetEventPromise : public GetPromiseBase<Vruta::event_promise> {
131 explicit GetEventPromise(std::shared_ptr<Vruta::NetworkSource> source = nullptr)
132 : m_source(std::move(source))
133 {
134 }
135
136 [[nodiscard]] Vruta::event_promise& await_resume() const noexcept
137 {
138 if (m_source) {
139 promise_ptr->own(m_source);
140 }
141 return *promise_ptr;
142 }
143
144private:
145 std::shared_ptr<Vruta::NetworkSource> m_source;
146};
147
148}
uint32_t h
Definition InkPress.cpp:28
Vruta::event_promise & await_resume() const noexcept
std::shared_ptr< Vruta::NetworkSource > m_source
GetEventPromise(std::shared_ptr< Vruta::NetworkSource > source=nullptr)
Event-domain promise accessor with optional NetworkSource ownership transfer.
void await_suspend(std::coroutine_handle< promise_handle > h) noexcept
Promise accessor specialization for cross_promise.
promise_handle & await_resume() const noexcept
bool await_ready() const noexcept
void await_suspend(std::coroutine_handle< promise_handle > h) noexcept
Templated awaitable for accessing a coroutine's promise object.
std::atomic< DelayContext > active_delay_context
Active delay context controlling which pump(s) may resume this routine.
Definition Promise.hpp:391
Coroutine promise for routines resumed by more than one clock.
Definition Promise.hpp:351
void own(std::shared_ptr< Vruta::NetworkSource > source)
Transfer ownership of a NetworkSource to this coroutine frame.
Definition Promise.hpp:485