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 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 <__utility/auto_cast.h> 18 #include <concepts> 19 #include <cstddef> 20 #include <type_traits> 21 22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 23 # pragma GCC system_header 24 #endif 25 26 _LIBCPP_BEGIN_NAMESPACE_STD 27 28 #if _LIBCPP_STD_VER > 17 29 30 namespace ranges { 31 template<class> 32 inline constexpr bool disable_sized_range = false; 33 } // namespace ranges 34 35 // [range.prim.size] 36 37 namespace ranges { 38 namespace __size { 39 void size(auto&) = delete; 40 void size(const auto&) = delete; 41 42 template <class _Tp> 43 concept __size_enabled = !disable_sized_range<remove_cvref_t<_Tp>>; 44 45 template <class _Tp> 46 concept __member_size = 47 __size_enabled<_Tp> && 48 __workaround_52970<_Tp> && 49 requires(_Tp&& __t) { 50 { _LIBCPP_AUTO_CAST(__t.size()) } -> __integer_like; 51 }; 52 53 template <class _Tp> 54 concept __unqualified_size = 55 __size_enabled<_Tp> && 56 !__member_size<_Tp> && 57 __class_or_enum<remove_cvref_t<_Tp>> && 58 requires(_Tp&& __t) { 59 { _LIBCPP_AUTO_CAST(size(__t)) } -> __integer_like; 60 }; 61 62 template <class _Tp> 63 concept __difference = 64 !__member_size<_Tp> && 65 !__unqualified_size<_Tp> && 66 __class_or_enum<remove_cvref_t<_Tp>> && 67 requires(_Tp&& __t) { 68 { ranges::begin(__t) } -> forward_iterator; 69 { ranges::end(__t) } -> sized_sentinel_for<decltype(ranges::begin(declval<_Tp>()))>; 70 }; 71 72 struct __fn { 73 74 // `[range.prim.size]`: the array case (for rvalues). 75 template <class _Tp, size_t _Sz> 76 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&&)[_Sz]) const noexcept { 77 return _Sz; 78 } 79 80 // `[range.prim.size]`: the array case (for lvalues). 81 template <class _Tp, size_t _Sz> 82 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&)[_Sz]) const noexcept { 83 return _Sz; 84 } 85 86 // `[range.prim.size]`: `auto(t.size())` is a valid expression. 87 template <__member_size _Tp> 88 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const 89 noexcept(noexcept(_LIBCPP_AUTO_CAST(__t.size()))) { 90 return _LIBCPP_AUTO_CAST(__t.size()); 91 } 92 93 // `[range.prim.size]`: `auto(size(t))` is a valid expression. 94 template <__unqualified_size _Tp> 95 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const 96 noexcept(noexcept(_LIBCPP_AUTO_CAST(size(__t)))) { 97 return _LIBCPP_AUTO_CAST(size(__t)); 98 } 99 100 // [range.prim.size]: the `to-unsigned-like` case. 101 template <__difference _Tp> 102 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const 103 noexcept(noexcept(std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t)))) 104 -> decltype( std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t))) 105 { return std::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t)); 106 } 107 }; 108 109 } // namespace __size 110 111 inline namespace __cpo { 112 inline constexpr auto size = __size::__fn{}; 113 } // namespace __cpo 114 } // namespace ranges 115 116 // [range.prim.ssize] 117 118 namespace ranges { 119 namespace __ssize { 120 struct __fn { 121 template<class _Tp> 122 requires requires (_Tp&& __t) { ranges::size(__t); } 123 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr integral auto operator()(_Tp&& __t) const 124 noexcept(noexcept(ranges::size(__t))) { 125 using _Signed = make_signed_t<decltype(ranges::size(__t))>; 126 if constexpr (sizeof(ptrdiff_t) > sizeof(_Signed)) 127 return static_cast<ptrdiff_t>(ranges::size(__t)); 128 else 129 return static_cast<_Signed>(ranges::size(__t)); 130 } 131 }; 132 } // namespace __ssize 133 134 inline namespace __cpo { 135 inline constexpr auto ssize = __ssize::__fn{}; 136 } // namespace __cpo 137 } // namespace ranges 138 139 #endif // _LIBCPP_STD_VER > 17 140 141 _LIBCPP_END_NAMESPACE_STD 142 143 #endif // _LIBCPP___RANGES_SIZE_H 144