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 Multi-domain promise accessor
78 *
79 * Usage in ComplexRoutine:
80 * ```cpp
81 * auto routine = []() -> ComplexRoutine {
82 * auto& promise = co_await GetComplexPromise{};
83 * // promise is complex_promise&
84 * };
85 * ```
86 */
88
89/**
90 * @struct GetEventPromise
91 * @brief Event-domain promise accessor with optional NetworkSource ownership transfer.
92 *
93 * Extends GetPromiseBase for the event domain. When constructed with a
94 * NetworkSource, deposits it into the promise's owned_sources on
95 * await_resume, binding the source lifetime to the coroutine frame.
96 *
97 * Users familiar with coroutine mechanics are expected to use this as the
98 * designated entry point for resource ownership in Event coroutines.
99 *
100 * @code
101 * auto handler = []() -> Vruta::Event {
102 * auto source = std::make_shared<Vruta::NetworkSource>(
103 * Core::EndpointInfo{ .transport = Core::NetworkTransport::UDP, .local_port = 8000 });
104 * co_await Kriya::GetEventPromise{ source };
105 * while (true) {
106 * auto msg = co_await source->next_message();
107 * }
108 * };
109 * @endcode
110 */
111struct MAYAFLUX_API GetEventPromise : public GetPromiseBase<Vruta::event_promise> {
112 explicit GetEventPromise(std::shared_ptr<Vruta::NetworkSource> source = nullptr)
113 : m_source(std::move(source))
114 {
115 }
116
117 [[nodiscard]] Vruta::event_promise& await_resume() const noexcept
118 {
119 if (m_source) {
120 promise_ptr->own(m_source);
121 }
122 return *promise_ptr;
123 }
124
125private:
126 std::shared_ptr<Vruta::NetworkSource> m_source;
127};
128
129}
uint32_t h
Definition InkPress.cpp:25
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.
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.
void own(std::shared_ptr< Vruta::NetworkSource > source)
Transfer ownership of a NetworkSource to this coroutine frame.
Definition Promise.hpp:367