Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
external_vector.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_EXTERNAL_VECTOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_EXTERNAL_VECTOR_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/detail/wrapped_iterator.hpp" // IWYU pragma: keep
13#include "arene/base/inline_container/detail/comparison_interface.hpp"
14#include "arene/base/inline_container/detail/try_construct_interface.hpp"
15#include "arene/base/inline_container/detail/unsafe_element_storage.hpp"
22#include "arene/base/stdlib_choice/cstddef.hpp"
23#include "arene/base/stdlib_choice/declval.hpp"
24#include "arene/base/stdlib_choice/enable_if.hpp"
25#include "arene/base/stdlib_choice/forward.hpp"
26#include "arene/base/stdlib_choice/ignore.hpp"
27#include "arene/base/stdlib_choice/initializer_list.hpp"
28#include "arene/base/stdlib_choice/integral_constant.hpp"
29#include "arene/base/stdlib_choice/is_assignable.hpp"
30#include "arene/base/stdlib_choice/is_constructible.hpp"
31#include "arene/base/stdlib_choice/is_copy_assignable.hpp"
32#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
33#include "arene/base/stdlib_choice/is_default_constructible.hpp"
34#include "arene/base/stdlib_choice/is_move_assignable.hpp"
35#include "arene/base/stdlib_choice/is_move_constructible.hpp"
36#include "arene/base/stdlib_choice/iterator_traits.hpp"
37#include "arene/base/stdlib_choice/move_iterator.hpp"
45
46// parasoft-end-suppress AUTOSAR-A16_2_2-a
47
48// parasoft-begin-suppress AUTOSAR-A7_1_5-a "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
49// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
50// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
51// parasoft-begin-suppress AUTOSAR-A1_1_1-b-2 "False positive: This is all conforming code"
52
53namespace arene {
54namespace base {
55
56/// @brief a container similar to @c std::vector<T> that uses an external buffer
57/// for storage
58/// @tparam T value type of the container
59///
60/// A container where storage for the values is provided as a buffer, which is
61/// external to the class. The lifetime of the external buffer must bound the
62/// lifetime of @c external_vector (i.e. the external buffer must outlive the
63/// @c external_vector that uses it).
64///
65template <typename T>
66class external_vector;
67
68namespace external_vector_detail {
69
70/// @brief @c try_construct_policy for @c external_vector
71/// @tparam T container value type
72///
73template <class T>
74class try_construct_policy {
75 public:
76 /// @brief value type stored by the @c external_vector
77 using value_type = T;
78 /// @brief size type of the @c external_vector
79 using size_type = std::size_t;
80
81 /// @brief obtain the value capacity of a byte buffer
82 /// @param buffer external buffer of bytes
83 /// @pre the alignment of @c buffer matches the alignment of @c value_type
84 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
85 /// @return number of values that can be placed in @c buffer
86 ///
87 static auto capacity_of(span<byte> buffer) noexcept -> size_type {
88 ARENE_PRECONDITION(is_aligned<alignof(value_type)>(buffer.data()));
89 ARENE_PRECONDITION((buffer.size() % sizeof(value_type)) == 0U);
90
91 return buffer.size() / sizeof(value_type);
92 }
93};
94
95/// @brief storage base class for @c external_vector
96/// @tparam T value type of the @c external_vector
97///
98/// Handles element construction and destruction for an @c external_vector into
99/// an externally provided buffer. This class provides a minimal interface to
100/// the underlying storage; @c vector_interface provides the bulk of
101/// "vector"-type functionality for @c external_vector.
102///
103template <class T>
104class storage_base {
105 /// @brief tag type used to protect construction of iterators
106 class iterator_passkey {
107 public:
108 /// @brief default constructor
109 explicit iterator_passkey() = default;
110 };
111
112 public:
113 /// @brief value type stored by this class
114 using value_type = T;
115 /// @brief size type
116 using size_type = std::size_t;
117 /// @brief type of a pointer to a stored value
118 using pointer = value_type*;
119 /// @brief type of a const pointer to a stored value
120 using const_pointer = value_type const*;
121 /// @brief iterator type
122 using iterator = detail::wrapped_iterator<pointer, iterator_passkey>;
123 /// @brief const iterator type
124 using const_iterator = detail::wrapped_iterator<const_pointer, iterator_passkey>;
125 /// @brief difference type
126 using difference_type = typename std::iterator_traits<iterator>::difference_type;
127
128 private:
129 /// @brief underlying element type that allows a value to exist or not exist
130 using element_type = inline_container::detail::unsafe_element_storage<value_type>;
131
132 /// @brief view to external storage
133 span<element_type> storage_;
134 /// @brief number of active values
135 size_type size_;
136
137 /// @brief convert a span of bytes into a span of elements
138 /// @param buffer external buffer of bytes
139 ///
140 /// Starts the lifetime of elements in the external buffer of bytes.
141 ///
142 /// @return span of elements
143 ///
144 static auto make_storage_array(span<byte> buffer) noexcept -> span<element_type> {
145 auto const count = try_construct_policy<T>::capacity_of(buffer);
146
147 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
148 auto* const start = (count == 0U) ? nullptr : new (buffer.data()) element_type[count]{};
149
150 return {start, count};
151 }
152
153 public:
154 /// @brief construct an empty @c storage_base
155 /// @param buffer external buffer of bytes
156 /// @post <tt> size() == 0 </tt>
157 ///
158 /// Initializes a @c storage_base to enable storage of @c value_type values in
159 /// @c buffer.
160 ///
161 explicit storage_base(span<byte> buffer) noexcept
162 : storage_{make_storage_array(buffer)},
163 size_{0U} {}
164
165 /// @brief destroy values stored in the external buffer
166 ///
167 ~storage_base() { storage_shrink_to(0U); }
168
169 /// @brief copying is managed externally from this type
170 storage_base(storage_base const&) = delete;
171 /// @brief moving is managed externally from this type
172 storage_base(storage_base&&) = delete;
173 /// @brief copying is managed externally from this type
174 auto operator=(storage_base const&) -> storage_base& = delete;
175 /// @brief moving is managed externally from this type
176 auto operator=(storage_base&&) -> storage_base& = delete;
177
178 /// @brief Obtain a span over the stored values
179 /// @return A @c span<value_type> for the initialized values
180 auto storage_values() noexcept -> span<value_type> {
181 if (storage_capacity() == 0U) {
182 return {};
183 }
184 return {&storage_.front().object, size_};
185 }
186 /// @brief Obtain a @c const span over the stored values
187 /// @return A <c>span<const value_type></c> for the initialized values
188 auto storage_values() const noexcept -> span<value_type const> {
189 if (storage_capacity() == 0U) {
190 return {};
191 }
192 return {&storage_.front().object, size_};
193 }
194
195 /// @brief obtain the number of values that can be stored
196 /// @return number of values that can be stored
197 ///
198 auto storage_capacity() const noexcept -> size_type { return storage_.size(); }
199
200 /// @brief obtain the number of values active in storage
201 /// @return number of values active in storage
202 ///
203 auto stored_size() const noexcept -> size_type { return size_; }
204
205 /// @brief append a value to the end, constructed in place from the
206 /// supplied arguments
207 /// @tparam Args argument types
208 /// @param args constructor arguments
209 /// @pre <tt> size() < capacity() </tt>
210 template <class... Args, constraints<std::enable_if_t<std::is_constructible<value_type, Args...>::value>> = nullptr>
211 void storage_emplace_back(Args&&... args) noexcept(std::is_nothrow_constructible<value_type, Args...>::value) {
212 ARENE_PRECONDITION(size_ < storage_capacity());
213
214 auto& location = storage_[size_];
215 destroy_at(&location.dummy);
216 std::ignore = construct_at(&location.object, std::forward<Args>(args)...);
217
218 ++size_;
219 }
220
221 /// @brief reduce size, removing values at the back
222 /// @param count target number of values
223 /// @pre <tt> count <= size() </tt>
224 /// @post <tt> size() == count </tt>
225 ///
226 auto storage_shrink_to(std::size_t count) noexcept -> void {
227 ARENE_PRECONDITION(count <= size_);
228
229 for (auto& location : storage_.subspan(count, size_ - count)) {
230 destroy_at(&location.object);
231 std::ignore = construct_at(&location.dummy);
232 }
233
234 size_ = count;
235 }
236
237 /// @brief swap contents with a different @c storage_base
238 /// @param other @c storage_base to swap contents with
239 /// @note does not swap individual elements but rather the underlying buffer
240 ///
241 auto swap(storage_base& other) noexcept -> void {
242 arene::base::swap(this->storage_, other.storage_);
243 arene::base::swap(this->size_, other.size_);
244 }
245
246 /// @brief check that buffer is not already used by this storage
247 /// @param buffer buffer to check against
248 ///
249 /// Some of the external_vector constructors have the precondition that there is no aliasing, but there is not a
250 /// portable way to check for aliasing in C++ today. This function performs a best-effort check that the data pointer
251 /// of the given buffer is not equal to the storage's data pointer, intended to help catch the case of a pointer/span
252 /// being accidentally reused.
253 ///
254 /// If a more expansive check is required, it will need to be platform-dependent and should be implemented in the
255 /// compiler_support package.
256 ///
257 /// @pre @c buffer.data() != storage_.data()
258 /// @return @c buffer
259 ///
260 auto verify_not_already_in_use(span<byte> buffer) const noexcept -> span<byte> {
261 ARENE_PRECONDITION(static_cast<void*>(buffer.data()) != static_cast<void*>(storage_.data()));
262 return buffer;
263 }
264};
265
266/// @brief Base class for @c vector_interface that redirects via the @c storage_base base class of the derived class to
267/// access the external storage
268/// @tparam ValueType The type of the value stored in the container
269template <typename ValueType>
270class external_storage_base {
271 protected:
272 /// @brief Passkey to allow access to the storage
273 class storage_access_tag {
274 public:
275 /// @brief Explicit default constructor to require named initialization
276 explicit storage_access_tag() = default;
277 };
278
279 private:
280 /// @brief The type of the derived class
281 using derived_external_vector = external_vector<ValueType>;
282
283 /// @brief Get the storage from the derived @c external_vector
284 /// @return A reference to the storage class
285 auto get_storage() const noexcept -> storage_base<ValueType> const& {
286 return derived_external_vector::get_storage(storage_access_tag{}, this);
287 }
288
289 /// @brief Get the storage from the derived @c external_vector
290 /// @return A reference to the storage class
291 auto get_storage() noexcept -> storage_base<ValueType>& {
292 return derived_external_vector::get_storage(storage_access_tag{}, this);
293 }
294
295 public:
296 /// @brief The type of the stored values
297 using value_type = ValueType;
298 /// @brief The type used for sizes of the vector
299 using size_type = std::size_t;
300
301 protected:
302 /// @brief Get the maximum number of items that can be stored in this vector
303 /// @return The maximum number of items that can be stored in this vector
304 auto capacity() const noexcept -> size_type { return get_storage().storage_capacity(); }
305 /// @brief Get the maximum number of items that can be stored in this vector
306 /// @return The maximum number of items that can be stored in this vector
307 auto max_size() const noexcept -> size_type { return capacity(); }
308
309 /// @brief Get a @c span for the initialized values in the vector
310 /// @return A @c span for the initialized values in the vector
311 auto values() noexcept -> span<value_type> { return get_storage().storage_values(); }
312 /// @brief Get a @c span for the initialized values in the vector
313 /// @return A @c span for the initialized values in the vector
314 auto values() const noexcept -> span<value_type const> { return get_storage().storage_values(); }
315
316 /// @brief Construct a new element at the end of the existing range, from the supplied arguments.
317 /// @tparam Args The types of the arguments
318 /// @param args The constructor arguments
319 template <typename... Args>
320 void internal_emplace_back(Args&&... args) noexcept(std::is_nothrow_constructible<ValueType, Args...>::value) {
321 get_storage().storage_emplace_back(std::forward<Args>(args)...);
322 }
323
324 /// @brief If the current size is more than @c new_size, reduce the size to @c new_size.
325 /// @param new_size The new size limit
326 void shrink_to(size_type new_size) noexcept { get_storage().storage_shrink_to(new_size); }
327};
328
329} // namespace external_vector_detail
330
331// parasoft-begin-suppress AUTOSAR-A10_1_1-a "False positive: Only inherits from a single non-interface base classes"
332// parasoft-begin-suppress AUTOSAR-M14_5_3-a "False positive: Copy assignment is defined or deleted as appropriate"
333// parasoft-begin-suppress AUTOSAR-A12_1_6-a "False positive: storage_base called as appropriate"
334
335// parasoft-begin-suppress AUTOSAR-A7_3_1-a "'storage_base::swap' is not part
336// of the public interface and is not hidden. 'storage_base' is a private base.
337
338// parasoft-begin-suppress AUTOSAR-A10_2_1-b "'storage_base::swap' is not part
339// of the public interface and is not hidden. 'storage_base' is a private base.
340
341/// @brief a container similar to @c std::vector<T> that uses an external buffer
342/// for storage
343/// @tparam T value type of the container
344///
345/// A container where storage for the values is provided as a buffer, which is
346/// external to the class. The lifetime of the external buffer must bound the
347/// lifetime of @c external_vector (i.e. the external buffer must outlive the
348/// @c external_vector that uses it).
349///
350template <class T>
351// NOLINTNEXTLINE(hicpp-special-member-functions) : false positive
353 : external_vector_detail::storage_base<T>
354 , public inline_vector_detail::vector_interface<external_vector_detail::external_storage_base<T>>
355 , public inline_container::detail::comparison_interface<external_vector<T>>
356 , public inline_container_detail::try_construct_interface<
357 external_vector<T>,
358 external_vector_detail::try_construct_policy<T>,
359 inline_container_detail::try_construct_head_args<span<byte>>> {
360 /// @brief storage base type
361 using storage_base = external_vector_detail::storage_base<T>;
362
363 /// @brief The type of the interface base class
364 using interface_base = inline_vector_detail::vector_interface<external_vector_detail::external_storage_base<T>>;
365
366 /// @brief comparison base type
367 using comparison_base = inline_container::detail::comparison_interface<external_vector<T>>;
368
369 /// @brief try_construct interface base type
370 using try_construct_interface = inline_container_detail::try_construct_interface<
372 external_vector_detail::try_construct_policy<T>,
373 inline_container_detail::try_construct_head_args<span<byte>>>;
374
375 /// @brief helper that provides a type to use in defining move or copy assignment
376 /// @tparam Enable @c true if defining the actual move/copy assignment
377 /// operator, @c false for the deleted overload
378 /// @tparam U qualified value type, <tt> T const & </tt> for copy assignment,
379 /// <tt> T&& </tt> for move assignment
380 /// @tparam Pred defaulted parameter that determines the result type
381 ///
382 /// Trait used when defining move or copy assignment operators.
383 ///
384 /// If @c Enable is @c true and the value type is copy/move
385 /// constructible and assignable, the result is @c external_vector.
386 /// If @c Enable is @c true and the value type is *not* copy/move
387 /// constructible and assignable, the result is @c non_constructible_dummy.
388 ///
389 /// If @c Enable is @c false and the value type is copy/move
390 /// constructible and assignable, the result is @c non_constructible_dummy.
391 /// If @c Enable is @c false and the value type is *not* copy/move
392 /// constructible and assignable, the result is @c external_vector.
393 ///
394 /// @note The result type has the reference and qualifiers properties of @c U.
395 ///
396 template <bool Enable, class U, bool Pred = std::is_constructible<T, U>::value && std::is_assignable<T&, U>::value>
397 using enable_if_assignable_t = decltype(arene::base::forward_like<U>( //
399 Enable,
402 ));
403
404 public:
405 // Alias in typedefs from base class
406 using typename interface_base::const_iterator;
407 using typename interface_base::const_pointer;
408 using typename interface_base::const_reference;
409 using typename interface_base::const_reverse_iterator;
410 using typename interface_base::difference_type;
411 using typename interface_base::iterator;
412 using typename interface_base::pointer;
413 using typename interface_base::reference;
414 using typename interface_base::reverse_iterator;
415 using typename interface_base::size_type;
416 using typename interface_base::value_type;
417
418 using interface_base::assign;
419 using interface_base::capacity;
420 using interface_base::max_size;
421
422 /// @brief default destructor
423 ///
424 /// Detroys values stored in the external buffer.
425 ///
426 ~external_vector() = default;
427
428 /// @brief construct an empty @c external_vector
429 /// @param buffer external buffer of bytes
430 /// @pre the alignment of @c buffer matches the alignment of @c value_type
431 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
432 /// @post <tt> size() == 0 </tt>
433 /// @post <tt> capacity() == buffer.size() / sizeof(value_type) </tt>
434 ///
435 /// Constructs an empty @c external_vector that is configured to use @c buffer
436 /// as external storage.
437 ///
438 explicit external_vector(span<byte> buffer) noexcept
439 : storage_base{buffer},
440 interface_base{},
441 comparison_base{this},
442 try_construct_interface{this} {}
443
444 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: All these constructors delegate to another, which does
445 // initialize the base class"
446
447 /// @brief construct an empty @c external_vector
448 /// @post <tt> size() == 0 </tt>
449 /// @post <tt> capacity() == 0 </tt>
450 ///
451 /// Constructs an empty @c external_vector without initializing any external
452 /// storage.
453 ///
456
457 /// @brief construct an @c external_vector from a range
458 /// @tparam I input iterator type
459 /// @param buffer external buffer of bytes
460 /// @param first beginning of the range of values
461 /// @param last end of the range of values
462 /// @pre the alignment of @c buffer matches the alignment of @c value_type
463 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
464 /// @pre <tt> std::distance(first, last) </tt> does not exceed the number of
465 /// values that can be stored in @c buffer
466 ///
467 /// Constructs an @c external_vector from range <tt> [first, last) </tt>.
468 ///
469 template <
470 class I,
474 nullptr>
475 external_vector(span<byte> buffer, I first, I last) noexcept(
478 )
479 : external_vector{buffer} {
480 // this is likely to be replaced with 'vector_interface'
481 while (first != last) {
482 std::ignore = this->emplace_back(*first);
483 arene::base::advance(first, typename std::iterator_traits<I>::difference_type{1});
484 }
485 }
486
487 /// @brief construct an @c external_vector with default constructed values
488 /// @tparam U dummy used to constrain this constructor to value types that are
489 /// default constructible
490 /// @param buffer external buffer of bytes
491 /// @param count size of the @c external_vector
492 /// @pre the alignment of @c buffer matches the alignment of @c value_type
493 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
494 /// @pre @c count does not exceed the number of values that can be stored in
495 /// @c buffer
496 ///
497 /// Constructs an @c external_vector containing @c count number of default
498 /// constructed values.
499 ///
500 template <class U = value_type, constraints<std::enable_if_t<std::is_default_constructible<U>::value>> = nullptr>
502 : external_vector{buffer} {
503 while (count != 0U) {
504 std::ignore = this->emplace_back();
505 --count;
506 }
507 }
508
509 /// @brief construct an @c external_vector with copies of a value
510 /// @tparam U dummy used to constrain this constructor to value types that are
511 /// copy constructible
512 /// @param buffer external buffer of bytes
513 /// @param count size of the @c external_vector
514 /// @param value value to copy into the @c external_vector
515 /// @pre the alignment of @c buffer matches the alignment of @c value_type
516 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
517 /// @pre @c count does not exceed the number of values that can be stored in
518 /// @c buffer
519 ///
520 /// Constructs an @c external_vector containing @c count number of copies of
521 /// @c value.
522 ///
523 template <class U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> = nullptr>
524 external_vector(span<byte> buffer, size_type count, value_type const& value) noexcept(
526 )
527 : external_vector{buffer} {
528 while (count != 0U) {
529 std::ignore = this->emplace_back(value);
530 --count;
531 }
532 }
533
534 // parasoft-begin-suppress AUTOSAR-A8_4_6-a "False positive: contents of 'other' are moved via use of 'move_iterator'"
535 // parasoft-begin-suppress AUTOSAR-A8_4_5-a "False positive: contents of 'other' are moved via use of 'move_iterator'"
536 // parasoft-begin-suppress AUTOSAR-A12_8_4-a "False positive: contents of 'other' are moved via use of
537 // 'move_iterator'"
538
539 /// @brief construct an @c external_vector from another @c external_vector
540 /// @tparam U dummy used to constrain this constructor to value types that are
541 /// move constructible
542 /// @param buffer external buffer of bytes
543 /// @param other another @c external_vector
544 /// @pre the alignment of @c buffer matches the alignment of @c value_type
545 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
546 /// @pre @c other.size() does not exceed the number of values that can be
547 /// stored in @c buffer
548 /// @pre @c buffer does not alias with the buffer managed by @c other
549 ///
550 /// Constructs an @c external_vector from @c other, moving values from the
551 /// external buffer managed by @c other to @c buffer.
552 ///
553 /// @post @c other is in a valid but unspecified state
554 ///
555 template <class U = value_type, constraints<std::enable_if_t<std::is_move_constructible<U>::value>> = nullptr>
556 external_vector(span<byte> buffer, external_vector&& other) noexcept(
558 )
560 other.verify_not_already_in_use(buffer),
561 std::make_move_iterator(other.begin()),
562 std::make_move_iterator(other.end())
563 } {}
564
565 // parasoft-end-suppress AUTOSAR-A8_4_6-a
566 // parasoft-end-suppress AUTOSAR-A8_4_5-a
567 // parasoft-end-suppress AUTOSAR-A12_8_4-a
568
569 /// @brief construct an @c external_vector from another @c external_vector
570 /// @tparam U dummy used to constrain this constructor to value types that are
571 /// copy constructible
572 /// @param buffer external buffer of bytes
573 /// @param other another @c external_vector
574 /// @pre the alignment of @c buffer matches the alignment of @c value_type
575 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
576 /// @pre @c other.size() does not exceed the number of values that can be
577 /// stored in @c buffer
578 /// @pre @c buffer does not alias with the buffer managed by @c other
579 ///
580 /// Constructs an @c external_vector from @c other, copying values from the
581 /// external buffer managed by @c other to @c buffer.
582 ///
583 /// @post <tt> std::equal(this->begin(), this->end(), other.begin(), other.end()) </tt> is @c true
584 ///
585 template <class U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> = nullptr>
586 external_vector(span<byte> buffer, external_vector const& other) noexcept(
588 )
589 : external_vector{other.verify_not_already_in_use(buffer), other.begin(), other.end()} {}
590
591 /// @brief construct an @c external_vector from a list of values
592 /// @tparam U dummy used to constrain this constructor to value types that are
593 /// copy constructible
594 /// @param buffer external buffer of bytes
595 /// @param init_list values to copy
596 /// @pre the alignment of @c buffer matches the alignment of @c value_type
597 /// @pre the size of @c buffer is an exact multiple of @c sizeof(value_type)
598 /// @pre @c init_list.size() does not exceed the number of values that can be
599 /// stored in @c buffer
600 ///
601 /// Constructs an @c external_vector with copies of values in @c init_list.
602 ///
603 template <class U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> = nullptr>
606 )
607 : external_vector{buffer, init_list.begin(), init_list.end()} {}
608
609 // parasoft-begin-suppress AUTOSAR-A8_4_6-a "The contents of 'other' are semantically moved via use of
610 // 'move_iterator'" parasoft-begin-suppress AUTOSAR-A8_4_5-a "The contents of 'other' are semantically moved via use
611 // of 'move_iterator'" parasoft-begin-suppress AUTOSAR-A12_8_4-a "The contents of 'other' are semantically moved via
612 // use of 'move_iterator'"
613
614 // parasoft-begin-suppress AUTOSAR-A8_4_6-a "The contents of 'other' are semantically moved via swap"
615 // parasoft-begin-suppress AUTOSAR-A8_4_5-a "The contents of 'other' are semantically moved via swap"
616 // parasoft-begin-suppress AUTOSAR-A12_8_4-a "The contents of 'other' are semantically moved via swap"
617
618 /// @brief construct an @c external_vector from another @c external_vector
619 /// @param other @c external_vector to move from
620 ///
621 /// Constructs an @c external_vector by moving contents from @c other. This
622 /// does not invoke any constructors of @c value_type.
623 ///
624 /// @post @c other is in a valid but unspecified state
625 ///
627 : external_vector{} {
629 }
630
631 // parasoft-end-suppress AUTOSAR-A8_4_6-a
632 // parasoft-end-suppress AUTOSAR-A8_4_5-a
633 // parasoft-end-suppress AUTOSAR-A12_8_4-a
634
635 /// @brief deleted copy constructor
637
638 // parasoft-end-suppress AUTOSAR-A12_1_1-a
639
640 /// @brief deleted move assignment operator
641 /// @note The argument type resolves to @c external_vector&& if
642 /// <tt> std::is_move_constructible<value_type>::value && std::is_move_assignable<value_type>::value </tt>
643 /// is @c false
644 /// @note The move assignment operator is deleted if
645 /// <tt> std::is_move_constructible<value_type>::value && std::is_move_assignable<value_type>::value </tt>
646 /// is @c false
647 ///
648 auto operator=(enable_if_assignable_t<false, value_type&&>) -> external_vector& = delete;
649
650 // parasoft-begin-suppress AUTOSAR-A8_4_6-a "The contents of 'other' are semantically moved via make_move_iterator"
651 // parasoft-begin-suppress AUTOSAR-A8_4_5-a "The contents of 'other' are semantically moved via make_move_iterator"
652 // parasoft-begin-suppress AUTOSAR-A12_8_4-a "The contents of 'other' are semantically moved via make_move_iterator"
653
654 /// @brief move assignment operator
655 /// @param other @c external_vector to move values from
656 /// @return @c *this
657 ///
658 /// Replaces the contents of the container with values from @c other. The
659 /// first <tt> std::min(size(), other.size()) </tt> values are move assigned;
660 /// remaining values are move constructed.
661 ///
662 /// @pre <tt> capacity() <= other.size() </tt>
663 /// @post @c other is in a valid but unspecified state
664 ///
665 /// @note The argument type resolves to @c external_vector&& if
666 /// <tt> std::is_move_constructible<value_type>::value && std::is_move_assignable<value_type>::value </tt>
667 /// is @c true
668 /// @note This assignment operator is deleted if
669 /// <tt> std::is_move_constructible<value_type>::value && std::is_move_assignable<value_type>::value </tt>
670 /// is @c false
671 ///
672 auto operator=(enable_if_assignable_t<true, value_type&&> other) noexcept(
674 ) -> external_vector& {
675 this->assign(std::make_move_iterator(other.begin()), std::make_move_iterator(other.end()));
676 return *this;
677 }
678
679 // parasoft-end-suppress AUTOSAR-A8_4_6-a
680 // parasoft-end-suppress AUTOSAR-A8_4_5-a
681 // parasoft-end-suppress AUTOSAR-A12_8_4-a
682
683 /// @brief deleted copy assignment operator
684 /// @note The argument type resolves to <tt> external_vector const& </tt> if
685 /// <tt> std::is_copy_constructible<value_type>::value && std::is_copy_assignable<value_type>::value </tt>
686 /// is @c false
687 /// @note The copy assignment operator is deleted if
688 /// <tt> std::is_copy_constructible<value_type>::value && std::is_copy_assignable<value_type>::value </tt>
689 /// is @c false
690 ///
691 auto operator=(enable_if_assignable_t<false, value_type const&>) -> external_vector& = delete;
692
693 /// @brief copy assignment operator
694 /// @param other @c external_vector to copy values from
695 /// @return @c *this
696 ///
697 /// Replaces the contents of the container with values from @c other. The
698 /// first <tt> std::min(size(), other.size()) </tt> values are copy assigned;
699 /// remaining values are copy constructed.
700 ///
701 /// @pre <tt> capacity() <= other.size() </tt>
702 /// @post <tt> std::equal(this->begin(), this->end(), other.begin(), other.end()) </tt> is @c true
703 ///
704 /// @note The argument type resolves to <tt> external_vector const& </tt> if
705 /// <tt> std::is_copy_constructible<value_type>::value && std::is_copy_assignable<value_type>::value </tt>
706 /// is @c true
707 /// @note This assignment operator is deleted if
708 /// <tt> std::is_copy_constructible<value_type>::value && std::is_copy_assignable<value_type>::value </tt>
709 /// is @c false
710 ///
711 auto operator=(enable_if_assignable_t<true, value_type const&> other) noexcept(
713 ) -> external_vector& {
714 this->assign(other.begin(), other.end());
715 return *this;
716 }
717
718 /// @brief assignment from a list of values
719 /// @tparam U dummy used to constrain this assignment operator to value types
720 /// that are copy constructible and copy assignable
721 /// @param init_list values to copy
722 /// @return @c *this
723 ///
724 /// Replaces the contents of the container with values from @c other. The
725 /// first <tt> std::min(size(), other.size()) </tt> values are copy assigned;
726 /// remaining values are copy constructed.
727 ///
728 /// @pre <tt> capacity() <= other.size() </tt>
729 /// @post <tt> std::equal(this->begin(), this->end(), other.begin(), other.end()) </tt> is @c true
730 ///
731 template <
732 class U = value_type,
734 nullptr>
737 ) -> external_vector& {
738 // GCC8 constraint failure without instantiating the trait here
739 // parasoft-begin-suppress CERT_C-PRE31-c "False positive: static_assert is
740 // a compile-time assert and can have no side-effects"
741 static_assert(is_random_access_iterator_v<decltype(init_list.begin())>, "");
742 // parasoft-end-suppress CERT_C-PRE31-c
743 this->assign(init_list.begin(), init_list.end());
744 return *this;
745 }
746
747 /// @brief swap contents with a different @c external_vector
748 /// @param other @c external_vector to swap contents with
749 /// @note does not swap individual elements but rather the underlying buffer
750 /// associated with each @c external_vector
751 ///
752 auto swap(external_vector& other) noexcept -> void { storage_base::swap(other); }
753
754 // parasoft-begin-suppress AUTOSAR-A7_1_1-a "Declaring 'lhs' or 'rhs' as reference to const changes semantics"
755 // parasoft-begin-suppress AUTOSAR-M7_1_2-c "Declaring 'lhs' or 'rhs' as reference to const changes semantics"
756 // parasoft-begin-suppress AUTOSAR-A8_4_9-a "Declaring 'lhs' or 'rhs' as reference to const changes semantics"
757 // parasoft-begin-suppress AUTOSAR-A11_3_1-a "Hidden friends permitted by A11-3-1 Permit #2"
758 // parasoft-begin-suppress AUTOSAR-A0_1_3-a "False positive: 'swap' is namespace scope and used in other
759 // translation units"
760
761 // parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: no commented-out code, this is documentation"
762 /// @brief swaps all the elements between two @c external_vector values
763 /// @param lhs the left hand vector to swap
764 /// @param rhs the right hand vector to swap
765 /// @post equivalent to having called @c lhs.swap(rhs);
766 /// @see external_vector::swap.
767 ///
768 // parasoft-end-suppress AUTOSAR-A2_7_2-a
769 friend auto swap(external_vector& lhs, external_vector& rhs) noexcept -> void { lhs.swap(rhs); }
770
771 // parasoft-end-suppress AUTOSAR-A7_1_1-a
772 // parasoft-end-suppress AUTOSAR-M7_1_2-c
773 // parasoft-end-suppress AUTOSAR-A8_4_9-a
774 // parasoft-end-suppress AUTOSAR-A11_3_1-a
775 // parasoft-end-suppress AUTOSAR-A0_1_3-a
776
777 /// @brief Internal function to get access to the storage for an @c external_vector
778 /// @param self The pointer to the internal base class
779 /// @return A reference to the storage class
780 static auto get_storage(
781 typename external_vector_detail::external_storage_base<T>::storage_access_tag,
782 external_vector_detail::external_storage_base<T> const* const self
783 ) noexcept -> storage_base const& {
784 return *static_cast<storage_base const*>(static_cast<external_vector const*>(self));
785 }
786
787 /// @brief Internal function to get access to the storage for an @c external_vector
788 /// @param self The pointer to the internal base class
789 /// @return A reference to the storage class
790 static auto get_storage(
791 typename external_vector_detail::external_storage_base<T>::storage_access_tag,
792 external_vector_detail::external_storage_base<T>* const self
793 ) noexcept -> storage_base& {
794 return *static_cast<storage_base*>(static_cast<external_vector*>(self));
795 }
796};
797// parasoft-end-suppress AUTOSAR-A10_1_1-a
798// parasoft-end-suppress AUTOSAR-M14_5_3-a
799// parasoft-end-suppress AUTOSAR-A7_3_1-a
800// parasoft-end-suppress AUTOSAR-A10_2_1-b
801// parasoft-end-suppress AUTOSAR-A12_1_6-a
802
803namespace ranges {
804/// @brief Indicate that @c external_vector is a borrowed range
805/// @tparam T The type of the elements in the external vector
806template <typename T>
808} // namespace ranges
809
810} // namespace base
811} // namespace arene
812#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_EXTERNAL_VECTOR_HPP_
Provides a backport of C++17's constexpr std::advance.
a container similar to std::vector<T> that uses an external buffer for storage
Definition external_vector.hpp:359
external_vector(span< byte > buffer, size_type count) noexcept(std::is_nothrow_default_constructible< value_type >::value)
construct an external_vector with default constructed values
Definition external_vector.hpp:501
external_vector(span< byte > buffer, std::initializer_list< value_type > init_list) noexcept(std::is_nothrow_copy_constructible< value_type >::value)
construct an external_vector from a list of values
Definition external_vector.hpp:604
auto operator=(enable_if_assignable_t< true, value_type const & > other) noexcept(std::is_nothrow_copy_constructible< value_type >::value &&std::is_nothrow_copy_assignable< value_type >::value) -> external_vector &
copy assignment operator
Definition external_vector.hpp:711
external_vector(span< byte > buffer, I first, I last) noexcept(denotes_nothrow_iterable_range_v< I > &&std::is_nothrow_constructible< value_type, typename std::iterator_traits< I >::reference >::value)
construct an external_vector from a range
Definition external_vector.hpp:475
external_vector(external_vector &&other) noexcept
construct an external_vector from another external_vector
Definition external_vector.hpp:626
friend auto swap(external_vector &lhs, external_vector &rhs) noexcept -> void
swaps all the elements between two external_vector values
Definition external_vector.hpp:769
auto swap(external_vector &other) noexcept -> void
swap contents with a different external_vector
Definition external_vector.hpp:752
static auto get_storage(typename external_vector_detail::external_storage_base< T >::storage_access_tag, external_vector_detail::external_storage_base< T > const *const self) noexcept -> storage_base const &
Internal function to get access to the storage for an external_vector.
Definition external_vector.hpp:780
external_vector(span< byte > buffer, size_type count, value_type const &value) noexcept(std::is_nothrow_copy_constructible< value_type >::value)
construct an external_vector with copies of a value
Definition external_vector.hpp:524
auto operator=(enable_if_assignable_t< false, value_type const & >) -> external_vector &=delete
deleted copy assignment operator
external_vector(external_vector const &)=delete
deleted copy constructor
~external_vector()=default
default destructor
auto operator=(enable_if_assignable_t< false, value_type && >) -> external_vector &=delete
deleted move assignment operator
static auto get_storage(typename external_vector_detail::external_storage_base< T >::storage_access_tag, external_vector_detail::external_storage_base< T > *const self) noexcept -> storage_base &
Internal function to get access to the storage for an external_vector.
Definition external_vector.hpp:790
external_vector(span< byte > buffer, external_vector const &other) noexcept(std::is_nothrow_copy_constructible< value_type >::value)
construct an external_vector from another external_vector
Definition external_vector.hpp:586
auto operator=(enable_if_assignable_t< true, value_type && > other) noexcept(std::is_nothrow_move_constructible< value_type >::value &&std::is_nothrow_move_assignable< value_type >::value) -> external_vector &
move assignment operator
Definition external_vector.hpp:672
auto operator=(std::initializer_list< value_type > init_list) noexcept(std::is_nothrow_copy_constructible< value_type >::value &&std::is_nothrow_copy_assignable< value_type >::value) -> external_vector &
assignment from a list of values
Definition external_vector.hpp:735
external_vector(span< byte > buffer, external_vector &&other) noexcept(std::is_nothrow_move_constructible< value_type >::value)
construct an external_vector from another external_vector
Definition external_vector.hpp:556
external_vector(span< byte > buffer) noexcept
construct an empty external_vector
Definition external_vector.hpp:438
external_vector() noexcept
construct an empty external_vector
Definition external_vector.hpp:454
An opt-in trait to indicate that a range type is a borrowed range.
Definition enable_borrowed_range.hpp:31
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
static constexpr value_type value
Definition integral_constant.hpp:34
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
Definition is_constructible.hpp:84
provides a uniform interface to the properties of an iterator
Definition iterator_traits.hpp:90
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Implementation of a backport of std::forward_like.
Definition external_vector.hpp:803
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::max_size
Definition array.hpp:537
constexpr bool is_random_access_iterator_v
Definition iterator_category_traits.hpp:332
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
constexpr bool denotes_nothrow_iterable_range_v
Check if an iterator/sentinel pair form an iterable range that can be iterated without throwing.
constexpr std::integral_constant< typename inline_map< Key, Value, Capacity, Compare >::size_type, Capacity > inline_map< Key, Value, Capacity, Compare >::capacity
Definition map.hpp:2305
constexpr auto forward_like
A backport of C++23's std::forward_like, which allows forwarding a type with the same reference and c...
constexpr auto is_aligned(Integer value) -> bool
Check if an integer is a multiple of the specified power-of-two alignment.
Definition alignment.hpp:62
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
STL namespace.
::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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
auto make_move_iterator(Iterator iter) -> std::move_iterator< Iterator >
Create a move_iterator with the specified underlying iterator.
Definition move_iterator.hpp:353
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24