execute.hpp 8.4 KB

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