1#ifndef MRS_LIB_CORO_TASK_HPP_
2#define MRS_LIB_CORO_TASK_HPP_
12#include "mrs_lib/coro/internal/attributes.hpp"
13#include "mrs_lib/coro/internal/continuation.hpp"
14#include "mrs_lib/coro/internal/result_storage.hpp"
15#include "mrs_lib/coro/internal/thread_local_continuation_scheduler.hpp"
24namespace mrs_lib::coro
27 template <
typename T =
void>
28 requires(std::same_as<T, std::remove_cvref_t<T>>)
40 void operator()(std::coroutine_handle<T> handle)
44 using pointer = std::coroutine_handle<T>;
47 template <
typename T =
void>
70 std::coroutine_handle<T> handle_;
79 template <
typename Derived>
90 FinalAwaitable() =
default;
91 ~FinalAwaitable() =
default;
92 FinalAwaitable(
const FinalAwaitable&) =
delete;
93 FinalAwaitable& operator=(
const FinalAwaitable&) =
delete;
94 FinalAwaitable(FinalAwaitable&&) =
delete;
95 FinalAwaitable& operator=(FinalAwaitable&&) =
delete;
98 bool await_ready()
noexcept;
105 void await_suspend(std::coroutine_handle<Derived> task_handle)
noexcept;
108 void await_resume()
noexcept;
113 std::suspend_always initial_suspend();
115 FinalAwaitable final_suspend()
noexcept;
121 return std::exchange(continuation_, {});
124 std::stop_token get_token()
const
138 template <
typename T>
144 void return_value(T&& ret_val);
146 void unhandled_exception();
150 return std::move(result_).get_value();
170 void unhandled_exception();
176 std::rethrow_exception(exception_);
181 std::exception_ptr exception_;
184 template <
typename T>
190 return promise.release_continuation();
193 static std::stop_token get_token(std::coroutine_handle<
PromiseType<T>> handle)
205 template <
typename T>
221 template <
typename CallerPromise>
222 void await_suspend(std::coroutine_handle<CallerPromise> continuation)
225 schedule_coroutine_continuation(task_handle_);
231 return this->task_handle_.promise().get_value();
243 void operator co_await()
const =
delete;
246 TaskAwaitable(std::coroutine_handle<Promise> task_handle) : task_handle_(task_handle)
250 std::coroutine_handle<Promise> task_handle_;
252 friend class Task<T>;
265 template <
typename T>
266 requires(std::same_as<T, std::remove_cvref_t<T>>)
267 class [[nodiscard(
"Task is lazy and does not run until `co_await`ed.")]]
Task
274 Task& operator=(
const Task&) =
delete;
284 explicit Task(internal::OwningCoroutineHandle<promise_type> coroutine) : coroutine_(std::move(coroutine))
288 internal::OwningCoroutineHandle<promise_type> coroutine_;
303#ifndef MRS_LIB_CORO_TASK_IMPL_HPP_
304#include "mrs_lib/coro/task.impl.hpp"
Task type for creating coroutines.
Definition task.hpp:268
Base class for the task's promise type.
Definition task.hpp:81
Owning coroutine handle supporting cancellation.
Definition continuation.hpp:54
std::stop_token get_token() const
Get stop token associated with the continuation.
Definition continuation.hpp:142
RAII class to destroy a coroutine at the end of a scope.
Definition task.hpp:55
Promise type for non-void task.
Definition task.hpp:140
A variant-like class for storing the result of non-void task.
Definition result_storage.hpp:18
Awaitable used to co_await other tasks.
Definition task.hpp:207
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Type trait to obtain data necessary for creating CancellableContinuation.
Definition continuation.hpp:37
Deleter for std::unique_ptr that stores a coroutine handle.
Definition task.hpp:39