Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
advance.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///
6/// @file advance.hpp
7/// @brief Provides a backport of C++17's constexpr std::advance.
8///
9
10// IWYU pragma: private, include "arene/base/iterator.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ADVANCE_HPP_
14#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ADVANCE_HPP_
15
16// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
17#include "arene/base/compiler_support/cpp14_inline.hpp"
18#include "arene/base/constraints/constraints.hpp"
19#include "arene/base/contracts/contract.hpp"
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/iterator_tags.hpp"
22#include "arene/base/stdlib_choice/iterator_traits.hpp"
23#include "arene/base/type_traits/iterator_category_traits.hpp"
24// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
25
26namespace arene {
27namespace base {
28
29namespace advance_detail {
30
31// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
32// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
33///
34/// @brief Implementation of advance for input iterators.
35///
36/// @tparam Iterator The iterator to advance
37/// @param itr The iterator to advance
38/// @param steps The number of steps to advance the iterator by.
39/// @pre steps is not negative, else @c ARENE_PRECONDITION violation.
40/// @pre If incrementing @c itr @c steps times results in attempting to produce an invalid iterator such as
41/// past-the-end, behavior is undefined.
42/// @post The iterator is incremented @c steps times.
43///
44template <typename Iterator>
45constexpr void do_advance(
46 Iterator& itr,
47 typename std::iterator_traits<Iterator>::difference_type steps,
48 std::input_iterator_tag
49) noexcept(noexcept(++itr)) {
50 ARENE_PRECONDITION(steps >= 0);
51 while (steps > 0) {
52 ++itr;
53 --steps;
54 } // CODEQLFP(A5-0-2)
55}
56// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
57// parasoft-end-suppress CERT_C-EXP37-a-3
58
59// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
60/// @brief Implementation of advance for bidirectional iterators.
61///
62/// @tparam Iterator The iterator to advance
63/// @param itr The iterator to advance
64/// @param steps The number of steps to advance the iterator by. If positive, the iterator is incremented. If
65/// negative, it is decremented.
66/// @pre If incrementing or decrementing @c itr @c steps times results in attempting to produce an invalid iterator
67/// such as past-the-end or before-the-begin, behavior is undefined.
68/// @post The iterator is incremented/decremented @c steps times.
69///
70template <typename Iterator>
71constexpr void do_advance(
72 Iterator& itr,
73 typename std::iterator_traits<Iterator>::difference_type steps,
74 std::bidirectional_iterator_tag
75) noexcept(noexcept(++itr) && noexcept(--itr)) {
76 while (steps > 0) {
77 ++itr;
78 --steps;
79 } // CODEQLFP(A5-0-2)
80 while (steps < 0) {
81 --itr;
82 ++steps;
83 } // CODEQLFP(A5-0-2)
84}
85// parasoft-end-suppress CERT_C-EXP37-a-3
86
87// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
88// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
89///
90/// @brief Implementation of advance for random_access iterators.
91///
92/// @tparam Iterator The iterator to advance
93/// @param itr The iterator to advance
94/// @param steps The number of steps to advance the iterator by. If positive, the iterator is incremented. If
95/// negative, it is decremented.
96/// @pre If incrementing or decrementing @c itr @c steps times results in attempting to produce an invalid iterator
97/// such as past-the-end or before-the-begin, behavior is undefined.
98/// @post The iterator is incremented/decremented @c steps times.
99///
100template <typename Iterator>
101constexpr void do_advance(
102 Iterator& itr,
103 typename std::iterator_traits<Iterator>::difference_type steps,
104 std::random_access_iterator_tag
105) noexcept(noexcept(itr += steps)) { // CODEQLFP(EXP52-CPP)
106 itr += steps;
107}
108// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
109// parasoft-end-suppress CERT_C-EXP37-a-3
110
111/// @brief Implementation helper for @c arene::base::advance
112class advance {
113 public:
114 ///
115 /// @brief Increments or decrements an iterator by a set number of steps.
116 ///
117 /// @tparam Iterator The iterator to advance. Does not participate in overload resolution unless @c Iterator is at
118 /// least an input iterator.
119 /// @param itr The iterator to advance
120 /// @param steps The number of steps to advance the iterator by. If positive, the iterator is incremented. If
121 /// negative, it is decremented.
122 /// @pre If incrementing or decrementing @c itr @c steps times results in attempting to produce an invalid iterator
123 /// such as past-the-end or before-the-begin, behavior is undefined.
124 /// @pre If @c Iterator is an input iterator, @c steps is not negative, else @c ARENE_PRECONDITION violation.
125 /// @post The iterator is incremented/decremented @c steps times.
126 ///
127 template <typename Iterator, constraints<std::enable_if_t<is_input_iterator_v<Iterator>>> = nullptr>
128 constexpr void operator()(Iterator& itr, typename std::iterator_traits<Iterator>::difference_type steps) const
129 noexcept(noexcept(::arene::base::advance_detail::do_advance(
130 itr,
131 steps,
132 typename std::iterator_traits<Iterator>::iterator_category{}
133 ))) { // CODEQLFP(EXP52-CPP)
134 ::arene::base::advance_detail::do_advance(itr, steps, typename std::iterator_traits<Iterator>::iterator_category{});
135 }
136};
137
138} // namespace advance_detail
139
140/// @def arene::base::advance
141/// @copydoc arene::base::advance_detail::advance_fn::operator()
142// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
143// object used in multiple TUs."
144// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
145// object used in multiple TUs."
146ARENE_CPP14_INLINE_VARIABLE(advance_detail::advance, advance);
147// parasoft-end-suppress AUTOSAR-M7_3_3-a
148// parasoft-end-suppress CERT_CPP-DCL59-a
149
150} // namespace base
151} // namespace arene
152
153#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ADVANCE_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10