associated_immediate_executor.hpp 8.2 KB

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