occupancy.hpp 7.0 KB

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