// // experimental/co_spawn.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2021-2022 Klemens D. Morgenstern // (klemens dot morgenstern at gmx dot net) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP #define BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include #include #include #include #include #include #include #include #include namespace boost { namespace asio { namespace experimental { namespace detail { template struct coro_spawn_op { coro c; void operator()(auto& self) { auto op = c.async_resume(deferred); std::move(op)((prepend)(std::move(self), 0)); } void operator()(auto& self, int, auto... res) { self.complete(std::move(res)...); } }; } // namespace detail /// Spawn a resumable coroutine. /** * This function spawns the coroutine for execution on its executor. It binds * the lifetime of the coroutine to the executor. * * @param c The coroutine * * @param token The completion token * * @returns Implementation defined */ template BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr, T)) co_spawn(coro c, CompletionToken&& token) { auto exec = c.get_executor(); return async_compose( detail::coro_spawn_op{std::move(c)}, token, exec); } /// Spawn a resumable coroutine. /** * This function spawns the coroutine for execution on its executor. It binds * the lifetime of the coroutine to the executor. * * @param c The coroutine * * @param token The completion token * * @returns Implementation defined */ template BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr, T)) co_spawn(coro c, CompletionToken&& token) { auto exec = c.get_executor(); return async_compose( detail::coro_spawn_op{std::move(c)}, token, exec); } /// Spawn a resumable coroutine. /** * This function spawns the coroutine for execution on its executor. It binds * the lifetime of the coroutine to the executor. * * @param c The coroutine * * @param token The completion token * * @returns Implementation defined */ template BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(T)) co_spawn(coro c, CompletionToken&& token) { auto exec = c.get_executor(); return async_compose( detail::coro_spawn_op{std::move(c)}, token, exec); } /// Spawn a resumable coroutine. /** * This function spawns the coroutine for execution on its executor. It binds * the lifetime of the coroutine to the executor. * * @param c The coroutine * * @param token The completion token * * @returns Implementation defined */ template BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr)) co_spawn(coro c, CompletionToken&& token) { auto exec = c.get_executor(); return async_compose( detail::coro_spawn_op{std::move(c)}, token, exec); } /// Spawn a resumable coroutine. /** * This function spawns the coroutine for execution on its executor. It binds * the lifetime of the coroutine to the executor. * * @param c The coroutine * * @param token The completion token * * @returns Implementation defined */ template BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( CompletionToken, void(std::exception_ptr)) co_spawn(coro c, CompletionToken&& token) { auto exec = c.get_executor(); return async_compose( detail::coro_spawn_op{std::move(c)}, token, exec); } /// Spawn a resumable coroutine. /** * This function spawns the coroutine for execution on its executor. It binds * the lifetime of the coroutine to the executor. * * @param c The coroutine * * @param token The completion token * * @returns Implementation defined */ template BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void()) co_spawn(coro c, CompletionToken&& token) { auto exec = c.get_executor(); return async_compose( detail::coro_spawn_op{std::move(c)}, token, exec); } } // namespace detail } // namespace asio } // namespace boost #include #endif //BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP