execute.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // execution/execute.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 BOOST_ASIO_EXECUTION_EXECUTE_HPP
  11. #define BOOST_ASIO_EXECUTION_EXECUTE_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. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/execution/detail/as_invocable.hpp>
  19. #include <boost/asio/execution/detail/as_receiver.hpp>
  20. #include <boost/asio/traits/execute_member.hpp>
  21. #include <boost/asio/traits/execute_free.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. #if defined(GENERATING_DOCUMENTATION)
  24. namespace boost {
  25. namespace asio {
  26. namespace execution {
  27. /// (Deprecated: Use @c execute member function.) A customisation point that
  28. /// executes a function on an executor.
  29. /**
  30. * The name <tt>execution::execute</tt> denotes a customisation point object.
  31. *
  32. * For some subexpressions <tt>e</tt> and <tt>f</tt>, let <tt>E</tt> be a type
  33. * such that <tt>decltype((e))</tt> is <tt>E</tt> and let <tt>F</tt> be a type
  34. * such that <tt>decltype((f))</tt> is <tt>F</tt>. The expression
  35. * <tt>execution::execute(e, f)</tt> is ill-formed if <tt>F</tt> does not model
  36. * <tt>invocable</tt>, or if <tt>E</tt> does not model either <tt>executor</tt>
  37. * or <tt>sender</tt>. Otherwise, it is expression-equivalent to:
  38. *
  39. * @li <tt>e.execute(f)</tt>, if that expression is valid. If the function
  40. * selected does not execute the function object <tt>f</tt> on the executor
  41. * <tt>e</tt>, the program is ill-formed with no diagnostic required.
  42. *
  43. * @li Otherwise, <tt>execute(e, f)</tt>, if that expression is valid, with
  44. * overload resolution performed in a context that includes the declaration
  45. * <tt>void execute();</tt> and that does not include a declaration of
  46. * <tt>execution::execute</tt>. If the function selected by overload
  47. * resolution does not execute the function object <tt>f</tt> on the executor
  48. * <tt>e</tt>, the program is ill-formed with no diagnostic required.
  49. */
  50. inline constexpr unspecified execute = unspecified;
  51. /// (Deprecated.) A type trait that determines whether an @c execute expression
  52. /// is well-formed.
  53. /**
  54. * Class template @c can_execute is a trait that is derived from
  55. * @c true_type if the expression <tt>execution::execute(std::declval<T>(),
  56. * std::declval<F>())</tt> is well formed; otherwise @c false_type.
  57. */
  58. template <typename T, typename F>
  59. struct can_execute :
  60. integral_constant<bool, automatically_determined>
  61. {
  62. };
  63. } // namespace execution
  64. } // namespace asio
  65. } // namespace boost
  66. #else // defined(GENERATING_DOCUMENTATION)
  67. namespace boost {
  68. namespace asio {
  69. namespace execution {
  70. template <typename T, typename R>
  71. struct is_sender_to;
  72. namespace detail {
  73. template <typename S, typename R>
  74. void submit_helper(BOOST_ASIO_MOVE_ARG(S) s, BOOST_ASIO_MOVE_ARG(R) r);
  75. } // namespace detail
  76. } // namespace execution
  77. } // namespace asio
  78. } // namespace boost
  79. namespace boost_asio_execution_execute_fn {
  80. using boost::asio::conditional;
  81. using boost::asio::decay;
  82. using boost::asio::declval;
  83. using boost::asio::enable_if;
  84. using boost::asio::execution::detail::as_receiver;
  85. using boost::asio::execution::detail::is_as_invocable;
  86. using boost::asio::execution::is_sender_to;
  87. using boost::asio::false_type;
  88. using boost::asio::result_of;
  89. using boost::asio::traits::execute_free;
  90. using boost::asio::traits::execute_member;
  91. using boost::asio::true_type;
  92. using boost::asio::void_type;
  93. void execute();
  94. enum overload_type
  95. {
  96. call_member,
  97. call_free,
  98. adapter,
  99. ill_formed
  100. };
  101. template <typename Impl, typename T, typename F, typename = void,
  102. typename = void, typename = void, typename = void, typename = void>
  103. struct call_traits
  104. {
  105. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = ill_formed);
  106. };
  107. template <typename Impl, typename T, typename F>
  108. struct call_traits<Impl, T, void(F),
  109. typename enable_if<
  110. execute_member<typename Impl::template proxy<T>::type, F>::is_valid
  111. >::type> :
  112. execute_member<typename Impl::template proxy<T>::type, F>
  113. {
  114. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = call_member);
  115. };
  116. template <typename Impl, typename T, typename F>
  117. struct call_traits<Impl, T, void(F),
  118. typename enable_if<
  119. !execute_member<typename Impl::template proxy<T>, F>::is_valid
  120. >::type,
  121. typename enable_if<
  122. execute_free<T, F>::is_valid
  123. >::type> :
  124. execute_free<T, F>
  125. {
  126. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = call_free);
  127. };
  128. template <typename Impl, typename T, typename F>
  129. struct call_traits<Impl, T, void(F),
  130. typename enable_if<
  131. !execute_member<typename Impl::template proxy<T>::type, F>::is_valid
  132. >::type,
  133. typename enable_if<
  134. !execute_free<T, F>::is_valid
  135. >::type,
  136. typename void_type<
  137. typename result_of<typename decay<F>::type&()>::type
  138. >::type,
  139. typename enable_if<
  140. !is_as_invocable<typename decay<F>::type>::value
  141. >::type,
  142. typename enable_if<
  143. is_sender_to<T, as_receiver<typename decay<F>::type, T> >::value
  144. >::type>
  145. {
  146. BOOST_ASIO_STATIC_CONSTEXPR(overload_type, overload = adapter);
  147. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  148. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
  149. typedef void result_type;
  150. };
  151. struct impl
  152. {
  153. template <typename T>
  154. struct proxy
  155. {
  156. #if defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  157. struct type
  158. {
  159. template <typename F>
  160. auto execute(BOOST_ASIO_MOVE_ARG(F) f)
  161. noexcept(
  162. noexcept(
  163. declval<typename conditional<true, T, F>::type>().execute(
  164. BOOST_ASIO_MOVE_CAST(F)(f))
  165. )
  166. )
  167. -> decltype(
  168. declval<typename conditional<true, T, F>::type>().execute(
  169. BOOST_ASIO_MOVE_CAST(F)(f))
  170. );
  171. };
  172. #else // defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  173. typedef T type;
  174. #endif // defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  175. };
  176. template <typename T, typename F>
  177. BOOST_ASIO_CONSTEXPR typename enable_if<
  178. call_traits<impl, T, void(F)>::overload == call_member,
  179. typename call_traits<impl, T, void(F)>::result_type
  180. >::type
  181. operator()(
  182. BOOST_ASIO_MOVE_ARG(T) t,
  183. BOOST_ASIO_MOVE_ARG(F) f) const
  184. BOOST_ASIO_NOEXCEPT_IF((
  185. call_traits<impl, T, void(F)>::is_noexcept))
  186. {
  187. return BOOST_ASIO_MOVE_CAST(T)(t).execute(BOOST_ASIO_MOVE_CAST(F)(f));
  188. }
  189. template <typename T, typename F>
  190. BOOST_ASIO_CONSTEXPR typename enable_if<
  191. call_traits<impl, T, void(F)>::overload == call_free,
  192. typename call_traits<impl, T, void(F)>::result_type
  193. >::type
  194. operator()(
  195. BOOST_ASIO_MOVE_ARG(T) t,
  196. BOOST_ASIO_MOVE_ARG(F) f) const
  197. BOOST_ASIO_NOEXCEPT_IF((
  198. call_traits<impl, T, void(F)>::is_noexcept))
  199. {
  200. return execute(BOOST_ASIO_MOVE_CAST(T)(t), BOOST_ASIO_MOVE_CAST(F)(f));
  201. }
  202. template <typename T, typename F>
  203. BOOST_ASIO_CONSTEXPR typename enable_if<
  204. call_traits<impl, T, void(F)>::overload == adapter,
  205. typename call_traits<impl, T, void(F)>::result_type
  206. >::type
  207. operator()(
  208. BOOST_ASIO_MOVE_ARG(T) t,
  209. BOOST_ASIO_MOVE_ARG(F) f) const
  210. BOOST_ASIO_NOEXCEPT_IF((
  211. call_traits<impl, T, void(F)>::is_noexcept))
  212. {
  213. return boost::asio::execution::detail::submit_helper(
  214. BOOST_ASIO_MOVE_CAST(T)(t),
  215. as_receiver<typename decay<F>::type, T>(
  216. BOOST_ASIO_MOVE_CAST(F)(f), 0));
  217. }
  218. };
  219. template <typename T = impl>
  220. struct static_instance
  221. {
  222. static const T instance;
  223. };
  224. template <typename T>
  225. const T static_instance<T>::instance = {};
  226. } // namespace boost_asio_execution_execute_fn
  227. namespace boost {
  228. namespace asio {
  229. namespace execution {
  230. namespace {
  231. static BOOST_ASIO_CONSTEXPR const boost_asio_execution_execute_fn::impl&
  232. execute = boost_asio_execution_execute_fn::static_instance<>::instance;
  233. } // namespace
  234. typedef boost_asio_execution_execute_fn::impl execute_t;
  235. template <typename T, typename F>
  236. struct can_execute :
  237. integral_constant<bool,
  238. boost_asio_execution_execute_fn::call_traits<
  239. execute_t, T, void(F)>::overload !=
  240. boost_asio_execution_execute_fn::ill_formed>
  241. {
  242. };
  243. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  244. template <typename T, typename F>
  245. constexpr bool can_execute_v = can_execute<T, F>::value;
  246. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  247. } // namespace execution
  248. } // namespace asio
  249. } // namespace boost
  250. #endif // defined(GENERATING_DOCUMENTATION)
  251. #include <boost/asio/detail/pop_options.hpp>
  252. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  253. #endif // BOOST_ASIO_EXECUTION_EXECUTE_HPP