// // experimental/detail/coro_completion_handler.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2021-2023 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 ASIO_EXPERIMENTAL_DETAIL_CORO_COMPLETION_HANDLER_HPP #define ASIO_EXPERIMENTAL_DETAIL_CORO_COMPLETION_HANDLER_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/deferred.hpp" #include "asio/experimental/coro.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace experimental { namespace detail { template struct coro_completion_handler { coro_completion_handler(coroutine_handle h, std::optional>& result) : self(h), result(result) { } coro_completion_handler(coro_completion_handler&&) = default; coroutine_handle self; std::optional>& result; using promise_type = Promise; void operator()(Args... args) { result.emplace(std::move(args)...); self.resume(); } using allocator_type = typename promise_type::allocator_type; allocator_type get_allocator() const noexcept { return self.promise().get_allocator(); } using executor_type = typename promise_type::executor_type; executor_type get_executor() const noexcept { return self.promise().get_executor(); } using cancellation_slot_type = typename promise_type::cancellation_slot_type; cancellation_slot_type get_cancellation_slot() const noexcept { return self.promise().get_cancellation_slot(); } }; template struct coro_completion_handler_type; template struct coro_completion_handler_type { using type = std::tuple; template using completion_handler = coro_completion_handler; }; template using coro_completion_handler_type_t = typename coro_completion_handler_type::type; inline void coro_interpret_result(std::tuple<>&&) { } template inline auto coro_interpret_result(std::tuple&& args) { return std::move(args); } template auto coro_interpret_result(std::tuple&& args) { if (std::get<0>(args)) std::rethrow_exception(std::get<0>(args)); return std::apply( [](auto, auto&&... rest) { return std::make_tuple(std::move(rest)...); }, std::move(args)); } template auto coro_interpret_result( std::tuple&& args) { if (std::get<0>(args)) asio::detail::throw_exception( asio::system_error(std::get<0>(args))); return std::apply( [](auto, auto&&... rest) { return std::make_tuple(std::move(rest)...); }, std::move(args)); } template inline auto coro_interpret_result(std::tuple&& args) { return std::get<0>(std::move(args)); } template auto coro_interpret_result(std::tuple&& args) { if (std::get<0>(args)) std::rethrow_exception(std::get<0>(args)); return std::get<1>(std::move(args)); } inline auto coro_interpret_result( std::tuple&& args) { if (std::get<0>(args)) asio::detail::throw_exception( asio::system_error(std::get<0>(args))); } inline auto coro_interpret_result(std::tuple&& args) { if (std::get<0>(args)) std::rethrow_exception(std::get<0>(args)); } template auto coro_interpret_result(std::tuple&& args) { if (std::get<0>(args)) asio::detail::throw_exception( asio::system_error(std::get<0>(args))); return std::get<1>(std::move(args)); } } // namespace detail } // namespace experimental } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_EXPERIMENTAL_DETAIL_CORO_COMPLETION_HANDLER_HPP