-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.cppm
More file actions
261 lines (199 loc) · 6.63 KB
/
Copy pathtask.cppm
File metadata and controls
261 lines (199 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
export module mcpplibs.cmp:task;
import std;
export namespace mcpplibs::cmp {
// 完成时对称转移到等待方,避免嵌套 Task 持续增长原生调用栈
template<typename T = void>
requires (
std::same_as<T, void> ||
(std::is_object_v<T> && !std::is_array_v<T>)
)
class [[nodiscard]] Task {
public:
struct promise_type {
std::optional<T> result_ {};
std::exception_ptr exception_ {};
std::coroutine_handle<> continuation_ { std::noop_coroutine() };
[[nodiscard]] Task get_return_object() noexcept;
[[nodiscard]] constexpr std::suspend_always initial_suspend() const noexcept {
return {};
}
class FinalAwaiter {
public:
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
[[nodiscard]] std::coroutine_handle<> await_suspend(
std::coroutine_handle<promise_type> coroutine) const noexcept {
return coroutine.promise().continuation_;
}
constexpr void await_resume() const noexcept {}
};
[[nodiscard]] constexpr FinalAwaiter final_suspend() const noexcept {
return {};
}
template<typename U>
requires std::constructible_from<T, U&&>
void return_value(U&& value)
noexcept(std::is_nothrow_constructible_v<T, U&&>) {
result_.emplace(std::forward<U>(value));
}
void unhandled_exception() noexcept {
exception_ = std::current_exception();
}
};
private:
using Handle = std::coroutine_handle<promise_type>;
class Awaiter {
private:
Handle coroutine_ {};
public:
explicit Awaiter(Handle coroutine) noexcept
: coroutine_ { coroutine } {}
Awaiter(const Awaiter&) = delete;
Awaiter& operator=(const Awaiter&) = delete;
Awaiter(Awaiter&& other) noexcept
: coroutine_ { std::exchange(other.coroutine_, {}) } {}
Awaiter& operator=(Awaiter&&) = delete;
~Awaiter() {
if (coroutine_) {
coroutine_.destroy();
}
}
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
[[nodiscard]] std::coroutine_handle<> await_suspend(
std::coroutine_handle<> continuation) noexcept {
coroutine_.promise().continuation_ = continuation;
return coroutine_;
}
T await_resume() {
auto& promise = coroutine_.promise();
if (promise.exception_) {
std::rethrow_exception(promise.exception_);
}
return std::move(*promise.result_);
}
};
Handle coroutine_ {};
explicit Task(Handle coroutine) noexcept
: coroutine_ { coroutine } {}
public:
Task() = delete;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task(Task&& other) noexcept
: coroutine_ { std::exchange(other.coroutine_, {}) } {}
Task& operator=(Task&&) = delete;
~Task() {
if (coroutine_) {
coroutine_.destroy();
}
}
[[nodiscard]] auto operator co_await() && noexcept {
if (!coroutine_) {
std::terminate();
}
return Awaiter { std::exchange(coroutine_, {}) };
}
};
template<typename T>
requires (
std::same_as<T, void> ||
(std::is_object_v<T> && !std::is_array_v<T>)
)
Task<T> Task<T>::promise_type::get_return_object() noexcept {
return Task {
std::coroutine_handle<promise_type>::from_promise(*this)
};
}
template<>
class [[nodiscard]] Task<void> {
public:
struct promise_type {
std::exception_ptr exception_ {};
std::coroutine_handle<> continuation_ { std::noop_coroutine() };
[[nodiscard]] Task get_return_object() noexcept;
[[nodiscard]] constexpr std::suspend_always initial_suspend() const noexcept {
return {};
}
class FinalAwaiter {
public:
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
[[nodiscard]] std::coroutine_handle<> await_suspend(
std::coroutine_handle<promise_type> coroutine) const noexcept {
return coroutine.promise().continuation_;
}
constexpr void await_resume() const noexcept {}
};
[[nodiscard]] constexpr FinalAwaiter final_suspend() const noexcept {
return {};
}
constexpr void return_void() const noexcept {}
void unhandled_exception() noexcept {
exception_ = std::current_exception();
}
};
private:
using Handle = std::coroutine_handle<promise_type>;
class Awaiter {
private:
Handle coroutine_ {};
public:
explicit Awaiter(Handle coroutine) noexcept
: coroutine_ { coroutine } {}
Awaiter(const Awaiter&) = delete;
Awaiter& operator=(const Awaiter&) = delete;
Awaiter(Awaiter&& other) noexcept
: coroutine_ { std::exchange(other.coroutine_, {}) } {}
Awaiter& operator=(Awaiter&&) = delete;
~Awaiter() {
if (coroutine_) {
coroutine_.destroy();
}
}
[[nodiscard]] constexpr bool await_ready() const noexcept {
return false;
}
[[nodiscard]] std::coroutine_handle<> await_suspend(
std::coroutine_handle<> continuation) noexcept {
coroutine_.promise().continuation_ = continuation;
return coroutine_;
}
void await_resume() {
auto& promise = coroutine_.promise();
if (promise.exception_) {
std::rethrow_exception(promise.exception_);
}
}
};
Handle coroutine_ {};
explicit Task(Handle coroutine) noexcept
: coroutine_ { coroutine } {}
public:
Task() = delete;
Task(const Task&) = delete;
Task& operator=(const Task&) = delete;
Task(Task&& other) noexcept
: coroutine_ { std::exchange(other.coroutine_, {}) } {}
Task& operator=(Task&&) = delete;
~Task() {
if (coroutine_) {
coroutine_.destroy();
}
}
[[nodiscard]] auto operator co_await() && noexcept {
if (!coroutine_) {
std::terminate();
}
return Awaiter { std::exchange(coroutine_, {}) };
}
};
inline Task<void> Task<void>::promise_type::get_return_object() noexcept {
return Task {
std::coroutine_handle<promise_type>::from_promise(*this)
};
}
} // namespace mcpplibs::cmp