context.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // execution/context.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_CONTEXT2_HPP
  11. #define BOOST_ASIO_EXECUTION_CONTEXT2_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/execution/scheduler.hpp>
  19. #include <boost/asio/execution/sender.hpp>
  20. #include <boost/asio/is_applicable_property.hpp>
  21. #include <boost/asio/traits/query_static_constexpr_member.hpp>
  22. #include <boost/asio/traits/static_query.hpp>
  23. #if defined(BOOST_ASIO_HAS_STD_ANY)
  24. # include <any>
  25. #endif // defined(BOOST_ASIO_HAS_STD_ANY)
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. #if defined(GENERATING_DOCUMENTATION)
  30. namespace execution {
  31. /// A property that is used to obtain the execution context that is associated
  32. /// with an executor.
  33. struct context_t
  34. {
  35. /// The context_t property applies to executors, senders, and schedulers.
  36. template <typename T>
  37. static constexpr bool is_applicable_property_v =
  38. is_executor_v<T> || is_sender_v<T> || is_scheduler_v<T>;
  39. /// The context_t property cannot be required.
  40. static constexpr bool is_requirable = false;
  41. /// The context_t property cannot be preferred.
  42. static constexpr bool is_preferable = false;
  43. /// The type returned by queries against an @c any_executor.
  44. typedef std::any polymorphic_query_result_type;
  45. };
  46. /// A special value used for accessing the context_t property.
  47. constexpr context_t context;
  48. } // namespace execution
  49. #else // defined(GENERATING_DOCUMENTATION)
  50. namespace execution {
  51. namespace detail {
  52. template <int I = 0>
  53. struct context_t
  54. {
  55. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  56. # if defined(BOOST_ASIO_NO_DEPRECATED)
  57. template <typename T>
  58. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  59. is_applicable_property_v = (
  60. is_executor<T>::value));
  61. # else // defined(BOOST_ASIO_NO_DEPRECATED)
  62. template <typename T>
  63. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  64. is_applicable_property_v = (
  65. is_executor<T>::value
  66. || conditional<
  67. is_executor<T>::value,
  68. false_type,
  69. is_sender<T>
  70. >::type::value
  71. || conditional<
  72. is_executor<T>::value,
  73. false_type,
  74. is_scheduler<T>
  75. >::type::value
  76. ));
  77. # endif // defined(BOOST_ASIO_NO_DEPRECATED)
  78. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  79. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
  80. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
  81. #if defined(BOOST_ASIO_HAS_STD_ANY)
  82. typedef std::any polymorphic_query_result_type;
  83. #endif // defined(BOOST_ASIO_HAS_STD_ANY)
  84. BOOST_ASIO_CONSTEXPR context_t()
  85. {
  86. }
  87. template <typename T>
  88. struct static_proxy
  89. {
  90. #if defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  91. struct type
  92. {
  93. template <typename P>
  94. static constexpr auto query(BOOST_ASIO_MOVE_ARG(P) p)
  95. noexcept(
  96. noexcept(
  97. conditional<true, T, P>::type::query(BOOST_ASIO_MOVE_CAST(P)(p))
  98. )
  99. )
  100. -> decltype(
  101. conditional<true, T, P>::type::query(BOOST_ASIO_MOVE_CAST(P)(p))
  102. )
  103. {
  104. return T::query(BOOST_ASIO_MOVE_CAST(P)(p));
  105. }
  106. };
  107. #else // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  108. typedef T type;
  109. #endif // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  110. };
  111. template <typename T>
  112. struct query_static_constexpr_member :
  113. traits::query_static_constexpr_member<
  114. typename static_proxy<T>::type, context_t> {};
  115. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  116. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  117. template <typename T>
  118. static BOOST_ASIO_CONSTEXPR
  119. typename query_static_constexpr_member<T>::result_type
  120. static_query()
  121. BOOST_ASIO_NOEXCEPT_IF((
  122. query_static_constexpr_member<T>::is_noexcept))
  123. {
  124. return query_static_constexpr_member<T>::value();
  125. }
  126. template <typename E, typename T = decltype(context_t::static_query<E>())>
  127. static BOOST_ASIO_CONSTEXPR const T static_query_v
  128. = context_t::static_query<E>();
  129. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  130. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  131. #if !defined(BOOST_ASIO_HAS_CONSTEXPR)
  132. static const context_t instance;
  133. #endif // !defined(BOOST_ASIO_HAS_CONSTEXPR)
  134. };
  135. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  136. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  137. template <int I> template <typename E, typename T>
  138. const T context_t<I>::static_query_v;
  139. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  140. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  141. #if !defined(BOOST_ASIO_HAS_CONSTEXPR)
  142. template <int I>
  143. const context_t<I> context_t<I>::instance;
  144. #endif
  145. } // namespace detail
  146. typedef detail::context_t<> context_t;
  147. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  148. constexpr context_t context;
  149. #else // defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  150. namespace { static const context_t& context = context_t::instance; }
  151. #endif
  152. } // namespace execution
  153. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  154. template <typename T>
  155. struct is_applicable_property<T, execution::context_t>
  156. : integral_constant<bool,
  157. execution::is_executor<T>::value
  158. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  159. || conditional<
  160. execution::is_executor<T>::value,
  161. false_type,
  162. execution::is_sender<T>
  163. >::type::value
  164. || conditional<
  165. execution::is_executor<T>::value,
  166. false_type,
  167. execution::is_scheduler<T>
  168. >::type::value
  169. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  170. >
  171. {
  172. };
  173. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  174. namespace traits {
  175. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  176. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  177. template <typename T>
  178. struct static_query<T, execution::context_t,
  179. typename enable_if<
  180. execution::detail::context_t<0>::
  181. query_static_constexpr_member<T>::is_valid
  182. >::type>
  183. {
  184. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  185. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  186. typedef typename execution::detail::context_t<0>::
  187. query_static_constexpr_member<T>::result_type result_type;
  188. static BOOST_ASIO_CONSTEXPR result_type value()
  189. {
  190. return execution::detail::context_t<0>::
  191. query_static_constexpr_member<T>::value();
  192. }
  193. };
  194. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  195. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  196. } // namespace traits
  197. #endif // defined(GENERATING_DOCUMENTATION)
  198. } // namespace asio
  199. } // namespace boost
  200. #include <boost/asio/detail/pop_options.hpp>
  201. #endif // BOOST_ASIO_EXECUTION_CONTEXT2_HPP