Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:43:20

0001 //
0002 // experimental/co_spawn.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2021-2023 Klemens D. Morgenstern
0006 //                         (klemens dot morgenstern at gmx dot net)
0007 //
0008 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 #ifndef BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP
0012 #define BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <utility>
0020 #include <boost/asio/compose.hpp>
0021 #include <boost/asio/deferred.hpp>
0022 #include <boost/asio/detail/type_traits.hpp>
0023 #include <boost/asio/experimental/coro.hpp>
0024 #include <boost/asio/prepend.hpp>
0025 #include <boost/asio/redirect_error.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace experimental {
0032 namespace detail {
0033 
0034 template <typename T, typename U, typename Executor>
0035 struct coro_spawn_op
0036 {
0037   coro<T, U, Executor> c;
0038 
0039   void operator()(auto& self)
0040   {
0041     auto op = c.async_resume(deferred);
0042     std::move(op)((prepend)(std::move(self), 0));
0043   }
0044 
0045   void operator()(auto& self, int, auto... res)
0046   {
0047     self.complete(std::move(res)...);
0048   }
0049 };
0050 
0051 } // namespace detail
0052 
0053 /// Spawn a resumable coroutine.
0054 /**
0055  * This function spawns the coroutine for execution on its executor. It binds
0056  * the lifetime of the coroutine to the executor.
0057  *
0058  * @param c The coroutine
0059  *
0060  * @param token The completion token
0061  *
0062  * @returns Implementation defined
0063  */
0064 template <typename T, typename Executor, typename CompletionToken>
0065 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
0066     CompletionToken, void(std::exception_ptr, T))
0067 co_spawn(coro<void, T, Executor> c, CompletionToken&& token)
0068 {
0069   auto exec = c.get_executor();
0070   return async_compose<CompletionToken, void(std::exception_ptr, T)>(
0071       detail::coro_spawn_op<void, T, Executor>{std::move(c)},
0072       token, exec);
0073 }
0074 
0075 /// Spawn a resumable coroutine.
0076 /**
0077  * This function spawns the coroutine for execution on its executor. It binds
0078  * the lifetime of the coroutine to the executor.
0079  *
0080  * @param c The coroutine
0081  *
0082  * @param token The completion token
0083  *
0084  * @returns Implementation defined
0085  */
0086 template <typename T, typename Executor, typename CompletionToken>
0087 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
0088     CompletionToken, void(std::exception_ptr, T))
0089 co_spawn(coro<void(), T, Executor> c, CompletionToken&& token)
0090 {
0091   auto exec = c.get_executor();
0092   return async_compose<CompletionToken, void(std::exception_ptr, T)>(
0093       detail::coro_spawn_op<void(), T, Executor>{std::move(c)},
0094       token, exec);
0095 }
0096 
0097 /// Spawn a resumable coroutine.
0098 /**
0099  * This function spawns the coroutine for execution on its executor. It binds
0100  * the lifetime of the coroutine to the executor.
0101  *
0102  * @param c The coroutine
0103  *
0104  * @param token The completion token
0105  *
0106  * @returns Implementation defined
0107  */
0108 template <typename T, typename Executor, typename CompletionToken>
0109 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void(T))
0110 co_spawn(coro<void() noexcept, T, Executor> c, CompletionToken&& token)
0111 {
0112   auto exec = c.get_executor();
0113   return async_compose<CompletionToken, void(T)>(
0114       detail::coro_spawn_op<void() noexcept, T, Executor>{std::move(c)},
0115       token, exec);
0116 }
0117 
0118 /// Spawn a resumable coroutine.
0119 /**
0120  * This function spawns the coroutine for execution on its executor. It binds
0121  * the lifetime of the coroutine to the executor.
0122  *
0123  * @param c The coroutine
0124  *
0125  * @param token The completion token
0126  *
0127  * @returns Implementation defined
0128  */
0129 template <typename Executor, typename CompletionToken>
0130 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
0131     CompletionToken, void(std::exception_ptr))
0132 co_spawn(coro<void, void, Executor> c, CompletionToken&& token)
0133 {
0134   auto exec = c.get_executor();
0135   return async_compose<CompletionToken, void(std::exception_ptr)>(
0136       detail::coro_spawn_op<void, void, Executor>{std::move(c)},
0137       token, exec);
0138 }
0139 
0140 /// Spawn a resumable coroutine.
0141 /**
0142  * This function spawns the coroutine for execution on its executor. It binds
0143  * the lifetime of the coroutine to the executor.
0144  *
0145  * @param c The coroutine
0146  *
0147  * @param token The completion token
0148  *
0149  * @returns Implementation defined
0150  */
0151 template <typename Executor, typename CompletionToken>
0152 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
0153     CompletionToken, void(std::exception_ptr))
0154 co_spawn(coro<void(), void, Executor> c, CompletionToken&& token)
0155 {
0156   auto exec = c.get_executor();
0157   return async_compose<CompletionToken, void(std::exception_ptr)>(
0158       detail::coro_spawn_op<void(), void, Executor>{std::move(c)},
0159       token, exec);
0160 }
0161 
0162 /// Spawn a resumable coroutine.
0163 /**
0164  * This function spawns the coroutine for execution on its executor. It binds
0165  * the lifetime of the coroutine to the executor.
0166  *
0167  * @param c The coroutine
0168  *
0169  * @param token The completion token
0170  *
0171  * @returns Implementation defined
0172  */
0173 template <typename Executor, typename CompletionToken>
0174 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, void())
0175 co_spawn(coro<void() noexcept, void, Executor> c, CompletionToken&& token)
0176 {
0177   auto exec = c.get_executor();
0178   return async_compose<CompletionToken, void()>(
0179       detail::coro_spawn_op<void() noexcept, void, Executor>{std::move(c)},
0180       token, exec);
0181 }
0182 
0183 } // namespace detail
0184 } // namespace asio
0185 } // namespace boost
0186 
0187 #include <boost/asio/detail/pop_options.hpp>
0188 
0189 #endif //BOOST_ASIO_EXPERIMENTAL_CO_SPAWN_HPP