context_as.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // execution/context_as.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_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. template <typename U>
  57. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  58. is_applicable_property_v = (
  59. is_executor<U>::value
  60. || conditional<
  61. is_executor<U>::value,
  62. false_type,
  63. is_sender<U>
  64. >::type::value
  65. || conditional<
  66. is_executor<U>::value,
  67. false_type,
  68. is_scheduler<U>
  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. typedef T polymorphic_query_result_type;
  74. BOOST_ASIO_CONSTEXPR context_as_t()
  75. {
  76. }
  77. BOOST_ASIO_CONSTEXPR context_as_t(context_t)
  78. {
  79. }
  80. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  81. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  82. template <typename E>
  83. static BOOST_ASIO_CONSTEXPR
  84. typename context_t::query_static_constexpr_member<E>::result_type
  85. static_query()
  86. BOOST_ASIO_NOEXCEPT_IF((
  87. context_t::query_static_constexpr_member<E>::is_noexcept))
  88. {
  89. return context_t::query_static_constexpr_member<E>::value();
  90. }
  91. template <typename E, typename U = decltype(context_as_t::static_query<E>())>
  92. static BOOST_ASIO_CONSTEXPR const U static_query_v
  93. = context_as_t::static_query<E>();
  94. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  95. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  96. template <typename Executor, typename U>
  97. friend BOOST_ASIO_CONSTEXPR U query(
  98. const Executor& ex, const context_as_t<U>&,
  99. typename enable_if<
  100. is_same<T, U>::value
  101. >::type* = 0,
  102. typename enable_if<
  103. can_query<const Executor&, const context_t&>::value
  104. >::type* = 0)
  105. #if !defined(__clang__) // Clang crashes if noexcept is used here.
  106. #if defined(BOOST_ASIO_MSVC) // Visual C++ wants the type to be qualified.
  107. BOOST_ASIO_NOEXCEPT_IF((
  108. is_nothrow_query<const Executor&, const context_t&>::value))
  109. #else // defined(BOOST_ASIO_MSVC)
  110. BOOST_ASIO_NOEXCEPT_IF((
  111. is_nothrow_query<const Executor&, const context_t&>::value))
  112. #endif // defined(BOOST_ASIO_MSVC)
  113. #endif // !defined(__clang__)
  114. {
  115. return boost::asio::query(ex, context);
  116. }
  117. };
  118. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  119. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  120. template <typename T> template <typename E, typename U>
  121. const U context_as_t<T>::static_query_v;
  122. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  123. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  124. #if (defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) \
  125. && defined(BOOST_ASIO_HAS_CONSTEXPR)) \
  126. || defined(GENERATING_DOCUMENTATION)
  127. template <typename T>
  128. constexpr context_as_t<T> context_as{};
  129. #endif // (defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  130. // && defined(BOOST_ASIO_HAS_CONSTEXPR))
  131. // || defined(GENERATING_DOCUMENTATION)
  132. } // namespace execution
  133. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  134. template <typename T, typename U>
  135. struct is_applicable_property<T, execution::context_as_t<U> >
  136. : integral_constant<bool,
  137. execution::is_executor<T>::value
  138. || conditional<
  139. execution::is_executor<T>::value,
  140. false_type,
  141. execution::is_sender<T>
  142. >::type::value
  143. || conditional<
  144. execution::is_executor<T>::value,
  145. false_type,
  146. execution::is_scheduler<T>
  147. >::type::value>
  148. {
  149. };
  150. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  151. namespace traits {
  152. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  153. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  154. template <typename T, typename U>
  155. struct static_query<T, execution::context_as_t<U>,
  156. typename enable_if<
  157. static_query<T, execution::context_t>::is_valid
  158. >::type> : static_query<T, execution::context_t>
  159. {
  160. };
  161. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  162. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  163. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  164. template <typename T, typename U>
  165. struct query_free<T, execution::context_as_t<U>,
  166. typename enable_if<
  167. can_query<const T&, const execution::context_t&>::value
  168. >::type>
  169. {
  170. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  171. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
  172. (is_nothrow_query<const T&, const execution::context_t&>::value));
  173. typedef U result_type;
  174. };
  175. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  176. } // namespace traits
  177. #endif // defined(GENERATING_DOCUMENTATION)
  178. } // namespace asio
  179. } // namespace boost
  180. #include <boost/asio/detail/pop_options.hpp>
  181. #endif // BOOST_ASIO_EXECUTION_CONTEXT_AS_HPP