context.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // execution/context.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_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. template <typename T>
  57. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  58. is_applicable_property_v = (
  59. is_executor<T>::value
  60. || conditional<
  61. is_executor<T>::value,
  62. false_type,
  63. is_sender<T>
  64. >::type::value
  65. || conditional<
  66. is_executor<T>::value,
  67. false_type,
  68. is_scheduler<T>
  69. >::type::value));
  70. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  71. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
  72. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
  73. #if defined(BOOST_ASIO_HAS_STD_ANY)
  74. typedef std::any polymorphic_query_result_type;
  75. #endif // defined(BOOST_ASIO_HAS_STD_ANY)
  76. BOOST_ASIO_CONSTEXPR context_t()
  77. {
  78. }
  79. template <typename T>
  80. struct static_proxy
  81. {
  82. #if defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  83. struct type
  84. {
  85. template <typename P>
  86. static constexpr auto query(BOOST_ASIO_MOVE_ARG(P) p)
  87. noexcept(
  88. noexcept(
  89. conditional<true, T, P>::type::query(BOOST_ASIO_MOVE_CAST(P)(p))
  90. )
  91. )
  92. -> decltype(
  93. conditional<true, T, P>::type::query(BOOST_ASIO_MOVE_CAST(P)(p))
  94. )
  95. {
  96. return T::query(BOOST_ASIO_MOVE_CAST(P)(p));
  97. }
  98. };
  99. #else // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  100. typedef T type;
  101. #endif // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  102. };
  103. template <typename T>
  104. struct query_static_constexpr_member :
  105. traits::query_static_constexpr_member<
  106. typename static_proxy<T>::type, context_t> {};
  107. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  108. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  109. template <typename T>
  110. static BOOST_ASIO_CONSTEXPR
  111. typename query_static_constexpr_member<T>::result_type
  112. static_query()
  113. BOOST_ASIO_NOEXCEPT_IF((
  114. query_static_constexpr_member<T>::is_noexcept))
  115. {
  116. return query_static_constexpr_member<T>::value();
  117. }
  118. template <typename E, typename T = decltype(context_t::static_query<E>())>
  119. static BOOST_ASIO_CONSTEXPR const T static_query_v
  120. = context_t::static_query<E>();
  121. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  122. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  123. #if !defined(BOOST_ASIO_HAS_CONSTEXPR)
  124. static const context_t instance;
  125. #endif // !defined(BOOST_ASIO_HAS_CONSTEXPR)
  126. };
  127. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  128. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  129. template <int I> template <typename E, typename T>
  130. const T context_t<I>::static_query_v;
  131. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  132. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  133. #if !defined(BOOST_ASIO_HAS_CONSTEXPR)
  134. template <int I>
  135. const context_t<I> context_t<I>::instance;
  136. #endif
  137. } // namespace detail
  138. typedef detail::context_t<> context_t;
  139. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  140. constexpr context_t context;
  141. #else // defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  142. namespace { static const context_t& context = context_t::instance; }
  143. #endif
  144. } // namespace execution
  145. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  146. template <typename T>
  147. struct is_applicable_property<T, execution::context_t>
  148. : integral_constant<bool,
  149. execution::is_executor<T>::value
  150. || conditional<
  151. execution::is_executor<T>::value,
  152. false_type,
  153. execution::is_sender<T>
  154. >::type::value
  155. || conditional<
  156. execution::is_executor<T>::value,
  157. false_type,
  158. execution::is_scheduler<T>
  159. >::type::value>
  160. {
  161. };
  162. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  163. namespace traits {
  164. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  165. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  166. template <typename T>
  167. struct static_query<T, execution::context_t,
  168. typename enable_if<
  169. execution::detail::context_t<0>::
  170. query_static_constexpr_member<T>::is_valid
  171. >::type>
  172. {
  173. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  174. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  175. typedef typename execution::detail::context_t<0>::
  176. query_static_constexpr_member<T>::result_type result_type;
  177. static BOOST_ASIO_CONSTEXPR result_type value()
  178. {
  179. return execution::detail::context_t<0>::
  180. query_static_constexpr_member<T>::value();
  181. }
  182. };
  183. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  184. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  185. } // namespace traits
  186. #endif // defined(GENERATING_DOCUMENTATION)
  187. } // namespace asio
  188. } // namespace boost
  189. #include <boost/asio/detail/pop_options.hpp>
  190. #endif // BOOST_ASIO_EXECUTION_CONTEXT2_HPP