// // compose.hpp // ~~~~~~~~~~~ // // Copyright (c) 2003-2022 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_COMPOSE_HPP #define ASIO_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/associated_executor.hpp" #include "asio/async_result.hpp" #include "asio/detail/base_from_cancellation_state.hpp" #include "asio/detail/composed_work.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/detail/push_options.hpp" namespace asio { namespace detail { #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 base_from_cancellation_state { public: template composed_op(ASIO_MOVE_ARG(I) impl, ASIO_MOVE_ARG(W) work, ASIO_MOVE_ARG(H) handler) : base_from_cancellation_state( handler, enable_terminal_cancellation()), impl_(ASIO_MOVE_CAST(I)(impl)), work_(ASIO_MOVE_CAST(W)(work)), handler_(ASIO_MOVE_CAST(H)(handler)), invocations_(0) { } #if defined(ASIO_HAS_MOVE) composed_op(composed_op&& other) : base_from_cancellation_state( ASIO_MOVE_CAST(base_from_cancellation_state< Handler>)(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 composed_work_guard< typename Work::head_type>::executor_type io_executor_type; io_executor_type get_io_executor() const ASIO_NOEXCEPT { return work_.head_.get_executor(); } 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_ < ~0u) ++invocations_; this->get_cancellation_state().slot().clear(); impl_(*this, ASIO_MOVE_CAST(T)(t)...); } void complete(Args... args) { this->work_.reset(); ASIO_MOVE_OR_LVALUE(Handler)(this->handler_)( ASIO_MOVE_CAST(Args)(args)...); } #else // defined(ASIO_HAS_VARIADIC_TEMPLATES) void operator()() { if (invocations_ < ~0u) ++invocations_; this->get_cancellation_state().slot().clear(); impl_(*this); } void complete() { this->work_.reset(); ASIO_MOVE_OR_LVALUE(Handler)(this->handler_)(); } #define ASIO_PRIVATE_COMPOSED_OP_DEF(n) \ template \ void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ if (invocations_ < ~0u) \ ++invocations_; \ this->get_cancellation_state().slot().clear(); \ impl_(*this, ASIO_VARIADIC_MOVE_ARGS(n)); \ } \ \ template \ void complete(ASIO_VARIADIC_MOVE_PARAMS(n)) \ { \ this->work_.reset(); \ ASIO_MOVE_OR_LVALUE(Handler)(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) void reset_cancellation_state() { base_from_cancellation_state::reset_cancellation_state(handler_); } template void reset_cancellation_state(ASIO_MOVE_ARG(Filter) filter) { base_from_cancellation_state::reset_cancellation_state(handler_, ASIO_MOVE_CAST(Filter)(filter)); } template void reset_cancellation_state(ASIO_MOVE_ARG(InFilter) in_filter, ASIO_MOVE_ARG(OutFilter) out_filter) { base_from_cancellation_state::reset_cancellation_state(handler_, ASIO_MOVE_CAST(InFilter)(in_filter), ASIO_MOVE_CAST(OutFilter)(out_filter)); } cancellation_type_t cancelled() const ASIO_NOEXCEPT { return base_from_cancellation_state::cancelled(); } //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(ASIO_NO_DEPRECATED) asio_handler_alloc_helpers::allocate(size, this_handler->handler_); return asio_handler_allocate_is_no_longer_used(); #else // defined(ASIO_NO_DEPRECATED) return asio_handler_alloc_helpers::allocate( size, this_handler->handler_); #endif // defined(ASIO_NO_DEPRECATED) } template inline asio_handler_deallocate_is_deprecated asio_handler_deallocate(void* pointer, std::size_t size, composed_op* this_handler) { asio_handler_alloc_helpers::deallocate( pointer, size, this_handler->handler_); #if defined(ASIO_NO_DEPRECATED) return asio_handler_deallocate_is_no_longer_used(); #endif // defined(ASIO_NO_DEPRECATED) } 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 asio_handler_invoke_is_deprecated asio_handler_invoke(Function& function, composed_op* this_handler) { asio_handler_invoke_helpers::invoke( function, this_handler->handler_); #if defined(ASIO_NO_DEPRECATED) return asio_handler_invoke_is_no_longer_used(); #endif // defined(ASIO_NO_DEPRECATED) } template inline asio_handler_invoke_is_deprecated asio_handler_invoke(const Function& function, composed_op* this_handler) { asio_handler_invoke_helpers::invoke( function, this_handler->handler_); #if defined(ASIO_NO_DEPRECATED) return asio_handler_invoke_is_no_longer_used(); #endif // defined(ASIO_NO_DEPRECATED) } template class initiate_composed_op { public: typedef typename composed_io_executors::head_type executor_type; template explicit initiate_composed_op(int, 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(0, ASIO_MOVE_CAST(composed_io_executors)(executors)); } } // namespace detail #if !defined(GENERATING_DOCUMENTATION) template