execute.hpp 7.9 KB

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