Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
fixed_array.hpp
Go to the documentation of this file.
1#ifndef SBL_PRIMITIVES_CONTAINERS_FIXED_ARRAY_HPP_
2#define SBL_PRIMITIVES_CONTAINERS_FIXED_ARRAY_HPP_
3
4/**
5 * @file fixed_array.hpp
6 * @brief Fixed-size array wrapper for ARM Cortex-M systems
7 * @ingroup primitives
8 *
9 * Provides std::array-like functionality for ARM Cortex-M platforms
10 * that may not have full STL support. All storage is statically
11 * allocated with zero dynamic memory usage.
12 */
13
14#include <cstdint>
15
16namespace sbl {
17namespace primitives {
18namespace containers {
19
20/**
21 * @brief Fixed-size array container
22 *
23 * Lightweight array wrapper providing bounds-safe access and
24 * iteration support. All storage is allocated at compile time.
25 *
26 * @tparam T Element type
27 * @tparam N Array size (must be greater than 0)
28 */
29template<typename T, size_t N>
30class Array {
31 static_assert(N > 0, "Array size must be greater than 0");
32
33public:
34 using value_type = T;
35 using size_type = size_t;
36 using reference = T&;
37 using const_reference = const T&;
38 using pointer = T*;
39 using const_pointer = const T*;
40 using iterator = T*;
41 using const_iterator = const T*;
42
43 /**
44 * @brief Default constructor
45 *
46 * Elements are default-initialized.
47 */
48 Array() = default;
49
50 /**
51 * @brief Fill constructor
52 *
53 * @param value Value to fill array with
54 */
55 explicit Array(const T& value) {
56 fill(value);
57 }
58
59 /**
60 * @brief Access element at index
61 *
62 * @param index Element index
63 * @return Reference to element
64 *
65 * @note No bounds checking in release builds for performance
66 */
68 return data_[index];
69 }
70
71 /**
72 * @brief Access element at index (const)
73 *
74 * @param index Element index
75 * @return Const reference to element
76 */
78 return data_[index];
79 }
80
81 /**
82 * @brief Access element with bounds checking
83 *
84 * @param index Element index
85 * @return Reference to element
86 *
87 * @note Returns first element if index out of bounds
88 */
90 return (index < N) ? data_[index] : data_[0];
91 }
92
93 /**
94 * @brief Access element with bounds checking (const)
95 *
96 * @param index Element index
97 * @return Const reference to element
98 */
100 return (index < N) ? data_[index] : data_[0];
101 }
102
103 /**
104 * @brief Access first element
105 *
106 * @return Reference to first element
107 */
109 return data_[0];
110 }
111
112 /**
113 * @brief Access first element (const)
114 *
115 * @return Const reference to first element
116 */
118 return data_[0];
119 }
120
121 /**
122 * @brief Access last element
123 *
124 * @return Reference to last element
125 */
127 return data_[N - 1];
128 }
129
130 /**
131 * @brief Access last element (const)
132 *
133 * @return Const reference to last element
134 */
136 return data_[N - 1];
137 }
138
139 /**
140 * @brief Get pointer to underlying array
141 *
142 * @return Pointer to first element
143 */
145 return data_;
146 }
147
148 /**
149 * @brief Get pointer to underlying array (const)
150 *
151 * @return Const pointer to first element
152 */
154 return data_;
155 }
156
157 /**
158 * @brief Get iterator to beginning
159 *
160 * @return Iterator to first element
161 */
163 return data_;
164 }
165
166 /**
167 * @brief Get iterator to beginning (const)
168 *
169 * @return Const iterator to first element
170 */
172 return data_;
173 }
174
175 /**
176 * @brief Get iterator to end
177 *
178 * @return Iterator past last element
179 */
181 return data_ + N;
182 }
183
184 /**
185 * @brief Get iterator to end (const)
186 *
187 * @return Const iterator past last element
188 */
190 return data_ + N;
191 }
192
193 /**
194 * @brief Check if array is empty
195 *
196 * @return Always false (fixed-size array is never empty)
197 */
198 constexpr bool empty() const {
199 return false;
200 }
201
202 /**
203 * @brief Get array size
204 *
205 * @return Number of elements (N)
206 */
207 constexpr size_type size() const {
208 return N;
209 }
210
211 /**
212 * @brief Get maximum size
213 *
214 * @return Maximum number of elements (same as size())
215 */
216 constexpr size_type max_size() const {
217 return N;
218 }
219
220 /**
221 * @brief Fill array with value
222 *
223 * @param value Value to fill with
224 */
225 void fill(const T& value) {
226 for (size_type i = 0; i < N; ++i) {
227 data_[i] = value;
228 }
229 }
230
231 /**
232 * @brief Swap contents with another array
233 *
234 * @param other Array to swap with
235 */
236 void swap(Array& other) {
237 for (size_type i = 0; i < N; ++i) {
238 T temp = data_[i];
239 data_[i] = other.data_[i];
240 other.data_[i] = temp;
241 }
242 }
243
244private:
245 T data_[N];
246};
247
248} // namespace containers
249} // namespace primitives
250} // namespace sbl
251
252#endif // SBL_PRIMITIVES_CONTAINERS_FIXED_ARRAY_HPP_
Fixed-size array container.
reference back()
Access last element.
iterator begin()
Get iterator to beginning.
const_iterator begin() const
Get iterator to beginning (const)
const_reference at(size_type index) const
Access element with bounds checking (const)
reference at(size_type index)
Access element with bounds checking.
pointer data()
Get pointer to underlying array.
constexpr size_type size() const
Get array size.
reference operator[](size_type index)
Access element at index.
void fill(const T &value)
Fill array with value.
void swap(Array &other)
Swap contents with another array.
constexpr bool empty() const
Check if array is empty.
const_reference operator[](size_type index) const
Access element at index (const)
constexpr size_type max_size() const
Get maximum size.
iterator end()
Get iterator to end.
const_reference back() const
Access last element (const)
const_iterator end() const
Get iterator to end (const)
Array()=default
Default constructor.
Array(const T &value)
Fill constructor.
const_reference front() const
Access first element (const)
const_pointer data() const
Get pointer to underlying array (const)
reference front()
Access first element.
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24