Embedded Template Library 1.0
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 //***************************************************************************
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 //***************************************************************************
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()
145 {
146 return &ARRAY_[BEGIN];
147 }
148
149 //*************************************************************************
151 //*************************************************************************
152 ETL_CONSTEXPR const_pointer data() const
153 {
154 return &ARRAY_[BEGIN];
155 }
156
157 //*************************************************************************
159 //*************************************************************************
160 iterator begin()
161 {
162 return &ARRAY_[BEGIN];
163 }
164
165 //*************************************************************************
167 //*************************************************************************
168 ETL_CONSTEXPR const_iterator begin() const
169 {
170 return &ARRAY_[BEGIN];
171 }
172
173 //*************************************************************************
175 //*************************************************************************
176 ETL_CONSTEXPR const_iterator cbegin() const
177 {
178 return &ARRAY_[BEGIN];
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 iterator end()
185 {
186 return &ARRAY_[END];
187 }
188
189 //*************************************************************************
191 //*************************************************************************
192 ETL_CONSTEXPR const_iterator end() const
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
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()
209 {
210 return reverse_iterator(&ARRAY_[END]);
211 }
212
213 //*************************************************************************
215 //*************************************************************************
216 ETL_CONSTEXPR const_reverse_iterator rbegin() const
217 {
218 return const_reverse_iterator(&ARRAY_[END]);
219 }
220
221 //*************************************************************************
223 //*************************************************************************
224 ETL_CONSTEXPR const_reverse_iterator crbegin() const
225 {
226 return const_reverse_iterator(&ARRAY_[END]);
227 }
228
229 //*************************************************************************
231 //*************************************************************************
232 reverse_iterator rend()
233 {
234 return reverse_iterator(&ARRAY_[BEGIN]);
235 }
236
237 //*************************************************************************
239 //*************************************************************************
240 ETL_CONSTEXPR const_reverse_iterator rend() const
241 {
242 return const_reverse_iterator(&ARRAY_[BEGIN]);
243 }
244
245 //*************************************************************************
247 //*************************************************************************
248 ETL_CONSTEXPR const_reverse_iterator crend() const
249 {
250 return const_reverse_iterator(&ARRAY_[BEGIN]);
251 }
252
253 //*************************************************************************
255 //*************************************************************************
256 ETL_CONSTEXPR size_t size() const
257 {
258 return SIZE;
259 }
260
261 //*************************************************************************
263 //*************************************************************************
264 ETL_CONSTEXPR size_t max_size() const
265 {
266 return MAX_SIZE;
267 }
268
269 //*************************************************************************
271 //*************************************************************************
272 reference operator[](size_t i)
273 {
274 return ARRAY_[i];
275 }
276
277 //*************************************************************************
279 //*************************************************************************
280 ETL_CONSTEXPR const_reference operator[](size_t i) const
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_]>
315 typename etl::enable_if<etl::is_same<T, U>::value, void>::type
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]>
411{
412 lhs.swap(rhs);
413}
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
The base class for array_wrapper exceptions.
Definition: array_wrapper.h:52
Array wrapper.
Definition: array_wrapper.h:80
iterator end()
Returns an iterator to the end of the array.
Definition: array_wrapper.h:184
reference back()
Returns a reference to the last element.
Definition: array_wrapper.h:128
iterator begin()
Returns an iterator to the beginning of the array.
Definition: array_wrapper.h:160
reference front()
Returns a reference to the first element.
Definition: array_wrapper.h:112
ETL_CONSTEXPR const_iterator cbegin() const
Returns a const iterator to the beginning of the array.
Definition: array_wrapper.h:176
ETL_CONSTEXPR const_reverse_iterator crend() const
Returns a const reverse iterator to the end of the array.
Definition: array_wrapper.h:248
void fill(parameter_t value)
Fills the array.
Definition: array_wrapper.h:306
reference operator[](size_t i)
Returns a reference to the indexed value.
Definition: array_wrapper.h:272
pointer data()
Returns a pointer to the first element of the internal storage.
Definition: array_wrapper.h:144
ETL_CONSTEXPR const_reverse_iterator crbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: array_wrapper.h:224
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
ETL_CONSTEXPR size_t max_size() const
Returns the maximum possible size of the array.
Definition: array_wrapper.h:264
reverse_iterator rend()
Returns a reverse iterator to the end of the array.
Definition: array_wrapper.h:232
ETL_CONSTEXPR size_t size() const
Returns the size of the array.
Definition: array_wrapper.h:256
reference at(size_t i)
Returns a reference to the indexed value.
Definition: array_wrapper.h:288
#define ETL_ASSERT(b, e)
Definition: error_handler.h:316
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition: exception.h:69
Definition: exception.h:47
Definition: array_wrapper.h:66
enable_if
Definition: type_traits_generator.h:1191
bitset_ext
Definition: absolute.h:38
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:684
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:696
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:657
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:672
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