context_as.hpp 5.3 KB

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