IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
tasks
public
ice
task.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/task_promise.hxx
>
6
7
namespace
ice
8
{
9
10
template
<
typename
Result>
11
class
Task
final
12
{
13
public
:
14
using
ValueType
=
Result
;
15
using
PromiseType
=
ice::TaskPromise<ValueType>
;
16
17
public
:
18
inline
explicit
Task
(
19
ice::coroutine_handle<PromiseType>
coro =
nullptr
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
{
37
ice::coroutine_handle<PromiseType>
_coroutine;
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>
65
inline
Task<Result>::Task
(
ice::coroutine_handle<PromiseType>
coro) noexcept
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
{
106
ICE_ASSERT
(
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
{
128
ICE_ASSERT
(
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
{
146
return
ice::Task<Value>
{
ice::coroutine_handle<ice::TaskPromise<Value>
>::from_promise(*
this
) };
147
}
148
149
template
<
typename
Value>
150
inline
auto
ice::TaskPromise<Value&>::get_return_object
() noexcept ->
ice
::
Task
<Value&>
151
{
152
return
ice::Task<Value&>
{
ice::coroutine_handle<ice::TaskPromise<Value&>
>::from_promise(*
this
) };
153
}
154
155
auto
ice::TaskPromise<void>::get_return_object
() noexcept ->
ice
::
Task
<
void
>
156
{
157
return
ice::Task<void>
{
ice::coroutine_handle<ice::TaskPromise<void>
>::from_promise(*
this
) };
158
}
159
160
}
// namespace ice
161
162
template
<
typename
Result
,
typename
... Args>
163
struct
std::coroutine_traits<
ice
::
Task
<Result>, Args...>
164
{
165
using
promise_type
=
typename
ice::Task<Result>::PromiseType
;
166
};
ICE_ASSERT
#define ICE_ASSERT(condition, message,...)
Definition
assert.hxx:29
ice::Task
Definition
task.hxx:12
ice::Task::PromiseType
ice::TaskPromise< ValueType > PromiseType
Definition
task.hxx:15
ice::Task::valid
bool valid() const noexcept
Definition
task.hxx:32
ice::Task::Task
Task(ice::coroutine_handle< PromiseType > coro=nullptr) noexcept
Definition
task.hxx:65
ice::Task::operator=
auto operator=(Task const &) noexcept=delete
ice::Task::~Task
~Task() noexcept
Definition
task.hxx:70
ice::Task::ValueType
Result ValueType
Definition
task.hxx:14
ice::TaskPromise
Definition
task_promise.hxx:13
ice::TaskPromise::get_return_object
auto get_return_object() noexcept -> ice::Task< Value >
Definition
task.hxx:144
ice
SPDX-License-Identifier: MIT.
Definition
array.hxx:12
ice::coroutine_handle
std::coroutine_handle< Type > coroutine_handle
Definition
task_types.hxx:42
ice::Result
ice::Expected< ice::ErrorCode > Result
Definition
expected.hxx:197
std::coroutine_traits< ice::Task< Result >, Args... >::promise_type
typename ice::Task< Result >::PromiseType promise_type
Definition
task.hxx:165
task_promise.hxx
Generated by
1.18.0