MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
EventAwaiter.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <coroutine>
7
8namespace MayaFlux::Kriya {
9
10// =============================================================================
11// EventAwaiter - base
12// =============================================================================
13
14/**
15 * @class EventAwaiter
16 * @brief Abstract base for all event-driven awaiters.
17 *
18 * Provides the interface EventSource uses to manage its waiter list
19 * without knowing the payload type or filter semantics of concrete awaiters.
20 *
21 * @see WindowEventAwaiter
22 * @see SignalAwaiter
23 */
24class MAYAFLUX_API EventAwaiter {
25public:
26 virtual ~EventAwaiter() = default;
27
28 EventAwaiter() = default;
29 EventAwaiter(const EventAwaiter&) = delete;
31 EventAwaiter(EventAwaiter&&) noexcept = default;
32 EventAwaiter& operator=(EventAwaiter&&) noexcept = default;
33
34 /**
35 * @brief Called by the source to attempt resumption of the suspended coroutine.
36 *
37 * @param event Type-erased pointer to the candidate signal value.
38 */
39 virtual void try_resume(const void* event) = 0;
40
41 /**
42 * @brief Returns true if the candidate signal passes this awaiter's filter.
43 *
44 * @param event Type-erased pointer to the candidate signal value.
45 */
46 [[nodiscard]] virtual bool filter_matches(const void* event) const = 0;
47
48protected:
49 std::coroutine_handle<> m_handle;
50 bool m_is_suspended {};
51};
52
53// =============================================================================
54// WindowEventAwaiter
55// =============================================================================
56
57/**
58 * @class WindowEventAwaiter
59 * @brief Awaiter for suspending on GLFW window input events with optional filtering.
60 *
61 * Payload type is Core::WindowEvent. Filter criteria are WindowEventType,
62 * IO::Keys, and IO::MouseButtons via EventFilter. Works with any coroutine
63 * type: SoundRoutine, GraphicsRoutine, Event, ComplexRoutine.
64 *
65 * Multiple awaiters may register against the same WindowEventSource simultaneously.
66 * The source must outlive any suspended awaiter.
67 *
68 * @code
69 * auto ev = co_await source.next_event();
70 * auto ev = co_await source.await_event(WindowEventType::KEY_PRESSED);
71 *
72 * Vruta::EventFilter f(IO::Keys::Escape);
73 * auto ev = co_await WindowEventAwaiter(source, f);
74 * @endcode
75 *
76 * @see WindowEventSource
77 * @see EventFilter
78 */
79class MAYAFLUX_API WindowEventAwaiter : public EventAwaiter {
80public:
82 : m_source(source)
83 , m_filter(std::move(filter))
84 {
85 }
86
87 ~WindowEventAwaiter() override;
88
92 WindowEventAwaiter& operator=(WindowEventAwaiter&&) noexcept = delete;
93
94 /**
95 * @brief Returns true if a matching event is already queued; stores it if so.
96 */
97 bool await_ready();
98
99 /**
100 * @brief Registers with the source and suspends the coroutine.
101 * @param handle Type-erased coroutine handle.
102 */
103 void await_suspend(std::coroutine_handle<> handle);
104
105 /**
106 * @brief Returns the event that caused resumption.
107 */
108 Core::WindowEvent await_resume();
109
110 /**
111 * @brief Casts event to Core::WindowEvent, checks filter, resumes if matched.
112 * @param event Type-erased pointer to Core::WindowEvent.
113 */
114 void try_resume(const void* event) override;
115
116 /**
117 * @brief Casts event to Core::WindowEvent and checks against stored filter.
118 * @param event Type-erased pointer to Core::WindowEvent.
119 */
120 [[nodiscard]] bool filter_matches(const void* event) const override;
121
122private:
123 Vruta::WindowEventSource& m_source;
124 Vruta::WindowEventFilter m_filter;
125 Core::WindowEvent m_result;
126
127 friend class Vruta::WindowEventSource;
128};
129
130} // namespace MayaFlux::Kriya
virtual ~EventAwaiter()=default
EventAwaiter & operator=(const EventAwaiter &)=delete
EventAwaiter(EventAwaiter &&) noexcept=default
EventAwaiter(const EventAwaiter &)=delete
Abstract base for all event-driven awaiters.
WindowEventAwaiter(WindowEventAwaiter &&) noexcept=default
WindowEventAwaiter & operator=(const WindowEventAwaiter &)=delete
WindowEventAwaiter(Vruta::WindowEventSource &source, Vruta::WindowEventFilter filter={})
WindowEventAwaiter(const WindowEventAwaiter &)=delete
Awaiter for suspending on GLFW window input events with optional filtering.
Awaitable stream of GLFW window input events.
Filter criteria for GLFW window input events.