schedule.hpp 7.4 KB

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