occupancy.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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/is_applicable_property.hpp"
  19. #include "asio/traits/query_static_constexpr_member.hpp"
  20. #include "asio/traits/static_query.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. #if defined(GENERATING_DOCUMENTATION)
  24. namespace execution {
  25. /// A property that gives an estimate of the number of execution agents that
  26. /// should occupy the associated execution context.
  27. struct occupancy_t
  28. {
  29. /// The occupancy_t property applies to executors.
  30. template <typename T>
  31. static constexpr bool is_applicable_property_v = is_executor_v<T>;
  32. /// The occupancy_t property cannot be required.
  33. static constexpr bool is_requirable = false;
  34. /// The occupancy_t property cannot be preferred.
  35. static constexpr bool is_preferable = false;
  36. /// The type returned by queries against an @c any_executor.
  37. typedef std::size_t polymorphic_query_result_type;
  38. };
  39. /// A special value used for accessing the occupancy_t property.
  40. constexpr occupancy_t occupancy;
  41. } // namespace execution
  42. #else // defined(GENERATING_DOCUMENTATION)
  43. namespace execution {
  44. namespace detail {
  45. template <int I = 0>
  46. struct occupancy_t
  47. {
  48. #if defined(ASIO_HAS_VARIABLE_TEMPLATES)
  49. template <typename T>
  50. static constexpr bool is_applicable_property_v = is_executor<T>::value;
  51. #endif // defined(ASIO_HAS_VARIABLE_TEMPLATES)
  52. static constexpr bool is_requirable = false;
  53. static constexpr bool is_preferable = false;
  54. typedef std::size_t polymorphic_query_result_type;
  55. constexpr occupancy_t()
  56. {
  57. }
  58. template <typename T>
  59. struct static_proxy
  60. {
  61. #if defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  62. struct type
  63. {
  64. template <typename P>
  65. static constexpr auto query(P&& p)
  66. noexcept(
  67. noexcept(
  68. conditional_t<true, T, P>::query(static_cast<P&&>(p))
  69. )
  70. )
  71. -> decltype(
  72. conditional_t<true, T, P>::query(static_cast<P&&>(p))
  73. )
  74. {
  75. return T::query(static_cast<P&&>(p));
  76. }
  77. };
  78. #else // defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  79. typedef T type;
  80. #endif // defined(ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  81. };
  82. template <typename T>
  83. struct query_static_constexpr_member :
  84. traits::query_static_constexpr_member<
  85. typename static_proxy<T>::type, occupancy_t> {};
  86. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  87. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  88. template <typename T>
  89. static constexpr typename query_static_constexpr_member<T>::result_type
  90. static_query()
  91. noexcept(query_static_constexpr_member<T>::is_noexcept)
  92. {
  93. return query_static_constexpr_member<T>::value();
  94. }
  95. template <typename E, typename T = decltype(occupancy_t::static_query<E>())>
  96. static constexpr const T static_query_v = occupancy_t::static_query<E>();
  97. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  98. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  99. };
  100. #if defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  101. && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  102. template <int I> template <typename E, typename T>
  103. const T occupancy_t<I>::static_query_v;
  104. #endif // defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  105. // && defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  106. } // namespace detail
  107. typedef detail::occupancy_t<> occupancy_t;
  108. constexpr occupancy_t occupancy;
  109. } // namespace execution
  110. #if !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  111. template <typename T>
  112. struct is_applicable_property<T, execution::occupancy_t>
  113. : integral_constant<bool, execution::is_executor<T>::value>
  114. {
  115. };
  116. #endif // !defined(ASIO_HAS_VARIABLE_TEMPLATES)
  117. namespace traits {
  118. #if !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT) \
  119. || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  120. template <typename T>
  121. struct static_query<T, execution::occupancy_t,
  122. enable_if_t<
  123. execution::detail::occupancy_t<0>::
  124. query_static_constexpr_member<T>::is_valid
  125. >>
  126. {
  127. static constexpr bool is_valid = true;
  128. static constexpr bool is_noexcept = true;
  129. typedef typename execution::detail::occupancy_t<0>::
  130. query_static_constexpr_member<T>::result_type result_type;
  131. static constexpr result_type value()
  132. {
  133. return execution::detail::occupancy_t<0>::
  134. query_static_constexpr_member<T>::value();
  135. }
  136. };
  137. #endif // !defined(ASIO_HAS_DEDUCED_STATIC_QUERY_TRAIT)
  138. // || !defined(ASIO_HAS_SFINAE_VARIABLE_TEMPLATES)
  139. } // namespace traits
  140. #endif // defined(GENERATING_DOCUMENTATION)
  141. } // namespace asio
  142. #include "asio/detail/pop_options.hpp"
  143. #endif // ASIO_EXECUTION_OCCUPANCY_HPP