// // associated_executor.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 BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP #define BOOST_ASIO_ASSOCIATED_EXECUTOR_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 namespace boost { namespace asio { template struct associated_executor; namespace detail { template struct has_executor_type : false_type { }; template struct has_executor_type::type> : true_type { }; template struct associated_executor_impl { typedef void asio_associated_executor_is_unspecialised; typedef E type; static type get(const T&, const E& e = E()) BOOST_ASIO_NOEXCEPT { return e; } }; template struct associated_executor_impl::type> { typedef typename T::executor_type type; static type get(const T& t, const E& = E()) BOOST_ASIO_NOEXCEPT { return t.get_executor(); } }; template struct associated_executor_impl::value >::type, typename void_type< typename associator::type >::type> : associator { }; } // namespace detail /// Traits type used to obtain the executor associated with an object. /** * A program may specialise this traits type if the @c T template parameter in * the specialisation is a user-defined type. The template parameter @c * Executor shall be a type meeting the Executor requirements. * * Specialisations shall meet the following requirements, where @c t is a const * reference to an object of type @c T, and @c e is an object of type @c * Executor. * * @li Provide a nested typedef @c type that identifies a type meeting the * Executor requirements. * * @li Provide a noexcept static member function named @c get, callable as @c * get(t) and with return type @c type. * * @li Provide a noexcept static member function named @c get, callable as @c * get(t,e) and with return type @c type. */ template struct associated_executor #if !defined(GENERATING_DOCUMENTATION) : detail::associated_executor_impl #endif // !defined(GENERATING_DOCUMENTATION) { #if defined(GENERATING_DOCUMENTATION) /// If @c T has a nested type @c executor_type, T::executor_type. /// Otherwise @c Executor. typedef see_below type; /// If @c T has a nested type @c executor_type, returns /// t.get_executor(). Otherwise returns @c ex. static type get(const T& t, const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT; #endif // defined(GENERATING_DOCUMENTATION) }; /// Helper function to obtain an object's associated executor. /** * @returns associated_executor::get(t) */ template BOOST_ASIO_NODISCARD inline typename associated_executor::type get_associated_executor(const T& t) BOOST_ASIO_NOEXCEPT { return associated_executor::get(t); } /// Helper function to obtain an object's associated executor. /** * @returns associated_executor::get(t, ex) */ template BOOST_ASIO_NODISCARD inline typename associated_executor::type get_associated_executor(const T& t, const Executor& ex, typename constraint< is_executor::value || execution::is_executor::value >::type = 0) BOOST_ASIO_NOEXCEPT { return associated_executor::get(t, ex); } /// Helper function to obtain an object's associated executor. /** * @returns associated_executor::get(t, ctx.get_executor()) */ template BOOST_ASIO_NODISCARD inline typename associated_executor::type get_associated_executor(const T& t, ExecutionContext& ctx, typename constraint::value>::type = 0) BOOST_ASIO_NOEXCEPT { return associated_executor::get(t, ctx.get_executor()); } #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) template using associated_executor_t = typename associated_executor::type; #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES) namespace detail { template struct associated_executor_forwarding_base { }; template struct associated_executor_forwarding_base::asio_associated_executor_is_unspecialised, void >::value >::type> { typedef void asio_associated_executor_is_unspecialised; }; } // namespace detail #if defined(BOOST_ASIO_HAS_STD_REFERENCE_WRAPPER) \ || defined(GENERATING_DOCUMENTATION) /// Specialisation of associated_executor for @c std::reference_wrapper. template struct associated_executor, Executor> #if !defined(GENERATING_DOCUMENTATION) : detail::associated_executor_forwarding_base #endif // !defined(GENERATING_DOCUMENTATION) { /// Forwards @c type to the associator specialisation for the unwrapped type /// @c T. typedef typename associated_executor::type type; /// Forwards the request to get the executor to the associator specialisation /// for the unwrapped type @c T. static type get(reference_wrapper t, const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT { return associated_executor::get(t.get(), ex); } }; #endif // defined(BOOST_ASIO_HAS_STD_REFERENCE_WRAPPER) // || defined(GENERATING_DOCUMENTATION) } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP