occupancy.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // execution/occupancy.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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. template <typename T>
  54. BOOST_ASIO_STATIC_CONSTEXPR(bool,
  55. is_applicable_property_v = (
  56. is_executor<T>::value
  57. || conditional<
  58. is_executor<T>::value,
  59. false_type,
  60. is_sender<T>
  61. >::type::value
  62. || conditional<
  63. is_executor<T>::value,
  64. false_type,
  65. is_scheduler<T>
  66. >::type::value));
  67. #endif // defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  68. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_requirable = false);
  69. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_preferable = false);
  70. typedef std::size_t polymorphic_query_result_type;
  71. BOOST_ASIO_CONSTEXPR occupancy_t()
  72. {
  73. }
  74. template <typename T>
  75. struct static_proxy
  76. {
  77. #if defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  78. struct type
  79. {
  80. template <typename P>
  81. static constexpr auto query(BOOST_ASIO_MOVE_ARG(P) p)
  82. noexcept(
  83. noexcept(
  84. conditional<true, T, P>::type::query(BOOST_ASIO_MOVE_CAST(P)(p))
  85. )
  86. )
  87. -> decltype(
  88. conditional<true, T, P>::type::query(BOOST_ASIO_MOVE_CAST(P)(p))
  89. )
  90. {
  91. return T::query(BOOST_ASIO_MOVE_CAST(P)(p));
  92. }
  93. };
  94. #else // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  95. typedef T type;
  96. #endif // defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  97. };
  98. template <typename T>
  99. struct query_static_constexpr_member :
  100. traits::query_static_constexpr_member<
  101. typename static_proxy<T>::type, occupancy_t> {};
  102. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  103. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  104. template <typename T>
  105. static BOOST_ASIO_CONSTEXPR
  106. typename query_static_constexpr_member<T>::result_type
  107. static_query()
  108. BOOST_ASIO_NOEXCEPT_IF((
  109. query_static_constexpr_member<T>::is_noexcept))
  110. {
  111. return query_static_constexpr_member<T>::value();
  112. }
  113. template <typename E, typename T = decltype(occupancy_t::static_query<E>())>
  114. static BOOST_ASIO_CONSTEXPR const T static_query_v
  115. = occupancy_t::static_query<E>();
  116. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  117. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  118. #if !defined(BOOST_ASIO_HAS_CONSTEXPR)
  119. static const occupancy_t instance;
  120. #endif // !defined(BOOST_ASIO_HAS_CONSTEXPR)
  121. };
  122. #if defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  123. && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  124. template <int I> template <typename E, typename T>
  125. const T occupancy_t<I>::static_query_v;
  126. #endif // defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  127. // && defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  128. #if !defined(BOOST_ASIO_HAS_CONSTEXPR)
  129. template <int I>
  130. const occupancy_t<I> occupancy_t<I>::instance;
  131. #endif
  132. } // namespace detail
  133. typedef detail::occupancy_t<> occupancy_t;
  134. #if defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  135. constexpr occupancy_t occupancy;
  136. #else // defined(BOOST_ASIO_HAS_CONSTEXPR) || defined(GENERATING_DOCUMENTATION)
  137. namespace { static const occupancy_t& occupancy = occupancy_t::instance; }
  138. #endif
  139. } // namespace execution
  140. #if !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  141. template <typename T>
  142. struct is_applicable_property<T, execution::occupancy_t>
  143. : integral_constant<bool,
  144. execution::is_executor<T>::value
  145. || conditional<
  146. execution::is_executor<T>::value,
  147. false_type,
  148. execution::is_sender<T>
  149. >::type::value
  150. || conditional<
  151. execution::is_executor<T>::value,
  152. false_type,
  153. execution::is_scheduler<T>
  154. >::type::value>
  155. {
  156. };
  157. #endif // !defined(BOOST_ASIO_HAS_VARIABLE_TEMPLATES)
  158. namespace traits {
  159. #if !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  160. || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  161. template <typename T>
  162. struct static_query<T, execution::occupancy_t,
  163. typename enable_if<
  164. execution::detail::occupancy_t<0>::
  165. query_static_constexpr_member<T>::is_valid
  166. >::type>
  167. {
  168. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  169. BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  170. typedef typename execution::detail::occupancy_t<0>::
  171. query_static_constexpr_member<T>::result_type result_type;
  172. static BOOST_ASIO_CONSTEXPR result_type value()
  173. {
  174. return execution::detail::occupancy_t<0>::
  175. query_static_constexpr_member<T>::value();
  176. }
  177. };
  178. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  179. // || !defined(BOOST_ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  180. } // namespace traits
  181. #endif // defined(GENERATING_DOCUMENTATION)
  182. } // namespace asio
  183. } // namespace boost
  184. #include <boost/asio/detail/pop_options.hpp>
  185. #endif // BOOST_ASIO_EXECUTION_OCCUPANCY_HPP