associated_immediate_executor.hpp 8.2 KB

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