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