Embedded Template Library  1.0
array_wrapper.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2017 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
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 "container.h"
40 #include "parameter_type.h"
41 
42 #include "algorithm.h"
43 
47 
48 #undef ETL_FILE
49 #define ETL_FILE "42"
50 
51 namespace etl
52 {
53  //***************************************************************************
55  //***************************************************************************
57  {
58  public:
59 
60  array_wrapper_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
61  : exception(reason_, file_name_, line_number_)
62  {
63  }
64  };
65 
66  //***************************************************************************
69  //***************************************************************************
71  {
72  public:
73 
74  array_wrapper_bounds(string_type file_name_, numeric_type line_number_)
75  : array_wrapper_exception(ETL_ERROR_TEXT("array_wrapper:bounds", ETL_FILE"A"), file_name_, line_number_)
76  {
77  }
78  };
79 
80  //***************************************************************************
82  //***************************************************************************
83  template <typename T, size_t SIZE_, T(&ARRAY_)[SIZE_]>
85  {
86  public:
87 
88  typedef T value_type;
89  typedef size_t size_type;
90  typedef T& reference;
91  typedef const T& const_reference;
92  typedef T* pointer;
93  typedef const T* const_pointer;
94  typedef T* iterator;
95  typedef const T* const_iterator;
96  typedef ETL_OR_STD::reverse_iterator<iterator> reverse_iterator;
97  typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
98 
99  typedef typename etl::parameter_type<T>::type parameter_t;
100 
101  // Indexes for positions in the array.
102  enum
103  {
104  SIZE = SIZE_,
105  MAX_SIZE = SIZE_,
106  FRONT = 0,
107  BACK = SIZE - 1,
108  BEGIN = 0,
109  END = SIZE,
110  RBEGIN = SIZE - 1,
111  REND = -1
112  };
113 
114  //*************************************************************************
116  //*************************************************************************
117  reference front()
118  {
119  return *&ARRAY_[FRONT];
120  }
121 
122  //*************************************************************************
124  //*************************************************************************
125  ETL_CONSTEXPR const_reference front() const
126  {
127  return *&ARRAY_[FRONT];
128  }
129 
130  //*************************************************************************
132  //*************************************************************************
133  reference back()
134  {
135  return *&ARRAY_[BACK];
136  }
137 
138  //*************************************************************************
140  //*************************************************************************
141  ETL_CONSTEXPR const_reference back() const
142  {
143  return *&ARRAY_[BACK];
144  }
145 
146  //*************************************************************************
148  //*************************************************************************
149  pointer data()
150  {
151  return &ARRAY_[BEGIN];
152  }
153 
154  //*************************************************************************
156  //*************************************************************************
157  ETL_CONSTEXPR const_pointer data() const
158  {
159  return &ARRAY_[BEGIN];
160  }
161 
162  //*************************************************************************
164  //*************************************************************************
165  iterator begin()
166  {
167  return &ARRAY_[BEGIN];
168  }
169 
170  //*************************************************************************
172  //*************************************************************************
173  ETL_CONSTEXPR const_iterator begin() const
174  {
175  return &ARRAY_[BEGIN];
176  }
177 
178  //*************************************************************************
180  //*************************************************************************
181  ETL_CONSTEXPR const_iterator cbegin() const
182  {
183  return &ARRAY_[BEGIN];
184  }
185 
186  //*************************************************************************
188  //*************************************************************************
189  iterator end()
190  {
191  return &ARRAY_[END];
192  }
193 
194  //*************************************************************************
196  //*************************************************************************
197  ETL_CONSTEXPR const_iterator end() const
198  {
199  return &ARRAY_[END];
200  }
201 
202  //*************************************************************************
203  // Returns a const iterator to the end of the array.
204  //*************************************************************************
205  ETL_CONSTEXPR const_iterator cend() const
206  {
207  return &ARRAY_[END];
208  }
209 
210  //*************************************************************************
211  // Returns an reverse iterator to the reverse beginning of the array.
212  //*************************************************************************
213  reverse_iterator rbegin()
214  {
215  return reverse_iterator(&ARRAY_[END]);
216  }
217 
218  //*************************************************************************
220  //*************************************************************************
221  ETL_CONSTEXPR const_reverse_iterator rbegin() const
222  {
223  return const_reverse_iterator(&ARRAY_[END]);
224  }
225 
226  //*************************************************************************
228  //*************************************************************************
229  ETL_CONSTEXPR const_reverse_iterator crbegin() const
230  {
231  return const_reverse_iterator(&ARRAY_[END]);
232  }
233 
234  //*************************************************************************
236  //*************************************************************************
237  reverse_iterator rend()
238  {
239  return reverse_iterator(&ARRAY_[BEGIN]);
240  }
241 
242  //*************************************************************************
244  //*************************************************************************
245  ETL_CONSTEXPR const_reverse_iterator rend() const
246  {
247  return const_reverse_iterator(&ARRAY_[BEGIN]);
248  }
249 
250  //*************************************************************************
252  //*************************************************************************
253  ETL_CONSTEXPR const_reverse_iterator crend() const
254  {
255  return const_reverse_iterator(&ARRAY_[BEGIN]);
256  }
257 
258  //*************************************************************************
260  //*************************************************************************
261  ETL_CONSTEXPR size_t size() const
262  {
263  return SIZE;
264  }
265 
266  //*************************************************************************
268  //*************************************************************************
269  ETL_CONSTEXPR size_t max_size() const
270  {
271  return MAX_SIZE;
272  }
273 
274  //*************************************************************************
276  //*************************************************************************
277  reference operator[](size_t i)
278  {
279  return ARRAY_[i];
280  }
281 
282  //*************************************************************************
284  //*************************************************************************
285  ETL_CONSTEXPR const_reference operator[](size_t i) const
286  {
287  return ARRAY_[i];
288  }
289 
290  //*************************************************************************
292  //*************************************************************************
293  reference at(size_t i)
294  {
295  ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
296  return ARRAY_[i];
297  }
298 
299  //*************************************************************************
301  //*************************************************************************
302  const_reference at(size_t i) const
303  {
304  ETL_ASSERT(i < SIZE, ETL_ERROR(etl::array_wrapper_bounds));
305  return ARRAY_[i];
306  }
307 
308  //*************************************************************************
310  //*************************************************************************
311  void fill(parameter_t value)
312  {
313  etl::fill(begin(), end(), value);
314  }
315 
316  //*************************************************************************
318  //*************************************************************************
319  template <typename U, U(&ARRAYOTHER)[SIZE_]>
320  typename etl::enable_if<etl::is_same<T, U>::value, void>::type
322  {
323  using ETL_OR_STD::swap; // Allow ADL
324 
325  for (size_t i = 0; i < SIZE; ++i)
326  {
327  swap(ARRAY_[i], other.begin()[i]);
328  }
329  }
330  };
331 
332  //*************************************************************************
334  //*************************************************************************
335  template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
338  {
339  return (SIZEL == SIZER) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
340  }
341 
342  //*************************************************************************
344  //*************************************************************************
345  template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
348  {
349  return !(lhs == rhs);
350  }
351 
352  //*************************************************************************
354  //*************************************************************************
355  template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
358  {
359  return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
360  }
361 
362  //*************************************************************************
364  //*************************************************************************
365  template <typename TL, typename TR, size_t SIZEL, size_t SIZER, TL(&ARRAYL)[SIZEL], TR(&ARRAYR)[SIZER]>
368  {
369  return rhs < lhs;
370  }
371 
372  //*************************************************************************
374  //*************************************************************************
375  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]>
388  {
389  return !(lhs < rhs);
390  }
391 
392  //*************************************************************************
394  //*************************************************************************
395 #if ETL_8BIT_SUPPORT
396  template <typename T, size_t SIZE, T(&ARRAY)[SIZE]>
397  struct hash<etl::array_wrapper<T, SIZE, ARRAY> >
398  {
399  size_t operator()(const etl::array_wrapper<T, SIZE, ARRAY>& aw) const
400  {
401  return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(&aw[0]),
402  reinterpret_cast<const uint8_t*>(&aw[aw.size()]));
403  }
404  };
405 #endif
406 }
407 
408 //*************************************************************************
410 //*************************************************************************
411 template <typename T, size_t SIZE, T(&ARRAYL)[SIZE], T(&ARRAYR)[SIZE]>
414 {
415  lhs.swap(rhs);
416 }
417 
418 #define ETL_ARRAY_WRAPPER(arraytype, arrayobject) etl::array_wrapper<arraytype, ETL_ARRAY_SIZE(arrayobject), arrayobject>
419 
420 #undef ETL_FILE
421 
422 #endif
423 
void swap(etl::array_wrapper< T, SIZE, ARRAYL > &lhs, etl::array_wrapper< T, SIZE, ARRAYR > &rhs)
Swap.
Definition: array_wrapper.h:412
The base class for array_wrapper exceptions.
Definition: array_wrapper.h:57
Array wrapper.
Definition: array_wrapper.h:85
iterator end()
Returns an iterator to the end of the array.
Definition: array_wrapper.h:189
reference back()
Returns a reference to the last element.
Definition: array_wrapper.h:133
iterator begin()
Returns an iterator to the beginning of the array.
Definition: array_wrapper.h:165
reference front()
Returns a reference to the first element.
Definition: array_wrapper.h:117
ETL_CONSTEXPR const_iterator cbegin() const
Returns a const iterator to the beginning of the array.
Definition: array_wrapper.h:181
ETL_CONSTEXPR const_reverse_iterator crend() const
Returns a const reverse iterator to the end of the array.
Definition: array_wrapper.h:253
void fill(parameter_t value)
Fills the array.
Definition: array_wrapper.h:311
reference operator[](size_t i)
Returns a reference to the indexed value.
Definition: array_wrapper.h:277
pointer data()
Returns a pointer to the first element of the internal storage.
Definition: array_wrapper.h:149
ETL_CONSTEXPR const_reverse_iterator crbegin() const
Returns a const reverse iterator to the reverse beginning of the array.
Definition: array_wrapper.h:229
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:321
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition: array_wrapper.h:302
ETL_CONSTEXPR size_t max_size() const
Returns the maximum possible size of the array.
Definition: array_wrapper.h:269
reverse_iterator rend()
Returns a reverse iterator to the end of the array.
Definition: array_wrapper.h:237
ETL_CONSTEXPR size_t size() const
Returns the size of the array.
Definition: array_wrapper.h:261
reference at(size_t i)
Returns a reference to the indexed value.
Definition: array_wrapper.h:293
#define ETL_ASSERT(b, e)
Definition: error_handler.h:290
exception(string_type reason_, string_type file_, numeric_type line_)
Constructor.
Definition: exception.h:67
Definition: exception.h:47
Definition: array_wrapper.h:71
enable_if
Definition: type_traits_generator.h:1228
Definition: absolute.h:37
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:645
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:594
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:582
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:606
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:621
etl::conditional< etl::is_fundamental< T >::value||etl::is_pointer< T >::value, T, const T & >::type type
By default fundamental and pointer types are passed by value.
Definition: parameter_type.h:48