context.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // execution/context.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 ASIO_EXECUTION_CONTEXT2_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/detail/type_traits.hpp"
  17. #include "asio/execution/executor.hpp"
  18. #include "asio/is_applicable_property.hpp"
  19. #include "asio/traits/query_static_constexpr_member.hpp"
  20. #include "asio/traits/static_query.hpp"
  21. #if defined(ASIO_HAS_STD_ANY)
  22. # include <any>
  23. #endif // defined(ASIO_HAS_STD_ANY)
  24. #include "asio/detail/push_options.hpp"
  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. struct context_t
  31. {
  32. /// The context_t property applies to executors.
  33. template <typename T>
  34. static constexpr bool is_applicable_property_v = is_executor_v<T>;
  35. /// The context_t property cannot be required.
  36. static constexpr bool is_requirable = false;
  37. /// The context_t property cannot be preferred.
  38. static constexpr bool is_preferable = false;
  39. /// The type returned by queries against an @c any_executor.
  40. typedef std::any polymorphic_query_result_type;
  41. };
  42. /// A special value used for accessing the context_t property.
  43. constexpr context_t context;
  44. } // namespace execution
  45. #else // defined(GENERATING_DOCUMENTATION)
  46. namespace execution {
  47. namespace detail {
  48. template <int I = 0>
  49. struct context_t
  50. {
  51. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  52. template <typename T>
  53. static constexpr bool is_applicable_property_v = is_executor<T>::value;
  54. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  55. static constexpr bool is_requirable = false;
  56. static constexpr bool is_preferable = false;
  57. #if defined(ASIO_HAS_STD_ANY)
  58. typedef std::any polymorphic_query_result_type;
  59. #endif // defined(ASIO_HAS_STD_ANY)
  60. constexpr context_t()
  61. {
  62. }
  63. template <typename T>
  64. struct static_proxy
  65. {
  66. #if defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  67. struct type
  68. {
  69. template <typename P>
  70. static constexpr auto query(P&& p)
  71. noexcept(
  72. noexcept(
  73. conditional_t<true, T, P>::query(static_cast<P&&>(p))
  74. )
  75. )
  76. -> decltype(
  77. conditional_t<true, T, P>::query(static_cast<P&&>(p))
  78. )
  79. {
  80. return T::query(static_cast<P&&>(p));
  81. }
  82. };
  83. #else // defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  84. typedef T type;
  85. #endif // defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  86. };
  87. template <typename T>
  88. struct query_static_constexpr_member :
  89. traits::query_static_constexpr_member<
  90. typename static_proxy<T>::type, context_t> {};
  91. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  92. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  93. template <typename T>
  94. static constexpr typename query_static_constexpr_member<T>::result_type
  95. static_query()
  96. noexcept(query_static_constexpr_member<T>::is_noexcept)
  97. {
  98. return query_static_constexpr_member<T>::value();
  99. }
  100. template <typename E, typename T = decltype(context_t::static_query<E>())>
  101. static constexpr const T static_query_v = context_t::static_query<E>();
  102. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  103. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  104. };
  105. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  106. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  107. template <int I> template <typename E, typename T>
  108. const T context_t<I>::static_query_v;
  109. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  110. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  111. } // namespace detail
  112. typedef detail::context_t<> context_t;
  113. constexpr context_t context;
  114. } // namespace execution
  115. #if !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  116. template <typename T>
  117. struct is_applicable_property<T, execution::context_t>
  118. : integral_constant<bool, execution::is_executor<T>::value>
  119. {
  120. };
  121. #endif // !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  122. namespace traits {
  123. #if !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  124. || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  125. template <typename T>
  126. struct static_query<T, execution::context_t,
  127. enable_if_t<
  128. execution::detail::context_t<0>::
  129. query_static_constexpr_member<T>::is_valid
  130. >>
  131. {
  132. static constexpr bool is_valid = true;
  133. static constexpr bool is_noexcept = true;
  134. typedef typename execution::detail::context_t<0>::
  135. query_static_constexpr_member<T>::result_type result_type;
  136. static constexpr result_type value()
  137. {
  138. return execution::detail::context_t<0>::
  139. query_static_constexpr_member<T>::value();
  140. }
  141. };
  142. #endif // !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  143. // || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  144. } // namespace traits
  145. #endif // defined(GENERATING_DOCUMENTATION)
  146. } // namespace asio
  147. #include "asio/detail/pop_options.hpp"
  148. #endif // ASIO_EXECUTION_CONTEXT2_HPP