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