NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
co_spawn.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_CO_SPAWN_HPP__
2#define NEFORCE_CORE_ASYNC_CO_SPAWN_HPP__
3
11
13#ifdef NEFORCE_STANDARD_20
15NEFORCE_BEGIN_NAMESPACE__
16
22
29struct co_spawn_task {
30 struct promise_type {
31 exception_ptr exception_;
32
33 co_spawn_task get_return_object() { return co_spawn_task{coroutine_handle<promise_type>::from_promise(*this)}; }
34 suspend_always initial_suspend() noexcept { return {}; }
35 auto final_suspend() noexcept {
36 struct final_awaiter {
37 bool await_ready() noexcept { return false; }
38 void await_suspend(coroutine_handle<promise_type> h) noexcept { h.destroy(); }
39 void await_resume() noexcept {}
40 };
41 return final_awaiter{};
42 }
43 void return_void() noexcept {}
44 void unhandled_exception() { exception_ = current_exception(); }
45
46 template <typename U>
47 auto await_transform(U&& u) {
48 return forward<U>(u);
49 }
50 };
51
53
54 explicit co_spawn_task(coroutine_handle<promise_type> h) :
55 handle_(h) {}
56};
57
72template <typename Executor, typename Func>
73void co_spawn(Executor&& exec, Func func) {
74 auto inner = [](Func f) -> co_spawn_task { co_await f(); };
75 auto task = inner(func);
76 exec.execute([h = task.handle_]() mutable { h.resume(); });
77}
78 // CoroutineSpawn
80
81NEFORCE_END_NAMESPACE__
82#endif // NEFORCE_STANDARD_20
83#endif // NEFORCE_CORE_ASYNC_CO_SPAWN_HPP__
协程支持
异常指针实现
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
std::coroutine_handle< Promise > coroutine_handle
协程句柄
void co_spawn(Executor &&exec, Func func)
在 executor 上启动协程
exception_ptr current_exception() noexcept
获取当前异常
co_spawn 使用的协程任务类型
始终暂停的等待器