Sound Byte Libs 1ee2ca6
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_CORE_PRIMITIVES_CONTAINERS_FIXED_ARRAY_HPP_
2#define SBL_CORE_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 core {
18namespace primitives {
19namespace containers {
20
21/**
22 * @brief Fixed-size array container
23 *
24 * Lightweight array wrapper providing bounds-safe access and
25 * iteration support. All storage is allocated at compile time.
26 *
27 * @tparam T Element type
28 * @tparam N Array size (must be greater than 0)
29 */
30template<typename T, size_t N>
31class Array {
32 static_assert(N > 0, "Array size must be greater than 0");
33
34public:
35 using value_type = T;
36 using size_type = size_t;
37 using reference = T&;
38 using const_reference = const T&;
39 using pointer = T*;
40 using const_pointer = const T*;
41 using iterator = T*;
42 using const_iterator = const T*;
43
44 /**
45 * @brief Default constructor
46 *
47 * Elements are default-initialized.
48 */
49 Array() = default;
50
51 /**
52 * @brief Fill constructor
53 *
54 * @param value Value to fill array with
55 */
56 explicit Array(const T& value) {
57 fill(value);
58 }
59
60 /**
61 * @brief Access element at index
62 *
63 * @param index Element index
64 * @return Reference to element
65 *
66 * @note No bounds checking in release builds for performance
67 */
69 return data_[index];
70 }
71
72 /**
73 * @brief Access element at index (const)
74 *
75 * @param index Element index
76 * @return Const reference to element
77 */
79 return data_[index];
80 }
81
82 /**
83 * @brief Access element with bounds checking
84 *
85 * @param index Element index
86 * @return Reference to element
87 *
88 * @note Returns first element if index out of bounds
89 */
91 return (index < N) ? data_[index] : data_[0];
92 }
93
94 /**
95 * @brief Access element with bounds checking (const)
96 *
97 * @param index Element index
98 * @return Const reference to element
99 */
101 return (index < N) ? data_[index] : data_[0];
102 }
103
104 /**
105 * @brief Access first element
106 *
107 * @return Reference to first element
108 */
110 return data_[0];
111 }
112
113 /**
114 * @brief Access first element (const)
115 *
116 * @return Const reference to first element
117 */
119 return data_[0];
120 }
121
122 /**
123 * @brief Access last element
124 *
125 * @return Reference to last element
126 */
128 return data_[N - 1];
129 }
130
131 /**
132 * @brief Access last element (const)
133 *
134 * @return Const reference to last element
135 */
137 return data_[N - 1];
138 }
139
140 /**
141 * @brief Get pointer to underlying array
142 *
143 * @return Pointer to first element
144 */
146 return data_;
147 }
148
149 /**
150 * @brief Get pointer to underlying array (const)
151 *
152 * @return Const pointer to first element
153 */
155 return data_;
156 }
157
158 /**
159 * @brief Get iterator to beginning
160 *
161 * @return Iterator to first element
162 */
164 return data_;
165 }
166
167 /**
168 * @brief Get iterator to beginning (const)
169 *
170 * @return Const iterator to first element
171 */
173 return data_;
174 }
175
176 /**
177 * @brief Get iterator to end
178 *
179 * @return Iterator past last element
180 */
182 return data_ + N;
183 }
184
185 /**
186 * @brief Get iterator to end (const)
187 *
188 * @return Const iterator past last element
189 */
191 return data_ + N;
192 }
193
194 /**
195 * @brief Check if array is empty
196 *
197 * @return Always false (fixed-size array is never empty)
198 */
199 constexpr bool empty() const {
200 return false;
201 }
202
203 /**
204 * @brief Get array size
205 *
206 * @return Number of elements (N)
207 */
208 constexpr size_type size() const {
209 return N;
210 }
211
212 /**
213 * @brief Get maximum size
214 *
215 * @return Maximum number of elements (same as size())
216 */
217 constexpr size_type max_size() const {
218 return N;
219 }
220
221 /**
222 * @brief Fill array with value
223 *
224 * @param value Value to fill with
225 */
226 void fill(const T& value) {
227 for (size_type i = 0; i < N; ++i) {
228 data_[i] = value;
229 }
230 }
231
232 /**
233 * @brief Swap contents with another array
234 *
235 * @param other Array to swap with
236 */
237 void swap(Array& other) {
238 for (size_type i = 0; i < N; ++i) {
239 T temp = data_[i];
240 data_[i] = other.data_[i];
241 other.data_[i] = temp;
242 }
243 }
244
245private:
246 T data_[N];
247};
248
249} // namespace containers
250} // namespace primitives
251} // namespace core
252} // namespace sbl
253
254#endif // SBL_CORE_PRIMITIVES_CONTAINERS_FIXED_ARRAY_HPP_
constexpr size_type max_size() const
Get maximum size.
const_reference back() const
Access last element (const)
const_reference operator[](size_type index) const
Access element at index (const)
constexpr bool empty() const
Check if array is empty.
pointer data()
Get pointer to underlying array.
const_iterator begin() const
Get iterator to beginning (const)
iterator begin()
Get iterator to beginning.
reference at(size_type index)
Access element with bounds checking.
reference front()
Access first element.
const_reference front() const
Access first element (const)
reference operator[](size_type index)
Access element at index.
reference back()
Access last element.
const_pointer data() const
Get pointer to underlying array (const)
void swap(Array &other)
Swap contents with another array.
constexpr size_type size() const
Get array size.
Array(const T &value)
Fill constructor.
Array()=default
Default constructor.
const_reference at(size_type index) const
Access element with bounds checking (const)
iterator end()
Get iterator to end.
const_iterator end() const
Get iterator to end (const)
void fill(const T &value)
Fill array with value.
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24