context_as.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // execution/context_as.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/is_applicable_property.hpp>
  20. #include <boost/asio/query.hpp>
  21. #include <boost/asio/traits/query_static_constexpr_member.hpp>
  22. #include <boost/asio/traits/static_query.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. #if defined(GENERATING_DOCUMENTATION)
  27. namespace execution {
  28. /// A property that is used to obtain the execution context that is associated
  29. /// with an executor.
  30. template <typename U>
  31. struct context_as_t
  32. {
  33. /// The context_as_t property applies to executors.
  34. template <typename T>
  35. static constexpr bool is_applicable_property_v = is_executor_v<T>;
  36. /// The context_t property cannot be required.
  37. static constexpr bool is_requirable = false;
  38. /// The context_t property cannot be preferred.
  39. static constexpr bool is_preferable = false;
  40. /// The type returned by queries against an @c any_executor.
  41. typedef T polymorphic_query_result_type;
  42. };
  43. /// A special value used for accessing the context_as_t property.
  44. template <typename U>
  45. constexpr context_as_t context_as;
  46. } // namespace execution
  47. #else // defined(GENERATING_DOCUMENTATION)
  48. namespace execution {
  49. template <typename T>
  50. struct context_as_t
  51. {
  52. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  53. template <typename U>
  54. static constexpr bool is_applicable_property_v = is_executor<U>::value;
  55. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  56. static constexpr bool is_requirable = false;
  57. static constexpr bool is_preferable = false;
  58. typedef T polymorphic_query_result_type;
  59. constexpr context_as_t()
  60. {
  61. }
  62. constexpr context_as_t(context_t)
  63. {
  64. }
  65. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  66. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  67. template <typename E>
  68. static constexpr
  69. typename context_t::query_static_constexpr_member<E>::result_type
  70. static_query()
  71. noexcept(context_t::query_static_constexpr_member<E>::is_noexcept)
  72. {
  73. return context_t::query_static_constexpr_member<E>::value();
  74. }
  75. template <typename E, typename U = decltype(context_as_t::static_query<E>())>
  76. static constexpr const U static_query_v
  77. = context_as_t::static_query<E>();
  78. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  79. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  80. template <typename Executor, typename U>
  81. friend constexpr U query(
  82. const Executor& ex, const context_as_t<U>&,
  83. enable_if_t<
  84. is_same<T, U>::value
  85. >* = 0,
  86. enable_if_t<
  87. can_query<const Executor&, const context_t&>::value
  88. >* = 0)
  89. #if !defined(__clang__) // Clang crashes if noexcept is used here.
  90. #if defined(BOOST_ASIO_MSVC) // Visual C++ wants the type to be qualified.
  91. noexcept(is_nothrow_query<const Executor&, const context_t&>::value)
  92. #else // defined(BOOST_ASIO_MSVC)
  93. noexcept(is_nothrow_query<const Executor&, const context_t&>::value)
  94. #endif // defined(BOOST_ASIO_MSVC)
  95. #endif // !defined(__clang__)
  96. {
  97. return boost::asio::query(ex, context);
  98. }
  99. };
  100. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  101. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  102. template <typename T> template <typename E, typename U>
  103. const U context_as_t<T>::static_query_v;
  104. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  105. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  106. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES) \
  107. || defined(GENERATING_DOCUMENTATION)
  108. template <typename T>
  109. constexpr context_as_t<T> context_as{};
  110. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  111. // || defined(GENERATING_DOCUMENTATION)
  112. } // namespace execution
  113. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  114. template <typename T, typename U>
  115. struct is_applicable_property<T, execution::context_as_t<U>>
  116. : integral_constant<bool, execution::is_executor<T>::value>
  117. {
  118. };
  119. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  120. namespace traits {
  121. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  122. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  123. template <typename T, typename U>
  124. struct static_query<T, execution::context_as_t<U>,
  125. enable_if_t<
  126. static_query<T, execution::context_t>::is_valid
  127. >> : static_query<T, execution::context_t>
  128. {
  129. };
  130. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  131. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  132. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  133. template <typename T, typename U>
  134. struct query_free<T, execution::context_as_t<U>,
  135. enable_if_t<
  136. can_query<const T&, const execution::context_t&>::value
  137. >>
  138. {
  139. static constexpr bool is_valid = true;
  140. static constexpr bool is_noexcept =
  141. is_nothrow_query<const T&, const execution::context_t&>::value;
  142. typedef U result_type;
  143. };
  144. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  145. } // namespace traits
  146. #endif // defined(GENERATING_DOCUMENTATION)
  147. } // namespace asio
  148. } // namespace boost
  149. #include <boost/asio/detail/pop_options.hpp>
  150. #endif // BOOST_ASIO_EXECUTION_CONTEXT_AS_HPP