1#ifndef MRS_LIB_CORO_TASK_HPP_
2#define MRS_LIB_CORO_TASK_HPP_
12#include <mrs_lib/coro/internal/attributes.hpp>
25 template <
typename T =
void>
26 requires(std::same_as<T, std::remove_cvref_t<T>>)
38 void operator()(std::coroutine_handle<T> handle)
42 using pointer = std::coroutine_handle<T>;
45 template <
typename T =
void>
68 std::coroutine_handle<T> handle_;
77 template <
typename Derived>
88 FinalAwaitable() =
default;
89 ~FinalAwaitable() =
default;
90 FinalAwaitable(
const FinalAwaitable&) =
delete;
91 FinalAwaitable& operator=(
const FinalAwaitable&) =
delete;
92 FinalAwaitable(FinalAwaitable&&) =
delete;
93 FinalAwaitable& operator=(FinalAwaitable&&) =
delete;
96 bool await_ready()
noexcept;
102 void await_suspend(std::coroutine_handle<Derived> task_handle)
noexcept;
105 void await_resume()
noexcept;
110 std::suspend_always initial_suspend();
112 FinalAwaitable final_suspend()
noexcept;
114 void set_continuation(OwningCoroutineHandle<> continuation);
117 OwningCoroutineHandle<> continuation_{std::noop_coroutine()};
123 template <
typename T>
145 constexpr void set_value(T&& val)
noexcept(std::is_nothrow_move_constructible_v<T>)
147 assert(data_.index() == State::empty);
148 data_.template emplace<State::value>(std::move(val));
158 assert(data_.index() == State::empty || data_.valueless_by_exception());
159 data_.template emplace<State::exception>(std::move(eptr));
172 size_t state = data_.index();
173 if (state == State::exception)
175 std::rethrow_exception(std::get<State::exception>(data_));
177 assert(state == State::value);
178 return std::get<State::value>(std::move(data_));
182 std::variant<std::monostate, T, std::exception_ptr> data_;
190 template <
typename T>
196 void return_value(T&& ret_val);
198 void unhandled_exception();
202 return std::move(result_).get_value();
222 void unhandled_exception();
228 std::rethrow_exception(exception_);
233 std::exception_ptr exception_;
242 template <
typename T>
253 std::coroutine_handle<> await_suspend(std::coroutine_handle<> continuation)
255 task_handle_.promise().set_continuation(OwningCoroutineHandle<>(continuation));
262 return this->task_handle_.promise().get_value();
272 TaskAwaitable(std::coroutine_handle<Promise> task_handle) : task_handle_(task_handle)
276 std::coroutine_handle<Promise> task_handle_;
278 friend class Task<T>;
291 template <
typename T>
292 requires(std::same_as<T, std::remove_cvref_t<T>>)
293 class [[nodiscard(
"Task is lazy and does not run until `co_await`ed.")]] MRS_LIB_INTERNAL_CORO_RETURN_TYPE MRS_LIB_INTERNAL_CORO_LIFETIMEBOUND
Task
300 Task& operator=(
const Task&) =
delete;
310 Task(internal::OwningCoroutineHandle<promise_type> coroutine) : coroutine_(std::move(coroutine))
314 internal::OwningCoroutineHandle<promise_type> coroutine_;
321#ifndef MRS_LIB_CORO_TASK_IMPL_HPP_
322#include <mrs_lib/coro/task.impl.hpp>
Task type for creating coroutines.
Definition task.hpp:294
Base class for the task's promise type.
Definition task.hpp:79
RAII class to destroy a coroutine at the end of a scope.
Definition task.hpp:53
Promise type for non-void task.
Definition task.hpp:192
A variant-like class for storing the result of non-void task.
Definition task.hpp:125
constexpr void set_value(T &&val) noexcept(std::is_nothrow_move_constructible_v< T >)
Store result of task.
Definition task.hpp:145
void set_exception(std::exception_ptr eptr) noexcept
Store exception into the result.
Definition task.hpp:156
constexpr T get_value() &&
Get previously stored result or exception.
Definition task.hpp:170
Awaitable used to co_await other tasks.
Definition task.hpp:244
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Deleter for std::unique_ptr that stores a coroutine handle.
Definition task.hpp:37