context_as.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // execution/context_as.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_CONTEXT_AS_HPP
  11. #define BOOST_ASIO_EXECUTION_CONTEXT_AS_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/context.hpp>
  18. #include <boost/asio/execution/executor.hpp>
  19. #include <boost/asio/execution/scheduler.hpp>
  20. #include <boost/asio/execution/sender.hpp>
  21. #include <boost/asio/is_applicable_property.hpp>
  22. #include <boost/asio/query.hpp>
  23. #include <boost/asio/traits/query_static_constexpr_member.hpp>
  24. #include <boost/asio/traits/static_query.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. #if defined(GENERATING_DOCUMENTATION)
  29. namespace execution {
  30. /// A property that is used to obtain the execution context that is associated
  31. /// with an executor.
  32. template <typename U>
  33. struct context_as_t
  34. {
  35. /// The context_as_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 T polymorphic_query_result_type;
  45. };
  46. /// A special value used for accessing the context_as_t property.
  47. template <typename U>
  48. constexpr context_as_t context_as;
  49. } // namespace execution
  50. #else // defined(GENERATING_DOCUMENTATION)
  51. namespace execution {
  52. template <typename T>
  53. struct context_as_t
  54. {
  55. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  56. # if defined(BOOST_ASIO_NO_DEPRECATED)
  57. template <typename U>
  58. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  59. is_applicable_property_v = (
  60. is_executor<U>::value));
  61. # else // defined(BOOST_ASIO_NO_DEPRECATED)
  62. template <typename U>
  63. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  64. is_applicable_property_v = (
  65. is_executor<U>::value
  66. || conditional<
  67. is_executor<U>::value,
  68. false_type,
  69. is_sender<U>
  70. >::type::value
  71. || conditional<
  72. is_executor<U>::value,
  73. false_type,
  74. is_scheduler<U>
  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. typedef T polymorphic_query_result_type;
  82. BOOST_ASIO_CONSTEXPR context_as_t()
  83. {
  84. }
  85. BOOST_ASIO_CONSTEXPR context_as_t(context_t)
  86. {
  87. }
  88. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  89. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  90. template <typename E>
  91. static BOOST_ASIO_CONSTEXPR
  92. typename context_t::query_static_constexpr_member<E>::result_type
  93. static_query()
  94. BOOST_ASIO_NOEXCEPT_IF((
  95. context_t::query_static_constexpr_member<E>::is_noexcept))
  96. {
  97. return context_t::query_static_constexpr_member<E>::value();
  98. }
  99. template <typename E, typename U = decltype(context_as_t::static_query<E>())>
  100. static BOOST_ASIO_CONSTEXPR const U static_query_v
  101. = context_as_t::static_query<E>();
  102. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  103. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  104. template <typename Executor, typename U>
  105. friend BOOST_ASIO_CONSTEXPR U query(
  106. const Executor& ex, const context_as_t<U>&,
  107. typename enable_if<
  108. is_same<T, U>::value
  109. >::type* = 0,
  110. typename enable_if<
  111. can_query<const Executor&, const context_t&>::value
  112. >::type* = 0)
  113. #if !defined(__clang__) // Clang crashes if noexcept is used here.
  114. #if defined(BOOST_ASIO_MSVC) // Visual C++ wants the type to be qualified.
  115. BOOST_ASIO_NOEXCEPT_IF((
  116. is_nothrow_query<const Executor&, const context_t&>::value))
  117. #else // defined(BOOST_ASIO_MSVC)
  118. BOOST_ASIO_NOEXCEPT_IF((
  119. is_nothrow_query<const Executor&, const context_t&>::value))
  120. #endif // defined(BOOST_ASIO_MSVC)
  121. #endif // !defined(__clang__)
  122. {
  123. return boost::asio::query(ex, context);
  124. }
  125. };
  126. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  127. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  128. template <typename T> template <typename E, typename U>
  129. const U context_as_t<T>::static_query_v;
  130. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  131. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  132. #if (defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) \
  133. && defined(BOOST_ASIO_HAS_CONSTEXPR)) \
  134. || defined(GENERATING_DOCUMENTATION)
  135. template <typename T>
  136. constexpr context_as_t<T> context_as{};
  137. #endif // (defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  138. // && defined(BOOST_ASIO_HAS_CONSTEXPR))
  139. // || defined(GENERATING_DOCUMENTATION)
  140. } // namespace execution
  141. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  142. template <typename T, typename U>
  143. struct is_applicable_property<T, execution::context_as_t<U> >
  144. : integral_constant<bool,
  145. execution::is_executor<T>::value
  146. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  147. || conditional<
  148. execution::is_executor<T>::value,
  149. false_type,
  150. execution::is_sender<T>
  151. >::type::value
  152. || conditional<
  153. execution::is_executor<T>::value,
  154. false_type,
  155. execution::is_scheduler<T>
  156. >::type::value
  157. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  158. >
  159. {
  160. };
  161. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  162. namespace traits {
  163. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  164. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  165. template <typename T, typename U>
  166. struct static_query<T, execution::context_as_t<U>,
  167. typename enable_if<
  168. static_query<T, execution::context_t>::is_valid
  169. >::type> : static_query<T, execution::context_t>
  170. {
  171. };
  172. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  173. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  174. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  175. template <typename T, typename U>
  176. struct query_free<T, execution::context_as_t<U>,
  177. typename enable_if<
  178. can_query<const T&, const execution::context_t&>::value
  179. >::type>
  180. {
  181. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  182. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
  183. (is_nothrow_query<const T&, const execution::context_t&>::value));
  184. typedef U result_type;
  185. };
  186. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  187. } // namespace traits
  188. #endif // defined(GENERATING_DOCUMENTATION)
  189. } // namespace asio
  190. } // namespace boost
  191. #include <boost/asio/detail/pop_options.hpp>
  192. #endif // BOOST_ASIO_EXECUTION_CONTEXT_AS_HPP