IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
task.hxx
Go to the documentation of this file.
1
3
4#pragma once
6
7namespace ice
8{
9
10 template<typename Result>
11 class Task final
12 {
13 public:
16
17 public:
18 inline explicit Task(
20 ) noexcept;
21 inline ~Task() noexcept;
22
23 inline Task(Task const&) noexcept = delete;
24 inline auto operator=(Task const&) noexcept = delete;
25
26 inline Task(Task&&) noexcept;
27 inline auto operator=(Task&& other) noexcept -> Task&;
28
29 inline auto operator co_await() & noexcept;
30 inline auto operator co_await() && noexcept;
31
32 bool valid() const noexcept { return _coroutine && _coroutine.done() == false; }
33
34 private:
35 struct AwaitableBase
36 {
38
39 inline bool await_ready() const noexcept;
40 inline auto await_suspend(
41 ice::coroutine_handle<> awaiting_coroutine
42 ) const noexcept -> ice::coroutine_handle<>;
43 };
44
45 private:
46 ice::coroutine_handle<PromiseType> _coroutine;
47 };
48
49 template<typename Result>
50 inline bool Task<Result>::AwaitableBase::await_ready() const noexcept
51 {
52 return !_coroutine || _coroutine.done();
53 }
54
55 template<typename Result>
56 inline auto Task<Result>::AwaitableBase::await_suspend(
57 ice::coroutine_handle<> awaiting_coroutine
58 ) const noexcept -> ice::coroutine_handle<>
59 {
60 _coroutine.promise().set_continuation(awaiting_coroutine);
61 return _coroutine;
62 }
63
64 template<typename Result>
66 : _coroutine{ coro }
67 { }
68
69 template<typename Result>
70 inline Task<Result>::~Task() noexcept
71 {
72 if (_coroutine)
73 {
74 _coroutine.destroy();
75 }
76 }
77
78 template<typename Result>
79 inline Task<Result>::Task(Task&& other) noexcept
80 : _coroutine{ ice::exchange(other._coroutine, nullptr) }
81 { }
82
83 template<typename Result>
84 inline auto Task<Result>::operator=(Task&& other) noexcept -> Task&
85 {
86 if (this != &other)
87 {
88 if (_coroutine != nullptr)
89 {
90 _coroutine.destroy();
91 }
92
93 _coroutine = ice::exchange(other._coroutine, nullptr);
94 }
95
96 return *this;
97 }
98
99 template<typename Result>
100 inline auto Task<Result>::operator co_await() & noexcept
101 {
102 struct TaskAwaitable : AwaitableBase
103 {
104 auto await_resume() const noexcept -> decltype(auto)
105 {
107 this->_coroutine.operator bool(),
108 "Broken promise on coroutine Task!"
109 );
110
111 if constexpr (std::is_same_v<ValueType, void> == false)
112 {
113 return this->_coroutine.promise().result();
114 }
115 }
116 };
117
118 return TaskAwaitable{ _coroutine };
119 }
120
121 template<typename Result>
122 inline auto Task<Result>::operator co_await() && noexcept
123 {
124 struct TaskAwaitable : AwaitableBase
125 {
126 auto await_resume() const noexcept -> decltype(auto)
127 {
129 this->_coroutine.operator bool(),
130 "Broken promise on coroutine Task!"
131 );
132
133 if constexpr (std::is_same_v<ValueType, void> == false)
134 {
135 return ice::move(this->_coroutine.promise().result());
136 }
137 }
138 };
139
140 return TaskAwaitable{ ice::move(_coroutine) };
141 }
142
143 template<typename Value>
144 inline auto ice::TaskPromise<Value>::get_return_object() noexcept -> ice::Task<Value>
145 {
147 }
148
149 template<typename Value>
150 inline auto ice::TaskPromise<Value&>::get_return_object() noexcept -> ice::Task<Value&>
151 {
153 }
154
156 {
158 }
159
160} // namespace ice
161
162template<typename Result, typename... Args>
163struct std::coroutine_traits<ice::Task<Result>, Args...>
164{
166};
#define ICE_ASSERT(condition, message,...)
Definition assert.hxx:29
Definition task.hxx:12
ice::TaskPromise< ValueType > PromiseType
Definition task.hxx:15
bool valid() const noexcept
Definition task.hxx:32
Task(ice::coroutine_handle< PromiseType > coro=nullptr) noexcept
Definition task.hxx:65
auto operator=(Task const &) noexcept=delete
~Task() noexcept
Definition task.hxx:70
Result ValueType
Definition task.hxx:14
Definition task_promise.hxx:13
auto get_return_object() noexcept -> ice::Task< Value >
Definition task.hxx:144
SPDX-License-Identifier: MIT.
Definition array.hxx:12
std::coroutine_handle< Type > coroutine_handle
Definition task_types.hxx:42
ice::Expected< ice::ErrorCode > Result
Definition expected.hxx:197
typename ice::Task< Result >::PromiseType promise_type
Definition task.hxx:165