Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
map_reference_iterator.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "inline_container/map_reference_iterator.hpp is clear enough for
2// inline_map_reference_iterator"
3
4// Copyright 2026, Toyota Motor Corporation
5//
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7
8#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_MAP_REFERENCE_ITERATOR_HPP_
9#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_MAP_REFERENCE_ITERATOR_HPP_
10
11// IWYU pragma: private, include "arene/base/inline_container/map_reference.hpp"
12// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
13
14// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
15#include "arene/base/constraints/constraints.hpp"
16#include "arene/base/inline_container/detail/iterator_interface.hpp"
17#include "arene/base/inline_container/map.hpp"
18#include "arene/base/stdlib_choice/cstddef.hpp"
19#include "arene/base/stdlib_choice/cstdint.hpp"
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/integral_constant.hpp"
22#include "arene/base/stdlib_choice/is_base_of.hpp"
23#include "arene/base/stdlib_choice/is_const.hpp"
24#include "arene/base/stdlib_choice/iterator_tags.hpp"
25#include "arene/base/stdlib_choice/iterator_traits.hpp"
26#include "arene/base/stdlib_choice/numeric_limits.hpp"
27#include "arene/base/stdlib_choice/remove_reference.hpp"
28#include "arene/base/type_traits/conditional.hpp"
29// parasoft-end-suppress AUTOSAR-A16_2_2-a
30
31// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
32// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
33
34namespace arene {
35namespace base {
36namespace inline_map_reference_iterator_detail {
37
38/// @brief poison pill declaration
39auto value_at_index() -> void;
40
41} // namespace inline_map_reference_iterator_detail
42
43// parasoft-begin-suppress AUTOSAR-A14_5_1-a "False positive: template
44// constructors do not participate in overload resolution for copy/move
45// construction"
46
47// parasoft-begin-suppress AUTOSAR-A10_1_1-a "False positive:
48// inline_map_reference_iterator does not inherit from more than one class that
49// is not an interface"
50
51// parasoft-begin-suppress AUTOSAR-A12_1_5-a "Delegating constructors would not reduce duplication"
52
53/// @brief iterator type used by @c inline_map_reference and
54/// @c const_inline_map_reference
55///
56/// @tparam MapBase base type of an @c inline_map
57/// @tparam IsConst specifies if this iterator provides const or non-const
58/// access
59///
60template <class MapBase, bool IsConst>
64 /// @brief alias to @c iterator_interface base type
65 ///
67
68 /// @brief add @c const if @c IsConst is @c true
69 /// @tparam T type to possible add @c const to
70 /// @tparam B predicate to use to apply @c const, defaults to @c IsConst
71 ///
72 template <class T, bool B = IsConst>
73 using maybe_const_t = conditional_t<B, T const, T>;
74
75 /// @brief alias to the @c inline_map passkey type
77
78 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Friendship allows conversion
79 // from non-const iterator to const iterator without exposing implementation
80 // details."
81 friend inline_map_reference_iterator<MapBase, true>;
82 // parasoft-end-suppress AUTOSAR-A11_3_1-a
83
84 public:
85 /// @brief iterator category
87 /// @brief iterator value type
88 using value_type = typename MapBase::value_type;
89 /// @brief iterator difference type
91 /// @brief pointer type
93 /// @brief reference type
95
96 /// @brief @c inline_map base type
97 using map_base_type = MapBase;
98
99 private:
100 /// @brief determine if an iterator is a non-const iterator
101 /// @tparam I iterator type
102 ///
103 template <class I>
104 static constexpr bool is_const_iterator_v{
106 };
107
108 /// @brief index type of this iterator
109 /// @note set to the maximum possible @c index_type of any @c inline_map
110 /// @note @c inline_map can never have an @c index_type larger than 16 bits
111 ///
112 using index_type = std::uint16_t;
113
114 // parasoft-begin-suppress CERT_C-EXP37-b "False positive: The rule does not mention naming all parameters"
115
116 /// @brief function pointer type for indexing into an @c inline_map
117 /// @tparam B determines if the function pointer type is assocated with the
118 /// const or non-const iterator
119 ///
120 template <bool B>
122
123 // parasoft-end-suppress CERT_C-EXP37-b
124
125 /// @brief function to index into a non-const @c inline_map
126 ///
127 /// @note While only one of these functions is used by @c do_deref for a
128 /// specific instance of this type, there must be some way to change the
129 /// function pointer when converting from a non-const iterator to a const
130 /// iterator. This implementation stores both functions.
131 ///
132 erased_index_fn_type<false> non_const_erased_index_;
133
134 /// @brief function to index into a const @c inline_map
135 ///
136 /// @note While only one of these functions is used by @c do_deref for a
137 /// specific instance of this type, there must be some way to change the
138 /// function pointer when converting from a non-const iterator to a const
139 /// iterator. This implementation stores both functions.
140 ///
141 erased_index_fn_type<true> const_erased_index_;
142
143 /// @brief pointer to the capacity-erased base of @c inline_map
144 ///
145 maybe_const_t<map_base_type>* map_;
146
147 /// @brief iterator position in the map
148 ///
149 index_type pos_;
150
151 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Use of a friend function with
152 // passkey improves encapsulation. It allows data members to remain private
153 // and does not unnecessarily expose a public member function."
154 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: Function is namespace scope and used in other
155 // translation units"
156 // parasoft-begin-suppress AUTOSAR-A7_1_1-a "Declaring 'iter' as reference to const changes semantics"
157 // parasoft-begin-suppress AUTOSAR-M7_1_2-c "Declaring 'iter' as reference to const changes semantics"
158 // parasoft-begin-suppress AUTOSAR-A8_4_9-a "Declaring 'iter' as reference to const changes semantics"
159
160 /// @brief basis function for increment
161 /// @param iter iterator to increment
162 ///
163 friend auto
167
168 /// @brief basis function for decrement
169 /// @param iter iterator to decrement
170 ///
171 friend auto
175
176 // parasoft-end-suppress AUTOSAR-A11_3_1-a
177 // parasoft-end-suppress AUTOSAR-A0_1_3-a
178 // parasoft-end-suppress AUTOSAR-A7_1_1-a
179 // parasoft-end-suppress AUTOSAR-M7_1_2-c
180 // parasoft-end-suppress AUTOSAR-A8_4_9-a
181
182 /// @brief perform iterator dereference
183 /// @tparam True deduced from tag argument, this function is disabled if
184 /// @c True is not @c true or @c IsConst is not @c true
185 /// @return reference to the indirect value represented by this iterator
186 ///
187 template <bool True, constraints<std::enable_if_t<IsConst && True>> = nullptr>
188 auto do_deref(std::integral_constant<bool, True>) const noexcept -> reference {
190 }
191
192 /// @brief perform iterator dereference
193 /// @tparam False deduced from tag argument, this function is disabled if
194 /// @c False is not @c false or @c IsConst is not @c false
195 /// @return reference to the indirect value represented by this iterator
196 ///
197 template <bool False, constraints<std::enable_if_t<!IsConst && !False>> = nullptr>
198 auto do_deref(std::integral_constant<bool, False>) const noexcept -> reference {
200 }
201
202 /// @brief obtains a value in a capacity-erased map
203 /// @tparam B switches between const and non-const access
204 /// @tparam Map the @c const qualified @c inline_map type
205 /// @param map pointer to capacity-erased @c inline_map
206 /// @param index index in @c inline_map
207 /// @return reference to the value at @c index in @c map
208 ///
209 template <bool B, class Map>
212 return value_at_index(inline_map_passkey{}, *static_cast<maybe_const_t<Map, B>*>(map), index);
213 }
214
215 public:
216 /// @brief default construct an invalid iterator
217 ///
221 const_erased_index_{nullptr},
222 map_{nullptr},
223 pos_{} {}
224
225 /// @brief construct from an @c inline_map iterator type
226 /// @tparam I @c inline_map iterator type
227 /// @param itr base iterator
228 ///
229 template <
230 class I,
233 nullptr>
234 explicit inline_map_reference_iterator(I const& itr) noexcept
240 constexpr auto capacity = I::inline_map_type::capacity;
241
242 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: numeric_limits::max() is constexpr and has no side
243 // effects"
244 static_assert(
246 "'index_type' is defined based on an upper limit for 'Capacity' which is no longer valid"
247 );
248 // parasoft-end-suppress CERT_C-PRE31-c
249 }
250
251 /// @brief implictly construct from a non-const @c inline_map_reference_iterator
252 /// @tparam OtherConst specifies if the other iterator is @c const
253 /// @param other iterator
254 ///
255 template <bool OtherConst, constraints<std::enable_if_t<IsConst && !OtherConst>> = nullptr>
256 // NOLINTNEXTLINE(hicpp-explicit-conversions)
263
264 /// @brief dereference
265 /// @return value in the associated @c inline_map
266 ///
267 auto operator*() const noexcept -> reference { return do_deref(std::integral_constant<bool, IsConst>{}); }
268
269 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
270 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: Function is namespace scope and used in other
271 // translation units"
272
273 /// @brief equality
274 /// @param lhs left iterator
275 /// @param rhs right iterator
276 /// @return @c true if iterators are equal, otherwise @c false
277 ///
279 -> bool {
280 return lhs.pos_ == rhs.pos_;
281 }
282
283 // parasoft-end-suppress AUTOSAR-A11_3_1-a
284 // parasoft-end-suppress AUTOSAR-A0_1_3-a
285
286 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not
287 // mention naming all parameters"
288
289 /// @brief obtain a pointer to the map that this iterator references
290 /// @return pointer to the referenced map
291 //
292 auto get_map(inline_map_passkey) const noexcept -> maybe_const_t<map_base_type>* { return map_; }
293
294 /// @brief obtain the position that this iterator references within the map
295 /// @return index position of this iterator
296 ///
297 auto get_index(inline_map_passkey) const noexcept -> index_type { return pos_; }
298
299 // parasoft-end-suppress CERT_C-EXP37-a
300};
301
302// parasoft-end-suppress AUTOSAR-A14_5_1-a
303// parasoft-end-suppress AUTOSAR-A10_1_1-a
304// parasoft-end-suppress AUTOSAR-A12_1_5-a
305
306} // namespace base
307} // namespace arene
308
309#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_MAP_REFERENCE_ITERATOR_HPP_
iterator type used by inline_map_reference and const_inline_map_reference
Definition map_reference_iterator.hpp:63
friend auto step_backward(inline_container_detail::iterator_interface_tag, inline_map_reference_iterator &iter) noexcept -> void
basis function for decrement
Definition map_reference_iterator.hpp:172
friend auto step_forward(inline_container_detail::iterator_interface_tag, inline_map_reference_iterator &iter) noexcept -> void
basis function for increment
Definition map_reference_iterator.hpp:164
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10