// // associated_immediate_executor.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_IMMEDIATE_EXECUTOR_HPP #define BOOST_ASIO_ASSOCIATED_IMMEDIATE_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 #include #include namespace boost { namespace asio { template struct associated_immediate_executor; namespace detail { template struct has_immediate_executor_type : false_type { }; template struct has_immediate_executor_type> : true_type { }; template struct default_immediate_executor { typedef require_result_t type; static type get(const E& e) noexcept { return boost::asio::require(e, execution::blocking.never); } }; template struct default_immediate_executor::value >, enable_if_t< is_executor::value >> { class type : public E { public: template explicit type(const Executor1& e, constraint_t< conditional_t< !is_same::value, is_convertible, false_type >::value > = 0) noexcept : E(e) { } type(const type& other) noexcept : E(static_cast(other)) { } type(type&& other) noexcept : E(static_cast(other)) { } template void dispatch(Function&& f, const Allocator& a) const { this->post(static_cast(f), a); } friend bool operator==(const type& a, const type& b) noexcept { return static_cast(a) == static_cast(b); } friend bool operator!=(const type& a, const type& b) noexcept { return static_cast(a) != static_cast(b); } }; static type get(const E& e) noexcept { return type(e); } }; template struct associated_immediate_executor_impl { typedef void asio_associated_immediate_executor_is_unspecialised; typedef typename default_immediate_executor::type type; static auto get(const T&, const E& e) noexcept -> decltype(default_immediate_executor::get(e)) { return default_immediate_executor::get(e); } }; template struct associated_immediate_executor_impl> { typedef typename T::immediate_executor_type type; static auto get(const T& t, const E&) noexcept -> decltype(t.get_immediate_executor()) { return t.get_immediate_executor(); } }; template struct associated_immediate_executor_impl::value >, void_t< typename associator::type >> : associator { }; } // namespace detail /// Traits type used to obtain the immediate 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 or a (possibly const) reference to @c * type. * * @li Provide a noexcept static member function named @c get, callable as @c * get(t,e) and with return type @c type or a (possibly const) reference to @c * type. */ template struct associated_immediate_executor #if !defined(GENERATING_DOCUMENTATION) : detail::associated_immediate_executor_impl #endif // !defined(GENERATING_DOCUMENTATION) { #if defined(GENERATING_DOCUMENTATION) /// If @c T has a nested type @c immediate_executor_type, // T::immediate_executor_type. Otherwise @c Executor. typedef see_below type; /// If @c T has a nested type @c immediate_executor_type, returns /// t.get_immediate_executor(). Otherwise returns /// boost::asio::require(ex, boost::asio::execution::blocking.never). static decltype(auto) get(const T& t, const Executor& ex) noexcept; #endif // defined(GENERATING_DOCUMENTATION) }; /// Helper function to obtain an object's associated executor. /** * @returns associated_immediate_executor::get(t, ex) */ template BOOST_ASIO_NODISCARD inline auto get_associated_immediate_executor( const T& t, const Executor& ex, constraint_t< is_executor::value || execution::is_executor::value > = 0) noexcept -> decltype(associated_immediate_executor::get(t, ex)) { return associated_immediate_executor::get(t, ex); } /// Helper function to obtain an object's associated executor. /** * @returns associated_immediate_executor::get(t, ctx.get_executor()) */ template BOOST_ASIO_NODISCARD inline typename associated_immediate_executor::type get_associated_immediate_executor(const T& t, ExecutionContext& ctx, constraint_t< is_convertible::value > = 0) noexcept { return associated_immediate_executor::get(t, ctx.get_executor()); } template using associated_immediate_executor_t = typename associated_immediate_executor::type; namespace detail { template struct associated_immediate_executor_forwarding_base { }; template struct associated_immediate_executor_forwarding_base::asio_associated_immediate_executor_is_unspecialised, void >::value >> { typedef void asio_associated_immediate_executor_is_unspecialised; }; } // namespace detail /// Specialisation of associated_immediate_executor for /// @c std::reference_wrapper. template struct associated_immediate_executor, Executor> #if !defined(GENERATING_DOCUMENTATION) : detail::associated_immediate_executor_forwarding_base #endif // !defined(GENERATING_DOCUMENTATION) { /// Forwards @c type to the associator specialisation for the unwrapped type /// @c T. typedef typename associated_immediate_executor::type type; /// Forwards the request to get the executor to the associator specialisation /// for the unwrapped type @c T. static auto get(reference_wrapper t, const Executor& ex) noexcept -> decltype(associated_immediate_executor::get(t.get(), ex)) { return associated_immediate_executor::get(t.get(), ex); } }; } // namespace asio } // namespace boost #include #endif // BOOST_ASIO_ASSOCIATED_IMMEDIATE_EXECUTOR_HPP