Embedded Template Library 1.0
Loading...
Searching...
No Matches
array_wrapper.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2017 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_ARRAY_WRAPPER_INCLUDED
32#define ETL_ARRAY_WRAPPER_INCLUDED
33
34#include "platform.h"
35#include "iterator.h"
36#include "error_handler.h"
37#include "exception.h"
38#include "hash.h"
39#include "parameter_type.h"
40#include "algorithm.h"
41
45
46namespace etl
47{
48 //***************************************************************************
50 //***************************************************************************
51 class array_wrapper_exception : public exception
52 {
53 public:
54
55 array_wrapper_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
56 : exception(reason_, file_name_, line_number_)
57 {
58 }
59 };
60
61 //***************************************************************************
64 //***************************************************************************
65 class array_wrapper_bounds : public array_wrapper_exception
66 {
67 public:
68
69 array_wrapper_bounds(string_type file_name_, numeric_type line_number_)
70 : array_wrapper_exception(ETL_ERROR_TEXT("array_wrapper:bounds", ETL_ARRAY_WRAPPER_FILE_ID"A"), file_name_, line_number_)
71 {
72 }
73 };
74
75 //***************************************************************************
77 //***************************************************************************
78 template <typename T, size_t SIZE_, T(&ARRAY_)[SIZE_]>
80 {
81 public:
82
83 typedef T value_type;
84 typedef size_t size_type;
85 typedef T& reference;
86 typedef const T& const_reference;
87 typedef T* pointer;
88 typedef const T* const_pointer;
89 typedef T* iterator;
90 typedef const T* const_iterator;
91 typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
92 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
93
94 typedef typename etl::parameter_type<T>::type parameter_t;
95
96 // Indexes for positions in the array.
97 enum
98 {
99 SIZE = SIZE_,
100 MAX_SIZE = SIZE_,
101 FRONT = 0,
102 BACK = SIZE - 1,
103 BEGIN = 0,
104 END = SIZE,
105 RBEGIN = SIZE - 1,
106 REND = -1
107 };
108
109 //*************************************************************************
111 //*************************************************************************
112 reference front()
113 {
114 return *&ARRAY_[FRONT];
115 }
116
117 //*************************************************************************
119 //*************************************************************************
120 ETL_CONSTEXPR const_reference front() const
121 {
122 return *&ARRAY_[FRONT];
123 }
124
125 //*************************************************************************
127 //*************************************************************************
128 reference back()
129 {
130 return *&ARRAY_[BACK];
131 }
132
133 //*************************************************************************
135 //*************************************************************************
136 ETL_CONSTEXPR const_reference back() const
137 {
138 return *&ARRAY_[BACK];
139 }
140
141 //*************************************************************************
143 //*************************************************************************
144 pointer data() ETL_NOEXCEPT
145 {
146 return &ARRAY_[BEGIN];
147 }
148
149 //*************************************************************************
151 //*************************************************************************
152 ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
153 {
154 return &ARRAY_[BEGIN];
155 }
156
157 //*************************************************************************
159 //*************************************************************************
160 iterator begin() ETL_NOEXCEPT
161 {
162 return &ARRAY_[BEGIN];
163 }
164
165 //*************************************************************************
167 //*************************************************************************
168 ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
169 {
170 return &ARRAY_[BEGIN];
171 }
172
173 //*************************************************************************
175 //*************************************************************************
176 ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
177 {
178 return &ARRAY_[BEGIN];
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 iterator end() ETL_NOEXCEPT
185 {
186 return &ARRAY_[END];
187 }
188
189 //*************************************************************************
191 //*************************************************************************
192 ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
193 {
194 return &ARRAY_[END];
195 }
196
197 //*************************************************************************
198 // Returns a const iterator to the end of the array.
199 //*************************************************************************
200 ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
201 {
202 return &ARRAY_[END];
203 }
204
205 //*************************************************************************
206 // Returns an reverse iterator to the reverse beginning of the array.
207 //*************************************************************************
208 reverse_iterator rbegin() ETL_NOEXCEPT
209 {
210 return reverse_iterator(&ARRAY_[END]);
211 }
212
213 //*************************************************************************
215 //*************************************************************************
216 ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
217 {
218 return const_reverse_iterator(&ARRAY_[END]);
219 }
220
221 //*************************************************************************
223 //*************************************************************************
224 ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
225 {
226 return const_reverse_iterator(&ARRAY_[END]);
227 }
228
229 //*************************************************************************
231 //*************************************************************************
232 reverse_iterator rend() ETL_NOEXCEPT
233 {
234 return reverse_iterator(&ARRAY_[BEGIN]);
235 }
236
237 //*************************************************************************
239 //*************************************************************************
240 ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
241 {
242 return const_reverse_iterator(&ARRAY_[BEGIN]);
243 }
244
245 //*************************************************************************
247 //*************************************************************************
248 ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
249 {
250 return const_reverse_iterator(&ARRAY_[BEGIN]);
251 }
252
253 //*************************************************************************
255 //*************************************************************************
256 ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
257 {
258 return SIZE;
259 }
260
261 //*************************************************************************
263 //*************************************************************************
264 ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
265 {
266 return MAX_SIZE;
267 }
268
269 //*************************************************************************
271 //*************************************************************************
272 reference operator[](size_t i) ETL_NOEXCEPT
273 {
274 return ARRAY_[i];
275 }
276
277 //*************************************************************************
279 //*************************************************************************
280 ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT
281 {
282 return ARRAY_[i];
283 }
284
285 //*************************************************************************
287 //*************************************************************************
288 reference at(size_t i)
289 {
290 ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
291 return ARRAY_[i];
292 }
293
294 //*************************************************************************
296 //*************************************************************************
297 const_reference at(size_t i) const
298 {
299 ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
300 return ARRAY_[i];
301 }
302
303 //*************************************************************************
305 //*************************************************************************
306 void fill(parameter_t value)
307 {
308 etl::fill(begin(), end(), value);
309 }
310
311 //*************************************************************************
313 //*************************************************************************
314 template <typename U, U(&ARRAYOTHER)[SIZE_]>
317 {
318 using ETL_OR_STD::swap; // Allow ADL
319
320 for (size_t i = 0UL; i < SIZE; ++i)
321 {
322 swap(ARRAY_[i], other.begin()[i]);
323 }
324 }
325 };
326
327 //*************************************************************************
329 //*************************************************************************
330 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
333 {
334 return (SIZEL == SIZER) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
335 }
336
337 //*************************************************************************
339 //*************************************************************************
340 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
343 {
344 return !(lhs == rhs);
345 }
346
347 //*************************************************************************
349 //*************************************************************************
350 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
353 {
354 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
355 }
356
357 //*************************************************************************
359 //*************************************************************************
360 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
363 {
364 return rhs < lhs;
365 }
366
367 //*************************************************************************
369 //*************************************************************************
370 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
373 {
374 return !(lhs > rhs);
375 }
376
377 //*************************************************************************
379 //*************************************************************************
380 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
383 {
384 return !(lhs < rhs);
385 }
386
387 //*************************************************************************
389 //*************************************************************************
390#if ETL_USING_8BIT_TYPES
391 template <typename T, size_t SIZE, T(&ARRAY)[SIZE]>
392 struct hash<etl::array_wrapper<T, SIZE, ARRAY> >
393 {
394 size_t operator()(const etl::array_wrapper<T, SIZE, ARRAY>& aw) const
395 {
396 const uint8_t* pb = reinterpret_cast<const uint8_t*>(aw.data());
397 const uint8_t* pe = pb + (SIZE * sizeof(T));
398
399 return etl::private_hash::generic_hash<size_t>(pb, pe);
400 }
401 };
402#endif
403}
404
405//*************************************************************************
407//*************************************************************************
408template <typename T, size_t SIZE, T(&ARRAYL)[SIZE], T(&ARRAYR)[SIZE]>
414
415#define ETL_ARRAY_WRAPPER(arraytype, arrayobject) etl::array_wrapper<arraytype, ETL_ARRAY_SIZE(arrayobject), arrayobject>
416
417#endif
418
void swap(etl::array_wrapper< T, SIZE, ARRAYL > &lhs, etl::array_wrapper< T, SIZE, ARRAYR > &rhs)
Swap.
Definition array_wrapper.h:409
Array wrapper.
Definition array_wrapper.h:80
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition array_wrapper.h:248
reference back()
Returns a reference to the last element.
Definition array_wrapper.h:128
reference front()
Returns a reference to the first element.
Definition array_wrapper.h:112
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition array_wrapper.h:176
reverse_iterator rend() ETL_NOEXCEPT
Returns a reverse iterator to the end of the array.
Definition array_wrapper.h:232
ETL_CONSTEXPR const_reference back() const
Returns a const reference to the last element.
Definition array_wrapper.h:136
ETL_CONSTEXPR const_reference front() const
Returns a const reference to the first element.
Definition array_wrapper.h:120
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition array_wrapper.h:152
void fill(parameter_t value)
Fills the array.
Definition array_wrapper.h:306
iterator end() ETL_NOEXCEPT
Returns an iterator to the end of the array.
Definition array_wrapper.h:184
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition array_wrapper.h:216
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition array_wrapper.h:240
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition array_wrapper.h:192
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
Definition array_wrapper.h:264
etl::enable_if< etl::is_same< T, U >::value, void >::type swap(etl::array_wrapper< U, SIZE_, ARRAYOTHER > &other)
Swaps the contents of arrays.
Definition array_wrapper.h:316
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition array_wrapper.h:297
reference at(size_t i)
Returns a reference to the indexed value.
Definition array_wrapper.h:288
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition array_wrapper.h:256
iterator begin() ETL_NOEXCEPT
Returns an iterator to the beginning of the array.
Definition array_wrapper.h:160
pointer data() ETL_NOEXCEPT
Returns a pointer to the first element of the internal storage.
Definition array_wrapper.h:144
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition array_wrapper.h:224
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition array_wrapper.h:168
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT
Returns a const reference to the indexed value.
Definition array_wrapper.h:280
reference operator[](size_t i) ETL_NOEXCEPT
Returns a reference to the indexed value.
Definition array_wrapper.h:272
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition array_wrapper.h:66
enable_if
Definition type_traits_generator.h:1254
bitset_ext
Definition absolute.h:39
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1190
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1202
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1151
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1139
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1012
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1163
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1178
etl::conditional< etl::is_fundamental< T >::value||etl::is_pointer< T >::value, T, constT & >::type type
By default fundamental and pointer types are passed by value.
Definition parameter_type.h:48