// // detail/composed_work.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_DETAIL_COMPOSED_WORK_HPP #define ASIO_DETAIL_COMPOSED_WORK_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/type_traits.hpp" #include "asio/detail/variadic_templates.hpp" #include "asio/execution/executor.hpp" #include "asio/execution/outstanding_work.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 class composed_work_guard { public: typedef typename decay< typename prefer_result::type >::type executor_type; composed_work_guard(const Executor& ex) : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) { } void reset() { } executor_type get_executor() const 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 ASIO_NOEXCEPT { return system_executor(); } }; #if !defined(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(ASIO_NO_TS_EXECUTORS) 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; composed_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; composed_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; composed_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; \ composed_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) 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 } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_COMPOSED_WORK_HPP