2 // associated_executor.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP
12 #define BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/type_traits.hpp>
20 #include <boost/asio/is_executor.hpp>
21 #include <boost/asio/system_executor.hpp>
23 #include <boost/asio/detail/push_options.hpp>
30 struct associated_executor_check
35 template <typename T, typename E, typename = void>
36 struct associated_executor_impl
40 static type get(const T&, const E& e) BOOST_ASIO_NOEXCEPT
46 template <typename T, typename E>
47 struct associated_executor_impl<T, E,
48 typename associated_executor_check<typename T::executor_type>::type>
50 typedef typename T::executor_type type;
52 static type get(const T& t, const E&) BOOST_ASIO_NOEXCEPT
54 return t.get_executor();
60 /// Traits type used to obtain the executor associated with an object.
62 * A program may specialise this traits type if the @c T template parameter in
63 * the specialisation is a user-defined type. The template parameter @c
64 * Executor shall be a type meeting the Executor requirements.
66 * Specialisations shall meet the following requirements, where @c t is a const
67 * reference to an object of type @c T, and @c e is an object of type @c
70 * @li Provide a nested typedef @c type that identifies a type meeting the
71 * Executor requirements.
73 * @li Provide a noexcept static member function named @c get, callable as @c
74 * get(t) and with return type @c type.
76 * @li Provide a noexcept static member function named @c get, callable as @c
77 * get(t,e) and with return type @c type.
79 template <typename T, typename Executor = system_executor>
80 struct associated_executor
82 /// If @c T has a nested type @c executor_type, <tt>T::executor_type</tt>.
83 /// Otherwise @c Executor.
84 #if defined(GENERATING_DOCUMENTATION)
85 typedef see_below type;
86 #else // defined(GENERATING_DOCUMENTATION)
87 typedef typename detail::associated_executor_impl<T, Executor>::type type;
88 #endif // defined(GENERATING_DOCUMENTATION)
90 /// If @c T has a nested type @c executor_type, returns
91 /// <tt>t.get_executor()</tt>. Otherwise returns @c ex.
92 static type get(const T& t,
93 const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
95 return detail::associated_executor_impl<T, Executor>::get(t, ex);
99 /// Helper function to obtain an object's associated executor.
101 * @returns <tt>associated_executor<T>::get(t)</tt>
103 template <typename T>
104 inline typename associated_executor<T>::type
105 get_associated_executor(const T& t) BOOST_ASIO_NOEXCEPT
107 return associated_executor<T>::get(t);
110 /// Helper function to obtain an object's associated executor.
112 * @returns <tt>associated_executor<T, Executor>::get(t, ex)</tt>
114 template <typename T, typename Executor>
115 inline typename associated_executor<T, Executor>::type
116 get_associated_executor(const T& t, const Executor& ex,
117 typename enable_if<is_executor<
118 Executor>::value>::type* = 0) BOOST_ASIO_NOEXCEPT
120 return associated_executor<T, Executor>::get(t, ex);
123 /// Helper function to obtain an object's associated executor.
125 * @returns <tt>associated_executor<T, typename
126 * ExecutionContext::executor_type>::get(t, ctx.get_executor())</tt>
128 template <typename T, typename ExecutionContext>
129 inline typename associated_executor<T,
130 typename ExecutionContext::executor_type>::type
131 get_associated_executor(const T& t, ExecutionContext& ctx,
132 typename enable_if<is_convertible<ExecutionContext&,
133 execution_context&>::value>::type* = 0) BOOST_ASIO_NOEXCEPT
135 return associated_executor<T,
136 typename ExecutionContext::executor_type>::get(t, ctx.get_executor());
139 #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
141 template <typename T, typename Executor = system_executor>
142 using associated_executor_t = typename associated_executor<T, Executor>::type;
144 #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
149 #include <boost/asio/detail/pop_options.hpp>
151 #endif // BOOST_ASIO_ASSOCIATED_EXECUTOR_HPP