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