schedule.hpp 7.6 KB

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