associated_executor.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // associated_executor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef ASIO_ASSOCIATED_EXECUTOR_HPP
  11. #define ASIO_ASSOCIATED_EXECUTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/associator.hpp"
  17. #include "asio/detail/functional.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/execution/executor.hpp"
  20. #include "asio/is_executor.hpp"
  21. #include "asio/system_executor.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. template <typename T, typename Executor>
  25. struct associated_executor;
  26. namespace detail {
  27. template <typename T, typename = void>
  28. struct has_executor_type : false_type
  29. {
  30. };
  31. template <typename T>
  32. struct has_executor_type<T,
  33. typename void_type<typename T::executor_type>::type>
  34. : true_type
  35. {
  36. };
  37. template <typename T, typename E, typename = void, typename = void>
  38. struct associated_executor_impl
  39. {
  40. typedef void asio_associated_executor_is_unspecialised;
  41. typedef E type;
  42. static type get(const T&, const E& e = E()) ASIO_NOEXCEPT
  43. {
  44. return e;
  45. }
  46. };
  47. template <typename T, typename E>
  48. struct associated_executor_impl<T, E,
  49. typename void_type<typename T::executor_type>::type>
  50. {
  51. typedef typename T::executor_type type;
  52. static type get(const T& t, const E& = E()) ASIO_NOEXCEPT
  53. {
  54. return t.get_executor();
  55. }
  56. };
  57. template <typename T, typename E>
  58. struct associated_executor_impl<T, E,
  59. typename enable_if<
  60. !has_executor_type<T>::value
  61. >::type,
  62. typename void_type<
  63. typename associator<associated_executor, T, E>::type
  64. >::type> : associator<associated_executor, T, E>
  65. {
  66. };
  67. } // namespace detail
  68. /// Traits type used to obtain the executor associated with an object.
  69. /**
  70. * A program may specialise this traits type if the @c T template parameter in
  71. * the specialisation is a user-defined type. The template parameter @c
  72. * Executor shall be a type meeting the Executor requirements.
  73. *
  74. * Specialisations shall meet the following requirements, where @c t is a const
  75. * reference to an object of type @c T, and @c e is an object of type @c
  76. * Executor.
  77. *
  78. * @li Provide a nested typedef @c type that identifies a type meeting the
  79. * Executor requirements.
  80. *
  81. * @li Provide a noexcept static member function named @c get, callable as @c
  82. * get(t) and with return type @c type.
  83. *
  84. * @li Provide a noexcept static member function named @c get, callable as @c
  85. * get(t,e) and with return type @c type.
  86. */
  87. template <typename T, typename Executor = system_executor>
  88. struct associated_executor
  89. #if !defined(GENERATING_DOCUMENTATION)
  90. : detail::associated_executor_impl<T, Executor>
  91. #endif // !defined(GENERATING_DOCUMENTATION)
  92. {
  93. #if defined(GENERATING_DOCUMENTATION)
  94. /// If @c T has a nested type @c executor_type, <tt>T::executor_type</tt>.
  95. /// Otherwise @c Executor.
  96. typedef see_below type;
  97. /// If @c T has a nested type @c executor_type, returns
  98. /// <tt>t.get_executor()</tt>. Otherwise returns @c ex.
  99. static type get(const T& t,
  100. const Executor& ex = Executor()) ASIO_NOEXCEPT;
  101. #endif // defined(GENERATING_DOCUMENTATION)
  102. };
  103. /// Helper function to obtain an object's associated executor.
  104. /**
  105. * @returns <tt>associated_executor<T>::get(t)</tt>
  106. */
  107. template <typename T>
  108. ASIO_NODISCARD inline typename associated_executor<T>::type
  109. get_associated_executor(const T& t) ASIO_NOEXCEPT
  110. {
  111. return associated_executor<T>::get(t);
  112. }
  113. /// Helper function to obtain an object's associated executor.
  114. /**
  115. * @returns <tt>associated_executor<T, Executor>::get(t, ex)</tt>
  116. */
  117. template <typename T, typename Executor>
  118. ASIO_NODISCARD inline typename associated_executor<T, Executor>::type
  119. get_associated_executor(const T& t, const Executor& ex,
  120. typename constraint<
  121. is_executor<Executor>::value || execution::is_executor<Executor>::value
  122. >::type = 0) ASIO_NOEXCEPT
  123. {
  124. return associated_executor<T, Executor>::get(t, ex);
  125. }
  126. /// Helper function to obtain an object's associated executor.
  127. /**
  128. * @returns <tt>associated_executor<T, typename
  129. * ExecutionContext::executor_type>::get(t, ctx.get_executor())</tt>
  130. */
  131. template <typename T, typename ExecutionContext>
  132. ASIO_NODISCARD inline typename associated_executor<T,
  133. typename ExecutionContext::executor_type>::type
  134. get_associated_executor(const T& t, ExecutionContext& ctx,
  135. typename constraint<is_convertible<ExecutionContext&,
  136. execution_context&>::value>::type = 0) ASIO_NOEXCEPT
  137. {
  138. return associated_executor<T,
  139. typename ExecutionContext::executor_type>::get(t, ctx.get_executor());
  140. }
  141. #if defined(ASIO_HAS_ALIAS_TEMPLATES)
  142. template <typename T, typename Executor = system_executor>
  143. using associated_executor_t = typename associated_executor<T, Executor>::type;
  144. #endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
  145. namespace detail {
  146. template <typename T, typename E, typename = void>
  147. struct associated_executor_forwarding_base
  148. {
  149. };
  150. template <typename T, typename E>
  151. struct associated_executor_forwarding_base<T, E,
  152. typename enable_if<
  153. is_same<
  154. typename associated_executor<T,
  155. E>::asio_associated_executor_is_unspecialised,
  156. void
  157. >::value
  158. >::type>
  159. {
  160. typedef void asio_associated_executor_is_unspecialised;
  161. };
  162. } // namespace detail
  163. #if defined(ASIO_HAS_STD_REFERENCE_WRAPPER) \
  164. || defined(GENERATING_DOCUMENTATION)
  165. /// Specialisation of associated_executor for @c std::reference_wrapper.
  166. template <typename T, typename Executor>
  167. struct associated_executor<reference_wrapper<T>, Executor>
  168. #if !defined(GENERATING_DOCUMENTATION)
  169. : detail::associated_executor_forwarding_base<T, Executor>
  170. #endif // !defined(GENERATING_DOCUMENTATION)
  171. {
  172. /// Forwards @c type to the associator specialisation for the unwrapped type
  173. /// @c T.
  174. typedef typename associated_executor<T, Executor>::type type;
  175. /// Forwards the request to get the executor to the associator specialisation
  176. /// for the unwrapped type @c T.
  177. static type get(reference_wrapper<T> t,
  178. const Executor& ex = Executor()) ASIO_NOEXCEPT
  179. {
  180. return associated_executor<T, Executor>::get(t.get(), ex);
  181. }
  182. };
  183. #endif // defined(ASIO_HAS_STD_REFERENCE_WRAPPER)
  184. // || defined(GENERATING_DOCUMENTATION)
  185. } // namespace asio
  186. #include "asio/detail/pop_options.hpp"
  187. #endif // ASIO_ASSOCIATED_EXECUTOR_HPP