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