Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
Awaitable.hpp
Go to the documentation of this file.
1#pragma once
3#include <coroutine>
4#include <exception>
5#include <optional>
6#include <stdexcept>
7#include <type_traits>
8#include <utility>
9
11
12template <typename T = void>
13class Awaitable;
14
15namespace detail {
16
17template <typename T>
19 template <typename U>
20 requires std::is_convertible_v<U&&, T>
21 void return_value(U&& value) {
22 value_.emplace(std::forward<U>(value));
23 }
24 T take() {
25 if (error_) std::rethrow_exception(error_);
26 return std::move(*value_);
27 }
28
29 std::optional<T> value_;
30 std::exception_ptr error_;
31};
32
33template <>
34struct AwaitableResult<void> {
35 void return_void() noexcept {}
36 void take() {
37 if (error_) std::rethrow_exception(error_);
38 }
39
40 std::exception_ptr error_;
41};
42
43} // namespace detail
44
76template <typename T>
77class [[nodiscard]] Awaitable {
78public:
80 std::coroutine_handle<> continuation = std::noop_coroutine();
81
83 return Awaitable(std::coroutine_handle<promise_type>::from_promise(*this));
84 }
85 std::suspend_always initial_suspend() noexcept { return {}; }
86
87 struct FinalAwaiter {
88 bool await_ready() noexcept { return false; }
89 std::coroutine_handle<> await_suspend(std::coroutine_handle<promise_type> h) noexcept {
90 return h.promise().continuation;
91 }
92 void await_resume() noexcept {}
93 };
94 FinalAwaiter final_suspend() noexcept { return {}; }
95
96 void unhandled_exception() noexcept { this->error_ = std::current_exception(); }
97 };
98
99 Awaitable(Awaitable&& other) noexcept : handle_(std::exchange(other.handle_, nullptr)) {}
100 Awaitable& operator=(Awaitable&& other) noexcept {
101 if (this != &other) {
102 if (handle_) handle_.destroy();
103 handle_ = std::exchange(other.handle_, nullptr);
104 }
105 return *this;
106 }
107 Awaitable(const Awaitable&) = delete;
108 Awaitable& operator=(const Awaitable&) = delete;
110 if (handle_) handle_.destroy();
111 }
112
113 bool await_ready() const noexcept { return !handle_ || handle_.done(); }
114 std::coroutine_handle<> await_suspend(std::coroutine_handle<> awaiting) noexcept {
115 handle_.promise().continuation = awaiting;
116 return handle_;
117 }
119 if (!handle_) throw std::logic_error("co_await on an empty (moved-from) Awaitable");
120 return handle_.promise().take();
121 }
122
123private:
124 explicit Awaitable(std::coroutine_handle<promise_type> h) noexcept : handle_(h) {}
125
126 std::coroutine_handle<promise_type> handle_;
127};
128
129} // namespace orbit::concurrency
A lazy coroutine that produces a T for whoever co_awaits it.
Definition Awaitable.hpp:77
Awaitable(const Awaitable &)=delete
std::coroutine_handle await_suspend(std::coroutine_handle<> awaiting) noexcept
Definition Awaitable.hpp:114
Awaitable & operator=(Awaitable &&other) noexcept
Definition Awaitable.hpp:100
std::coroutine_handle< promise_type > handle_
Definition Awaitable.hpp:126
Awaitable(std::coroutine_handle< promise_type > h) noexcept
Definition Awaitable.hpp:124
T await_resume()
Definition Awaitable.hpp:118
Awaitable & operator=(const Awaitable &)=delete
Awaitable(Awaitable &&other) noexcept
Definition Awaitable.hpp:99
bool await_ready() const noexcept
Definition Awaitable.hpp:113
~Awaitable()
Definition Awaitable.hpp:109
Definition Awaitable.hpp:10
bool await_ready() noexcept
Definition Awaitable.hpp:88
void await_resume() noexcept
Definition Awaitable.hpp:92
std::coroutine_handle await_suspend(std::coroutine_handle< promise_type > h) noexcept
Definition Awaitable.hpp:89
FinalAwaiter final_suspend() noexcept
Definition Awaitable.hpp:94
std::suspend_always initial_suspend() noexcept
Definition Awaitable.hpp:85
void unhandled_exception() noexcept
Definition Awaitable.hpp:96
Awaitable get_return_object() noexcept
Definition Awaitable.hpp:82
void return_void() noexcept
Definition Awaitable.hpp:35
void take()
Definition Awaitable.hpp:36
std::exception_ptr error_
Definition Awaitable.hpp:40
T take()
Definition Awaitable.hpp:24
std::exception_ptr error_
Definition Awaitable.hpp:30
std::optional< T > value_
Definition Awaitable.hpp:29
void return_value(U &&value)
Definition Awaitable.hpp:21