Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
move_iterator.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MOVE_ITERATOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MOVE_ITERATOR_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
11
12// IWYU pragma: private, include <iterator>
13// IWYU pragma: friend "stdlib_detail/.*"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/constraints.hpp"
17#include "arene/base/iterator.hpp"
18#include "arene/base/type_traits/iterator_category_traits.hpp"
19#include "stdlib/include/stdlib_detail/conditional.hpp"
20#include "stdlib/include/stdlib_detail/enable_if.hpp"
21#include "stdlib/include/stdlib_detail/is_assignable.hpp"
22#include "stdlib/include/stdlib_detail/is_copy_constructible.hpp"
23#include "stdlib/include/stdlib_detail/is_default_constructible.hpp"
24#include "stdlib/include/stdlib_detail/is_reference.hpp"
25#include "stdlib/include/stdlib_detail/iterator_traits.hpp"
26#include "stdlib/include/stdlib_detail/remove_reference.hpp"
27// parasoft-end-suppress AUTOSAR-A16_2_2-a
28
29namespace std {
30// parasoft-begin-suppress AUTOSAR-A13_5_1-a "This is implemented as specified by the C++ Standard"
31/// @brief An iterator adaptor that wraps another iterator such that dereferencing the wrapped iterator yields an rvalue
32/// reference to the source value
33/// @tparam Iterator the type of the underlying iterator
34template <typename Iterator>
36 static_assert(
38 "The underlying iterator must be at least an input iterator"
39 );
40
41 /// @brief The stored value of the underlying iterator
42 Iterator current_;
43
44 public:
45 // parasoft-begin-suppress AUTOSAR-M2_10_1 "False positive: These declarations don't hide anything"
46 // parasoft-begin-suppress AUTOSAR-A2_10_1 "False positive: These declarations don't hide anything"
47 /// @brief The type of the underlying iterator
48 using iterator_type = Iterator;
49 /// @brief The difference type of the iterator
51 /// @brief iterator pointer type
52 using pointer = Iterator;
53 /// @brief iterator value type
55 /// @brief iterator category tag type
57 /// @brief iterator reference type
62 // parasoft-end-suppress AUTOSAR-A2_10_1
63 // parasoft-end-suppress AUTOSAR-M2_10_1
64
65 /// @brief Default construct the iterator, holding a default-constructed underlying iterator. Only available if @c
66 /// Iterator is default-constructible.
67 /// @tparam SfinaeIterator Dummy type to ensure underlying iterator is default-constructible
68 template <
69 typename SfinaeIterator = Iterator,
70 arene::base::constraints<enable_if_t<is_default_constructible_v<SfinaeIterator>>> = nullptr>
73
74 /// @brief Construct an iterator holding the specified value of the underlying iterator.
75 /// @param iter The iterator to store
76 explicit move_iterator(Iterator iter) noexcept(is_nothrow_copy_constructible_v<Iterator>)
77 : current_(iter) {}
78
79 /// @brief Default copy constructor
80 move_iterator(move_iterator const&) = default;
81 /// @brief Default move constructor
83 /// @brief Default copy assignment operator
84 auto operator=(move_iterator const&) -> move_iterator& = default;
85 /// @brief Default move assignment operator
86 auto operator=(move_iterator&&) -> move_iterator& = default;
87
88 /// @brief Default destructor
89 ~move_iterator() = default;
90
91 /// @brief Conversion constructor from another move iterator with a different type of underlying iterator. The stored
92 /// iterator is constructed from the underlying iterator of the source move iterator. Requires that the source
93 /// underlying iterator is convertible to @c Iterator
94 /// @tparam OtherIterator The type of the underlying iterator of the source iterator
95 /// @param source The source iterator
96 template <
97 typename OtherIterator,
98 arene::base::constraints<enable_if_t<is_convertible_v<OtherIterator, Iterator>>> = nullptr>
99 // NOLINTNEXTLINE(hicpp-explicit-conversions)
100 move_iterator(move_iterator<OtherIterator> const& source)
101 : current_(source.base()) {}
102
103 /// @brief Conversion assignment from another move iterator with a different type of underlying iterator. The stored
104 /// iterator is assigned from the underlying iterator of the source move iterator. Requires that the source
105 /// underlying iterator can be assigned to @c Iterator
106 /// @tparam OtherIterator The type of the underlying iterator of the source iterator
107 /// @param source The source iterator
108 /// @return A reference to @c *this
109 template <
110 typename OtherIterator,
111 arene::base::constraints<enable_if_t<is_convertible_v<OtherIterator, Iterator>>> = nullptr>
112 auto operator=(move_iterator<OtherIterator> const& source) -> move_iterator& {
113 current_ = source.base();
114 return *this;
115 }
116
117 /// @brief Retrieve the underlying iterator
118 /// @return A copy of the stored underlying iterator
119 auto base() const -> iterator_type { return current_; }
120
121 // parasoft-begin-suppress AUTOSAR-A5_2_2-a "False positive: No C style cast is used"
122 /// @brief Dereference the iterator
123 /// @return An rvalue reference to the referred to value, if the underlying iterator returns a reference, or a copy of
124 /// the referenced value otherwise
125 auto operator*() const -> reference { return static_cast<reference>(*current_); }
126 // parasoft-end-suppress AUTOSAR-A5_2_2-a
127
128 /// @brief Arrow dereference operator for accessing the referenced value
129 /// @return A copy of the stored underlying iterator
130 auto operator->() const -> pointer { return current_; }
131
132 /// @brief Preincrement operator. Increments the stored iterator
133 /// @return A reference to @c *this
134 auto operator++() -> move_iterator& {
135 arene::base::advance(current_, 1);
136 return *this;
137 }
138 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "False positive: postincrement requires an int parameter"
139 /// @brief Postincrement operator. Increments the stored iterator
140 /// @return A copy of @c *this prior to the increment
141 auto operator++(int) -> move_iterator {
142 move_iterator temp{*this};
143 arene::base::advance(current_, 1);
144 return temp;
145 }
146 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
147
148 /// @brief Predecrement operator. Decrements the stored iterator
149 /// @return A reference to @c *this
150 /// @pre @c Iterator must be at least a bidirectional iterator
151 auto operator--() -> move_iterator& {
152 static_assert(
153 arene::base::is_bidirectional_iterator_v<Iterator>,
154 "The underlying iterator must be at least a bidirectional iterator to decrement"
155 );
156 arene::base::advance(current_, -1);
157 return *this;
158 }
159 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "False positive: postdecrement requires an int parameter"
160 /// @brief Postdecrement operator. Decrements the stored iterator
161 /// @return A copy of @c *this prior to the decrement
162 /// @pre @c Iterator must be at least a bidirectional iterator
163 auto operator--(int) -> move_iterator {
164 static_assert(
165 arene::base::is_bidirectional_iterator_v<Iterator>,
166 "The underlying iterator must be at least a bidirectional iterator to decrement"
167 );
168 move_iterator temp{*this};
169 arene::base::advance(current_, -1);
170 return temp;
171 }
172 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
173
174 // parasoft-begin-suppress "AUTOSAR-M5_17_1-a" "Overloaded operators have consistent semantics"
175
176 /// @brief Obtain a new iterator referencing the value from moving the current iterator forwards by a specified amount
177 /// @param delta The amount to move
178 /// @return A new iterator referencing the new value
179 /// @pre @c Iterator must be a random access iterator
180 auto operator+(difference_type delta) const -> move_iterator {
181 static_assert(
182 arene::base::is_random_access_iterator_v<Iterator>,
183 "The underlying iterator must be at least a random access iterator for random access operations"
184 );
185 move_iterator temp{*this};
186 temp += delta;
187 return temp;
188 }
189
190 /// @brief Move the current iterator forwards by a specified amount
191 /// @param delta The amount to move
192 /// @return A reference to @c *this
193 /// @pre @c Iterator must be a random access iterator
194 auto operator+=(difference_type delta) -> move_iterator& {
195 static_assert(
196 arene::base::is_random_access_iterator_v<Iterator>,
197 "The underlying iterator must be at least a random access iterator for random access operations"
198 );
199 arene::base::advance(current_, delta);
200 return *this;
201 }
202
203 /// @brief Obtain a new iterator referencing the value from moving the current iterator backwards by a specified
204 /// amount
205 /// @param delta The amount to move
206 /// @return A new iterator referencing the new value
207 /// @pre @c Iterator must be a random access iterator
208 auto operator-(difference_type delta) const -> move_iterator {
209 static_assert(
210 arene::base::is_random_access_iterator_v<Iterator>,
211 "The underlying iterator must be at least a random access iterator for random access operations"
212 );
213 move_iterator temp{*this};
214 temp -= delta;
215 return temp;
216 }
217
218 /// @brief Move the current iterator backwards by a specified amount
219 /// @param delta The amount to move
220 /// @return A reference to @c *this
221 /// @pre @c Iterator must be a random access iterator
222 auto operator-=(difference_type delta) -> move_iterator& {
223 static_assert(
224 arene::base::is_random_access_iterator_v<Iterator>,
225 "The underlying iterator must be at least a random access iterator for random access operations"
226 );
227 arene::base::advance(current_, -delta);
228 return *this;
229 }
230
231 // parasoft-end-suppress "AUTOSAR-M5_17_1-a"
232
233 /// @brief Access the value at a specified offset from the current iterator
234 /// @param delta The offset to the new position
235 /// @return An rvalue reference to the specified value
236 /// @pre @c Iterator must be a random access iterator
237 auto operator[](difference_type delta) const -> reference {
238 static_assert(
239 arene::base::is_random_access_iterator_v<Iterator>,
240 "The underlying iterator must be at least a random access iterator for random access operations"
241 );
242 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "This is an iterator type, so indexing is OK"
243 return static_cast<reference>(current_[delta]);
244 // parasoft-end-suppress AUTOSAR-M5_0_15-a
245 }
246};
247// parasoft-end-suppress AUTOSAR-A13_5_1-a
248
249// parasoft-begin-suppress AUTOSAR-A13_5_5-b "This is implemented as specified by the C++ Standard"
250// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
251/// @brief Check if two iterators are equal
252/// @tparam Iterator1 The type of the first iterator
253/// @tparam Iterator2 The type of the second iterator
254/// @param lhs The first iterator
255/// @param rhs The second iterator
256/// @return The value @c true if the iterators are equal, and @c false otherwise
257template <typename Iterator1, typename Iterator2>
258auto operator==(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs) -> bool {
259 return lhs.base() == rhs.base();
260}
261// parasoft-end-suppress AUTOSAR-M3_3_2-a
262
263// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
264/// @brief Check if two iterators are not equal
265/// @tparam Iterator1 The type of the first iterator
266/// @tparam Iterator2 The type of the second iterator
267/// @param lhs The first iterator
268/// @param rhs The second iterator
269/// @return The value @c true if the iterators are not equal, and @c false otherwise
270template <typename Iterator1, typename Iterator2>
271auto operator!=(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs) -> bool {
272 return !(lhs == rhs);
273}
274// parasoft-end-suppress AUTOSAR-M3_3_2-a
275
276/// @brief Check if one iterator is less than another
277/// @tparam Iterator1 The type of the first iterator
278/// @tparam Iterator2 The type of the second iterator
279/// @param lhs The first iterator
280/// @param rhs The second iterator
281/// @return The value @c true if the first iterator is less than the second, and @c false otherwise
282template <typename Iterator1, typename Iterator2>
283auto operator<(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs) -> bool {
284 return lhs.base() < rhs.base();
285}
286
287/// @brief Check if one iterator is less than or equal to another
288/// @tparam Iterator1 The type of the first iterator
289/// @tparam Iterator2 The type of the second iterator
290/// @param lhs The first iterator
291/// @param rhs The second iterator
292/// @return The value @c true if the first iterator is less than or equal to the second, and @c false otherwise
293template <typename Iterator1, typename Iterator2>
294auto operator<=(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs) -> bool {
295 return !(rhs < lhs);
296}
297
298/// @brief Check if one iterator is greater than another
299/// @tparam Iterator1 The type of the first iterator
300/// @tparam Iterator2 The type of the second iterator
301/// @param lhs The first iterator
302/// @param rhs The second iterator
303/// @return The value @c true if the first iterator is greater than the second, and @c false otherwise
304template <typename Iterator1, typename Iterator2>
305auto operator>(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs) -> bool {
306 return rhs < lhs;
307}
308
309/// @brief Check if one iterator is greater than or equal to another
310/// @tparam Iterator1 The type of the first iterator
311/// @tparam Iterator2 The type of the second iterator
312/// @param lhs The first iterator
313/// @param rhs The second iterator
314/// @return The value @c true if the first iterator is greater than or equal to the second, and @c false otherwise
315template <typename Iterator1, typename Iterator2>
316auto operator>=(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs) -> bool {
317 return !(lhs < rhs);
318}
319
320// parasoft-end-suppress AUTOSAR-A13_5_5-b
321
322// parasoft-begin-suppress AUTOSAR-M5_0_15-a "This is an iterator type, so arithmetic is OK"
323/// @brief Obtain the distance between two iterators
324/// @tparam Iterator1 The type of the first iterator
325/// @tparam Iterator2 The type of the second iterator
326/// @param lhs The first iterator
327/// @param rhs The second iterator
328/// @return The difference between the iterators
329template <typename Iterator1, typename Iterator2>
330auto operator-(move_iterator<Iterator1> const& lhs, move_iterator<Iterator2> const& rhs)
331 -> decltype(lhs.base() - rhs.base()) {
332 return lhs.base() - rhs.base();
333}
334// parasoft-end-suppress AUTOSAR-M5_0_15-a
335
336/// @brief Obtain a new iterator referencing the value from moving the specified iterator forwards by a specified amount
337/// @param delta The amount to move
338/// @param iter The iterator to start from
339/// @return A new iterator referencing the new value
340/// @pre @c Iterator must be a random access iterator
341template <typename Iterator>
342auto operator+(typename move_iterator<Iterator>::difference_type delta, move_iterator<Iterator> const& iter)
343 -> move_iterator<Iterator> {
344 return iter + delta;
345}
346
347// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
348/// @brief Create a @c move_iterator with the specified underlying iterator
349/// @tparam Iterator The type of the underlying iterator
350/// @param iter The value to use for the underlying iterator
351/// @return The new @c move_iterator
352template <typename Iterator>
353auto make_move_iterator(Iterator iter) -> std::move_iterator<Iterator> {
354 return std::move_iterator<Iterator>{iter};
355}
356// parasoft-end-suppress AUTOSAR-M3_3_2-a
357
358} // namespace std
359
360#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_MOVE_ITERATOR_HPP_
An iterator adaptor that wraps another iterator such that dereferencing the wrapped iterator yields a...
Definition move_iterator.hpp:35
auto operator[](difference_type delta) const -> reference
Access the value at a specified offset from the current iterator.
Definition move_iterator.hpp:237
auto operator+=(difference_type delta) -> move_iterator &
Move the current iterator forwards by a specified amount.
Definition move_iterator.hpp:194
auto operator*() const -> reference
Dereference the iterator.
Definition move_iterator.hpp:125
move_iterator(Iterator iter) noexcept(is_nothrow_copy_constructible_v< Iterator >)
Construct an iterator holding the specified value of the underlying iterator.
Definition move_iterator.hpp:76
move_iterator() noexcept(is_nothrow_default_constructible_v< Iterator >)
Default construct the iterator, holding a default-constructed underlying iterator....
Definition move_iterator.hpp:71
auto operator-(difference_type delta) const -> move_iterator
Obtain a new iterator referencing the value from moving the current iterator backwards by a specified...
Definition move_iterator.hpp:208
auto operator->() const -> pointer
Arrow dereference operator for accessing the referenced value.
Definition move_iterator.hpp:130
~move_iterator()=default
Default destructor.
auto base() const -> iterator_type
Retrieve the underlying iterator.
Definition move_iterator.hpp:119
auto operator=(move_iterator< OtherIterator > const &source) -> move_iterator &
Conversion assignment from another move iterator with a different type of underlying iterator....
Definition move_iterator.hpp:112
move_iterator(move_iterator const &)=default
Default copy constructor.
auto operator-=(difference_type delta) -> move_iterator &
Move the current iterator backwards by a specified amount.
Definition move_iterator.hpp:222
auto operator++(int) -> move_iterator
Postincrement operator. Increments the stored iterator.
Definition move_iterator.hpp:141
auto operator++() -> move_iterator &
Preincrement operator. Increments the stored iterator.
Definition move_iterator.hpp:134
auto operator--(int) -> move_iterator
Postdecrement operator. Decrements the stored iterator.
Definition move_iterator.hpp:163
auto operator--() -> move_iterator &
Predecrement operator. Decrements the stored iterator.
Definition move_iterator.hpp:151
move_iterator(move_iterator< OtherIterator > const &source)
Conversion constructor from another move iterator with a different type of underlying iterator....
Definition move_iterator.hpp:100
auto operator+(difference_type delta) const -> move_iterator
Obtain a new iterator referencing the value from moving the current iterator forwards by a specified ...
Definition move_iterator.hpp:180
auto operator=(move_iterator &&) -> move_iterator &=default
Default move assignment operator.
auto operator=(move_iterator const &) -> move_iterator &=default
Default copy assignment operator.
move_iterator(move_iterator &&)=default
Default move constructor.
auto operator<=(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) -> bool
Check if one iterator is less than or equal to another.
Definition move_iterator.hpp:294
auto operator>=(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) -> bool
Check if one iterator is greater than or equal to another.
Definition move_iterator.hpp:316
auto operator!=(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) -> bool
Check if two iterators are not equal.
Definition move_iterator.hpp:271
auto operator+(typename move_iterator< Iterator >::difference_type delta, move_iterator< Iterator > const &iter) -> move_iterator< Iterator >
Obtain a new iterator referencing the value from moving the specified iterator forwards by a specifie...
Definition move_iterator.hpp:342
auto operator<(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) -> bool
Check if one iterator is less than another.
Definition move_iterator.hpp:283
auto make_move_iterator(Iterator iter) -> std::move_iterator< Iterator >
Create a move_iterator with the specified underlying iterator.
Definition move_iterator.hpp:353
auto operator==(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) -> bool
Check if two iterators are equal.
Definition move_iterator.hpp:258
auto operator-(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) ->
Obtain the distance between two iterators.
Definition move_iterator.hpp:330
auto operator>(move_iterator< Iterator1 > const &lhs, move_iterator< Iterator2 > const &rhs) -> bool
Check if one iterator is greater than another.
Definition move_iterator.hpp:305
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827