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 _LIBCPP_WARN_UNUSED __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_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_CONSTEXPR_SINCE_CXX14 __wrap_iter(const __wrap_iter<_OtherIter>& __u) _NOEXCEPT : __i_(__u.__i_) {}
57 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT { return *__i_; }
58 _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT { return std::__to_address(__i_); }
59 _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator++() _NOEXCEPT {
60 ++__i_;
61 return *this;
62 }
63 _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator++(int) _NOEXCEPT {
64 __wrap_iter __tmp(*this);
65 ++(*this);
66 return __tmp;
67 }
68
69 _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator--() _NOEXCEPT {
70 --__i_;
71 return *this;
72 }
73 _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator--(int) _NOEXCEPT {
74 __wrap_iter __tmp(*this);
75 --(*this);
76 return __tmp;
77 }
78 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator+(difference_type __n) const _NOEXCEPT {
79 __wrap_iter __w(*this);
80 __w += __n;
81 return __w;
82 }
83 _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator+=(difference_type __n) _NOEXCEPT {
84 __i_ += __n;
85 return *this;
86 }
87 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter operator-(difference_type __n) const _NOEXCEPT {
88 return *this + (-__n);
89 }
90 _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter& operator-=(difference_type __n) _NOEXCEPT {
91 *this += -__n;
92 return *this;
93 }
94 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
95 return __i_[__n];
96 }
97
98private:
99 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __wrap_iter(_Iter __x) _NOEXCEPT : __i_(__x) {}
100
101 template <class _Up>
102 friend class __wrap_iter;
103 template <class _CharT, class _Traits, class _Alloc>
104 friend class basic_string;
105 template <class _CharT, class _Traits>
106 friend class basic_string_view;
107 template <class _Tp, class _Alloc>
108 friend class vector;
109 template <class _Tp, size_t>
110 friend class span;
111 template <class _Tp, size_t _Size>
112 friend struct array;
113
114#if _LIBCPP_STD_VER <= 17
115 friend _LIBCPP_CONSTEXPR bool operator==(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
116 return __x.__i_ == __y.__i_;
117 }
118
119 template <class _Iter2>
120 friend _LIBCPP_CONSTEXPR bool operator==(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
121 return __x.__i_ == __y.__i_;
122 }
123
124 friend _LIBCPP_CONSTEXPR bool operator<(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
125 return __x.__i_ < __y.__i_;
126 }
127
128 template <class _Iter2>
129 friend _LIBCPP_CONSTEXPR bool operator<(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
130 return __x.__i_ < __y.__i_;
131 }
132
133 friend _LIBCPP_CONSTEXPR bool operator!=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
134 return !(__x == __y);
135 }
136
137 template <class _Iter2>
138 friend _LIBCPP_CONSTEXPR bool operator!=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
139 return !(__x == __y);
140 }
141
142 friend _LIBCPP_CONSTEXPR bool operator>(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
143 return __y < __x;
144 }
145
146 template <class _Iter2>
147 friend _LIBCPP_CONSTEXPR bool operator>(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
148 return __y < __x;
149 }
150
151 friend _LIBCPP_CONSTEXPR bool operator>=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
152 return !(__x < __y);
153 }
154
155 template <class _Iter2>
156 friend _LIBCPP_CONSTEXPR bool operator>=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
157 return !(__x < __y);
158 }
159
160 friend _LIBCPP_CONSTEXPR bool operator<=(const __wrap_iter& __x, const __wrap_iter& __y) _NOEXCEPT {
161 return !(__y < __x);
162 }
163
164 template <class _Iter2>
165 friend _LIBCPP_CONSTEXPR bool operator<=(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT {
166 return !(__y < __x);
167 }
168
169#else
170 friend bool operator==(const __wrap_iter&, const __wrap_iter&) = default;
171
172 template <class _Iter2>
173 friend constexpr bool operator==(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) noexcept {
174 return __x.__i_ == __y.__i_;
175 }
176
177 template <class _Iter2>
178 friend constexpr strong_ordering operator<=>(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) noexcept {
179 if constexpr (three_way_comparable_with<_Iter, _Iter2, strong_ordering>) {
180 return __x.__i_ <=> __y.__i_;
181 } else {
182 if (__x.__i_ < __y.__i_)
183 return strong_ordering::less;
184
185 if (__x.__i_ == __y.__i_)
186 return strong_ordering::equal;
187
188 return strong_ordering::greater;
189 }
190 }
191#endif // _LIBCPP_STD_VER >= 20
192
193#ifndef _LIBCPP_CXX03_LANG
194 template <class _Iter2>
195 [[__nodiscard__]] friend _LIBCPP_CONSTEXPR_SINCE_CXX14 auto
196 operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT->decltype(__x.__i_ - __y.__i_)
197#else
198 template <class _Iter2>
199 [[__nodiscard__]] friend _LIBCPP_CONSTEXPR_SINCE_CXX14
200 typename __wrap_iter::difference_type operator-(const __wrap_iter& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
201#endif // C++03
202 {
203 return __x.__i_ - __y.__i_;
204 }
205
206 [[__nodiscard__]] friend _LIBCPP_CONSTEXPR_SINCE_CXX14 __wrap_iter
207 operator+(typename __wrap_iter::difference_type __n, __wrap_iter __x) _NOEXCEPT {
208 __x += __n;
209 return __x;
210 }
211};
212
213#if _LIBCPP_STD_VER <= 17
214template <class _It>
215struct __libcpp_is_contiguous_iterator<__wrap_iter<_It> > : true_type {};
216#endif
217
218_LIBCPP_END_NAMESPACE_STD
219
220#endif // _LIBCPP___ITERATOR_WRAP_ITER_H
221