size.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 >= 20
  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 = __size_enabled<_Tp> && __workaround_52970<_Tp> && requires(_Tp&& __t) {
  42. { _LIBCPP_AUTO_CAST(__t.size()) } -> __integer_like;
  43. };
  44. template <class _Tp>
  45. concept __unqualified_size =
  46. __size_enabled<_Tp> && !__member_size<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
  47. { _LIBCPP_AUTO_CAST(size(__t)) } -> __integer_like;
  48. };
  49. template <class _Tp>
  50. concept __difference =
  51. !__member_size<_Tp> && !__unqualified_size<_Tp> && __class_or_enum<remove_cvref_t<_Tp>> && requires(_Tp&& __t) {
  52. { ranges::begin(__t) } -> forward_iterator;
  53. { ranges::end(__t) } -> sized_sentinel_for<decltype(ranges::begin(std::declval<_Tp>()))>;
  54. };
  55. struct __fn {
  56. // `[range.prim.size]`: the array case (for rvalues).
  57. template <class _Tp, size_t _Sz>
  58. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&&)[_Sz]) const noexcept {
  59. return _Sz;
  60. }
  61. // `[range.prim.size]`: the array case (for lvalues).
  62. template <class _Tp, size_t _Sz>
  63. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&)[_Sz]) const noexcept {
  64. return _Sz;
  65. }
  66. // `[range.prim.size]`: `auto(t.size())` is a valid expression.
  67. template <__member_size _Tp>
  68. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
  69. noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.size()))) {
  70. return _LIBCPP_AUTO_CAST(__t.size());
  71. }
  72. // `[range.prim.size]`: `auto(size(t))` is a valid expression.
  73. template <__unqualified_size _Tp>
  74. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
  75. noexcept(noexcept(_LIBCPP_AUTO_CAST(size(__t)))) {
  76. return _LIBCPP_AUTO_CAST(size(__t));
  77. }
  78. // [range.prim.size]: the `to-unsigned-like` case.
  79. template <__difference _Tp>
  80. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
  81. noexcept(noexcept(std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t))))
  82. -> decltype(std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t))) {
  83. return std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t));
  84. }
  85. };
  86. } // namespace __size
  87. inline namespace __cpo {
  88. inline constexpr auto size = __size::__fn{};
  89. } // namespace __cpo
  90. } // namespace ranges
  91. // [range.prim.ssize]
  92. namespace ranges {
  93. namespace __ssize {
  94. struct __fn {
  95. template <class _Tp>
  96. requires requires(_Tp&& __t) { ranges::size(__t); }
  97. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr integral auto operator()(_Tp&& __t) const
  98. noexcept(noexcept(ranges::size(__t))) {
  99. using _Signed = make_signed_t<decltype(ranges::size(__t))>;
  100. if constexpr (sizeof(ptrdiff_t) > sizeof(_Signed))
  101. return static_cast<ptrdiff_t>(ranges::size(__t));
  102. else
  103. return static_cast<_Signed>(ranges::size(__t));
  104. }
  105. };
  106. } // namespace __ssize
  107. inline namespace __cpo {
  108. inline constexpr auto ssize = __ssize::__fn{};
  109. } // namespace __cpo
  110. } // namespace ranges
  111. #endif // _LIBCPP_STD_VER >= 20
  112. _LIBCPP_END_NAMESPACE_STD
  113. #endif // _LIBCPP___RANGES_SIZE_H