occupancy.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // execution/occupancy.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_OCCUPANCY_HPP
  11. #define ASIO_EXECUTION_OCCUPANCY_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/execution/scheduler.hpp"
  19. #include "asio/execution/sender.hpp"
  20. #include "asio/is_applicable_property.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 gives an estimate of the number of execution agents that
  28. /// should occupy the associated execution context.
  29. struct occupancy_t
  30. {
  31. /// The occupancy_t property applies to executors, senders, and schedulers.
  32. template <typename T>
  33. static constexpr bool is_applicable_property_v =
  34. is_executor_v<T> || is_sender_v<T> || is_scheduler_v<T>;
  35. /// The occupancy_t property cannot be required.
  36. static constexpr bool is_requirable = false;
  37. /// The occupancy_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::size_t polymorphic_query_result_type;
  41. };
  42. /// A special value used for accessing the occupancy_t property.
  43. constexpr occupancy_t occupancy;
  44. } // namespace execution
  45. #else // defined(GENERATING_DOCUMENTATION)
  46. namespace execution {
  47. namespace detail {
  48. template <int I = 0>
  49. struct occupancy_t
  50. {
  51. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  52. template <typename T>
  53. ASIO_STATIC_CONSTEXPR(bool,
  54. is_applicable_property_v = (
  55. is_executor<T>::value
  56. || conditional<
  57. is_executor<T>::value,
  58. false_type,
  59. is_sender<T>
  60. >::type::value
  61. || conditional<
  62. is_executor<T>::value,
  63. false_type,
  64. is_scheduler<T>
  65. >::type::value));
  66. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  67. ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
  68. ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
  69. typedef std::size_t polymorphic_query_result_type;
  70. ASIO_CONSTEXPR occupancy_t()
  71. {
  72. }
  73. template <typename T>
  74. struct static_proxy
  75. {
  76. #if defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  77. struct type
  78. {
  79. template <typename P>
  80. static constexpr auto query(ASIO_MOVE_ARG(P) p)
  81. noexcept(
  82. noexcept(
  83. conditional<true, T, P>::type::query(ASIO_MOVE_CAST(P)(p))
  84. )
  85. )
  86. -> decltype(
  87. conditional<true, T, P>::type::query(ASIO_MOVE_CAST(P)(p))
  88. )
  89. {
  90. return T::query(ASIO_MOVE_CAST(P)(p));
  91. }
  92. };
  93. #else // defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  94. typedef T type;
  95. #endif // defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  96. };
  97. template <typename T>
  98. struct query_static_constexpr_member :
  99. traits::query_static_constexpr_member<
  100. typename static_proxy<T>::type, occupancy_t> {};
  101. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  102. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  103. template <typename T>
  104. static ASIO_CONSTEXPR
  105. typename query_static_constexpr_member<T>::result_type
  106. static_query()
  107. ASIO_NOEXCEPT_IF((
  108. query_static_constexpr_member<T>::is_noexcept))
  109. {
  110. return query_static_constexpr_member<T>::value();
  111. }
  112. template <typename E, typename T = decltype(occupancy_t::static_query<E>())>
  113. static ASIO_CONSTEXPR const T static_query_v
  114. = occupancy_t::static_query<E>();
  115. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  116. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  117. #if !defined(ASIO_HAS_CONSTEXPR)
  118. static const occupancy_t instance;
  119. #endif // !defined(ASIO_HAS_CONSTEXPR)
  120. };
  121. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  122. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  123. template <int I> template <typename E, typename T>
  124. const T occupancy_t<I>::static_query_v;
  125. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  126. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  127. #if !defined(ASIO_HAS_CONSTEXPR)
  128. template <int I>
  129. const occupancy_t<I> occupancy_t<I>::instance;
  130. #endif
  131. } // namespace detail
  132. typedef detail::occupancy_t<> occupancy_t;
  133. #if defined(ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  134. constexpr occupancy_t occupancy;
  135. #else // defined(ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  136. namespace { static const occupancy_t& occupancy = occupancy_t::instance; }
  137. #endif
  138. } // namespace execution
  139. #if !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  140. template <typename T>
  141. struct is_applicable_property<T, execution::occupancy_t>
  142. : integral_constant<bool,
  143. execution::is_executor<T>::value
  144. || conditional<
  145. execution::is_executor<T>::value,
  146. false_type,
  147. execution::is_sender<T>
  148. >::type::value
  149. || conditional<
  150. execution::is_executor<T>::value,
  151. false_type,
  152. execution::is_scheduler<T>
  153. >::type::value>
  154. {
  155. };
  156. #endif // !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  157. namespace traits {
  158. #if !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  159. || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  160. template <typename T>
  161. struct static_query<T, execution::occupancy_t,
  162. typename enable_if<
  163. execution::detail::occupancy_t<0>::
  164. query_static_constexpr_member<T>::is_valid
  165. >::type>
  166. {
  167. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  168. ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  169. typedef typename execution::detail::occupancy_t<0>::
  170. query_static_constexpr_member<T>::result_type result_type;
  171. static ASIO_CONSTEXPR result_type value()
  172. {
  173. return execution::detail::occupancy_t<0>::
  174. query_static_constexpr_member<T>::value();
  175. }
  176. };
  177. #endif // !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  178. // || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  179. } // namespace traits
  180. #endif // defined(GENERATING_DOCUMENTATION)
  181. } // namespace asio
  182. #include "asio/detail/pop_options.hpp"
  183. #endif // ASIO_EXECUTION_OCCUPANCY_HPP