1#ifndef NEFORCE_CORE_ASYNC_USE_AWAITABLE_HPP__
2#define NEFORCE_CORE_ASYNC_USE_AWAITABLE_HPP__
13#ifdef NEFORCE_STANDARD_20
17NEFORCE_BEGIN_NAMESPACE__
37template <
typename... Args>
40 awaitable() =
default;
50 continuation_.resume();
61 continuation_.resume();
65 NEFORCE_NODISCARD
bool await_ready() const noexcept {
return ready_; }
67 void await_suspend(coroutine_handle<> h)
noexcept { continuation_ = h; }
73 if constexpr (
sizeof...(Args) == 1) {
74 return _NEFORCE
move(_NEFORCE get<0>(data_));
75 }
else if constexpr (
sizeof...(Args) == 0) {
78 return _NEFORCE
move(data_);
83 tuple<Args...> data_{};
85 exception_ptr exception_{
nullptr};
86 coroutine_handle<> continuation_{
nullptr};
92template <
typename... Args>
93struct awaitable_handler {
94 shared_ptr<awaitable<Args...>> awaitable_;
96 void operator()(Args... args) { awaitable_->set_value(_NEFORCE
move(args)...); }
101template <
typename... Args>
102struct async_result<use_awaitable_t, void(Args...)> {
103 using handler_type = inner::awaitable_handler<Args...>;
104 using return_type = awaitable<Args...>;
106 shared_ptr<return_type> awaitable_;
107 handler_type handler_;
109 explicit async_result(use_awaitable_t ) {
110 awaitable_ = make_shared<return_type>();
111 handler_.awaitable_ = awaitable_;
114 handler_type get_handler() {
return handler_; }
116 return_type
get() {
return _NEFORCE
move(*awaitable_); }
127class awaitable<void> {
155 state_->ready_ =
true;
156 if (state_->continuation_) {
157 state_->continuation_.resume();
165 state_->exception_ =
move(e);
166 state_->ready_ =
true;
167 if (state_->continuation_) {
168 state_->continuation_.resume();
172 NEFORCE_NODISCARD
bool await_ready() const noexcept {
return state_->ready_; }
173 void await_suspend(coroutine_handle<> h)
noexcept { state_->continuation_ = h; }
175 void await_resume() {
176 if (state_->exception_) {
182struct awaitable<void>::promise_type {
183 shared_ptr<awaitable<void>::state> state_;
188 awaitable<void> get_return_object() {
return awaitable<void>(state_); }
189 suspend_never initial_suspend() noexcept {
return {}; }
191 auto final_suspend() noexcept {
193 shared_ptr<awaitable<void>::state> state;
194 NEFORCE_NODISCARD
bool await_ready() const noexcept {
return false; }
195 void await_suspend(coroutine_handle<> h)
noexcept {
196 state->ready_ =
true;
197 if (state->continuation_) {
198 state->continuation_.resume();
202 void await_resume() noexcept {}
204 return awaiter{state_};
207 void return_void() noexcept {}
208 void unhandled_exception() {
210 state_->ready_ =
true;
216NEFORCE_END_NAMESPACE__
void set_exception(exception_ptr e)
设置异常
void set_value(Args... args)
设置完成值
void set_exception(exception_ptr e)
设置异常
enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
constexpr use_awaitable_t use_awaitable
use_awaitable 常量
std::coroutine_handle< Promise > coroutine_handle
协程句柄
void rethrow_exception(const exception_ptr &p)
重新抛出异常
exception_ptr current_exception() noexcept
获取当前异常
enable_if_t<!is_unbounded_array_v< T > &&is_constructible_v< T, Args... >, shared_ptr< T > > make_shared(Args &&... args)
融合分配创建共享指针
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr tuple< unwrap_ref_decay_t< Types >... > make_tuple(Types &&... args)
从参数创建元组