context_as.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // execution/context_as.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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/execution/scheduler.hpp"
  20. #include "asio/execution/sender.hpp"
  21. #include "asio/is_applicable_property.hpp"
  22. #include "asio/query.hpp"
  23. #include "asio/traits/query_static_constexpr_member.hpp"
  24. #include "asio/traits/static_query.hpp"
  25. #include "asio/detail/push_options.hpp"
  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. template <typename U>
  32. struct context_as_t
  33. {
  34. /// The context_as_t property applies to executors, senders, and schedulers.
  35. template <typename T>
  36. static constexpr bool is_applicable_property_v =
  37. is_executor_v<T> || is_sender_v<T> || is_scheduler_v<T>;
  38. /// The context_t property cannot be required.
  39. static constexpr bool is_requirable = false;
  40. /// The context_t property cannot be preferred.
  41. static constexpr bool is_preferable = false;
  42. /// The type returned by queries against an @c any_executor.
  43. typedef T polymorphic_query_result_type;
  44. };
  45. /// A special value used for accessing the context_as_t property.
  46. template <typename U>
  47. constexpr context_as_t context_as;
  48. } // namespace execution
  49. #else // defined(GENERATING_DOCUMENTATION)
  50. namespace execution {
  51. template <typename T>
  52. struct context_as_t
  53. {
  54. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  55. template <typename U>
  56. ASIO_STATIC_CONSTEXPR(bool,
  57. is_applicable_property_v = (
  58. is_executor<U>::value
  59. || conditional<
  60. is_executor<U>::value,
  61. false_type,
  62. is_sender<U>
  63. >::type::value
  64. || conditional<
  65. is_executor<U>::value,
  66. false_type,
  67. is_scheduler<U>
  68. >::type::value));
  69. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  70. ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
  71. ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
  72. typedef T polymorphic_query_result_type;
  73. ASIO_CONSTEXPR context_as_t()
  74. {
  75. }
  76. ASIO_CONSTEXPR context_as_t(context_t)
  77. {
  78. }
  79. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  80. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  81. template <typename E>
  82. static ASIO_CONSTEXPR
  83. typename context_t::query_static_constexpr_member<E>::result_type
  84. static_query()
  85. ASIO_NOEXCEPT_IF((
  86. context_t::query_static_constexpr_member<E>::is_noexcept))
  87. {
  88. return context_t::query_static_constexpr_member<E>::value();
  89. }
  90. template <typename E, typename U = decltype(context_as_t::static_query<E>())>
  91. static ASIO_CONSTEXPR const U static_query_v
  92. = context_as_t::static_query<E>();
  93. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  94. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  95. template <typename Executor, typename U>
  96. friend ASIO_CONSTEXPR U query(
  97. const Executor& ex, const context_as_t<U>&,
  98. typename enable_if<
  99. is_same<T, U>::value
  100. >::type* = 0,
  101. typename enable_if<
  102. can_query<const Executor&, const context_t&>::value
  103. >::type* = 0)
  104. #if !defined(__clang__) // Clang crashes if noexcept is used here.
  105. #if defined(ASIO_MSVC) // Visual C++ wants the type to be qualified.
  106. ASIO_NOEXCEPT_IF((
  107. is_nothrow_query<const Executor&, const context_t&>::value))
  108. #else // defined(ASIO_MSVC)
  109. ASIO_NOEXCEPT_IF((
  110. is_nothrow_query<const Executor&, const context_t&>::value))
  111. #endif // defined(ASIO_MSVC)
  112. #endif // !defined(__clang__)
  113. {
  114. return asio::query(ex, context);
  115. }
  116. };
  117. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  118. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  119. template <typename T> template <typename E, typename U>
  120. const U context_as_t<T>::static_query_v;
  121. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  122. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  123. #if (defined(ASIO_HAS_VARIABLE_TEMPLATES) \
  124. && defined(ASIO_HAS_CONSTEXPR)) \
  125. || defined(GENERATING_DOCUMENTATION)
  126. template <typename T>
  127. constexpr context_as_t<T> context_as{};
  128. #endif // (defined(ASIO_HAS_VARIABLE_TEMPLATES)
  129. // && defined(ASIO_HAS_CONSTEXPR))
  130. // || defined(GENERATING_DOCUMENTATION)
  131. } // namespace execution
  132. #if !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  133. template <typename T, typename U>
  134. struct is_applicable_property<T, execution::context_as_t<U> >
  135. : integral_constant<bool,
  136. execution::is_executor<T>::value
  137. || conditional<
  138. execution::is_executor<T>::value,
  139. false_type,
  140. execution::is_sender<T>
  141. >::type::value
  142. || conditional<
  143. execution::is_executor<T>::value,
  144. false_type,
  145. execution::is_scheduler<T>
  146. >::type::value>
  147. {
  148. };
  149. #endif // !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  150. namespace traits {
  151. #if !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  152. || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  153. template <typename T, typename U>
  154. struct static_query<T, execution::context_as_t<U>,
  155. typename enable_if<
  156. static_query<T, execution::context_t>::is_valid
  157. >::type> : static_query<T, execution::context_t>
  158. {
  159. };
  160. #endif // !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  161. // || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  162. #if !defined(ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  163. template <typename T, typename U>
  164. struct query_free<T, execution::context_as_t<U>,
  165. typename enable_if<
  166. can_query<const T&, const execution::context_t&>::value
  167. >::type>
  168. {
  169. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  170. ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
  171. (is_nothrow_query<const T&, const execution::context_t&>::value));
  172. typedef U result_type;
  173. };
  174. #endif // !defined(ASIO_HAS_DEDUCED_QUERY_FREE_TRAIT)
  175. } // namespace traits
  176. #endif // defined(GENERATING_DOCUMENTATION)
  177. } // namespace asio
  178. #include "asio/detail/pop_options.hpp"
  179. #endif // ASIO_EXECUTION_CONTEXT_AS_HPP