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___ITERATOR_WRAP_ITER_H
11#define _LIBCPP___ITERATOR_WRAP_ITER_H
12
13#include <__compare/ordering.h>
14#include <__compare/three_way_comparable.h>
15#include <__config>
16#include <__cstddef/size_t.h>
17#include <__iterator/iterator_traits.h>
18#include <__memory/pointer_traits.h>
19#include <__type_traits/conjunction.h>
20#include <__type_traits/disjunction.h>
21#include <__type_traits/enable_if.h>
22#include <__type_traits/integral_constant.h>
23#include <__type_traits/is_convertible.h>
24#include <__type_traits/is_same.h>
25#include <__type_traits/make_const_lvalue_ref.h>
26
27#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28# pragma GCC system_header
29#endif
30
31_LIBCPP_BEGIN_NAMESPACE_STD
32
33template <class _Iter>
34class __wrap_iter {
35public:
36 typedef typename iterator_traits<_Iter>::value_type value_type;
37 typedef typename iterator_traits<_Iter>::difference_type difference_type;
38 typedef typename iterator_traits<_Iter>::pointer pointer;
39 typedef typename iterator_traits<_Iter>::reference reference;
40 typedef typename iterator_traits<_Iter>::iterator_category iterator_category;
41#if _LIBCPP_STD_VER >= 20
42 typedef contiguous_iterator_tag iterator_concept;
43#endif
44
45private:
46 _Iter __i_;
47
48public:
49 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter() _NOEXCEPT : __i_() {}
50 template <class _OtherIter,
51 __enable_if_t<
52 _And<is_convertible<const _OtherIter&, _Iter>,
53 _Or<is_same<reference, __iterator_reference<_OtherIter> >,
54 is_same<reference, __make_const_lvalue_ref<__iterator_reference<_OtherIter> > > > >::value,
55 int> = 0>
56 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT
57 : __i_(__u.__i_) {}
58 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
59 return *__i_;
60 }
61 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
62 return std::__to_address(__i_);
63 }
64 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++() _NOEXCEPT {
65 ++__i_;
66 return *this;
67 }
68 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator++(int) _NOEXCEPT {
69 __wrap_iter __tmp(*this);
70 ++(*this);
71 return __tmp;
72 }
73
74 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator--() _NOEXCEPT {
75 --__i_;
76 return *this;
77 }
78 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator--(int) _NOEXCEPT {
79 __wrap_iter __tmp(*this);
80 --(*this);
81 return __tmp;
82 }
83 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
84 operator+(difference_type __n) const _NOEXCEPT {
85 __wrap_iter __w(*this);
86 __w += __n;
87 return __w;
88 }
89 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT {
90 __i_ += __n;
91 return *this;
92 }
93 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
94 operator-(difference_type __n) const _NOEXCEPT {
95 return *this + (-__n);
96 }
97 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT {
98 *this += -__n;
99 return *this;
100 }
101 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference
102 operator[](difference_type __n) const _NOEXCEPT {
103 return __i_[__n];
104 }
105
106private:
107 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __wrap_iter(_Iter __x) _NOEXCEPT : __i_(__x) {}
108
109 template <class _Up>
110 friend class __wrap_iter;
111 template <class _CharT, class _Traits, class _Alloc>
112 friend class basic_string;
113 template <class _CharT, class _Traits>
114 friend class basic_string_view;
115 template <class _Tp, class _Alloc>
116 friend class vector;
117 template <class _Tp, size_t>
118 friend class span;
119 template <class _Tp, size_t _Size>
120 friend struct array;
121
122#if _LIBCPP_STD_VER <= 17
123 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
124 operator==(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
125 return __x.__i_ == __y.__i_;
126 }
127
128 template <class _Iter2>
129 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
130 operator==(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
131 return __x.__i_ == __y.__i_;
132 }
133
134 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
135 operator<(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
136 return __x.__i_ < __y.__i_;
137 }
138
139 template <class _Iter2>
140 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
141 operator<(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
142 return __x.__i_ < __y.__i_;
143 }
144
145 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
146 operator!=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
147 return !(__x == __y);
148 }
149
150 template <class _Iter2>
151 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
152 operator!=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
153 return !(__x == __y);
154 }
155
156 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
157 operator>(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
158 return __y < __x;
159 }
160
161 template <class _Iter2>
162 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
163 operator>(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
164 return __y < __x;
165 }
166
167 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
168 operator>=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
169 return !(__x < __y);
170 }
171
172 template <class _Iter2>
173 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
174 operator>=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
175 return !(__x < __y);
176 }
177
178 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
179 operator<=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
180 return !(__y < __x);
181 }
182
183 template <class _Iter2>
184 _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR bool
185 operator<=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
186 return !(__y < __x);
187 }
188
189#else
190 _LIBCPP_HIDE_FROM_ABI friend bool operator==(const __wrap_iter&, const __wrap_iter&) = default;
191
192 template <class _Iter2>
193 _LIBCPP_HIDE_FROM_ABI friend constexpr bool
194 operator==(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) noexcept {
195 return __x.__i_ == __y.__i_;
196 }
197
198 template <class _Iter2>
199 _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering
200 operator<=>(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) noexcept {
201 if constexpr (three_way_comparable_with<_Iter, _Iter2, strong_ordering>) {
202 return __x.__i_ <=> __y.__i_;
203 } else {
204 if (__x.__i_ < __y.__i_)
205 return strong_ordering::less;
206
207 if (__x.__i_ == __y.__i_)
208 return strong_ordering::equal;
209
210 return strong_ordering::greater;
211 }
212 }
213#endif // _LIBCPP_STD_VER >= 20
214
215#ifndef _LIBCPP_CXX03_LANG
216 template <class _Iter2>
217 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 auto
218 operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT->decltype(__x.__i_ - __y.__i_)
219#else
220 template <class _Iter2>
221 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14
222 typename __wrap_iter::difference_type operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
223#endif // C++03
224 {
225 return __x.__i_ - __y.__i_;
226 }
227
228 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI friend _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
229 operator+(typename __wrap_iter::difference_type __n, __wrap_iter __x) _NOEXCEPT {
230 __x += __n;
231 return __x;
232 }
233};
234
235#if _LIBCPP_STD_VER <= 17
236template <class _It>
237struct __libcpp_is_contiguous_iterator<__wrap_iter<_It> > : true_type {};
238#endif
239
240_LIBCPP_END_NAMESPACE_STD
241
242#endif // _LIBCPP___ITERATOR_WRAP_ITER_H
243