// // impl/compose.hpp // ~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2021 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 BOOST_ASIO_IMPL_COMPOSE_HPP #define BOOST_ASIO_IMPL_COMPOSE_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 #include #include #include #include #include namespace boost { namespace asio { namespace detail { template class composed_work_guard { public: typedef typename decay< typename prefer_result::type >::type executor_type; composed_work_guard(const Executor& ex) : executor_(boost::asio::prefer(ex, execution::outstanding_work.tracked)) { } void reset() { } executor_type get_executor() const BOOST_ASIO_NOEXCEPT { return executor_; } private: executor_type executor_; }; template <> struct composed_work_guard { public: typedef system_executor executor_type; composed_work_guard(const system_executor&) { } void reset() { } executor_type get_executor() const BOOST_ASIO_NOEXCEPT { return system_executor(); } }; #if !defined(BOOST_ASIO_NO_TS_EXECUTORS) template struct composed_work_guard::value >::type> : executor_work_guard { composed_work_guard(const Executor& ex) : executor_work_guard(ex) { } }; #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS) template struct composed_io_executors; template <> struct composed_io_executors { composed_io_executors() BOOST_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) BOOST_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(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) template struct composed_io_executors { explicit composed_io_executors(const Head& head, const Tail&... tail) BOOST_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(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) #define BOOST_ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF(n) \ template \ struct composed_io_executors \ { \ explicit composed_io_executors(const Head& head, \ BOOST_ASIO_VARIADIC_CONSTREF_PARAMS(n)) BOOST_ASIO_NOEXCEPT \ : head_(head), \ tail_(BOOST_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, \ BOOST_ASIO_VARIADIC_CONSTREF_PARAMS(n)) \ { \ return composed_io_executors< \ void(Head, BOOST_ASIO_VARIADIC_TARGS(n))>( \ head, BOOST_ASIO_VARIADIC_BYVAL_ARGS(n)); \ } \ /**/ BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF) #undef BOOST_ASIO_PRIVATE_COMPOSED_IO_EXECUTORS_DEF #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) template struct composed_work; template <> struct composed_work { typedef composed_io_executors executors_type; composed_work(const executors_type&) BOOST_ASIO_NOEXCEPT : head_(system_executor()) { } void reset() { head_.reset(); } typedef system_executor head_type; composed_work_guard head_; }; template struct composed_work { typedef composed_io_executors executors_type; explicit composed_work(const executors_type& ex) BOOST_ASIO_NOEXCEPT : head_(ex.head_) { } void reset() { head_.reset(); } typedef Head head_type; composed_work_guard head_; }; #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) template struct composed_work { typedef composed_io_executors executors_type; explicit composed_work(const executors_type& ex) BOOST_ASIO_NOEXCEPT : head_(ex.head_), tail_(ex.tail_) { } void reset() { head_.reset(); tail_.reset(); } typedef Head head_type; composed_work_guard head_; composed_work tail_; }; #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) #define BOOST_ASIO_PRIVATE_COMPOSED_WORK_DEF(n) \ template \ struct composed_work \ { \ typedef composed_io_executors executors_type; \ \ explicit composed_work(const executors_type& ex) BOOST_ASIO_NOEXCEPT \ : head_(ex.head_), \ tail_(ex.tail_) \ { \ } \ \ void reset() \ { \ head_.reset(); \ tail_.reset(); \ } \ \ typedef Head head_type; \ composed_work_guard head_; \ composed_work tail_; \ }; \ /**/ BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_COMPOSED_WORK_DEF) #undef BOOST_ASIO_PRIVATE_COMPOSED_WORK_DEF #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) template class composed_op; template class composed_op #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) template class composed_op #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) : public base_from_cancellation_state { public: template composed_op(BOOST_ASIO_MOVE_ARG(I) impl, BOOST_ASIO_MOVE_ARG(W) work, BOOST_ASIO_MOVE_ARG(H) handler) : base_from_cancellation_state( handler, enable_terminal_cancellation()), impl_(BOOST_ASIO_MOVE_CAST(I)(impl)), work_(BOOST_ASIO_MOVE_CAST(W)(work)), handler_(BOOST_ASIO_MOVE_CAST(H)(handler)), invocations_(0) { } #if defined(BOOST_ASIO_HAS_MOVE) composed_op(composed_op&& other) : base_from_cancellation_state( BOOST_ASIO_MOVE_CAST(base_from_cancellation_state< Handler>)(other)), impl_(BOOST_ASIO_MOVE_CAST(Impl)(other.impl_)), work_(BOOST_ASIO_MOVE_CAST(Work)(other.work_)), handler_(BOOST_ASIO_MOVE_CAST(Handler)(other.handler_)), invocations_(other.invocations_) { } #endif // defined(BOOST_ASIO_HAS_MOVE) typedef typename associated_executor::executor_type >::type executor_type; executor_type get_executor() const BOOST_ASIO_NOEXCEPT { return (get_associated_executor)(handler_, work_.head_.get_executor()); } typedef typename associated_allocator >::type allocator_type; allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT { return (get_associated_allocator)(handler_, std::allocator()); } #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) template void operator()(BOOST_ASIO_MOVE_ARG(T)... t) { if (invocations_ < ~0u) ++invocations_; this->get_cancellation_state().slot().clear(); impl_(*this, BOOST_ASIO_MOVE_CAST(T)(t)...); } void complete(Args... args) { this->work_.reset(); BOOST_ASIO_MOVE_OR_LVALUE(Handler)(this->handler_)( BOOST_ASIO_MOVE_CAST(Args)(args)...); } #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) void operator()() { if (invocations_ < ~0u) ++invocations_; this->get_cancellation_state().slot().clear(); impl_(*this); } void complete() { this->work_.reset(); BOOST_ASIO_MOVE_OR_LVALUE(Handler)(this->handler_)(); } #define BOOST_ASIO_PRIVATE_COMPOSED_OP_DEF(n) \ template \ void operator()(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ if (invocations_ < ~0u) \ ++invocations_; \ this->get_cancellation_state().slot().clear(); \ impl_(*this, BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ \ template \ void complete(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ this->work_.reset(); \ BOOST_ASIO_MOVE_OR_LVALUE(Handler)(this->handler_)( \ BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ /**/ BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_COMPOSED_OP_DEF) #undef BOOST_ASIO_PRIVATE_COMPOSED_OP_DEF #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES) void reset_cancellation_state() { base_from_cancellation_state::reset_cancellation_state(handler_); } template void reset_cancellation_state(BOOST_ASIO_MOVE_ARG(Filter) filter) { base_from_cancellation_state::reset_cancellation_state(handler_, BOOST_ASIO_MOVE_CAST(Filter)(filter)); } template void reset_cancellation_state(BOOST_ASIO_MOVE_ARG(InFilter) in_filter, BOOST_ASIO_MOVE_ARG(OutFilter) out_filter) { base_from_cancellation_state::reset_cancellation_state(handler_, BOOST_ASIO_MOVE_CAST(InFilter)(in_filter), BOOST_ASIO_MOVE_CAST(OutFilter)(out_filter)); } //private: Impl impl_; Work work_; Handler handler_; unsigned invocations_; }; template inline asio_handler_allocate_is_deprecated asio_handler_allocate(std::size_t size, composed_op* this_handler) { #if defined(BOOST_ASIO_NO_DEPRECATED) boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_); return asio_handler_allocate_is_no_longer_used(); #else // defined(BOOST_ASIO_NO_DEPRECATED) return boost_asio_handler_alloc_helpers::allocate( size, this_handler->handler_); #endif // defined(BOOST_ASIO_NO_DEPRECATED) } template inline asio_handler_deallocate_is_deprecated asio_handler_deallocate(void* pointer, std::size_t size, composed_op* this_handler) { boost_asio_handler_alloc_helpers::deallocate( pointer, size, this_handler->handler_); #if defined(BOOST_ASIO_NO_DEPRECATED) return asio_handler_deallocate_is_no_longer_used(); #endif // defined(BOOST_ASIO_NO_DEPRECATED) } template inline bool asio_handler_is_continuation( composed_op* this_handler) { return this_handler->invocations_ > 1 ? true : boost_asio_handler_cont_helpers::is_continuation( this_handler->handler_); } template inline asio_handler_invoke_is_deprecated asio_handler_invoke(Function& function, composed_op* this_handler) { boost_asio_handler_invoke_helpers::invoke( function, this_handler->handler_); #if defined(BOOST_ASIO_NO_DEPRECATED) return asio_handler_invoke_is_no_longer_used(); #endif // defined(BOOST_ASIO_NO_DEPRECATED) } template inline asio_handler_invoke_is_deprecated asio_handler_invoke(const Function& function, composed_op* this_handler) { boost_asio_handler_invoke_helpers::invoke( function, this_handler->handler_); #if defined(BOOST_ASIO_NO_DEPRECATED) return asio_handler_invoke_is_no_longer_used(); #endif // defined(BOOST_ASIO_NO_DEPRECATED) } template class initiate_composed_op { public: typedef typename composed_io_executors::head_type executor_type; template explicit initiate_composed_op(int, BOOST_ASIO_MOVE_ARG(T) executors) : executors_(BOOST_ASIO_MOVE_CAST(T)(executors)) { } executor_type get_executor() const BOOST_ASIO_NOEXCEPT { return executors_.head_; } template void operator()(BOOST_ASIO_MOVE_ARG(Handler) handler, BOOST_ASIO_MOVE_ARG(Impl) impl) const { composed_op::type, composed_work, typename decay::type, Signature>( BOOST_ASIO_MOVE_CAST(Impl)(impl), composed_work(executors_), BOOST_ASIO_MOVE_CAST(Handler)(handler))(); } private: composed_io_executors executors_; }; template inline initiate_composed_op make_initiate_composed_op( BOOST_ASIO_MOVE_ARG(composed_io_executors) executors) { return initiate_composed_op(0, BOOST_ASIO_MOVE_CAST(composed_io_executors)(executors)); } template inline typename IoObject::executor_type get_composed_io_executor(IoObject& io_object, typename enable_if< !is_executor::value >::type* = 0, typename enable_if< !execution::is_executor::value >::type* = 0) { return io_object.get_executor(); } template inline const Executor& get_composed_io_executor(const Executor& ex, typename enable_if< is_executor::value || execution::is_executor::value >::type* = 0) { return ex; } } // namespace detail #if !defined(GENERATING_DOCUMENTATION) template