2 // experimental/co_spawn.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2021-2022 Klemens D. Morgenstern
6 // (klemens dot morgenstern at gmx dot net)
8 // Distributed under the Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP
12 #define BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/asio/compose.hpp>
21 #include <boost/asio/detail/type_traits.hpp>
22 #include <boost/asio/experimental/coro.hpp>
23 #include <boost/asio/experimental/deferred.hpp>
24 #include <boost/asio/experimental/prepend.hpp>
25 #include <boost/asio/redirect_error.hpp>
27 #include <boost/asio/detail/push_options.hpp>
31 namespace experimental {
34 template <typename T, typename U, typename Executor>
37 coro<T, U, Executor> c;
39 void operator()(auto& self)
41 auto op = c.async_resume(deferred);
42 std::move(op)((prepend)(std::move(self), 0));
45 void operator()(auto& self, int, auto... res)
47 self.complete(std::move(res)...);
53 /// Spawn a resumable coroutine.
55 * This function spawns the coroutine for execution on its executor. It binds
56 * the lifetime of the coroutine to the executor.
58 * @param c The coroutine
60 * @param token The completion token
62 * @returns Implementation defined
64 template <typename T, typename Executor, typename CompletionToken>
65 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
66 CompletionToken, void(std::exception_ptr, T))
67 co_spawn(coro<void, T, Executor> c, CompletionToken&& token)
69 auto exec = c.get_executor();
70 return async_compose<CompletionToken, void(std::exception_ptr, T)>(
71 detail::coro_spawn_op<void, T, Executor>{std::move(c)},
75 /// Spawn a resumable coroutine.
77 * This function spawns the coroutine for execution on its executor. It binds
78 * the lifetime of the coroutine to the executor.
80 * @param c The coroutine
82 * @param token The completion token
84 * @returns Implementation defined
86 template <typename T, typename Executor, typename CompletionToken>
87 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
88 CompletionToken, void(std::exception_ptr, T))
89 co_spawn(coro<void(), T, Executor> c, CompletionToken&& token)
91 auto exec = c.get_executor();
92 return async_compose<CompletionToken, void(std::exception_ptr, T)>(
93 detail::coro_spawn_op<void(), T, Executor>{std::move(c)},
97 /// Spawn a resumable coroutine.
99 * This function spawns the coroutine for execution on its executor. It binds
100 * the lifetime of the coroutine to the executor.
102 * @param c The coroutine
104 * @param token The completion token
106 * @returns Implementation defined
108 template <typename T, typename Executor, typename CompletionToken>
109 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(T))
110 co_spawn(coro<void() noexcept, T, Executor> c, CompletionToken&& token)
112 auto exec = c.get_executor();
113 return async_compose<CompletionToken, void(T)>(
114 detail::coro_spawn_op<void() noexcept, T, Executor>{std::move(c)},
118 /// Spawn a resumable coroutine.
120 * This function spawns the coroutine for execution on its executor. It binds
121 * the lifetime of the coroutine to the executor.
123 * @param c The coroutine
125 * @param token The completion token
127 * @returns Implementation defined
129 template <typename Executor, typename CompletionToken>
130 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
131 CompletionToken, void(std::exception_ptr))
132 co_spawn(coro<void, void, Executor> c, CompletionToken&& token)
134 auto exec = c.get_executor();
135 return async_compose<CompletionToken, void(std::exception_ptr)>(
136 detail::coro_spawn_op<void, void, Executor>{std::move(c)},
140 /// Spawn a resumable coroutine.
142 * This function spawns the coroutine for execution on its executor. It binds
143 * the lifetime of the coroutine to the executor.
145 * @param c The coroutine
147 * @param token The completion token
149 * @returns Implementation defined
151 template <typename Executor, typename CompletionToken>
152 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
153 CompletionToken, void(std::exception_ptr))
154 co_spawn(coro<void(), void, Executor> c, CompletionToken&& token)
156 auto exec = c.get_executor();
157 return async_compose<CompletionToken, void(std::exception_ptr)>(
158 detail::coro_spawn_op<void(), void, Executor>{std::move(c)},
162 /// Spawn a resumable coroutine.
164 * This function spawns the coroutine for execution on its executor. It binds
165 * the lifetime of the coroutine to the executor.
167 * @param c The coroutine
169 * @param token The completion token
171 * @returns Implementation defined
173 template <typename Executor, typename CompletionToken>
174 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void())
175 co_spawn(coro<void() noexcept, void, Executor> c, CompletionToken&& token)
177 auto exec = c.get_executor();
178 return async_compose<CompletionToken, void()>(
179 detail::coro_spawn_op<void() noexcept, void, Executor>{std::move(c)},
183 } // namespace detail
187 #include <boost/asio/detail/pop_options.hpp>
189 #endif //BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP