context.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // execution/context.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_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/is_applicable_property.hpp>
  19. #include <boost/asio/traits/query_static_constexpr_member.hpp>
  20. #include <boost/asio/traits/static_query.hpp>
  21. #if defined(BOOST_ASIO_HAS_STD_ANY)
  22. # include <any>
  23. #endif // defined(BOOST_ASIO_HAS_STD_ANY)
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. #if defined(GENERATING_DOCUMENTATION)
  28. namespace execution {
  29. /// A property that is used to obtain the execution context that is associated
  30. /// with an executor.
  31. struct context_t
  32. {
  33. /// The context_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 std::any polymorphic_query_result_type;
  42. };
  43. /// A special value used for accessing the context_t property.
  44. constexpr context_t context;
  45. } // namespace execution
  46. #else // defined(GENERATING_DOCUMENTATION)
  47. namespace execution {
  48. namespace detail {
  49. template <int I = 0>
  50. struct context_t
  51. {
  52. #if defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  53. template <typename T>
  54. static constexpr bool is_applicable_property_v = is_executor<T>::value;
  55. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  56. static constexpr bool is_requirable = false;
  57. static constexpr bool is_preferable = false;
  58. #if defined(BOOST_ASIO_HAS_STD_ANY)
  59. typedef std::any polymorphic_query_result_type;
  60. #endif // defined(BOOST_ASIO_HAS_STD_ANY)
  61. constexpr context_t()
  62. {
  63. }
  64. template <typename T>
  65. struct static_proxy
  66. {
  67. #if defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  68. struct type
  69. {
  70. template <typename P>
  71. static constexpr auto query(P&& p)
  72. noexcept(
  73. noexcept(
  74. conditional_t<true, T, P>::query(static_cast<P&&>(p))
  75. )
  76. )
  77. -> decltype(
  78. conditional_t<true, T, P>::query(static_cast<P&&>(p))
  79. )
  80. {
  81. return T::query(static_cast<P&&>(p));
  82. }
  83. };
  84. #else // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  85. typedef T type;
  86. #endif // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  87. };
  88. template <typename T>
  89. struct query_static_constexpr_member :
  90. traits::query_static_constexpr_member<
  91. typename static_proxy<T>::type, context_t> {};
  92. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  93. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  94. template <typename T>
  95. static constexpr typename query_static_constexpr_member<T>::result_type
  96. static_query()
  97. noexcept(query_static_constexpr_member<T>::is_noexcept)
  98. {
  99. return query_static_constexpr_member<T>::value();
  100. }
  101. template <typename E, typename T = decltype(context_t::static_query<E>())>
  102. static constexpr const T static_query_v = context_t::static_query<E>();
  103. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  104. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  105. };
  106. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  107. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  108. template <int I> template <typename E, typename T>
  109. const T context_t<I>::static_query_v;
  110. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  111. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  112. } // namespace detail
  113. typedef detail::context_t<> context_t;
  114. BOOST_ASIO_INLINE_VARIABLE constexpr context_t context;
  115. } // namespace execution
  116. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  117. template <typename T>
  118. struct is_applicable_property<T, execution::context_t>
  119. : integral_constant<bool, execution::is_executor<T>::value>
  120. {
  121. };
  122. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  123. namespace traits {
  124. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  125. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  126. template <typename T>
  127. struct static_query<T, execution::context_t,
  128. enable_if_t<
  129. execution::detail::context_t<0>::
  130. query_static_constexpr_member<T>::is_valid
  131. >>
  132. {
  133. static constexpr bool is_valid = true;
  134. static constexpr bool is_noexcept = true;
  135. typedef typename execution::detail::context_t<0>::
  136. query_static_constexpr_member<T>::result_type result_type;
  137. static constexpr result_type value()
  138. {
  139. return execution::detail::context_t<0>::
  140. query_static_constexpr_member<T>::value();
  141. }
  142. };
  143. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  144. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  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_CONTEXT2_HPP