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_cancelation_token.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/task_handle.hxx
>
6
7
namespace
ice
8
{
9
10
struct
TaskCancelationToken
:
TaskTokenBase
11
{
12
using
TaskTokenBase::TaskTokenBase
;
13
14
inline
bool
was_cancelled
() const noexcept {
_handle
.was_cancelled(); }
15
16
inline
auto
checkpoint
() const noexcept;
17
18
template<typename Fn>
19
inline auto
checkpoint
(Fn&& on_cancel_cb) const noexcept;
20
};
21
22
inline auto
TaskCancelationToken
::
checkpoint
() const noexcept
23
{
24
struct
Awaitable
25
{
26
ice::TaskState
state;
27
28
// Skip checkpoint if the task was not canceled.
29
inline
bool
await_ready()
const
noexcept
{
return
ice::has_none
(state,
TaskState::Canceled
); }
30
31
// If we where canceled destroy the coroutine to clean everything up and make the Task's coroutine handle object invalid.
32
inline
auto
await_suspend(std::coroutine_handle<> coro)
const
noexcept
->
ice::coroutine_handle<>
33
{
34
// Get the base promise type for all of our coroutines. (We assume the TaskPromiseBase is always the base type)
35
auto
const
promise_coro =
ice::coroutine_handle<ice::TaskPromiseBase>::from_address
(coro.address());
36
37
// If there is a continuation, resume into it
38
if
(
ice::coroutine_handle<>
const
continuation = promise_coro.promise().continuation(); continuation)
39
{
40
return
continuation;
41
}
42
return
std::noop_coroutine();
43
}
44
45
constexpr
void
await_resume()
const
noexcept
46
{
47
}
48
49
} await{
_handle
.state() };
50
return
await;
51
}
52
53
template
<
typename
Fn>
54
inline
auto
TaskCancelationToken::checkpoint
(Fn&& on_cancel_cb)
const
noexcept
55
{
56
struct
Awaitable
57
{
58
ice::TaskState
state;
59
Fn _fn;
60
61
// Skip checkpoint if the task was not canceled.
62
inline
bool
await_ready()
const
noexcept
{
return
ice::has_none
(state,
TaskState::Canceled
); }
63
64
// If we where canceled destroy the coroutine to clean everything up and make the Task's coroutine handle object invalid.
65
inline
auto
await_suspend(std::coroutine_handle<> coro)
const
noexcept
->
ice::coroutine_handle<>
66
{
67
_fn();
// We already know we are cancelled, can be used to cleanup
68
69
// Get the base promise type for all of our coroutines. (We assume the TaskPromiseBase is always the base type)
70
auto
const
promise_coro =
ice::coroutine_handle<ice::TaskPromiseBase>::from_address
(coro.address());
71
72
// If there is a continuation, resume into it
73
if
(
ice::coroutine_handle<>
const
continuation = promise_coro.promise().continuation(); continuation)
74
{
75
return
continuation;
76
}
77
return
std::noop_coroutine();
78
}
79
80
constexpr
void
await_resume()
const
noexcept
81
{
82
}
83
84
} await{
_handle
.state(), ice::forward<Fn>(on_cancel_cb) };
85
return
await;
86
}
87
88
}
// namespace ice
89
90
// Free function traits
91
template
<
typename
Result
,
typename
... Args>
92
struct
std::coroutine_traits<
ice
::Task<Result>,
ice::TaskCancelationToken
, Args...>
93
{
94
using
promise_type
=
ice::TaskInfoPromise<Result>
;
95
};
96
97
// Member function traits
98
template
<
typename
Result
,
typename
Class,
typename
... Args>
99
struct
std::coroutine_traits<
ice
::Task<Result>, Class,
ice::TaskCancelationToken
, Args...>
100
{
101
using
promise_type
=
ice::TaskInfoPromise<Result>
;
102
};
ice
SPDX-License-Identifier: MIT.
Definition
array.hxx:12
ice::TaskState
TaskState
All states a task can be in.
Definition
task_info.hxx:13
ice::TaskState::Canceled
@ Canceled
Task was canceled at any point of it's lifetime.
Definition
task_info.hxx:32
ice::coroutine_handle
std::coroutine_handle< Type > coroutine_handle
Definition
task_types.hxx:42
ice::has_none
constexpr bool has_none(T value, T expected_flags) noexcept
Definition
enum_flags.hxx:89
ice::Result
ice::Expected< ice::ErrorCode > Result
Definition
expected.hxx:197
ice::TaskCancelationToken
Definition
task_cancelation_token.hxx:11
ice::TaskCancelationToken::checkpoint
auto checkpoint() const noexcept
Definition
task_cancelation_token.hxx:22
ice::TaskCancelationToken::was_cancelled
bool was_cancelled() const noexcept
Definition
task_cancelation_token.hxx:14
ice::TaskCancelationToken::TaskTokenBase
TaskTokenBase(ice::TaskHandle &handle) noexcept
Definition
task_handle.hxx:235
ice::TaskInfoPromise
Definition
task_handle.hxx:143
ice::TaskTokenBase::_handle
ice::TaskHandle & _handle
Definition
task_handle.hxx:240
ice::TaskTokenBase::TaskTokenBase
TaskTokenBase(ice::TaskHandle &handle) noexcept
Definition
task_handle.hxx:235
std::coroutine_traits< ice::Task< Result >, Class, ice::TaskCancelationToken, Args... >::promise_type
ice::TaskInfoPromise< Result > promise_type
Definition
task_cancelation_token.hxx:101
std::coroutine_traits< ice::Task< Result >, ice::TaskCancelationToken, Args... >::promise_type
ice::TaskInfoPromise< Result > promise_type
Definition
task_cancelation_token.hxx:94
task_handle.hxx
Generated by
1.18.0