associated_immediate_executor.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // associated_immediate_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_IMMEDIATE_EXECUTOR_HPP
  11. #define ASIO_ASSOCIATED_IMMEDIATE_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/blocking.hpp"
  20. #include "asio/execution/executor.hpp"
  21. #include "asio/execution_context.hpp"
  22. #include "asio/is_executor.hpp"
  23. #include "asio/require.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. template <typename T, typename Executor>
  27. struct associated_immediate_executor;
  28. namespace detail {
  29. template <typename T, typename = void>
  30. struct has_immediate_executor_type : false_type
  31. {
  32. };
  33. template <typename T>
  34. struct has_immediate_executor_type<T,
  35. void_t<typename T::immediate_executor_type>>
  36. : true_type
  37. {
  38. };
  39. template <typename E, typename = void, typename = void>
  40. struct default_immediate_executor
  41. {
  42. typedef require_result_t<E, execution::blocking_t::never_t> type;
  43. static type get(const E& e) noexcept
  44. {
  45. return asio::require(e, execution::blocking.never);
  46. }
  47. };
  48. template <typename E>
  49. struct default_immediate_executor<E,
  50. enable_if_t<
  51. !execution::is_executor<E>::value
  52. >,
  53. enable_if_t<
  54. is_executor<E>::value
  55. >>
  56. {
  57. class type : public E
  58. {
  59. public:
  60. template <typename Executor1>
  61. explicit type(const Executor1& e,
  62. constraint_t<
  63. conditional_t<
  64. !is_same<Executor1, type>::value,
  65. is_convertible<Executor1, E>,
  66. false_type
  67. >::value
  68. > = 0) noexcept
  69. : E(e)
  70. {
  71. }
  72. type(const type& other) noexcept
  73. : E(static_cast<const E&>(other))
  74. {
  75. }
  76. type(type&& other) noexcept
  77. : E(static_cast<E&&>(other))
  78. {
  79. }
  80. template <typename Function, typename Allocator>
  81. void dispatch(Function&& f, const Allocator& a) const
  82. {
  83. this->post(static_cast<Function&&>(f), a);
  84. }
  85. friend bool operator==(const type& a, const type& b) noexcept
  86. {
  87. return static_cast<const E&>(a) == static_cast<const E&>(b);
  88. }
  89. friend bool operator!=(const type& a, const type& b) noexcept
  90. {
  91. return static_cast<const E&>(a) != static_cast<const E&>(b);
  92. }
  93. };
  94. static type get(const E& e) noexcept
  95. {
  96. return type(e);
  97. }
  98. };
  99. template <typename T, typename E, typename = void, typename = void>
  100. struct associated_immediate_executor_impl
  101. {
  102. typedef void asio_associated_immediate_executor_is_unspecialised;
  103. typedef typename default_immediate_executor<E>::type type;
  104. static auto get(const T&, const E& e) noexcept
  105. -> decltype(default_immediate_executor<E>::get(e))
  106. {
  107. return default_immediate_executor<E>::get(e);
  108. }
  109. };
  110. template <typename T, typename E>
  111. struct associated_immediate_executor_impl<T, E,
  112. void_t<typename T::immediate_executor_type>>
  113. {
  114. typedef typename T::immediate_executor_type type;
  115. static auto get(const T& t, const E&) noexcept
  116. -> decltype(t.get_immediate_executor())
  117. {
  118. return t.get_immediate_executor();
  119. }
  120. };
  121. template <typename T, typename E>
  122. struct associated_immediate_executor_impl<T, E,
  123. enable_if_t<
  124. !has_immediate_executor_type<T>::value
  125. >,
  126. void_t<
  127. typename associator<associated_immediate_executor, T, E>::type
  128. >> : associator<associated_immediate_executor, T, E>
  129. {
  130. };
  131. } // namespace detail
  132. /// Traits type used to obtain the immediate executor associated with an object.
  133. /**
  134. * A program may specialise this traits type if the @c T template parameter in
  135. * the specialisation is a user-defined type. The template parameter @c
  136. * Executor shall be a type meeting the Executor requirements.
  137. *
  138. * Specialisations shall meet the following requirements, where @c t is a const
  139. * reference to an object of type @c T, and @c e is an object of type @c
  140. * Executor.
  141. *
  142. * @li Provide a nested typedef @c type that identifies a type meeting the
  143. * Executor requirements.
  144. *
  145. * @li Provide a noexcept static member function named @c get, callable as @c
  146. * get(t) and with return type @c type or a (possibly const) reference to @c
  147. * type.
  148. *
  149. * @li Provide a noexcept static member function named @c get, callable as @c
  150. * get(t,e) and with return type @c type or a (possibly const) reference to @c
  151. * type.
  152. */
  153. template <typename T, typename Executor>
  154. struct associated_immediate_executor
  155. #if !defined(GENERATING_DOCUMENTATION)
  156. : detail::associated_immediate_executor_impl<T, Executor>
  157. #endif // !defined(GENERATING_DOCUMENTATION)
  158. {
  159. #if defined(GENERATING_DOCUMENTATION)
  160. /// If @c T has a nested type @c immediate_executor_type,
  161. // <tt>T::immediate_executor_type</tt>. Otherwise @c Executor.
  162. typedef see_below type;
  163. /// If @c T has a nested type @c immediate_executor_type, returns
  164. /// <tt>t.get_immediate_executor()</tt>. Otherwise returns
  165. /// <tt>asio::require(ex, asio::execution::blocking.never)</tt>.
  166. static decltype(auto) get(const T& t, const Executor& ex) noexcept;
  167. #endif // defined(GENERATING_DOCUMENTATION)
  168. };
  169. /// Helper function to obtain an object's associated executor.
  170. /**
  171. * @returns <tt>associated_immediate_executor<T, Executor>::get(t, ex)</tt>
  172. */
  173. template <typename T, typename Executor>
  174. ASIO_NODISCARD inline auto get_associated_immediate_executor(
  175. const T& t, const Executor& ex,
  176. constraint_t<
  177. is_executor<Executor>::value || execution::is_executor<Executor>::value
  178. > = 0) noexcept
  179. -> decltype(associated_immediate_executor<T, Executor>::get(t, ex))
  180. {
  181. return associated_immediate_executor<T, Executor>::get(t, ex);
  182. }
  183. /// Helper function to obtain an object's associated executor.
  184. /**
  185. * @returns <tt>associated_immediate_executor<T, typename
  186. * ExecutionContext::executor_type>::get(t, ctx.get_executor())</tt>
  187. */
  188. template <typename T, typename ExecutionContext>
  189. ASIO_NODISCARD inline typename associated_immediate_executor<T,
  190. typename ExecutionContext::executor_type>::type
  191. get_associated_immediate_executor(const T& t, ExecutionContext& ctx,
  192. constraint_t<
  193. is_convertible<ExecutionContext&, execution_context&>::value
  194. > = 0) noexcept
  195. {
  196. return associated_immediate_executor<T,
  197. typename ExecutionContext::executor_type>::get(t, ctx.get_executor());
  198. }
  199. template <typename T, typename Executor>
  200. using associated_immediate_executor_t =
  201. typename associated_immediate_executor<T, Executor>::type;
  202. namespace detail {
  203. template <typename T, typename E, typename = void>
  204. struct associated_immediate_executor_forwarding_base
  205. {
  206. };
  207. template <typename T, typename E>
  208. struct associated_immediate_executor_forwarding_base<T, E,
  209. enable_if_t<
  210. is_same<
  211. typename associated_immediate_executor<T,
  212. E>::asio_associated_immediate_executor_is_unspecialised,
  213. void
  214. >::value
  215. >>
  216. {
  217. typedef void asio_associated_immediate_executor_is_unspecialised;
  218. };
  219. } // namespace detail
  220. /// Specialisation of associated_immediate_executor for
  221. /// @c std::reference_wrapper.
  222. template <typename T, typename Executor>
  223. struct associated_immediate_executor<reference_wrapper<T>, Executor>
  224. #if !defined(GENERATING_DOCUMENTATION)
  225. : detail::associated_immediate_executor_forwarding_base<T, Executor>
  226. #endif // !defined(GENERATING_DOCUMENTATION)
  227. {
  228. /// Forwards @c type to the associator specialisation for the unwrapped type
  229. /// @c T.
  230. typedef typename associated_immediate_executor<T, Executor>::type type;
  231. /// Forwards the request to get the executor to the associator specialisation
  232. /// for the unwrapped type @c T.
  233. static auto get(reference_wrapper<T> t, const Executor& ex) noexcept
  234. -> decltype(associated_immediate_executor<T, Executor>::get(t.get(), ex))
  235. {
  236. return associated_immediate_executor<T, Executor>::get(t.get(), ex);
  237. }
  238. };
  239. } // namespace asio
  240. #include "asio/detail/pop_options.hpp"
  241. #endif // ASIO_ASSOCIATED_IMMEDIATE_EXECUTOR_HPP