mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
runners.hpp
1#ifndef MRS_LIB_CORO_RUNNERS_HPP_
2#define MRS_LIB_CORO_RUNNERS_HPP_
3
4#include <concepts>
5#include <coroutine>
6
7#include <mrs_lib/coro/internal/thread_local_continuation_scheduler.hpp>
8#include <mrs_lib/coro/task.hpp>
9
10
11namespace mrs_lib
12{
13
14 namespace internal
15 {
16
25 {
26 public:
28 {
29 AsyncRun get_return_object()
30 {
31 return AsyncRun();
32 }
34 {
35 return {};
36 }
37 std::suspend_never final_suspend() noexcept
38 {
39 return {};
40 }
41 void return_void()
42 {
43 }
44 void unhandled_exception()
45 {
46 throw;
47 }
48 };
49
50 private:
51 AsyncRun() = default;
52 };
53
54 template <class T>
55 requires std::convertible_to<T, std::decay_t<T>>
56 constexpr std::decay_t<T> decay_copy(T&& value) noexcept(std::is_nothrow_convertible_v<T, std::decay_t<T>>)
57 {
58 return std::forward<T>(value);
59 }
60
61 template <typename F, typename... Args>
62 requires std::invocable<std::decay_t<F>, std::decay_t<Args>...> && std::same_as<Task<void>, std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
63 internal::AsyncRun start_task_impl(F&& task, Args&&... args)
64 {
65 // The `decay-copy` of the arguments ensures that they are copied into
66 // the coroutine frame, preventing dangling references like `std::thread`.
67 co_await std::invoke(decay_copy(std::forward<F>(task)), decay_copy(std::forward<Args>(args))...);
68 }
69
81 template <typename F, typename... Args>
82 requires std::invocable<std::decay_t<F>, std::decay_t<Args>...> && std::same_as<Task<void>, std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>
83 void start_task(F&& task, Args&&... args)
84 {
85 internal::start_task_impl(std::forward<F>(task), std::forward<Args>(args)...);
86 }
87
88 } // namespace internal
89
90} // namespace mrs_lib
91
92#endif // MRS_LIB_CORO_RUNNERS_HPP_
Coroutine type used to start asynchronous computation.
Definition runners.hpp:25
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Definition thread_local_continuation_scheduler.hpp:16