uvco 0.1
Loading...
Searching...
No Matches
awaiter.h
Go to the documentation of this file.
1// uvco (c) 2024 Lewin Bormann. See LICENSE for specific terms.
2
3#include "uvco/loop/loop.h"
4#include <boost/assert.hpp>
5
6#include <coroutine>
7#include <optional>
8
9namespace uvco {
10
11template <typename Result> class GenericAwaiter {
12public:
13 GenericAwaiter() = default;
14 GenericAwaiter(const GenericAwaiter &) = delete;
18
19 [[nodiscard]] bool await_ready() const { return result_.has_value(); }
20
21 void await_suspend(std::coroutine_handle<> handle) {
22 BOOST_ASSERT(!handle_);
23 handle_ = handle;
24 }
25
26 Result await_resume() {
27 BOOST_ASSERT(result_.has_value());
28 return std::move(*result_);
29 }
30
31 void resume(Result result) {
32 result_ = std::move(result);
33 if (handle_) {
34 std::coroutine_handle<> handle = *handle_;
35 handle_.reset();
36 Loop::enqueue(handle);
37 }
38 }
39
40private:
41 std::optional<Result> result_;
42 std::optional<std::coroutine_handle<>> handle_;
43};
44
45} // namespace uvco
Definition awaiter.h:11
GenericAwaiter & operator=(GenericAwaiter &&)=default
GenericAwaiter(GenericAwaiter &&)=default
bool await_ready() const
Definition awaiter.h:19
Result await_resume()
Definition awaiter.h:26
GenericAwaiter()=default
void resume(Result result)
Definition awaiter.h:31
void await_suspend(std::coroutine_handle<> handle)
Definition awaiter.h:21
std::optional< Result > result_
Definition awaiter.h:41
std::optional< std::coroutine_handle<> > handle_
Definition awaiter.h:42
GenericAwaiter & operator=(const GenericAwaiter &)=delete
GenericAwaiter(const GenericAwaiter &)=delete
static void enqueue(std::coroutine_handle<> handle)
Definition loop.cc:73
Definition async_work.cc:17