MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Event.cpp
Go to the documentation of this file.
1#include "Event.hpp"
2
4
5namespace MayaFlux::Vruta {
6
8{
9 return Event { std::coroutine_handle<event_promise>::from_promise(*this) };
10}
11
12Event::Event(std::coroutine_handle<promise_type> h)
13 : m_handle(h)
14{
15 if (!m_handle || !m_handle.address()) {
16 error<std::invalid_argument>(Journal::Component::Vruta, Journal::Context::EventDispatch, std::source_location::current(),
17 "Event: Invalid coroutine handle");
18 }
19}
20
21Event::Event(const Event& other)
22 : m_handle(nullptr)
23{
24 if (other.m_handle) {
25 m_handle = other.m_handle;
26 }
27}
28
30{
31 if (this != &other) {
32 if (m_handle) {
33 m_handle.destroy();
34 }
35
36 if (other.m_handle) {
37 m_handle = other.m_handle;
38 } else {
39 m_handle = nullptr;
40 }
41 }
42 return *this;
43}
44
45Event::Event(Event&& other) noexcept
46 : m_handle(std::exchange(other.m_handle, {}))
47{
48}
49
50Event& Event::operator=(Event&& other) noexcept
51{
52 if (this != &other) {
53 if (m_handle && m_handle.address())
54 m_handle.destroy();
55
56 m_handle = std::exchange(other.m_handle, {});
57 }
58 return *this;
59}
60
62{
63 if (m_handle && m_handle.address())
64 m_handle.destroy();
65}
66
68{
69 if (!m_handle) {
71 }
72 return m_handle.promise().processing_token;
73}
74
75bool Event::is_active() const
76{
77 if (!m_handle) {
78 return false;
79 }
80 return m_handle.address() != nullptr && !m_handle.done();
81}
82
83void Event::set_state_impl(const std::string& key, std::any value)
84{
85 if (m_handle) {
86 m_handle.promise().state[key] = std::move(value);
87 }
88}
89
90void* Event::get_state_impl_raw(const std::string& key)
91{
92 if (!m_handle) {
93 return nullptr;
94 }
95
96 auto& state_map = m_handle.promise().state;
97 auto it = state_map.find(key);
98 if (it != state_map.end()) {
99 return &it->second;
100 }
101 return nullptr;
102}
103
105{
106 if (m_handle && !m_handle.done()) {
107 m_handle.resume();
108 }
109}
110
111std::coroutine_handle<Event::promise_type> Event::get_handle() const
112{
113 return m_handle;
114}
115
116bool Event::done() const
117{
118 return m_handle ? m_handle.done() : true;
119}
120}
uint32_t h
Definition InkPress.cpp:28
virtual ProcessingToken get_processing_token() const
Get the processing token that determines how this routine should be scheduled.
Definition Event.cpp:67
virtual bool is_active() const
Checks if the coroutine is still active.
Definition Event.cpp:75
virtual ~Event()
Destructor.
Definition Event.cpp:61
virtual void * get_state_impl_raw(const std::string &key)
Definition Event.cpp:90
bool done() const
Definition Event.cpp:116
std::coroutine_handle< promise_type > m_handle
Definition Event.hpp:225
Event & operator=(const Event &other)
Copy assignment operator.
Definition Event.cpp:29
Event(std::coroutine_handle< promise_type > h)
Constructs a Event from a coroutine handle.
Definition Event.cpp:12
virtual void set_state_impl(const std::string &key, std::any value)
Definition Event.cpp:83
std::coroutine_handle< promise_type > get_handle() const
Definition Event.cpp:111
Coroutine type for event-driven suspension.
Definition Event.hpp:26
@ EventDispatch
Event dispatching and coordination.
@ Vruta
Coroutines, schedulers, clocks, task management.
@ ON_DEMAND
Coroutine is executed on demand, not scheduled.