Embedded Template Library  1.0
utility.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) 2016 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_UTILITY_INCLUDED
32 #define ETL_UTILITY_INCLUDED
33 
34 #include "platform.h"
35 #include "type_traits.h"
36 
37 #if ETL_USING_STL
38  #if ETL_CPP11_SUPPORTED
39  #include <utility>
40  #else
41  #include <algorithm>
42  #endif
43 #endif
44 
47 
48 namespace etl
49 {
50 #if ETL_CPP11_SUPPORTED
51  //******************************************************************************
52  template <typename T>
53  constexpr typename etl::remove_reference<T>::type&& move(T&& t) ETL_NOEXCEPT
54  {
55  return static_cast<typename etl::remove_reference<T>::type&&>(t);
56  }
57 
58  //******************************************************************************
59  template <typename T>
60  constexpr T&& forward(typename etl::remove_reference<T>::type& t) ETL_NOEXCEPT
61  {
62  return static_cast<T&&>(t);
63  }
64 
65  template <typename T>
66  constexpr T&& forward(typename etl::remove_reference<T>::type&& t) ETL_NOEXCEPT
67  {
68  return static_cast<T&&>(t);
69  }
70 #endif
71 
72  // We can't have std::swap and etl::swap templates coexisting in the unit tests
73  // as the compiler will be unable to decide of which one to use, due to ADL.
74 #if ETL_NOT_USING_STL && !defined(ETL_IN_UNIT_TEST)
75  //***************************************************************************
76  // swap
77 #if ETL_CPP11_SUPPORTED
78  template <typename T>
79  void swap(T& a, T& b) ETL_NOEXCEPT
80  {
81  T temp(etl::move(a));
82  a = etl::move(b);
83  b = etl::move(temp);
84  }
85 #else
86  template <typename T>
87  void swap(T& a, T& b) ETL_NOEXCEPT
88  {
89  T temp(a);
90  a = b;
91  b = temp;
92  }
93 #endif
94 
95  template< class T, size_t N >
96  void swap(T(&a)[N], T(&b)[N]) ETL_NOEXCEPT
97  {
98  for (size_t i = 0; i < N; ++i)
99  {
100  swap(a[i], b[i]);
101  }
102  }
103 #endif
104 
105  //******************************************************************************
106  template <typename T1, typename T2>
107  struct pair
108  {
109  typedef T1 first_type;
110  typedef T2 second_type;
111 
112  T1 first;
113  T2 second;
114 
116  ETL_CONSTEXPR pair()
117  : first(T1())
118  , second(T2())
119  {
120  }
121 
123  ETL_CONSTEXPR14 pair(const T1& a, const T2& b)
124  : first(a)
125  , second(b)
126  {
127  }
128 
129 #if ETL_CPP11_SUPPORTED
131  template <typename U1, typename U2>
132  ETL_CONSTEXPR14 pair(U1&& a, U2&& b)
133  : first(etl::forward<U1>(a))
134  , second(etl::forward<U2>(b))
135  {
136  }
137 #endif
138 
140  template <typename U1, typename U2>
141  ETL_CONSTEXPR14 pair(const pair<U1, U2>& other)
142  : first(other.first)
143  , second(other.second)
144  {
145  }
146 
148  pair(const pair<T1, T2>& other)
149  : first(other.first)
150  , second(other.second)
151  {
152  }
153 
154 #if ETL_CPP11_SUPPORTED
156  template <typename U1, typename U2>
157  ETL_CONSTEXPR14 pair(pair<U1, U2>&& other)
158  : first(etl::forward<U1>(other.first))
159  , second(etl::forward<U2>(other.second))
160  {
161  }
162 #endif
163 
164 #if defined(ETL_IN_UNIT_TEST) || ETL_USING_STL
166  template <typename U1, typename U2>
167  operator std::pair<U1, U2>()
168  {
169  return std::make_pair(first, second);
170  }
171 
173  template <typename U1, typename U2>
174  pair(const std::pair<U1, U2>& other)
175  : first(other.first)
176  , second(other.second)
177  {
178  }
179 
180 #if ETL_CPP11_SUPPORTED
182  template <typename U1, typename U2>
183  pair(std::pair<U1, U2>&& other)
184  : first(etl::forward<U1>(other.first))
185  , second(etl::forward<U2>(other.second))
186  {
187  }
188 #endif
189 #endif
190 
191  void swap(pair<T1, T2>& other)
192  {
193  using ETL_OR_STD::swap;
194 
195  swap(first, other.first);
196  swap(second, other.second);
197  }
198 
199  pair<T1, T2>& operator =(const pair<T1, T2>& other)
200  {
201  first = other.first;
202  second = other.second;
203 
204  return *this;
205  }
206 
207  template <typename U1, typename U2>
208  pair<U1, U2>& operator =(const pair<U1, U2>& other)
209  {
210  first = other.first;
211  second = other.second;
212 
213  return *this;
214  }
215 
216 #if ETL_CPP11_SUPPORTED
217  pair<T1, T2>& operator =(pair<T1, T2>&& other)
218  {
219  first = etl::forward<T1>(other.first);
220  second = etl::forward<T2>(other.second);
221 
222  return *this;
223  }
224 
225  template <typename U1, typename U2>
226  pair<U1, U2>& operator =(pair<U1, U2>&& other)
227  {
228  first = etl::forward<U1>(other.first);
229  second = etl::forward<U2>(other.second);
230 
231  return *this;
232  }
233 #endif
234  };
235 
236  //******************************************************************************
237 #if ETL_CPP11_SUPPORTED
238  template <typename T1, typename T2>
239  inline pair<T1, T2> make_pair(T1&& a, T2&& b)
240  {
241  return pair<T1, T2>(etl::forward<T1>(a), etl::forward<T2>(b));
242  }
243 #else
244  template <typename T1, typename T2>
245  inline pair<T1, T2> make_pair(T1 a, T2 b)
246  {
247  return pair<T1, T2>(a, b);
248  }
249 #endif
250 
251  //******************************************************************************
252  template <typename T1, typename T2>
253  inline void swap(pair<T1, T2>& a, pair<T1, T2>& b)
254  {
255  a.swap(b);
256  }
257 
258  //******************************************************************************
259  template <typename T1, typename T2>
260  inline bool operator ==(const pair<T1, T2>& a, const pair<T1, T2>& b)
261  {
262  return (a.first == b.first) && (a.second == b.second);
263  }
264 
265  template <typename T1, typename T2>
266  inline bool operator !=(const pair<T1, T2>& a, const pair<T1, T2>& b)
267  {
268  return !(a == b);
269  }
270 
271  template <typename T1, typename T2>
272  inline bool operator <(const pair<T1, T2>& a, const pair<T1, T2>& b)
273  {
274  return (a.first < b.first) ||
275  (!(b.first < a.first) && (a.second < b.second));
276  }
277 
278  template <typename T1, typename T2>
279  inline bool operator >(const pair<T1, T2>& a, const pair<T1, T2>& b)
280  {
281  return (b < a);
282  }
283 
284  template <typename T1, typename T2>
285  inline bool operator <=(const pair<T1, T2>& a, const pair<T1, T2>& b)
286  {
287  return !(b < a);
288  }
289 
290  template <typename T1, typename T2>
291  inline bool operator >=(const pair<T1, T2>& a, const pair<T1, T2>& b)
292  {
293  return !(a < b);
294  }
295 
296 #if ETL_NOT_USING_STL || ETL_CPP14_NOT_SUPPORTED
297  //***************************************************************************
299  //***************************************************************************
300  template <typename T>
301  T exchange(T& object, const T& new_value)
302  {
303  T old_value = object;
304  object = new_value;
305  return old_value;
306  }
307 
308  template <typename T, typename U>
309  T exchange(T& object, const U& new_value)
310  {
311  T old_value = object;
312  object = new_value;
313  return old_value;
314  }
315 #else
316  //***************************************************************************
318  //***************************************************************************
319  template <typename T, typename U = T>
320  T exchange(T& object, const U& new_value)
321  {
322  return std::exchange(object, new_value);
323  }
324 #endif
325 
326  //***************************************************************************
328  //***************************************************************************
329  template <typename T>
330  typename etl::add_const<T>::type& as_const(T& t)
331  {
332  return t;
333  }
334 
335  //******************************************************************************
337  template <typename T>
339  {
340  coordinate_2d()
341  : x(T(0))
342  , y(T(0))
343  {
344  }
345 
346  coordinate_2d(T x_, T y_)
347  : x(x_)
348  , y(y_)
349  {
350  }
351 
352  friend bool operator ==(const coordinate_2d& lhs, const coordinate_2d& rhs)
353  {
354  return (lhs.x == rhs.x) && (lhs.y == rhs.y);
355  }
356 
357  friend bool operator !=(const coordinate_2d& lhs, const coordinate_2d& rhs)
358  {
359  return !(lhs == rhs);
360  }
361 
362  T x;
363  T y;
364  };
365 }
366 
367 #endif
368 
Definition: absolute.h:37
T exchange(T &object, const T &new_value)
exchange (const)
Definition: utility.h:301
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
etl::add_const< T >::type & as_const(T &t)
as_const
Definition: utility.h:330
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition: array.h:570
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
2D coordinate type.
Definition: utility.h:339
Definition: utility.h:108
pair(const std::pair< U1, U2 > &other)
Constructing from std::pair.
Definition: utility.h:174
ETL_CONSTEXPR pair()
Default constructor.
Definition: utility.h:116
pair(const pair< T1, T2 > &other)
Copy constructor.
Definition: utility.h:148