size.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___RANGES_SIZE_H
  10. #define _LIBCPP___RANGES_SIZE_H
  11. #include <__concepts/arithmetic.h>
  12. #include <__concepts/class_or_enum.h>
  13. #include <__config>
  14. #include <__iterator/concepts.h>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__ranges/access.h>
  17. #include <__type_traits/decay.h>
  18. #include <__type_traits/make_signed.h>
  19. #include <__type_traits/make_unsigned.h>
  20. #include <__type_traits/remove_cvref.h>
  21. #include <__utility/auto_cast.h>
  22. #include <__utility/declval.h>
  23. #include <cstddef>
  24. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  25. # pragma GCC system_header
  26. #endif
  27. _LIBCPP_BEGIN_NAMESPACE_STD
  28. #if _LIBCPP_STD_VER > 17
  29. namespace ranges {
  30. template<class>
  31. inline constexpr bool disable_sized_range = false;
  32. } // namespace ranges
  33. // [range.prim.size]
  34. namespace ranges {
  35. namespace __size {
  36. void size(auto&) = delete;
  37. void size(const auto&) = delete;
  38. template <class _Tp>
  39. concept __size_enabled = !disable_sized_range<remove_cvref_t<_Tp>>;
  40. template <class _Tp>
  41. concept __member_size =
  42. __size_enabled<_Tp> &&
  43. __workaround_52970<_Tp> &&
  44. requires(_Tp&& __t) {
  45. { _LIBCPP_AUTO_CAST(__t.size()) } -> __integer_like;
  46. };
  47. template <class _Tp>
  48. concept __unqualified_size =
  49. __size_enabled<_Tp> &&
  50. !__member_size<_Tp> &&
  51. __class_or_enum<remove_cvref_t<_Tp>> &&
  52. requires(_Tp&& __t) {
  53. { _LIBCPP_AUTO_CAST(size(__t)) } -> __integer_like;
  54. };
  55. template <class _Tp>
  56. concept __difference =
  57. !__member_size<_Tp> &&
  58. !__unqualified_size<_Tp> &&
  59. __class_or_enum<remove_cvref_t<_Tp>> &&
  60. requires(_Tp&& __t) {
  61. { ranges::begin(__t) } -> forward_iterator;
  62. { ranges::end(__t) } -> sized_sentinel_for<decltype(ranges::begin(std::declval<_Tp>()))>;
  63. };
  64. struct __fn {
  65. // `[range.prim.size]`: the array case (for rvalues).
  66. template <class _Tp, size_t _Sz>
  67. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&&)[_Sz]) const noexcept {
  68. return _Sz;
  69. }
  70. // `[range.prim.size]`: the array case (for lvalues).
  71. template <class _Tp, size_t _Sz>
  72. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&)[_Sz]) const noexcept {
  73. return _Sz;
  74. }
  75. // `[range.prim.size]`: `auto(t.size())` is a valid expression.
  76. template <__member_size _Tp>
  77. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
  78. noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.size()))) {
  79. return _LIBCPP_AUTO_CAST(__t.size());
  80. }
  81. // `[range.prim.size]`: `auto(size(t))` is a valid expression.
  82. template <__unqualified_size _Tp>
  83. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
  84. noexcept(noexcept(_LIBCPP_AUTO_CAST(size(__t)))) {
  85. return _LIBCPP_AUTO_CAST(size(__t));
  86. }
  87. // [range.prim.size]: the `to-unsigned-like` case.
  88. template <__difference _Tp>
  89. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  90. noexcept(noexcept(std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t))))
  91. -> decltype( std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t)))
  92. { return std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t));
  93. }
  94. };
  95. } // namespace __size
  96. inline namespace __cpo {
  97. inline constexpr auto size = __size::__fn{};
  98. } // namespace __cpo
  99. } // namespace ranges
  100. // [range.prim.ssize]
  101. namespace ranges {
  102. namespace __ssize {
  103. struct __fn {
  104. template<class _Tp>
  105. requires requires (_Tp&& __t) { ranges::size(__t); }
  106. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr integral auto operator()(_Tp&& __t) const
  107. noexcept(noexcept(ranges::size(__t))) {
  108. using _Signed = make_signed_t<decltype(ranges::size(__t))>;
  109. if constexpr (sizeof(ptrdiff_t) > sizeof(_Signed))
  110. return static_cast<ptrdiff_t>(ranges::size(__t));
  111. else
  112. return static_cast<_Signed>(ranges::size(__t));
  113. }
  114. };
  115. } // namespace __ssize
  116. inline namespace __cpo {
  117. inline constexpr auto ssize = __ssize::__fn{};
  118. } // namespace __cpo
  119. } // namespace ranges
  120. #endif // _LIBCPP_STD_VER > 17
  121. _LIBCPP_END_NAMESPACE_STD
  122. #endif // _LIBCPP___RANGES_SIZE_H