associated_executor.hpp 6.7 KB

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