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"
16#include "mrs_lib/utility/scope_cleanup.hpp"
25namespace mrs_lib::coro
28 template <
typename T =
void>
29 requires(std::same_as<T, std::remove_cvref_t<T>>)
41 template <
typename Derived>
52 FinalAwaitable() =
default;
53 ~FinalAwaitable() =
default;
54 FinalAwaitable(
const FinalAwaitable&) =
delete;
55 FinalAwaitable& operator=(
const FinalAwaitable&) =
delete;
56 FinalAwaitable(FinalAwaitable&&) =
delete;
57 FinalAwaitable& operator=(FinalAwaitable&&) =
delete;
60 bool await_ready()
noexcept;
67 void await_suspend(std::coroutine_handle<Derived> task_handle)
noexcept;
70 void await_resume()
noexcept;
75 std::suspend_always initial_suspend();
77 FinalAwaitable final_suspend()
noexcept;
83 return std::exchange(continuation_, {});
86 [[nodiscard]] std::stop_token get_token()
const
100 template <
typename T>
106 void return_value(T&& ret_val);
108 void unhandled_exception();
112 return std::move(result_).get_value();
132 void unhandled_exception();
138 std::rethrow_exception(exception_);
143 std::exception_ptr exception_;
146 template <
typename T>
152 return promise.release_continuation();
155 static std::stop_token get_token(std::coroutine_handle<
PromiseType<T>> handle)
167 template <
typename T>
170 using Promise = Task<T>::promise_type;
183 template <
typename CallerPromise>
184 void await_suspend(std::coroutine_handle<CallerPromise> continuation)
187 schedule_coroutine_continuation(task_handle_);
192 ScopeCleanup destroyer_cleanup([
this] { this->task_handle_.destroy(); });
193 return this->task_handle_.promise().get_value();
196 ~TaskAwaitable() =
default;
197 TaskAwaitable(
const TaskAwaitable&) =
delete;
198 TaskAwaitable& operator=(
const TaskAwaitable&) =
delete;
199 TaskAwaitable(TaskAwaitable&&) =
delete;
200 TaskAwaitable& operator=(TaskAwaitable&&) =
delete;
205 void operator co_await()
const =
delete;
208 TaskAwaitable(std::coroutine_handle<Promise> task_handle) : task_handle_(task_handle)
212 std::coroutine_handle<Promise> task_handle_;
214 friend class Task<T>;
227 template <
typename T>
228 requires(std::same_as<T, std::remove_cvref_t<T>>)
229 class [[nodiscard(
"Task is lazy and does not run until `co_await`ed.")]] Task
238 coroutine_.destroy();
242 Task(
const Task&) =
delete;
243 Task& operator=(
const Task&) =
delete;
244 Task(Task&&) =
delete;
245 Task& operator=(Task&&) =
delete;
253 explicit Task(std::coroutine_handle<promise_type> coroutine) : coroutine_(std::move(coroutine))
257 std::coroutine_handle<promise_type> coroutine_;
272#ifndef MRS_LIB_CORO_TASK_IMPL_HPP_
273#include "mrs_lib/coro/task.impl.hpp"
Utility class for running cleanup at the end of a scope.
Definition scope_cleanup.hpp:17
Task type for creating coroutines.
Definition task.hpp:230
Base class for the task's promise type.
Definition task.hpp:43
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
Promise type for non-void task.
Definition task.hpp:102
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:169
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