Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
vector_reference.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_VECTOR_REFERENCE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_VECTOR_REFERENCE_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
9#include "arene/base/compiler_support/attributes.hpp"
10#include "arene/base/compiler_support/cpp14_inline.hpp"
11#include "arene/base/constraints/constraints.hpp"
12#include "arene/base/contracts/contract.hpp"
13#include "arene/base/detail/exceptions.hpp"
14#include "arene/base/inline_container/external_vector.hpp"
15#include "arene/base/inline_container/vector.hpp"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/ignore.hpp"
19#include "arene/base/stdlib_choice/is_base_of.hpp"
20#include "arene/base/utility/make_subrange.hpp"
21
22// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
23
24namespace arene {
25namespace base {
26
27// forward declaration
28// IWYU pragma: begin_keep
29template <typename T>
30class const_vector_reference;
31// IWYU pragma: end_keep
32
33namespace vector_reference_detail {
34
35/// @brief function pointers to be used inside const member functions
36/// @tparam T The type of each element
37template <typename T>
38struct const_operations {
39 /// @brief The type of the base class
40 using vector_base = inline_vector_detail::vector_base<T>;
41 /// @brief The type of a const iterator to a position in the vector
42 using const_iterator = typename vector_base::const_iterator;
43 /// @brief The type of a const pointer to the base class
44 using vector_ptr = vector_base const*;
45 // parasoft-begin-suppress CERT_C-EXP37-b-3 "False positive: The rule does not mention naming all parameters"
46 /// @brief The function pointer type of the data operation
47 using data_op_type = T const* (*)(vector_ptr);
48 /// @brief The function pointer type of the size operation
49 using size_op_type = std::size_t (*)(vector_ptr);
50 /// @brief The function pointer type of the capacity operation
51 using capacity_op_type = std::size_t (*)(vector_ptr);
52 /// @brief The function pointer type of the empty operation
53 using empty_op_type = bool (*)(vector_ptr);
54 /// @brief The function pointer type of the element accessor
55 using element_op_type = T const& (*)(vector_ptr, std::size_t);
56 /// @brief The function pointer type of the at operation
57 using at_op_type = T const& (*)(vector_ptr, std::size_t);
58 /// @brief The function pointer type of the front operation
59 using front_op_type = T const& (*)(vector_ptr);
60 /// @brief The function pointer type of the back operation
61 using back_op_type = T const& (*)(vector_ptr);
62 /// @brief The function pointer type of the begin operation
63 using begin_op_type = const_iterator (*)(vector_ptr);
64 /// @brief The function pointer type of the end operation
65 using end_op_type = const_iterator (*)(vector_ptr);
66 // parasoft-end-suppress CERT_C-EXP37-b-3
67
68 /// @brief The function pointer of the data operation
69 data_op_type data;
70 /// @brief The function pointer of the size operation
71 size_op_type size;
72 /// @brief The function pointer of the capacity operation
73 capacity_op_type capacity;
74 /// @brief The function pointer of the empty operation
75 empty_op_type empty;
76 /// @brief The function pointer of the element accessor
77 element_op_type element;
78 /// @brief The function pointer of the at operation
79 at_op_type at;
80 /// @brief The function pointer of the front operation
81 front_op_type front;
82 /// @brief The function pointer of the back operation
83 back_op_type back;
84 /// @brief The function pointer of the begin iterator
85 begin_op_type begin;
86 /// @brief The function pointer of the end iterator
87 end_op_type end;
88};
89
90/// @brief function pointers to be used inside non-const member functions
91/// @tparam T The type of each element
92template <typename T>
93struct operations {
94 /// @brief The type of the base class
95 using vector_base = inline_vector_detail::vector_base<T>;
96 /// @brief The type of an iterator to a position in the vector
97 using iterator = typename vector_base::iterator;
98 /// @brief The type of a const iterator to a position in the vector
99 using const_iterator = typename vector_base::const_iterator;
100 /// @brief The type of a const pointer to the base class
101 using vector_ptr = vector_base*;
102 // parasoft-begin-suppress CERT_C-EXP37-b-3 "False positive: The rule does not mention naming all parameters"
103 /// @brief The function pointer type of the data operation
104 using data_op_type = T* (*)(vector_ptr);
105 /// @brief The function pointer type of the element accessor
106 using element_op_type = T& (*)(vector_ptr, std::size_t);
107 /// @brief The function pointer type of the at operation
108 using at_op_type = T& (*)(vector_ptr, std::size_t);
109 /// @brief The function pointer type of the front operation
110 using front_op_type = T& (*)(vector_ptr);
111 /// @brief The function pointer type of the back operation
112 using back_op_type = T& (*)(vector_ptr);
113 /// @brief The function pointer type of the begin operation
114 using begin_op_type = iterator (*)(vector_ptr);
115 /// @brief The function pointer type of the end operation
116 using end_op_type = iterator (*)(vector_ptr);
117 /// @brief The function pointer type of the push_back operation
118 using push_back_op_type = void (*)(vector_ptr, T const&);
119 /// @brief The function pointer type of the insert operation
120 using insert_op_type = iterator (*)(vector_ptr, const_iterator, T const&);
121 /// @brief The function pointer type of the erase operation
122 using erase_op_type = iterator (*)(vector_ptr, const_iterator);
123 /// @brief The function pointer type of the erase range operation
124 using erase_range_op_type = iterator (*)(vector_ptr, const_iterator, const_iterator);
125 /// @brief The function pointer type of the pop_back operation
126 using pop_back_op_type = void (*)(vector_ptr);
127 /// @brief The function pointer type of the clear operation
128 using clear_op_type = void (*)(vector_ptr);
129 /// @brief The function pointer type of the resize operation
130 using resize_op_type = void (*)(vector_ptr, std::size_t);
131 // parasoft-end-suppress CERT_C-EXP37-b-3
132
133 /// @brief The pointer to the object holding the const operations
134 const_operations<T> const* const_ops;
135 /// @brief The function pointer of the data operation
136 data_op_type data;
137 /// @brief The function pointer of the element accessor
138 element_op_type element;
139 /// @brief The function pointer of the at operation
140 at_op_type at;
141 /// @brief The function pointer of the front operation
142 front_op_type front;
143 /// @brief The function pointer of the back operation
144 back_op_type back;
145 /// @brief The function pointer of the begin iterator
146 begin_op_type begin;
147 /// @brief The function pointer of the end iterator
148 end_op_type end;
149 /// @brief The function pointer of the push_back operation
150 push_back_op_type push_back;
151 /// @brief The function pointer of the insert operation
152 insert_op_type insert;
153 /// @brief The function pointer of the erase operation
154 erase_op_type erase;
155 /// @brief The function pointer of the erase range operation
156 erase_range_op_type erase_range;
157 /// @brief The function pointer of the pop_back operation
158 pop_back_op_type pop_back;
159 /// @brief The function pointer of the clear operation
160 clear_op_type clear;
161 /// @brief The function pointer of the resize operation
162 resize_op_type resize;
163};
164
165/// @brief implementation of the const operations for a specific vector type
166/// @tparam Vector the specific vector type
167template <class Vector>
168class const_operations_impl {
169 public:
170 /// @brief The value type of the underlying vector
171 using value_type = typename Vector::value_type;
172
173 /// @brief The type of the base class of the underlying vector
174 using vector_ptr = inline_vector_detail::vector_base<value_type> const*;
175
176 // parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
177 /// @brief Cast base class pointer back to the original vector type
178 /// @param vec The base class pointer
179 /// @return The pointer as the original vector type
180 static constexpr auto vector(vector_ptr vec) noexcept -> Vector const* { return static_cast<Vector const*>(vec); }
181 // parasoft-end-suppress AUTOSAR-M2_10_1-a-2
182
183 /// @brief Obtain a pointer to the stored elements
184 /// @param ptr The base class pointer
185 /// @return A raw pointer to the elements
186 static constexpr auto do_data(vector_ptr ptr) noexcept -> value_type const* { return vector(ptr)->data(); }
187 /// @brief Get the current number of live elements in the vector
188 /// @param ptr The base class pointer
189 /// @return the size
190 static constexpr auto do_size(vector_ptr ptr) noexcept -> std::size_t { return vector(ptr)->size(); }
191 /// @brief Get the capacity
192 /// @param ptr The base class pointer
193 /// @return the capacity
194 static constexpr auto do_capacity(vector_ptr ptr) noexcept -> std::size_t { return vector(ptr)->capacity(); }
195 /// @brief Check if the vector is empty.
196 /// @param ptr The base class pointer
197 /// @return @c true if the vector is empty, @c false otherwise
198 static constexpr auto do_empty(vector_ptr ptr) noexcept -> bool { return vector(ptr)->empty(); }
199 /// @brief Obtain a const reference to the element with the specified index
200 /// @param ptr The base class pointer
201 /// @param index The index of the element
202 /// @return A reference to the element
203 /// @pre @c index must be less than @c size()
204 static constexpr auto do_element(vector_ptr ptr, std::size_t index) noexcept -> value_type const& {
205 return (*vector(ptr))[index];
206 }
207 /// @brief Obtain a @c const reference to the element with the specified index
208 /// @param ptr The base class pointer
209 /// @param index The index of the element
210 /// @return A reference to the element
211 /// @throws std::out_of_range if the index is out of range
212 template <
213 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
214 constraints<std::enable_if_t<AreExceptionsEnabled>> = nullptr>
215 static constexpr auto do_at(vector_ptr ptr, std::size_t index) -> value_type const& {
216 return vector(ptr)->at(index);
217 }
218 /// @brief When exceptions are disabled, invoking this is a precondition violation.
219 /// @return Never returns
220 template <
221 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
222 constraints<std::enable_if_t<!AreExceptionsEnabled>> = nullptr>
223 ARENE_NORETURN static constexpr auto do_at(vector_ptr, std::size_t) -> value_type const& {
224 ARENE_INVARIANT_UNREACHABLE("do_at() called when exceptions are disabled");
225 }
226 /// @brief Get a const reference to the first element.
227 /// @param ptr The base class pointer
228 /// @pre The vector must not be empty
229 /// @return A reference to the element
230 static constexpr auto do_front(vector_ptr ptr) noexcept -> value_type const& { return vector(ptr)->front(); }
231 /// @brief Get a const reference to the last element.
232 /// @param ptr The base class pointer
233 /// @pre The vector must not be empty
234 /// @return A reference to the element
235 static constexpr auto do_back(vector_ptr ptr) noexcept -> value_type const& { return vector(ptr)->back(); }
236 /// @brief Obtain a const iterator to the start of the vector.
237 /// @param ptr The base class pointer
238 /// @return The iterator
239 static constexpr auto do_begin(vector_ptr ptr) noexcept -> typename Vector::const_iterator {
240 return vector(ptr)->begin();
241 }
242 /// @brief Obtain a const iterator to the end of the vector.
243 /// @param ptr The base class pointer
244 /// @return The iterator
245 static constexpr auto do_end(vector_ptr ptr) noexcept -> typename Vector::const_iterator {
246 return vector(ptr)->end();
247 }
248};
249
250/// @brief instance of the holder of the const operations of a specific vector type
251template <class Vector>
252extern constexpr const_operations<typename Vector::value_type> const_vector_operations{
253 &const_operations_impl<Vector>::do_data,
254 &const_operations_impl<Vector>::do_size,
255 &const_operations_impl<Vector>::do_capacity,
256 &const_operations_impl<Vector>::do_empty,
257 &const_operations_impl<Vector>::do_element,
258 &const_operations_impl<Vector>::template do_at<>,
259 &const_operations_impl<Vector>::do_front,
260 &const_operations_impl<Vector>::do_back,
261 &const_operations_impl<Vector>::do_begin,
262 &const_operations_impl<Vector>::do_end
263};
264
265/// @brief implementation of the operations for a specific vector type
266/// @tparam Vector the specific vector type
267template <typename Vector>
268class operations_impl {
269 public:
270 /// @brief The value type of the underlying vector
271 using value_type = typename Vector::value_type;
272
273 /// @brief The non-const iterator type
274 using iterator = typename Vector::iterator;
275 /// @brief The @c const iterator type
276 using const_iterator = typename Vector::const_iterator;
277 /// @brief A pointer to the vector base class
278 using vector_ptr = inline_vector_detail::vector_base<value_type>*;
279
280 // parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
281 /// @brief Cast base class pointer back to the original vector type
282 /// @param vec The base class pointer
283 /// @return The pointer as the original vector type
284 static constexpr auto vector(vector_ptr vec) noexcept -> Vector* { return static_cast<Vector*>(vec); }
285 // parasoft-end-suppress AUTOSAR-M2_10_1-a-2
286
287 /// @brief Forward the call to the object's member function
288 /// @param ptr The base class pointer
289 /// @return The return value of the called function
290 static constexpr auto do_data(vector_ptr ptr) noexcept -> value_type* { return vector(ptr)->data(); }
291
292 /// @brief Forward the call to the object's member function
293 /// @param ptr The base class pointer
294 /// @param index The index of the element
295 /// @return The return value of the called function
296 static constexpr auto do_element(vector_ptr ptr, std::size_t index) noexcept -> value_type& {
297 return (*vector(ptr))[index];
298 }
299 /// @brief Forward the call to the object's member function
300 /// @param ptr The base class pointer
301 /// @param index The index of the element
302 /// @return The return value of the called function
303 template <
304 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
305 constraints<std::enable_if_t<AreExceptionsEnabled>> = nullptr>
306 static constexpr auto do_at(vector_ptr ptr, std::size_t index) -> value_type& {
307 return vector(ptr)->at(index);
308 }
309 /// @brief Specialization for when exceptions are disabled, is always a precondition violation.
310 /// @return Never returns.
311 template <
312 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
313 constraints<std::enable_if_t<!AreExceptionsEnabled>> = nullptr>
314 ARENE_NORETURN static constexpr auto do_at(vector_ptr, std::size_t) -> value_type& {
315 ARENE_INVARIANT_UNREACHABLE("do_at() called when exceptions are disabled");
316 }
317
318 /// @brief Forward the call to the object's member function
319 /// @param ptr The base class pointer
320 /// @return The return value of the called function
321 static constexpr auto do_front(vector_ptr ptr) noexcept -> value_type& { return vector(ptr)->front(); }
322 /// @brief Forward the call to the object's member function
323 /// @param ptr The base class pointer
324 /// @return The return value of the called function
325 static constexpr auto do_back(vector_ptr ptr) noexcept -> value_type& { return vector(ptr)->back(); }
326
327 /// @brief Forward the call to the object's member function
328 /// @param ptr The base class pointer
329 /// @return The return value of the called function
330 static constexpr auto do_begin(vector_ptr ptr) noexcept -> iterator { return vector(ptr)->begin(); }
331 /// @brief Forward the call to the object's member function
332 /// @param ptr The base class pointer
333 /// @return The return value of the called function
334 static constexpr auto do_end(vector_ptr ptr) noexcept -> iterator { return vector(ptr)->end(); }
335
336 /// @brief Forward the call to the object's member function
337 /// @param ptr The base class pointer
338 /// @param value The value to copy
339 static constexpr void do_push_back(vector_ptr ptr, value_type const& value) noexcept {
340 vector(ptr)->push_back(value);
341 }
342 /// @brief Forward the call to the object's member function
343 /// @param ptr The base class pointer
344 /// @param pos The position to insert the new element
345 /// @param element The value to copy
346 /// @return The return value of the called function
347 static constexpr auto do_insert(vector_ptr ptr, const_iterator pos, value_type const& element) noexcept -> iterator {
348 return vector(ptr)->insert(pos, element);
349 }
350
351 /// @brief Forward the call to the object's member function
352 /// @param ptr The base class pointer
353 /// @param pos An iterator referring to the element to erase
354 /// @return The return value of the called function
355 static constexpr auto do_erase(vector_ptr ptr, const_iterator pos) noexcept -> iterator {
356 return vector(ptr)->erase(pos);
357 }
358 /// @brief Forward the call to the object's member function
359 /// @param ptr The base class pointer
360 /// @param first An iterator referring to the start of the range to erase
361 /// @param last An iterator referring to the end of the range to erase
362 /// @return The return value of the called function
363 static constexpr auto do_erase_range(vector_ptr ptr, const_iterator first, const_iterator last) noexcept -> iterator {
364 return vector(ptr)->erase(first, last);
365 }
366
367 /// @brief Obtain a const iterator to the end of the vector.
368 /// @param ptr The base class pointer
369 static constexpr void do_pop_back(vector_ptr ptr) noexcept { vector(ptr)->pop_back(); }
370
371 /// @brief Obtain a const iterator to the end of the vector.
372 /// @param ptr The base class pointer
373 static constexpr void do_clear(vector_ptr ptr) noexcept { vector(ptr)->clear(); }
374
375 /// @brief Resize the vector to the specified size. Default-constructs any missing
376 /// elements, and destroys any excess.
377 /// @param ptr The base class pointer
378 /// @param count The desired size
379 /// @pre @c count<=Capacity, else @c ARENE_PRECONDITION violation
380 static constexpr void do_resize(vector_ptr ptr, std::size_t count) noexcept { vector(ptr)->resize(count); }
381};
382
383/// @brief instance of the holder of the operations of a specific vector type
384template <class Vector>
385extern constexpr operations<typename Vector::value_type> vector_operations{
386 &const_vector_operations<Vector>,
387 &operations_impl<Vector>::do_data,
388 &operations_impl<Vector>::do_element,
389 &operations_impl<Vector>::template do_at<>,
390 &operations_impl<Vector>::do_front,
391 &operations_impl<Vector>::do_back,
392 &operations_impl<Vector>::do_begin,
393 &operations_impl<Vector>::do_end,
394 &operations_impl<Vector>::do_push_back,
395 &operations_impl<Vector>::do_insert,
396 &operations_impl<Vector>::do_erase,
397 &operations_impl<Vector>::do_erase_range,
398 &operations_impl<Vector>::do_pop_back,
399 &operations_impl<Vector>::do_clear,
400 &operations_impl<Vector>::do_resize
401};
402
403// parasoft-begin-suppress AUTOSAR-A11_3_1-a-2 "Passkey idiom permitted by A11-3-1 Permit #1"
404/// @brief Passkey type for internal access
405class ivref_passkey {
406 /// @brief Prevent construction from @c {}
407 explicit ivref_passkey() = default;
408 /// @brief friend declaration to allow this class to construct the passkey
409 template <typename T>
410 friend class ::arene::base::const_vector_reference;
411};
412// parasoft-end-suppress AUTOSAR-A11_3_1-a-2
413
414} // namespace vector_reference_detail
415
416// parasoft-begin-suppress AUTOSAR-A13_5_1-a-2 "False positive: This follows the std::map interface. The non-const
417// parasoft-begin-suppress AUTOSAR-A12_1_5-a-2 "False positive: no duplication"
418/// @brief A reference class to an inline_vector object, with size-erased type
419/// @tparam T The type of each element
420template <typename T>
422 /// @brief A pointer to the base class
423 inline_vector_detail::vector_base<T>* vector_ptr_;
424 /// @brief A pointer to the struct holding operation implementations
425 vector_reference_detail::operations<T> const* op_ptrs_;
426
427 public:
428 /// @brief The value type
429 using value_type = T;
430 /// @brief The size type
432 /// @brief The iterator type of the underlying vector
434 /// @brief The const iterator type of the underlying vector
436
437 /// @brief Construct a vector reference to the supplied inline_vector
438 /// @tparam Capacity The maximum number of elements that can be stored
439 /// @param vec The inline_vector to reference
440 template <std::size_t Capacity>
441 constexpr explicit vector_reference(inline_vector<T, Capacity>& vec)
442 : vector_ptr_(&vec),
443 op_ptrs_(&vector_reference_detail::vector_operations<inline_vector<T, Capacity>>) {}
444
445 /// @brief Construct a vector reference to the supplied external_vector
446 /// @param vec The external_vector to reference
450
451 /// @brief Obtain a pointer to the stored elements
452 /// @return A raw pointer to the elements
453 constexpr auto data() const noexcept -> T* { return op_ptrs_->data(vector_ptr_); }
454
455 /// @brief Get the current number of live elements in the vector
456 /// @return the size
457 constexpr auto size() const noexcept -> size_type { return op_ptrs_->const_ops->size(vector_ptr_); }
458
459 /// @brief Get the capacity
460 /// @return the capacity
461 constexpr auto capacity() const noexcept -> size_type { return op_ptrs_->const_ops->capacity(vector_ptr_); }
462 /// @brief Check if the vector is empty.
463 /// @return @c true if the vector is empty, @c false otherwise
464 constexpr auto max_size() const noexcept -> size_type { return op_ptrs_->const_ops->capacity(vector_ptr_); }
465
466 /// @brief Check if the vector is empty.
467 /// @return @c true if the vector is empty, @c false otherwise
468 constexpr auto empty() const noexcept -> bool { return op_ptrs_->const_ops->empty(vector_ptr_); }
469
470 /// @brief Obtain a const reference to the element with the specified index
471 /// @param index The index of the element
472 /// @return A reference to the element
473 /// @pre @c index must be less than @c size()
474 constexpr auto operator[](size_type index) const noexcept -> T& { return op_ptrs_->element(vector_ptr_, index); }
475 /// @brief Obtain a @c const reference to the element with the specified index
476 /// @tparam AreExceptionsEnabled Used to disable this overload if exceptions are not enabled.
477 /// @param index The index of the element
478 /// @return A reference to the element
479 /// @throws std::out_of_range if the index is out of range
480 template <
483 constexpr auto at(size_type index) const -> T& {
484 return op_ptrs_->at(vector_ptr_, index);
485 }
486
487 /// @brief Get a const reference to the first element.
488 /// @return A reference to the element
489 /// @pre The vector must not be empty
490 constexpr auto front() const noexcept -> T& { return op_ptrs_->front(vector_ptr_); }
491 /// @brief Get a const reference to the last element.
492 /// @return A reference to the element
493 /// @pre The vector must not be empty
494 constexpr auto back() const noexcept -> T& { return op_ptrs_->back(vector_ptr_); }
495
496 /// @brief Obtain a const iterator to the start of the vector.
497 /// @return The iterator
498 constexpr auto begin() const noexcept -> iterator { return op_ptrs_->begin(vector_ptr_); }
499 /// @brief Obtain a const iterator to the end of the vector.
500 /// @return The iterator
501 constexpr auto end() const noexcept -> iterator { return op_ptrs_->end(vector_ptr_); }
502 /// @brief Obtain an const iterator to the start of the vector.
503 /// @return The iterator
504 constexpr auto cbegin() const noexcept -> const_iterator { return op_ptrs_->const_ops->begin(vector_ptr_); }
505 /// @brief Obtain a const iterator to the end of the vector.
506 /// @return The iterator
507 constexpr auto cend() const noexcept -> const_iterator { return op_ptrs_->const_ops->end(vector_ptr_); }
508
509 /// @brief Add a new element to the end of the vector as a copy of the supplied value.
510 /// @param element The value to copy
511 /// @pre @c size()<Capacity, else @c ARENE_PRECONDITION violation
512 constexpr void push_back(T const& element) const noexcept { return op_ptrs_->push_back(vector_ptr_, element); }
513
514 /// @brief Insert a new element at the specified position in the vector by
515 /// copy-constructing or copy-assigning from the supplied value. Any existing
516 /// elements from the specified position to the end of the vector are moved to
517 /// make room.
518 /// @param pos The position to insert the new element
519 /// @param value The value to copy from
520 /// @return An iterator referring to the new element
521 /// @pre @c size()<Capacity, else @c ARENE_PRECONDITION violation
522 constexpr auto insert(const_iterator pos, T const& value) const noexcept -> iterator {
524 }
525
526 /// @brief Insert a range of elements at the specified position.
527 /// @param pos An iterator referring to the location to insert the elemnets
528 /// @param first An iterator referring to the start of the range to insert
529 /// @param last An iterator referring to the end of the range to insert
530 /// @return An iterator referring to the first inserted value if any, or representing the same position as @c pos
531 /// otherwise
532 /// @pre @c pos Must be a valid iterator to @c this
533 template <typename InputIterator>
535 auto ncpos = begin() + (pos - begin());
536 // parasoft-begin-suppress AUTOSAR-A7_1_5-a "False Positive: 'element' has return type of range element, which may
537 // be non-fundamental"
538 for (auto&& elem : make_subrange(first, last)) {
539 // parasoft-end-suppress AUTOSAR-A7_1_5-a
540 std::ignore = this->insert(pos++, elem);
541 }
542 return ncpos;
543 }
544
545 /// @brief Erase the element at the specified position.
546 /// @param iter An iterator referring to the element to erase
547 /// @return An iterator to the first element following the erased element, or @c end() if there is no such element
548 /// @pre @c pos Must be a valid iterator to a live element in @c this
549 constexpr auto erase(const_iterator iter) const noexcept -> iterator { return op_ptrs_->erase(vector_ptr_, iter); }
550 /// @brief Erase all the element in the specified range.
551 /// @param first An iterator referring to the start of the range to erase
552 /// @param last An iterator referring to the end of the range to erase
553 /// @return An iterator to the first element following the erased elements, or @c end() if there is no such element
554 /// @pre @c [first,last) Must be a valid iterator range in @c this
555 constexpr auto erase(const_iterator first, const_iterator last) const noexcept -> iterator {
557 }
558 /// @brief Destroy the last element in the vector and decrease the size
559 /// @pre The vector must not be empty
560 constexpr void pop_back() const noexcept { op_ptrs_->pop_back(vector_ptr_); }
561
562 /// @brief Destroy all elements and set the size to zero
563 constexpr void clear() const noexcept { op_ptrs_->clear(vector_ptr_); }
564
565 /// @brief Resize the vector to the specified size. Default-constructs any missing
566 /// elements, and destroys any excess.
567 /// @param count The desired size
568 /// @pre @c count<=Capacity, else @c ARENE_PRECONDITION violation
569 constexpr void resize(size_type count) const { op_ptrs_->resize(vector_ptr_, count); }
570
571 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
572
573 /// @brief Get a pointer to the underlying vector object
574 /// @return A pointer to the underlying vector object
577 return vector_ptr_;
578 }
579
580 /// @brief Get a pointer to the struct holding const operation implementations
581 /// @return A pointer to the struct holding const operation implementations
584 return op_ptrs_->const_ops;
585 }
586
587 // parasoft-end-suppress CERT_C-EXP37-a
588};
589// parasoft-end-suppress AUTOSAR-A12_1_5-a-2
590// parasoft-end-suppress AUTOSAR-A13_5_1-a-2
591
592// parasoft-begin-suppress AUTOSAR-A12_1_5-a-2 "False positive: no duplication"
593// parasoft-begin-suppress AUTOSAR-A13_5_1-a-2 "False positive: Only const operator[] provided"
594/// @brief A const reference class to an inline_vector object, with size-erased type
595/// @tparam T The type of each element
596template <typename T>
597class const_vector_reference {
598 /// @brief A pointer to the base class
599 inline_vector_detail::vector_base<T> const* vector_ptr_;
600 /// @brief A pointer to the struct holding operation implementations
601 vector_reference_detail::const_operations<T> const* op_ptrs_;
602
603 public:
604 /// @brief The value type
605 using value_type = T;
606 /// @brief The size type
607 using size_type = std::size_t;
608 /// @brief The const iterator type of the underlying vector
609 using const_iterator = typename inline_vector_detail::vector_base<T>::const_iterator;
610 /// @brief The iterator type to be used in this const context
611 using iterator = const_iterator;
612
613 /// @brief Construct a vector reference to the supplied inline_vector
614 /// @tparam Capacity The maximum number of elements that can be stored
615 /// @param vec The inline_vector to reference
616 template <std::size_t Capacity>
617 constexpr explicit const_vector_reference(inline_vector<T, Capacity> const& vec)
618 : vector_ptr_(&vec),
619 op_ptrs_(&vector_reference_detail::const_vector_operations<inline_vector<T, Capacity>>) {}
620
621 /// @brief Construct a vector reference to the supplied external_vector
622 /// @param vec The external_vector to reference
623 constexpr explicit const_vector_reference(external_vector<T> const& vec)
624 : vector_ptr_(&vec),
625 op_ptrs_(&vector_reference_detail::const_vector_operations<external_vector<T>>) {}
626
627 /// @brief Construct a const reference out of a non-const one
628 /// @param ref The non-const reference class
629 constexpr explicit const_vector_reference(vector_reference<T> const ref)
630 : vector_ptr_(ref.get_vector_ptr(vector_reference_detail::ivref_passkey{})),
631 op_ptrs_(ref.const_operations_ptr(vector_reference_detail::ivref_passkey{})) {}
632
633 /// @brief Obtain a pointer to the stored elements
634 /// @return A raw pointer to the elements
635 constexpr auto data() const noexcept -> T const* { return op_ptrs_->data(vector_ptr_); }
636
637 /// @brief Get the current number of live elements in the vector
638 /// @return the size
639 constexpr auto size() const noexcept -> size_type { return op_ptrs_->size(vector_ptr_); }
640
641 /// @brief Get the capacity
642 /// @return the capacity
643 constexpr auto capacity() const noexcept -> size_type { return op_ptrs_->capacity(vector_ptr_); }
644 /// @brief Check if the vector is empty.
645 /// @return @c true if the vector is empty, @c false otherwise
646 constexpr auto max_size() const noexcept -> size_type { return op_ptrs_->capacity(vector_ptr_); }
647
648 /// @brief Check if the vector is empty.
649 /// @return @c true if the vector is empty, @c false otherwise
650 constexpr auto empty() const noexcept -> bool { return op_ptrs_->empty(vector_ptr_); }
651
652 /// @brief Obtain a const reference to the element with the specified index
653 /// @param index The index of the element
654 /// @return A reference to the element
655 /// @pre @c index must be less than @c size()
656 constexpr auto operator[](size_type index) const noexcept -> T const& {
657 return op_ptrs_->element(vector_ptr_, index);
658 }
659 /// @brief Obtain a @c const reference to the element with the specified index
660 /// @tparam AreExceptionsEnabled Used to disable this overload if exceptions are not enabled.
661 /// @param index The index of the element
662 /// @return A reference to the element
663 /// @throws std::out_of_range if the index is out of range
664 template <
665 bool AreExceptionsEnabled = detail::are_exceptions_enabled::value,
666 constraints<std::enable_if_t<AreExceptionsEnabled>> = nullptr>
667 constexpr auto at(size_type index) const -> T const& {
668 return op_ptrs_->at(vector_ptr_, index);
669 }
670
671 /// @brief Get a const reference to the first element.
672 /// @return A reference to the first element
673 /// @pre The vector must not be empty
674 constexpr auto front() const noexcept -> T const& { return op_ptrs_->front(vector_ptr_); }
675 /// @brief Get a const reference to the last element.
676 /// @return A reference to the lastelement
677 /// @pre The vector must not be empty
678 constexpr auto back() const noexcept -> T const& { return op_ptrs_->back(vector_ptr_); }
679
680 /// @brief Obtain a const iterator to the start of the vector.
681 /// @return The iterator
682 constexpr auto begin() const noexcept -> const_iterator { return op_ptrs_->begin(vector_ptr_); }
683 /// @brief Obtain a const iterator to the end of the vector.
684 /// @return The iterator
685 constexpr auto end() const noexcept -> const_iterator { return op_ptrs_->end(vector_ptr_); }
686 /// @brief Obtain an const iterator to the start of the vector.
687 /// @return The iterator
688 constexpr auto cbegin() const noexcept -> const_iterator { return op_ptrs_->begin(vector_ptr_); }
689 /// @brief Obtain a const iterator to the end of the vector.
690 /// @return The iterator
691 constexpr auto cend() const noexcept -> const_iterator { return op_ptrs_->end(vector_ptr_); }
692};
693// parasoft-end-suppress AUTOSAR-A13_5_1-a-2
694// parasoft-end-suppress AUTOSAR-A12_1_5-a-2
695
696namespace vector_reference_detail {
697
698/// @brief helper type used to define the @c make_const_vector_reference global
699/// function object
700///
701class make_const_vector_reference_fn {
702 public:
703 /// @brief creates a @c const_vector_reference, deducing the value type
704 /// from the argument
705 /// @tparam Vector the type of the vector
706 /// @param vec @c inline_vector to create a reference from
707 ///
708 /// Helper to create an object of type @c const_vector_reference, using
709 /// template argument deduction to determine the template argument of the
710 /// result.
711 ///
712 template <
713 class Vector,
714 constraints<std::enable_if_t<
715 std::is_base_of<inline_vector_detail::vector_base<typename Vector::value_type>, Vector>::value>> = nullptr>
716 constexpr auto operator()(Vector const& vec) const noexcept -> const_vector_reference<typename Vector::value_type> {
717 return const_vector_reference<typename Vector::value_type>{vec};
718 }
719
720 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
721
722 /// @brief creates a @c const_vector_reference, deducing the value type
723 /// from the argument
724 /// @tparam Vector the type of the vector
725 ///
726 /// Helper to create an object of type @c const_vector_reference, using
727 /// template argument deduction to determine the template argument of the
728 /// result.
729 ///
730 /// @note This overload is deleted to prevent construction from an rvalue
731 /// reference.
732 ///
733 template <
734 class Vector,
735 constraints<std::enable_if_t<
736 std::is_base_of<inline_vector_detail::vector_base<typename Vector::value_type>, Vector>::value>> = nullptr>
737 constexpr auto operator()(Vector const&& vec) const noexcept = delete;
738
739 // parasoft-end-suppress CERT_C-EXP37-a
740};
741
742/// @brief helper type used to define the @c make_vector_reference global
743/// function object
744///
745class make_vector_reference_fn : make_const_vector_reference_fn {
746 public:
747 /// @brief creates a @c vector_reference, deducing the value type from
748 /// the argument
749 /// @tparam Vector the type of the vector
750 /// @param vec @c vector to create a reference from
751 ///
752 /// Helper to create an object of type @c vector_reference, using
753 /// template argument deduction to determine the template argument of the
754 /// result.
755 ///
756 template <
757 class Vector,
758 constraints<std::enable_if_t<
759 std::is_base_of<inline_vector_detail::vector_base<typename Vector::value_type>, Vector>::value>> = nullptr>
760 constexpr auto operator()(Vector& vec) const noexcept -> vector_reference<typename Vector::value_type> {
761 return vector_reference<typename Vector::value_type>{vec};
762 }
763
764 // otherwise create a const_vector_reference
765 using make_const_vector_reference_fn::operator();
766};
767
768} // namespace vector_reference_detail
769
770// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to
771// create a per-TU reference to a global object used in multiple TUs."
772// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to
773// create a per-TU reference to a global object used in multiple TUs."
774
775namespace {
776
777// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: variable is initialized"
778
779/// @def arene::base::make_vector_reference
780/// @brief creates a @c vector_reference or @c
781/// const_vector_reference, deducing the value type from the argument
782///
783/// Global function object used to create a @c vector_reference from an
784/// @c inline_vector.
785///
786ARENE_CPP14_INLINE_VARIABLE(vector_reference_detail::make_vector_reference_fn, make_vector_reference);
787
788/// @def arene::base::make_const_vector_reference
789/// @brief creates a @c const_vector_reference, deducing the value type from
790/// the argument
791///
792/// Global function object used to create a @c const_vector_reference from an
793/// @c inline_vector.
794///
795ARENE_CPP14_INLINE_VARIABLE(vector_reference_detail::make_const_vector_reference_fn, make_const_vector_reference);
796
797// parasoft-end-suppress CERT_CPP-DCL56-a
798
799/// @brief Alias for backward compatibility
800/// @tparam T The type of each element
801/// @deprecated Prefer to use @c vector_reference, as it supports both @c inline_vector and @c external_vector
802template <class T>
803using inline_vector_reference = vector_reference<T>;
804
805/// @brief Alias for backward compatibility
806/// @tparam T The type of each element
807/// @deprecated Prefer to use @c vector_reference, as it supports both @c inline_vector and @c external_vector
808template <class T>
809using const_inline_vector_reference = const_vector_reference<T>;
810
811} // namespace
812
813// parasoft-end-suppress AUTOSAR-M7_3_3-a
814// parasoft-end-suppress CERT_CPP-DCL59-a
815
816} // namespace base
817} // namespace arene
818
819#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_VECTOR_REFERENCE_HPP_
A reference class to an inline_vector object, with size-erased type.
Definition vector_reference.hpp:421
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10