Created
November 30, 2021 09:05
-
-
Save HappyCerberus/d76e0fe10b69f494d71e255027e239c8 to your computer and use it in GitHub Desktop.
[Article] C++20 Practical Coroutines - main_task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct main_task { | |
struct main_promise { | |
using handle_t = std::coroutine_handle<main_promise>; | |
main_task get_return_object() { | |
return main_task{handle_t::from_promise(*this)}; | |
} | |
std::suspend_never initial_suspend() noexcept { return {}; } | |
std::suspend_always final_suspend() noexcept { return {}; } | |
void unhandled_exception() { std::terminate(); } | |
void return_value(int ret) { ret_ = ret; } | |
private: | |
int ret_; | |
friend main_task; | |
}; | |
using promise_type = main_promise; | |
main_task(promise_type::handle_t handle) : handle_(handle) {} | |
~main_task() { | |
if (handle_) | |
handle_.destroy(); | |
} | |
operator int() { return handle_.promise().ret_; } | |
private: | |
promise_type::handle_t handle_; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment