1#ifndef MRS_LIB_CORO_TASK_HPP_
2#define MRS_LIB_CORO_TASK_HPP_
12#include <mrs_lib/coro/internal/attributes.hpp>
24 template <
typename T =
void>
25 requires(std::same_as<T, std::remove_cvref_t<T>>)
37 void operator()(std::coroutine_handle<T> handle)
41 using pointer = std::coroutine_handle<T>;
44 template <
typename T =
void>
67 std::coroutine_handle<T> handle_;
76 template <
typename Derived>
87 FinalAwaitable() =
default;
88 ~FinalAwaitable() =
default;
89 FinalAwaitable(
const FinalAwaitable&) =
delete;
90 FinalAwaitable& operator=(
const FinalAwaitable&) =
delete;
91 FinalAwaitable(FinalAwaitable&&) =
delete;
92 FinalAwaitable& operator=(FinalAwaitable&&) =
delete;
95 bool await_ready()
noexcept;
101 void await_suspend(std::coroutine_handle<Derived> task_handle)
noexcept;
104 void await_resume()
noexcept;
109 std::suspend_always initial_suspend();
111 FinalAwaitable final_suspend()
noexcept;
113 void set_continuation(OwningCoroutineHandle<> continuation);
116 OwningCoroutineHandle<> continuation_{std::noop_coroutine()};
122 template <
typename T>
143 constexpr void set_value(T&& val)
noexcept(std::is_nothrow_move_constructible_v<T>)
145 assert(state_ == State::empty);
146 std::construct_at(&value_, std::move(val));
147 state_ = State::value;
157 assert(state_ == State::empty);
158 std::construct_at(&exception_, std::move(eptr));
159 state_ = State::exception;
172 if (state_ == State::exception)
174 std::rethrow_exception(exception_);
176 assert(state_ == State::value);
177 return std::move(value_);
187 std::destroy_at(&value_);
189 case State::exception:
190 std::destroy_at(&exception_);
199 std::exception_ptr exception_;
209 template <
typename T>
215 void return_value(T&& ret_val);
217 void unhandled_exception();
221 return std::move(result_).get_value();
241 void unhandled_exception();
247 std::rethrow_exception(exception_);
252 std::exception_ptr exception_;
261 template <
typename T>
272 std::coroutine_handle<> await_suspend(std::coroutine_handle<> continuation)
274 task_handle_.promise().set_continuation(OwningCoroutineHandle<>(continuation));
281 return this->task_handle_.promise().get_value();
291 TaskAwaitable(std::coroutine_handle<Promise> task_handle) : task_handle_(task_handle)
295 std::coroutine_handle<Promise> task_handle_;
297 friend class Task<T>;
310 template <
typename T>
311 requires(std::same_as<T, std::remove_cvref_t<T>>)
312 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
319 Task& operator=(
const Task&) =
delete;
329 Task(internal::OwningCoroutineHandle<promise_type> coroutine) : coroutine_(std::move(coroutine))
333 internal::OwningCoroutineHandle<promise_type> coroutine_;
340#ifndef MRS_LIB_CORO_TASK_IMPL_HPP_
341#include <mrs_lib/coro/task.impl.hpp>
Task type for creating coroutines.
Definition task.hpp:313
Base class for the task's promise type.
Definition task.hpp:78
RAII class to destroy a coroutine at the end of a scope.
Definition task.hpp:52
Promise type for non-void task.
Definition task.hpp:211
A variant-like class for storing the result of non-void task.
Definition task.hpp:124
constexpr void set_value(T &&val) noexcept(std::is_nothrow_move_constructible_v< T >)
Store result of task.
Definition task.hpp:143
void set_exception(std::exception_ptr eptr) noexcept
Store exception into the result.
Definition task.hpp:155
constexpr T get_value() &&
Get previously stored result or exception.
Definition task.hpp:170
Awaitable used to co_await other tasks.
Definition task.hpp:263
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:36