// // impl/compose.hpp // ~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // 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_IMPL_COMPOSE_HPP #define ASIO_IMPL_COMPOSE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_cont_helpers.hpp" #include "asio/detail/handler_invoke_helpers.hpp" #include "asio/detail/type_traits.hpp" #include "asio/detail/variadic_templates.hpp" #include "asio/executor_work_guard.hpp" #include "asio/is_executor.hpp" #include "asio/system_executor.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template struct composed_io_executors; template <> struct composed_io_executors { composed_io_executors() ASIO_NOEXCEPT : head_(system_executor()) { } typedef system_executor head_type; system_executor head_; }; inline composed_io_executors make_composed_io_executors() { return composed_io_executors(); } template struct composed_io_executors { explicit composed_io_executors(const Head& ex) ASIO_NOEXCEPT : head_(ex) { } typedef Head head_type; Head head_; }; template inline composed_io_executors make_composed_io_executors(const Head& head) { return composed_io_executors(head); } #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template struct composed_io_executors { explicit composed_io_executors(const Head& head, const Tail&... tail) ASIO_NOEXCEPT : head_(head), tail_(tail...) { } void reset() { head_.reset(); tail_.reset(); } typedef Head head_type; Head head_; composed_io_executors tail_; }; template inline composed_io_executors make_composed_io_executors(const Head& head, const Tail&... tail) { return composed_io_executors(head, tail...); } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) #define ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF(n) \ template \ struct composed_io_executors \ { \ explicit composed_io_executors(const Head& head, \ ASIO_VARIADIC_CONSTREF_PARAMS(n)) ASIO_NOEXCEPT \ : head_(head), \ tail_(ASIO_VARIADIC_BYVAL_ARGS(n)) \ { \ } \ \ void reset() \ { \ head_.reset(); \ tail_.reset(); \ } \ \ typedef Head head_type; \ Head head_; \ composed_io_executors tail_; \ }; \ \ template \ inline composed_io_executors \ make_composed_io_executors(const Head& head, \ ASIO_VARIADIC_CONSTREF_PARAMS(n)) \ { \ return composed_io_executors< \ void(Head, ASIO_VARIADIC_TARGS(n))>( \ head, ASIO_VARIADIC_BYVAL_ARGS(n)); \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF) #undef ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) template struct composed_work; template <> struct composed_work { typedef composed_io_executors executors_type; composed_work(const executors_type&) ASIO_NOEXCEPT : head_(system_executor()) { } void reset() { head_.reset(); } typedef system_executor head_type; executor_work_guard head_; }; template struct composed_work { typedef composed_io_executors executors_type; explicit composed_work(const executors_type& ex) ASIO_NOEXCEPT : head_(ex.head_) { } void reset() { head_.reset(); } typedef Head head_type; executor_work_guard head_; }; #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template struct composed_work { typedef composed_io_executors executors_type; explicit composed_work(const executors_type& ex) ASIO_NOEXCEPT : head_(ex.head_), tail_(ex.tail_) { } void reset() { head_.reset(); tail_.reset(); } typedef Head head_type; executor_work_guard head_; composed_work tail_; }; #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) #define ASIO_PRIVATE_COMPOSED_WORK_DEF(n) \ template \ struct composed_work \ { \ typedef composed_io_executors executors_type; \ \ explicit composed_work(const executors_type& ex) ASIO_NOEXCEPT \ : head_(ex.head_), \ tail_(ex.tail_) \ { \ } \ \ void reset() \ { \ head_.reset(); \ tail_.reset(); \ } \ \ typedef Head head_type; \ executor_work_guard head_; \ composed_work tail_; \ }; \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_WORK_DEF) #undef ASIO_PRIVATE_COMPOSED_WORK_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template class composed_op; template class composed_op #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) template class composed_op #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) { public: composed_op(ASIO_MOVE_ARG(Impl) impl, ASIO_MOVE_ARG(Work) work, ASIO_MOVE_ARG(Handler) handler) : impl_(ASIO_MOVE_CAST(Impl)(impl)), work_(ASIO_MOVE_CAST(Work)(work)), handler_(ASIO_MOVE_CAST(Handler)(handler)), invocations_(0) { } #if defined(ASIO_HAS_MOVE) composed_op(composed_op&& other) : impl_(ASIO_MOVE_CAST(Impl)(other.impl_)), work_(ASIO_MOVE_CAST(Work)(other.work_)), handler_(ASIO_MOVE_CAST(Handler)(other.handler_)), invocations_(other.invocations_) { } #endif // defined(ASIO_HAS_MOVE) typedef typename associated_executor::type executor_type; executor_type get_executor() const ASIO_NOEXCEPT { return (get_associated_executor)(handler_, work_.head_.get_executor()); } typedef typename associated_allocator >::type allocator_type; allocator_type get_allocator() const ASIO_NOEXCEPT { return (get_associated_allocator)(handler_, std::allocator()); } #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template void operator()(ASIO_MOVE_ARG(T)... t) { if (invocations_ < ~unsigned(0)) ++invocations_; impl_(*this, ASIO_MOVE_CAST(T)(t)...); } void complete(Args... args) { this->work_.reset(); this->handler_(ASIO_MOVE_CAST(Args)(args)...); } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) void operator()() { if (invocations_ < ~unsigned(0)) ++invocations_; impl_(*this); } void complete() { this->work_.reset(); this->handler_(); } #define ASIO_PRIVATE_COMPOSED_OP_DEF(n) \ template \ void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ if (invocations_ < ~unsigned(0)) \ ++invocations_; \ impl_(*this, ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ \ template \ void complete(ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ this->work_.reset(); \ this->handler_(ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_OP_DEF) #undef ASIO_PRIVATE_COMPOSED_OP_DEF #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) //private: Impl impl_; Work work_; Handler handler_; unsigned invocations_; }; template inline void* asio_handler_allocate(std::size_t size, composed_op* this_handler) { return asio_handler_alloc_helpers::allocate( size, this_handler->handler_); } template inline void asio_handler_deallocate(void* pointer, std::size_t size, composed_op* this_handler) { asio_handler_alloc_helpers::deallocate( pointer, size, this_handler->handler_); } template inline bool asio_handler_is_continuation( composed_op* this_handler) { return this_handler->invocations_ > 1 ? true : asio_handler_cont_helpers::is_continuation( this_handler->handler_); } template inline void asio_handler_invoke(Function& function, composed_op* this_handler) { asio_handler_invoke_helpers::invoke( function, this_handler->handler_); } template inline void asio_handler_invoke(const Function& function, composed_op* this_handler) { asio_handler_invoke_helpers::invoke( function, this_handler->handler_); } template class initiate_composed_op { public: typedef typename composed_io_executors::head_type executor_type; template explicit initiate_composed_op(ASIO_MOVE_ARG(T) executors) : executors_(ASIO_MOVE_CAST(T)(executors)) { } executor_type get_executor() const ASIO_NOEXCEPT { return executors_.head_; } template void operator()(ASIO_MOVE_ARG(Handler) handler, ASIO_MOVE_ARG(Impl) impl) const { composed_op::type, composed_work, typename decay::type, Signature>( ASIO_MOVE_CAST(Impl)(impl), composed_work(executors_), ASIO_MOVE_CAST(Handler)(handler))(); } private: composed_io_executors executors_; }; template inline initiate_composed_op make_initiate_composed_op( ASIO_MOVE_ARG(composed_io_executors) executors) { return initiate_composed_op( ASIO_MOVE_CAST(composed_io_executors)(executors)); } template inline typename IoObject::executor_type get_composed_io_executor(IoObject& io_object, typename enable_if::value>::type* = 0) { return io_object.get_executor(); } template inline const Executor& get_composed_io_executor(const Executor& ex, typename enable_if::value>::type* = 0) { return ex; } } // namespace detail #if !defined(GENERATING_DOCUMENTATION) #if defined(ASIO_HAS_VARIADIC_TEMPLATES) template ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature) async_compose(ASIO_MOVE_ARG(Implementation) implementation, ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors) { return async_initiate( detail::make_initiate_composed_op( detail::make_composed_io_executors( detail::get_composed_io_executor( ASIO_MOVE_CAST(IoObjectsOrExecutors)( io_objects_or_executors))...)), token, ASIO_MOVE_CAST(Implementation)(implementation)); } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) template ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature) async_compose(ASIO_MOVE_ARG(Implementation) implementation, ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token) { return async_initiate( detail::make_initiate_composed_op( detail::make_composed_io_executors()), token, ASIO_MOVE_CAST(Implementation)(implementation)); } # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n) \ ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_##n # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1 \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)) # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2 \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)) # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3 \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)) # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4 \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4)) # define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5 \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4)), \ detail::get_composed_io_executor(ASIO_MOVE_CAST(T5)(x5)) #define ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \ template \ ASIO_INITFN_AUTO_RESULT_TYPE(CompletionToken, Signature) \ async_compose(ASIO_MOVE_ARG(Implementation) implementation, \ ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \ ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ return async_initiate( \ detail::make_initiate_composed_op( \ detail::make_composed_io_executors( \ ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n))), \ token, ASIO_MOVE_CAST(Implementation)(implementation)); \ } \ /**/ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_COMPOSE_DEF) #undef ASIO_PRIVATE_ASYNC_COMPOSE_DEF #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4 #undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5 #endif // defined(ASIO_HAS_VARIADIC_TEMPLATES) #endif // !defined(GENERATING_DOCUMENTATION) } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_IMPL_COMPOSE_HPP