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 "algorithm.h"
36#include "error_handler.h"
37#include "exception.h"
38#include "hash.h"
39#include "iterator.h"
40#include "parameter_type.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 //*************************************************************************
273 //*************************************************************************
274 reference operator[](size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR)
275 {
276 ETL_ASSERT_CHECK_INDEX_OPERATOR(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
277 return ARRAY_[i];
278 }
279
280 //*************************************************************************
284 //*************************************************************************
285 ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR)
286 {
287 // Throwing from c++11 constexpr requires special syntax
288#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
289 return i < SIZE ? ARRAY_[i] : throw(ETL_ERROR(etl::array_wrapper_bounds));
290#else
291 ETL_ASSERT_CHECK_INDEX_OPERATOR(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
292 return ARRAY_[i];
293#endif
294 }
295
296 //*************************************************************************
298 //*************************************************************************
299 reference at(size_t i)
300 {
301 ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
302 return ARRAY_[i];
303 }
304
305 //*************************************************************************
307 //*************************************************************************
308 const_reference at(size_t i) const
309 {
310 ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
311 return ARRAY_[i];
312 }
313
314 //*************************************************************************
316 //*************************************************************************
317 void fill(parameter_t value)
318 {
319 etl::fill(begin(), end(), value);
320 }
321
322 //*************************************************************************
324 //*************************************************************************
325 template <typename U, U (&ARRAYOTHER)[SIZE_]>
326 typename etl::enable_if<etl::is_same<T, U>::value, void>::type swap(etl::array_wrapper<U, SIZE_, ARRAYOTHER>& other)
327 {
328 using ETL_OR_STD::swap; // Allow ADL
329
330 for (size_t i = 0UL; i < SIZE; ++i)
331 {
332 swap(ARRAY_[i], other.begin()[i]);
333 }
334 }
335 };
336
337 //*************************************************************************
339 //*************************************************************************
340 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL (&ARRAYL)[SIZEL], TR (&ARRAYR)[SIZER]>
342 {
343 return (SIZEL == SIZER) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
344 }
345
346 //*************************************************************************
348 //*************************************************************************
349 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL (&ARRAYL)[SIZEL], TR (&ARRAYR)[SIZER]>
351 {
352 return !(lhs == rhs);
353 }
354
355 //*************************************************************************
357 //*************************************************************************
358 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL (&ARRAYL)[SIZEL], TR (&ARRAYR)[SIZER]>
360 {
361 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
362 }
363
364 //*************************************************************************
366 //*************************************************************************
367 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL (&ARRAYL)[SIZEL], TR (&ARRAYR)[SIZER]>
369 {
370 return rhs < lhs;
371 }
372
373 //*************************************************************************
375 //*************************************************************************
376 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL (&ARRAYL)[SIZEL], TR (&ARRAYR)[SIZER]>
378 {
379 return !(lhs > rhs);
380 }
381
382 //*************************************************************************
384 //*************************************************************************
385 template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL (&ARRAYL)[SIZEL], TR (&ARRAYR)[SIZER]>
387 {
388 return !(lhs < rhs);
389 }
390
391 //*************************************************************************
393 //*************************************************************************
394#if ETL_USING_8BIT_TYPES
395 template <typename T, size_t SIZE, T (&ARRAY)[SIZE]>
396 struct hash<etl::array_wrapper<T, SIZE, ARRAY> >
397 {
398 size_t operator()(const etl::array_wrapper<T, SIZE, ARRAY>& aw) const
399 {
400 const uint8_t* pb = reinterpret_cast<const uint8_t*>(aw.data());
401 const uint8_t* pe = pb + (SIZE * sizeof(T));
402
403 return etl::private_hash::generic_hash<size_t>(pb, pe);
404 }
405 };
406#endif
407} // namespace etl
408
409//*************************************************************************
411//*************************************************************************
412template <typename T, size_t SIZE, T (&ARRAYL)[SIZE], T (&ARRAYR)[SIZE]>
417
418#define ETL_ARRAY_WRAPPER(arraytype, arrayobject) etl::array_wrapper<arraytype, ETL_ARRAY_SIZE(arrayobject), arrayobject>
419
420#endif
void swap(etl::array_wrapper< T, SIZE, ARRAYL > &lhs, etl::array_wrapper< T, SIZE, ARRAYR > &rhs)
Swap.
Definition array_wrapper.h:413
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
reference operator[](size_t i) ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS||ETL_NOT_CHECKING_INDEX_OPERATOR)
Definition array_wrapper.h:274
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
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS||ETL_NOT_CHECKING_INDEX_OPERATOR)
Definition array_wrapper.h:285
void fill(parameter_t value)
Fills the array.
Definition array_wrapper.h:317
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:326
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition array_wrapper.h:308
reference at(size_t i)
Returns a reference to the indexed value.
Definition array_wrapper.h:299
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
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition array_wrapper.h:66
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1081
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1133
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1147
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1017
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1106
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1120
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:46