xref: /openbsd-src/gnu/llvm/libcxx/include/__iterator/common_iterator.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
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_COMMON_ITERATOR_H
11 #define _LIBCPP___ITERATOR_COMMON_ITERATOR_H
12 
13 #include <__assert>
14 #include <__concepts/assignable.h>
15 #include <__concepts/constructible.h>
16 #include <__concepts/convertible_to.h>
17 #include <__concepts/copyable.h>
18 #include <__concepts/derived_from.h>
19 #include <__concepts/equality_comparable.h>
20 #include <__concepts/same_as.h>
21 #include <__config>
22 #include <__iterator/concepts.h>
23 #include <__iterator/incrementable_traits.h>
24 #include <__iterator/iter_move.h>
25 #include <__iterator/iter_swap.h>
26 #include <__iterator/iterator_traits.h>
27 #include <__iterator/readable_traits.h>
28 #include <__type_traits/is_pointer.h>
29 #include <__utility/declval.h>
30 #include <variant>
31 
32 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33 #  pragma GCC system_header
34 #endif
35 
36 _LIBCPP_BEGIN_NAMESPACE_STD
37 
38 #if _LIBCPP_STD_VER > 17
39 
40 template<class _Iter>
41 concept __can_use_postfix_proxy =
42   constructible_from<iter_value_t<_Iter>, iter_reference_t<_Iter>> &&
43   move_constructible<iter_value_t<_Iter>>;
44 
45 template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
46   requires (!same_as<_Iter, _Sent> && copyable<_Iter>)
47 class common_iterator {
48   struct __proxy {
49     constexpr const iter_value_t<_Iter>* operator->() const noexcept {
50       return _VSTD::addressof(__value_);
51     }
52     iter_value_t<_Iter> __value_;
53   };
54 
55   struct __postfix_proxy {
56     constexpr const iter_value_t<_Iter>& operator*() const noexcept {
57       return __value_;
58     }
59     iter_value_t<_Iter> __value_;
60   };
61 
62 public:
63   variant<_Iter, _Sent> __hold_;
64 
65   common_iterator() requires default_initializable<_Iter> = default;
66 
common_iterator(_Iter __i)67   constexpr common_iterator(_Iter __i) : __hold_(in_place_type<_Iter>, _VSTD::move(__i)) {}
common_iterator(_Sent __s)68   constexpr common_iterator(_Sent __s) : __hold_(in_place_type<_Sent>, _VSTD::move(__s)) {}
69 
70   template<class _I2, class _S2>
71     requires convertible_to<const _I2&, _Iter> && convertible_to<const _S2&, _Sent>
common_iterator(const common_iterator<_I2,_S2> & __other)72   constexpr common_iterator(const common_iterator<_I2, _S2>& __other)
73     : __hold_([&]() -> variant<_Iter, _Sent> {
74       _LIBCPP_ASSERT(!__other.__hold_.valueless_by_exception(), "Attempted to construct from a valueless common_iterator");
75       if (__other.__hold_.index() == 0)
76         return variant<_Iter, _Sent>{in_place_index<0>, _VSTD::__unchecked_get<0>(__other.__hold_)};
77       return variant<_Iter, _Sent>{in_place_index<1>, _VSTD::__unchecked_get<1>(__other.__hold_)};
78     }()) {}
79 
80   template<class _I2, class _S2>
81     requires convertible_to<const _I2&, _Iter> && convertible_to<const _S2&, _Sent> &&
82              assignable_from<_Iter&, const _I2&> && assignable_from<_Sent&, const _S2&>
83   common_iterator& operator=(const common_iterator<_I2, _S2>& __other) {
84     _LIBCPP_ASSERT(!__other.__hold_.valueless_by_exception(), "Attempted to assign from a valueless common_iterator");
85 
86     auto __idx = __hold_.index();
87     auto __other_idx = __other.__hold_.index();
88 
89     // If they're the same index, just assign.
90     if (__idx == 0 && __other_idx == 0)
91       _VSTD::__unchecked_get<0>(__hold_) = _VSTD::__unchecked_get<0>(__other.__hold_);
92     else if (__idx == 1 && __other_idx == 1)
93       _VSTD::__unchecked_get<1>(__hold_) = _VSTD::__unchecked_get<1>(__other.__hold_);
94 
95     // Otherwise replace with the oposite element.
96     else if (__other_idx == 1)
97       __hold_.template emplace<1>(_VSTD::__unchecked_get<1>(__other.__hold_));
98     else if (__other_idx == 0)
99       __hold_.template emplace<0>(_VSTD::__unchecked_get<0>(__other.__hold_));
100 
101     return *this;
102   }
103 
decltype(auto)104   constexpr decltype(auto) operator*()
105   {
106     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__hold_), "Attempted to dereference a non-dereferenceable common_iterator");
107     return *_VSTD::__unchecked_get<_Iter>(__hold_);
108   }
109 
decltype(auto)110   constexpr decltype(auto) operator*() const
111     requires __dereferenceable<const _Iter>
112   {
113     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__hold_), "Attempted to dereference a non-dereferenceable common_iterator");
114     return *_VSTD::__unchecked_get<_Iter>(__hold_);
115   }
116 
117   template<class _I2 = _Iter>
118   decltype(auto) operator->() const
119     requires indirectly_readable<const _I2> &&
120     (requires(const _I2& __i) { __i.operator->(); } ||
121      is_reference_v<iter_reference_t<_I2>> ||
122      constructible_from<iter_value_t<_I2>, iter_reference_t<_I2>>)
123   {
124     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__hold_), "Attempted to dereference a non-dereferenceable common_iterator");
125     if constexpr (is_pointer_v<_Iter> || requires(const _Iter& __i) { __i.operator->(); })    {
126       return _VSTD::__unchecked_get<_Iter>(__hold_);
127     } else if constexpr (is_reference_v<iter_reference_t<_Iter>>) {
128       auto&& __tmp = *_VSTD::__unchecked_get<_Iter>(__hold_);
129       return _VSTD::addressof(__tmp);
130     } else {
131       return __proxy{*_VSTD::__unchecked_get<_Iter>(__hold_)};
132     }
133   }
134 
135   common_iterator& operator++() {
136     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__hold_), "Attempted to increment a non-dereferenceable common_iterator");
137     ++_VSTD::__unchecked_get<_Iter>(__hold_); return *this;
138   }
139 
140   decltype(auto) operator++(int) {
141     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__hold_), "Attempted to increment a non-dereferenceable common_iterator");
142     if constexpr (forward_iterator<_Iter>) {
143       auto __tmp = *this;
144       ++*this;
145       return __tmp;
146     } else if constexpr (requires (_Iter& __i) { { *__i++ } -> __can_reference; } ||
147                          !__can_use_postfix_proxy<_Iter>) {
148       return _VSTD::__unchecked_get<_Iter>(__hold_)++;
149     } else {
150       auto __p = __postfix_proxy{**this};
151       ++*this;
152       return __p;
153     }
154   }
155 
156   template<class _I2, sentinel_for<_Iter> _S2>
157     requires sentinel_for<_Sent, _I2>
158   _LIBCPP_HIDE_FROM_ABI
159   friend constexpr bool operator==(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) {
160     _LIBCPP_ASSERT(!__x.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator");
161     _LIBCPP_ASSERT(!__y.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator");
162 
163     auto __x_index = __x.__hold_.index();
164     auto __y_index = __y.__hold_.index();
165 
166     if (__x_index == __y_index)
167       return true;
168 
169     if (__x_index == 0)
170       return _VSTD::__unchecked_get<_Iter>(__x.__hold_) == _VSTD::__unchecked_get<_S2>(__y.__hold_);
171 
172     return _VSTD::__unchecked_get<_Sent>(__x.__hold_) == _VSTD::__unchecked_get<_I2>(__y.__hold_);
173   }
174 
175   template<class _I2, sentinel_for<_Iter> _S2>
176     requires sentinel_for<_Sent, _I2> && equality_comparable_with<_Iter, _I2>
177   _LIBCPP_HIDE_FROM_ABI
178   friend constexpr bool operator==(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) {
179     _LIBCPP_ASSERT(!__x.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator");
180     _LIBCPP_ASSERT(!__y.__hold_.valueless_by_exception(), "Attempted to compare a valueless common_iterator");
181 
182     auto __x_index = __x.__hold_.index();
183     auto __y_index = __y.__hold_.index();
184 
185     if (__x_index == 1 && __y_index == 1)
186       return true;
187 
188     if (__x_index == 0 && __y_index == 0)
189       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) ==  _VSTD::__unchecked_get<_I2>(__y.__hold_);
190 
191     if (__x_index == 0)
192       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) == _VSTD::__unchecked_get<_S2>(__y.__hold_);
193 
194     return _VSTD::__unchecked_get<_Sent>(__x.__hold_) ==  _VSTD::__unchecked_get<_I2>(__y.__hold_);
195   }
196 
197   template<sized_sentinel_for<_Iter> _I2, sized_sentinel_for<_Iter> _S2>
198     requires sized_sentinel_for<_Sent, _I2>
199   _LIBCPP_HIDE_FROM_ABI
200   friend constexpr iter_difference_t<_I2> operator-(const common_iterator& __x, const common_iterator<_I2, _S2>& __y) {
201     _LIBCPP_ASSERT(!__x.__hold_.valueless_by_exception(), "Attempted to subtract from a valueless common_iterator");
202     _LIBCPP_ASSERT(!__y.__hold_.valueless_by_exception(), "Attempted to subtract a valueless common_iterator");
203 
204     auto __x_index = __x.__hold_.index();
205     auto __y_index = __y.__hold_.index();
206 
207     if (__x_index == 1 && __y_index == 1)
208       return 0;
209 
210     if (__x_index == 0 && __y_index == 0)
211       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) - _VSTD::__unchecked_get<_I2>(__y.__hold_);
212 
213     if (__x_index == 0)
214       return  _VSTD::__unchecked_get<_Iter>(__x.__hold_) - _VSTD::__unchecked_get<_S2>(__y.__hold_);
215 
216     return _VSTD::__unchecked_get<_Sent>(__x.__hold_) - _VSTD::__unchecked_get<_I2>(__y.__hold_);
217   }
218 
iter_move(const common_iterator & __i)219   _LIBCPP_HIDE_FROM_ABI friend constexpr iter_rvalue_reference_t<_Iter> iter_move(const common_iterator& __i)
220     noexcept(noexcept(ranges::iter_move(std::declval<const _Iter&>())))
221       requires input_iterator<_Iter>
222   {
223     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__i.__hold_), "Attempted to iter_move a non-dereferenceable common_iterator");
224     return ranges::iter_move( _VSTD::__unchecked_get<_Iter>(__i.__hold_));
225   }
226 
227   template<indirectly_swappable<_Iter> _I2, class _S2>
iter_swap(const common_iterator & __x,const common_iterator<_I2,_S2> & __y)228   _LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(const common_iterator& __x, const common_iterator<_I2, _S2>& __y)
229       noexcept(noexcept(ranges::iter_swap(std::declval<const _Iter&>(), std::declval<const _I2&>())))
230   {
231     _LIBCPP_ASSERT(std::holds_alternative<_Iter>(__x.__hold_), "Attempted to iter_swap a non-dereferenceable common_iterator");
232     _LIBCPP_ASSERT(std::holds_alternative<_I2>(__y.__hold_), "Attempted to iter_swap a non-dereferenceable common_iterator");
233     return ranges::iter_swap(_VSTD::__unchecked_get<_Iter>(__x.__hold_), _VSTD::__unchecked_get<_I2>(__y.__hold_));
234   }
235 };
236 
237 template<class _Iter, class _Sent>
238 struct incrementable_traits<common_iterator<_Iter, _Sent>> {
239   using difference_type = iter_difference_t<_Iter>;
240 };
241 
242 template<class _Iter>
243 concept __denotes_forward_iter =
244   requires { typename iterator_traits<_Iter>::iterator_category; } &&
245   derived_from<typename iterator_traits<_Iter>::iterator_category, forward_iterator_tag>;
246 
247 template<class _Iter, class _Sent>
248 concept __common_iter_has_ptr_op = requires(const common_iterator<_Iter, _Sent>& __a) {
249   __a.operator->();
250 };
251 
252 template<class, class>
253 struct __arrow_type_or_void {
254     using type = void;
255 };
256 
257 template<class _Iter, class _Sent>
258   requires __common_iter_has_ptr_op<_Iter, _Sent>
259 struct __arrow_type_or_void<_Iter, _Sent> {
260     using type = decltype(std::declval<const common_iterator<_Iter, _Sent>&>().operator->());
261 };
262 
263 template<input_iterator _Iter, class _Sent>
264 struct iterator_traits<common_iterator<_Iter, _Sent>> {
265   using iterator_concept = _If<forward_iterator<_Iter>,
266                                forward_iterator_tag,
267                                input_iterator_tag>;
268   using iterator_category = _If<__denotes_forward_iter<_Iter>,
269                                 forward_iterator_tag,
270                                 input_iterator_tag>;
271   using pointer = typename __arrow_type_or_void<_Iter, _Sent>::type;
272   using value_type = iter_value_t<_Iter>;
273   using difference_type = iter_difference_t<_Iter>;
274   using reference = iter_reference_t<_Iter>;
275 };
276 
277 #endif // _LIBCPP_STD_VER > 17
278 
279 _LIBCPP_END_NAMESPACE_STD
280 
281 #endif // _LIBCPP___ITERATOR_COMMON_ITERATOR_H
282