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"
16#include "arene/base/detail/iterator_interface.hpp"
18#include "arene/base/stdlib_choice/cstddef.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20#include "arene/base/stdlib_choice/integral_constant.hpp"
21#include "arene/base/stdlib_choice/is_base_of.hpp"
22#include "arene/base/stdlib_choice/is_const.hpp"
23#include "arene/base/stdlib_choice/iterator_tags.hpp"
24#include "arene/base/stdlib_choice/iterator_traits.hpp"
25#include "arene/base/stdlib_choice/remove_reference.hpp"
27// parasoft-end-suppress AUTOSAR-A16_2_2-a
28
29// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
30// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
31
32namespace arene {
33namespace base {
34namespace inline_map_reference_iterator_detail {
35
36/// @brief poison pill declaration
37auto value_at_index() -> void;
38
39} // namespace inline_map_reference_iterator_detail
40
41// parasoft-begin-suppress AUTOSAR-A14_5_1-a "False positive: template
42// constructors do not participate in overload resolution for copy/move
43// construction"
44
45// parasoft-begin-suppress AUTOSAR-A10_1_1-a "False positive:
46// inline_map_reference_iterator does not inherit from more than one class that
47// is not an interface"
48
49// parasoft-begin-suppress AUTOSAR-A12_1_5-a "Delegating constructors would not reduce duplication"
50
51/// @brief iterator type used by @c inline_map_reference and
52/// @c const_inline_map_reference
53///
54/// @tparam MapBase base type of an @c inline_map
55/// @tparam IsConst specifies if this iterator provides const or non-const
56/// access
57///
58template <class MapBase, bool IsConst>
60 : public detail::iterator_interface<inline_map_reference_iterator<MapBase, IsConst>>
61 , inline_map_detail::inline_map_passkey_base<MapBase> {
62 /// @brief alias to @c iterator_interface base type
63 ///
64 using iterator_interface = detail::iterator_interface<inline_map_reference_iterator>;
65
66 /// @brief add @c const if @c IsConst is @c true
67 /// @tparam T type to possible add @c const to
68 /// @tparam B predicate to use to apply @c const, defaults to @c IsConst
69 ///
70 template <class T, bool B = IsConst>
71 using maybe_const_t = conditional_t<B, T const, T>;
72
73 /// @brief alias to the @c inline_map passkey type
74 using inline_map_passkey = typename inline_map_detail::inline_map_passkey_base<MapBase>::inline_map_passkey;
75
76 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Friendship allows conversion
77 // from non-const iterator to const iterator without exposing implementation
78 // details."
80 // parasoft-end-suppress AUTOSAR-A11_3_1-a
81
82 public:
83 /// @brief iterator category
85 /// @brief iterator value type
86 using value_type = typename MapBase::value_type;
87 /// @brief iterator difference type
89 /// @brief pointer type
90 using pointer = maybe_const_t<value_type>*;
91 /// @brief reference type
92 using reference = maybe_const_t<value_type>&;
93
94 /// @brief @c inline_map base type
95 using map_base_type = MapBase;
96
97 private:
98 /// @brief determine if an iterator is a non-const iterator
99 /// @tparam I iterator type
100 ///
101 template <class I>
102 static constexpr bool is_const_iterator_v{
104 };
105
106 /// @brief index type of this iterator
107 /// @note must be wide enough to index any @c inline_map regardless of its
108 /// capacity, so it is set to @c std::size_t
109 ///
110 using index_type = std::size_t;
111
112 // parasoft-begin-suppress CERT_C-EXP37-b "False positive: The rule does not mention naming all parameters"
113
114 /// @brief function pointer type for indexing into an @c inline_map
115 /// @tparam B determines if the function pointer type is assocated with the
116 /// const or non-const iterator
117 ///
118 template <bool B>
119 using erased_index_fn_type = auto (*)(maybe_const_t<map_base_type, B>*, index_type) -> maybe_const_t<value_type, B>&;
120
121 // parasoft-end-suppress CERT_C-EXP37-b
122
123 /// @brief function to index into a non-const @c inline_map
124 ///
125 /// @note While only one of these functions is used by @c do_deref for a
126 /// specific instance of this type, there must be some way to change the
127 /// function pointer when converting from a non-const iterator to a const
128 /// iterator. This implementation stores both functions.
129 ///
130 erased_index_fn_type<false> non_const_erased_index_;
131
132 /// @brief function to index into a const @c inline_map
133 ///
134 /// @note While only one of these functions is used by @c do_deref for a
135 /// specific instance of this type, there must be some way to change the
136 /// function pointer when converting from a non-const iterator to a const
137 /// iterator. This implementation stores both functions.
138 ///
139 erased_index_fn_type<true> const_erased_index_;
140
141 /// @brief pointer to the capacity-erased base of @c inline_map
142 ///
143 maybe_const_t<map_base_type>* map_;
144
145 /// @brief iterator position in the map
146 ///
147 index_type pos_;
148
149 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Use of a friend function with
150 // passkey improves encapsulation. It allows data members to remain private
151 // and does not unnecessarily expose a public member function."
152 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: Function is namespace scope and used in other
153 // translation units"
154 // parasoft-begin-suppress AUTOSAR-A7_1_1-a "Declaring 'iter' as reference to const changes semantics"
155 // parasoft-begin-suppress AUTOSAR-M7_1_2-c "Declaring 'iter' as reference to const changes semantics"
156 // parasoft-begin-suppress AUTOSAR-A8_4_9-a "Declaring 'iter' as reference to const changes semantics"
157
158 /// @brief basis function for increment
159 /// @param iter iterator to increment
160 ///
161 friend auto step_forward(detail::iterator_interface_tag, inline_map_reference_iterator& iter) noexcept -> void {
162 ++iter.pos_;
163 }
164
165 /// @brief basis function for decrement
166 /// @param iter iterator to decrement
167 ///
168 friend auto step_backward(detail::iterator_interface_tag, inline_map_reference_iterator& iter) noexcept -> void {
169 --iter.pos_;
170 }
171
172 // parasoft-end-suppress AUTOSAR-A11_3_1-a
173 // parasoft-end-suppress AUTOSAR-A0_1_3-a
174 // parasoft-end-suppress AUTOSAR-A7_1_1-a
175 // parasoft-end-suppress AUTOSAR-M7_1_2-c
176 // parasoft-end-suppress AUTOSAR-A8_4_9-a
177
178 /// @brief perform iterator dereference
179 /// @tparam True deduced from tag argument, this function is disabled if
180 /// @c True is not @c true or @c IsConst is not @c true
181 /// @return reference to the indirect value represented by this iterator
182 ///
183 template <bool True, constraints<std::enable_if_t<IsConst && True>> = nullptr>
184 auto do_deref(std::integral_constant<bool, True>) const noexcept -> reference {
185 return const_erased_index_(map_, pos_);
186 }
187
188 /// @brief perform iterator dereference
189 /// @tparam False deduced from tag argument, this function is disabled if
190 /// @c False is not @c false or @c IsConst is not @c false
191 /// @return reference to the indirect value represented by this iterator
192 ///
193 template <bool False, constraints<std::enable_if_t<!IsConst && !False>> = nullptr>
194 auto do_deref(std::integral_constant<bool, False>) const noexcept -> reference {
195 return non_const_erased_index_(map_, pos_);
196 }
197
198 /// @brief obtains a value in a capacity-erased map
199 /// @tparam B switches between const and non-const access
200 /// @tparam Map the @c const qualified @c inline_map type
201 /// @param map pointer to capacity-erased @c inline_map
202 /// @param index index in @c inline_map
203 /// @return reference to the value at @c index in @c map
204 ///
205 template <bool B, class Map>
206 static auto value_in_map(maybe_const_t<MapBase, B>* map, index_type index) noexcept -> maybe_const_t<value_type, B>& {
207 using ::arene::base::inline_map_reference_iterator_detail::value_at_index;
208 return value_at_index(inline_map_passkey{}, *static_cast<maybe_const_t<Map, B>*>(map), index);
209 }
210
211 public:
212 /// @brief default construct an invalid iterator
213 ///
215 : iterator_interface{},
216 non_const_erased_index_{nullptr},
217 const_erased_index_{nullptr},
218 map_{nullptr},
219 pos_{} {}
220
221 /// @brief construct from an @c inline_map iterator type
222 /// @tparam I @c inline_map iterator type
223 /// @param itr base iterator
224 ///
225 template <
226 class I,
228 std::is_base_of<MapBase, typename I::inline_map_type>::value && (IsConst || (!is_const_iterator_v<I>))>> =
229 nullptr>
230 explicit inline_map_reference_iterator(I const& itr) noexcept
231 : iterator_interface{},
232 non_const_erased_index_{&value_in_map<false, typename I::inline_map_type>},
233 const_erased_index_{&value_in_map<true, typename I::inline_map_type>},
234 map_{itr.get_map(inline_map_passkey{})},
235 pos_{itr.get_index(inline_map_passkey{})} {}
236
237 /// @brief implictly construct from a non-const @c inline_map_reference_iterator
238 /// @tparam OtherConst specifies if the other iterator is @c const
239 /// @param other iterator
240 ///
241 template <bool OtherConst, constraints<std::enable_if_t<IsConst && !OtherConst>> = nullptr>
242 // NOLINTNEXTLINE(hicpp-explicit-conversions)
244 : iterator_interface{},
245 non_const_erased_index_{other.non_const_erased_index_},
246 const_erased_index_{other.const_erased_index_},
247 map_{other.map_},
248 pos_{other.pos_} {}
249
250 /// @brief dereference
251 /// @return value in the associated @c inline_map
252 ///
253 auto operator*() const noexcept -> reference { return do_deref(std::integral_constant<bool, IsConst>{}); }
254
255 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2 v1.0.0"
256 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: Function is namespace scope and used in other
257 // translation units"
258
259 /// @brief equality
260 /// @param lhs left iterator
261 /// @param rhs right iterator
262 /// @return @c true if iterators are equal, otherwise @c false
263 ///
264 friend auto operator==(inline_map_reference_iterator const& lhs, inline_map_reference_iterator const& rhs) noexcept
265 -> bool {
266 return lhs.pos_ == rhs.pos_;
267 }
268
269 // parasoft-end-suppress AUTOSAR-A11_3_1-a
270 // parasoft-end-suppress AUTOSAR-A0_1_3-a
271
272 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not
273 // mention naming all parameters"
274
275 /// @brief obtain a pointer to the map that this iterator references
276 /// @return pointer to the referenced map
277 //
278 auto get_map(inline_map_passkey) const noexcept -> maybe_const_t<map_base_type>* { return map_; }
279
280 /// @brief obtain the position that this iterator references within the map
281 /// @return index position of this iterator
282 ///
283 auto get_index(inline_map_passkey) const noexcept -> index_type { return pos_; }
284
285 // parasoft-end-suppress CERT_C-EXP37-a
286};
287
288// parasoft-end-suppress AUTOSAR-A14_5_1-a
289// parasoft-end-suppress AUTOSAR-A10_1_1-a
290// parasoft-end-suppress AUTOSAR-A12_1_5-a
291
292} // namespace base
293} // namespace arene
294
295#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_MAP_REFERENCE_ITERATOR_HPP_
auto get_index(inline_map_passkey) const noexcept -> index_type
obtain the position that this iterator references within the map
Definition map_reference_iterator.hpp:283
inline_map_reference_iterator(I const &itr) noexcept
construct from an inline_map iterator type
Definition map_reference_iterator.hpp:230
std::ptrdiff_t difference_type
Definition map_reference_iterator.hpp:88
friend auto step_forward(detail::iterator_interface_tag, inline_map_reference_iterator &iter) noexcept -> void
basis function for increment
Definition map_reference_iterator.hpp:161
MapBase map_base_type
Definition map_reference_iterator.hpp:95
inline_map_reference_iterator(inline_map_reference_iterator< MapBase, OtherConst > const &other) noexcept
implictly construct from a non-const inline_map_reference_iterator
Definition map_reference_iterator.hpp:243
friend auto step_backward(detail::iterator_interface_tag, inline_map_reference_iterator &iter) noexcept -> void
basis function for decrement
Definition map_reference_iterator.hpp:168
inline_map_reference_iterator() noexcept
default construct an invalid iterator
Definition map_reference_iterator.hpp:214
auto operator*() const noexcept -> reference
dereference
Definition map_reference_iterator.hpp:253
auto get_map(inline_map_passkey) const noexcept -> maybe_const_t< map_base_type > *
obtain a pointer to the map that this iterator references
Definition map_reference_iterator.hpp:278
maybe_const_t< value_type > & reference
Definition map_reference_iterator.hpp:92
maybe_const_t< value_type > * pointer
Definition map_reference_iterator.hpp:90
std::bidirectional_iterator_tag iterator_category
Definition map_reference_iterator.hpp:84
typename MapBase::value_type value_type
Definition map_reference_iterator.hpp:86
friend auto operator==(inline_map_reference_iterator const &lhs, inline_map_reference_iterator const &rhs) noexcept -> bool
equality
Definition map_reference_iterator.hpp:264
An empty tag type corresponding to a bidirectional iterator.
Definition iterator_tags.hpp:25
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
static constexpr value_type value
Definition integral_constant.hpp:34
A type trait to check if a type is const. The class is derived from true_type if Type is const,...
Definition is_const.hpp:32
Definition accumulate.hpp:37
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41