Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
circular_buffer.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_CIRCULAR_BUFFER_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_CIRCULAR_BUFFER_HPP_
7
10#include "arene/base/inline_container/detail/try_construct_interface.hpp"
12#include "arene/base/stdlib_choice/cstddef.hpp"
13#include "arene/base/stdlib_choice/enable_if.hpp"
14#include "arene/base/stdlib_choice/initializer_list.hpp"
15#include "arene/base/stdlib_choice/is_constructible.hpp"
16#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
17#include "arene/base/stdlib_choice/iterator_traits.hpp"
19
20// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
21
22namespace arene {
23namespace base {
24
25/// @brief A container with fixed capacity featuring constant time insertion and
26/// removal at opposite ends; i.e., with an interface similar to std::queue. The
27/// storage for the elements is held directly within the class. Storing more
28/// than @c Capacity elements results in overwriting the oldest element.
29/// @tparam T The type of each element
30/// @tparam Capacity The maximum number of elements that can be stored
31// parasoft-begin-suppress AUTOSAR-A10_1_1-a "False positive: try_construct_interface is an interface"
32template <typename T, std::size_t Capacity>
34 : private inline_deque_detail::inline_deque_impl<T, Capacity, true>
35 , public inline_container_detail::try_construct_interface<
36 circular_buffer<T, Capacity>,
37 typename inline_deque_detail::inline_deque_try_construct_policy<T, Capacity>> {
38 /// @brief The implementation class, separate to share implementations with other similar queues
39 using impl = inline_deque_detail::inline_deque_impl<T, Capacity, true>;
40
41 /// @brief Shorthand to the mixin type that synthesizes @c try_construct functions based on the normal constructors
42 using try_construct_interface = inline_container_detail::try_construct_interface<
44 typename inline_deque_detail::inline_deque_try_construct_policy<T, Capacity>>;
45
46 public:
47 /// @copydoc impl::const_iterator
48 using typename impl::const_iterator;
49 /// @copydoc impl::const_reference
50 using typename impl::const_reference;
51 /// @copydoc impl::iterator
52 using typename impl::iterator;
53 /// @copydoc impl::reference
54 using typename impl::reference;
55 /// @copydoc impl::size_type
56 using typename impl::size_type;
57 /// @copydoc impl::value_type
58 using typename impl::value_type;
59
60 /// @copydoc impl::wrapping_allowed
61 using impl::wrapping_allowed;
62
63 // parasoft-begin-suppress AUTOSAR-A12_7_1-a "False positive: this is not the default implementation"
64 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: all base class constructors are explicitly called"
65 /// @brief Construct an empty queue
66 constexpr circular_buffer() noexcept
67 : impl(),
68 try_construct_interface(this) {}
69 // parasoft-end-suppress AUTOSAR-A12_7_1-a
70 // parasoft-end-suppress AUTOSAR-A12_1_1-a
71
72 /// @brief Construct a queue from a range of elements, taken to be in front -> back order
73 /// @tparam Iterator The type of the iterators to construct from
74 /// @param in_begin An iterator to the beginning (front) of the input range
75 /// @param in_end An iterator to the end (back+1) of the input range
76 /// @pre <c>distance(in_begin, in_end) >= 0</c> and <c><= capacity()</c>, otherwise @c ARENE_PRECONDITION violation
77 /// @pre If the behaviour of <c>distance(in_begin, in_end)</c> is undefined, then this function's behaviour is too
78 template <
79 typename Iterator,
84 constexpr circular_buffer(Iterator in_begin, Iterator in_end) noexcept(
85 noexcept(::arene::base::distance(in_begin, in_end)) &&
87 )
88 : impl(in_begin, in_end),
89 try_construct_interface(this) {}
90
91 // parasoft-begin-suppress AUTOSAR-A12_1_1-a "False positive: this is a delegating constructor"
92 /// @brief Construct a queue by copying from an initializer list of elements, taken to be in front -> back order
93 /// @param init An initializer list of the value type
94 /// @pre <c>init.size() <= capacity()</c>, otherwise @c ARENE_PRECONDITION violation
95 template <typename U = value_type, constraints<std::enable_if_t<std::is_copy_constructible<U>::value>> = nullptr>
97 ) noexcept(noexcept(circular_buffer(init.begin(), init.end())))
98 : circular_buffer(init.begin(), init.end()) {}
99 // parasoft-end-suppress AUTOSAR-A12_1_1-a
100
101 /// @copydoc impl::capacity
102 using impl::capacity;
103 /// @copydoc impl::empty
104 using impl::empty;
105 /// @copydoc impl::size
106 using impl::size;
107
108 /// @copydoc impl::begin
109 using impl::begin;
110 /// @copydoc impl::cbegin
111 using impl::cbegin;
112 /// @copydoc impl::cend
113 using impl::cend;
114 /// @copydoc impl::crbegin
115 using impl::crbegin;
116 /// @copydoc impl::crend
117 using impl::crend;
118 /// @copydoc impl::end
119 using impl::end;
120 /// @copydoc impl::rbegin
121 using impl::rbegin;
122 /// @copydoc impl::rend
123 using impl::rend;
124
125 /// @copydoc impl::back
126 using impl::back;
127 /// @copydoc impl::front
128 using impl::front;
129 /// @copydoc impl::operator[]
130 using impl::operator[];
131
132 /// @copydoc impl::emplace_back
133 using impl::emplace_back;
134 /// @copydoc impl::pop_front
135 using impl::pop_front;
136 /// @copydoc impl::push_back
137 using impl::push_back;
138};
139// parasoft-end-suppress AUTOSAR-A10_1_1-a
140
141} // namespace base
142} // namespace arene
143
144// parasoft-end-suppress AUTOSAR-M2_10_1-a-2
145
146#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_CIRCULAR_BUFFER_HPP_
constexpr auto cend() const noexcept -> const_iterator
Definition array.hpp:201
constexpr auto end() noexcept -> iterator
Definition array.hpp:163
constexpr auto back() noexcept -> T &
Definition array.hpp:284
detail::wrapped_iterator< const_pointer, array_detail::array_iter_passkey > const_iterator
Definition array.hpp:107
constexpr auto rbegin() noexcept -> reverse_iterator
Definition array.hpp:181
constexpr auto front() noexcept -> T &
Definition array.hpp:274
detail::wrapped_iterator< pointer, array_detail::array_iter_passkey > iterator
Definition array.hpp:105
constexpr auto crend() const noexcept -> const_reverse_iterator
Definition array.hpp:209
constexpr auto cbegin() const noexcept -> const_iterator
Definition array.hpp:197
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Definition array.hpp:205
constexpr auto rend() noexcept -> reverse_iterator
Definition array.hpp:185
constexpr auto begin() noexcept -> iterator
Definition array.hpp:158
constexpr circular_buffer(Iterator in_begin, Iterator in_end) noexcept(noexcept(::arene::base::distance(in_begin, in_end)) &&std::is_nothrow_constructible< value_type, typename std::iterator_traits< Iterator >::reference >::value)
Construct a queue from a range of elements, taken to be in front -> back order.
Definition circular_buffer.hpp:84
constexpr circular_buffer(std::initializer_list< value_type > init) noexcept(noexcept(circular_buffer(init.begin(), init.end())))
Construct a queue by copying from an initializer list of elements, taken to be in front -> back order...
Definition circular_buffer.hpp:96
constexpr circular_buffer() noexcept
Construct an empty queue.
Definition circular_buffer.hpp:66
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
Type trait to detect if a type is constructible from arguments of the specified types.
Definition is_constructible.hpp:29
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
Definition is_constructible.hpp:84
Definition accumulate.hpp:37
constexpr auto distance(Iterator first, Iterator last) noexcept(noexcept(distance_impl::do_distance(first, last, typename std::iterator_traits< Iterator >::iterator_category{}))) -> typename std::iterator_traits< Iterator >::difference_type
Find the number of elements between first and last.
Definition distance.hpp:79
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
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