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