Embedded Template Library 1.0
Loading...
Searching...
No Matches
cyclic_value.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) 2014 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_CYCLIC_VALUE_INCLUDED
32#define ETL_CYCLIC_VALUE_INCLUDED
33
37
38#include "platform.h"
39#include "algorithm.h"
40#include "exception.h"
41#include "type_traits.h"
42
43namespace etl
44{
45 //***************************************************************************
47 //***************************************************************************
48 template <typename T, T First = 0, T Last = 0, bool EtlRuntimeSpecialisation = ((First == 0) && (Last == 0))>
50
51 //***************************************************************************
58 //***************************************************************************
59 template <typename T, T First, T Last>
60 class cyclic_value<T, First, Last, false>
61 {
62 public:
63
64 //*************************************************************************
67 //*************************************************************************
68 ETL_CONSTEXPR cyclic_value()
69 : value(First)
70 {
71 }
72
73 //*************************************************************************
77 //*************************************************************************
78 ETL_CONSTEXPR14 explicit cyclic_value(T initial)
79 {
80 set(initial);
81 }
82
83 //*************************************************************************
85 //*************************************************************************
86 ETL_CONSTEXPR cyclic_value(const cyclic_value<T, First, Last>& other)
87 : value(other.value)
88 {
89 }
90
91 //*************************************************************************
93 //*************************************************************************
95 {
96 value = other.value;
97
98 return *this;
99 }
100
101 //*************************************************************************
105 //*************************************************************************
106 ETL_CONSTEXPR14 void set(T value_)
107 {
108 value = etl::clamp(value_, First, Last);
109 }
110
111 //*************************************************************************
113 //*************************************************************************
114 ETL_CONSTEXPR14 void to_first()
115 {
116 value = First;
117 }
118
119 //*************************************************************************
121 //*************************************************************************
122 ETL_CONSTEXPR14 void to_last()
123 {
124 value = Last;
125 }
126
127 //*************************************************************************
130 //*************************************************************************
131 ETL_CONSTEXPR14 void advance(int n)
132 {
133 if (n > 0)
134 {
135 for (int i = 0; i < n; ++i)
136 {
137 operator++();
138 }
139 }
140 else
141 {
142 for (int i = 0; i < -n; ++i)
143 {
144 operator--();
145 }
146 }
147 }
148
149 //*************************************************************************
152 //*************************************************************************
153 ETL_CONSTEXPR14 operator T()
154 {
155 return value;
156 }
157
158 //*************************************************************************
161 //*************************************************************************
162 ETL_CONSTEXPR operator const T() const
163 {
164 return value;
165 }
166
167 //*************************************************************************
169 //*************************************************************************
170 ETL_CONSTEXPR14 cyclic_value& operator++()
171 {
172 if (value >= Last) ETL_UNLIKELY
173 {
174 value = First;
175 }
176 else
177 {
178 ++value;
179 }
180
181 return *this;
182 }
183
184 //*************************************************************************
186 //*************************************************************************
187 ETL_CONSTEXPR14 cyclic_value operator++(int)
188 {
189 cyclic_value temp(*this);
190
191 operator++();
192
193 return temp;
194 }
195
196 //*************************************************************************
198 //*************************************************************************
199 ETL_CONSTEXPR14 cyclic_value& operator--()
200 {
201 if (value <= First) ETL_UNLIKELY
202 {
203 value = Last;
204 }
205 else
206 {
207 --value;
208 }
209
210 return *this;
211 }
212
213 //*************************************************************************
215 //*************************************************************************
216 ETL_CONSTEXPR14 cyclic_value operator--(int)
217 {
218 cyclic_value temp(*this);
219
220 operator--();
221
222 return temp;
223 }
224
225 //*************************************************************************
227 //*************************************************************************
228 ETL_CONSTEXPR14 cyclic_value& operator=(T t)
229 {
230 set(t);
231 return *this;
232 }
233
234 //*************************************************************************
236 //*************************************************************************
237 template <const T FIRST2, const T LAST2>
239 {
240 set(other.get());
241 return *this;
242 }
243
244 //*************************************************************************
246 //*************************************************************************
247 ETL_CONSTEXPR T get() const
248 {
249 return value;
250 }
251
252 //*************************************************************************
254 //*************************************************************************
255 static ETL_CONSTEXPR T first()
256 {
257 return First;
258 }
259
260 //*************************************************************************
262 //*************************************************************************
263 static ETL_CONSTEXPR T last()
264 {
265 return Last;
266 }
267
268 //*************************************************************************
270 //*************************************************************************
272 {
273 using ETL_OR_STD::swap; // Allow ADL
274
275 swap(value, other.value);
276 }
277
278 //*************************************************************************
280 //*************************************************************************
282 {
283 lhs.swap(rhs);
284 }
285
286 //*************************************************************************
288 //*************************************************************************
289 friend ETL_CONSTEXPR bool operator==(const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
290 {
291 return lhs.value == rhs.value;
292 }
293
294 //*************************************************************************
296 //*************************************************************************
297 friend ETL_CONSTEXPR bool operator!=(const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
298 {
299 return !(lhs == rhs);
300 }
301
302 private:
303
304 T value;
305 };
306
307 //***************************************************************************
314 //***************************************************************************
315 template <typename T, T First, T Last>
316 class cyclic_value<T, First, Last, true>
317 {
318 public:
319
320 //*************************************************************************
324 //*************************************************************************
325 ETL_CONSTEXPR cyclic_value()
326 : value(First)
327 , first_value(First)
328 , last_value(Last)
329 {
330 }
331
332 //*************************************************************************
337 //*************************************************************************
338 ETL_CONSTEXPR cyclic_value(T first_, T last_)
339 : value(first_)
340 , first_value(first_)
341 , last_value(last_)
342 {
343 }
344
345 //*************************************************************************
351 //*************************************************************************
352 ETL_CONSTEXPR14 cyclic_value(T first_, T last_, T initial)
353 : first_value(first_)
354 , last_value(last_)
355 {
356 set(initial);
357 }
358
359 //*************************************************************************
361 //*************************************************************************
362 ETL_CONSTEXPR cyclic_value(const cyclic_value& other)
363 : value(other.value)
364 , first_value(other.first_value)
365 , last_value(other.last_value)
366 {
367 }
368
369 //*************************************************************************
374 //*************************************************************************
375 ETL_CONSTEXPR14 void set(T first_, T last_)
376 {
377 first_value = first_;
378 last_value = last_;
379 value = first_;
380 }
381
382 //*************************************************************************
385 //*************************************************************************
386 ETL_CONSTEXPR14 void set(T value_)
387 {
388 value = etl::clamp(value_, first_value, last_value);
389 }
390
391 //*************************************************************************
393 //*************************************************************************
394 ETL_CONSTEXPR14 void to_first()
395 {
396 value = first_value;
397 }
398
399 //*************************************************************************
401 //*************************************************************************
402 ETL_CONSTEXPR14 void to_last()
403 {
404 value = last_value;
405 }
406
407 //*************************************************************************
410 //*************************************************************************
411 ETL_CONSTEXPR14 void advance(int n)
412 {
413 if (n > 0)
414 {
415 for (int i = 0; i < n; ++i)
416 {
417 operator++();
418 }
419 }
420 else
421 {
422 for (int i = 0; i < -n; ++i)
423 {
424 operator--();
425 }
426 }
427 }
428
429 //*************************************************************************
432 //*************************************************************************
433 ETL_CONSTEXPR14 operator T()
434 {
435 return value;
436 }
437
438 //*************************************************************************
441 //*************************************************************************
442 ETL_CONSTEXPR operator const T() const
443 {
444 return value;
445 }
446
447 //*************************************************************************
449 //*************************************************************************
450 ETL_CONSTEXPR14 cyclic_value& operator++()
451 {
452 if (value >= last_value)
453 {
454 value = first_value;
455 }
456 else
457 {
458 ++value;
459 }
460
461 return *this;
462 }
463
464 //*************************************************************************
466 //*************************************************************************
467 ETL_CONSTEXPR14 cyclic_value operator++(int)
468 {
469 cyclic_value temp(*this);
470
471 operator++();
472
473 return temp;
474 }
475
476 //*************************************************************************
478 //*************************************************************************
479 ETL_CONSTEXPR14 cyclic_value& operator--()
480 {
481 if (value <= first_value)
482 {
483 value = last_value;
484 }
485 else
486 {
487 --value;
488 }
489
490 return *this;
491 }
492
493 //*************************************************************************
495 //*************************************************************************
496 ETL_CONSTEXPR14 cyclic_value operator--(int)
497 {
498 cyclic_value temp(*this);
499
500 operator--();
501
502 return temp;
503 }
504
505 //*************************************************************************
507 //*************************************************************************
508 ETL_CONSTEXPR14 cyclic_value& operator=(T t)
509 {
510 set(t);
511 return *this;
512 }
513
514 //*************************************************************************
516 //*************************************************************************
517 ETL_CONSTEXPR14 cyclic_value& operator=(const cyclic_value& other)
518 {
519 value = other.value;
520 first_value = other.first_value;
521 last_value = other.last_value;
522 return *this;
523 }
524
525 //*************************************************************************
527 //*************************************************************************
528 ETL_CONSTEXPR T get() const
529 {
530 return value;
531 }
532
533 //*************************************************************************
535 //*************************************************************************
536 ETL_CONSTEXPR T first() const
537 {
538 return first_value;
539 }
540
541 //*************************************************************************
543 //*************************************************************************
544 ETL_CONSTEXPR T last() const
545 {
546 return last_value;
547 }
548
549 //*************************************************************************
551 //*************************************************************************
553 {
554 using ETL_OR_STD::swap; // Allow ADL
555
556 swap(first_value, other.first_value);
557 swap(last_value, other.last_value);
558 swap(value, other.value);
559 }
560
561 //*************************************************************************
563 //*************************************************************************
565 {
566 lhs.swap(rhs);
567 }
568
569 //*************************************************************************
571 //*************************************************************************
572 friend ETL_CONSTEXPR bool operator==(const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
573 {
574 return (lhs.value == rhs.value) && (lhs.first_value == rhs.first_value) && (lhs.last_value == rhs.last_value);
575 }
576
577 //*************************************************************************
579 //*************************************************************************
580 friend ETL_CONSTEXPR bool operator!=(const cyclic_value<T, First, Last>& lhs, const cyclic_value<T, First, Last>& rhs)
581 {
582 return !(lhs == rhs);
583 }
584
585 private:
586
587 T value;
588 T first_value;
589 T last_value;
590 };
591} // namespace etl
592
593#endif
Provides a value that cycles between two limits.
Definition cyclic_value.h:49
ETL_CONSTEXPR T clamp(const T &value, const T &low, const T &high, TCompare compare)
Definition algorithm.h:2353
ETL_CONSTEXPR14 void advance(int n)
Definition cyclic_value.h:411
void swap(cyclic_value< T, First, Last > &other)
Swaps the values.
Definition cyclic_value.h:271
friend void swap(cyclic_value< T, First, Last > &lhs, cyclic_value< T, First, Last > &rhs)
Swaps the values.
Definition cyclic_value.h:281
ETL_CONSTEXPR14 cyclic_value & operator=(const cyclic_value< T, First, Last > &other)
Assignment operator.
Definition cyclic_value.h:94
ETL_CONSTEXPR14 void to_first()
Resets the value to the first in the range.
Definition cyclic_value.h:114
ETL_CONSTEXPR14 void to_last()
Resets the value to the last in the range.
Definition cyclic_value.h:122
ETL_CONSTEXPR14 void set(T first_, T last_)
Definition cyclic_value.h:375
friend ETL_CONSTEXPR bool operator!=(const cyclic_value< T, First, Last > &lhs, const cyclic_value< T, First, Last > &rhs)
Operator !=.
Definition cyclic_value.h:297
ETL_CONSTEXPR14 cyclic_value & operator=(const cyclic_value< T, FIRST2, LAST2 > &other)
= operator.
Definition cyclic_value.h:238
ETL_CONSTEXPR14 void set(T value_)
Definition cyclic_value.h:106
ETL_CONSTEXPR14 cyclic_value operator++(int)
++ operator.
Definition cyclic_value.h:467
ETL_CONSTEXPR T get() const
Gets the value.
Definition cyclic_value.h:247
ETL_CONSTEXPR T last() const
Gets the last value.
Definition cyclic_value.h:544
ETL_CONSTEXPR T get() const
Gets the value.
Definition cyclic_value.h:528
ETL_CONSTEXPR14 cyclic_value(T initial)
Definition cyclic_value.h:78
friend ETL_CONSTEXPR bool operator==(const cyclic_value< T, First, Last > &lhs, const cyclic_value< T, First, Last > &rhs)
Operator ==.
Definition cyclic_value.h:289
ETL_CONSTEXPR14 cyclic_value & operator++()
++ operator.
Definition cyclic_value.h:450
ETL_CONSTEXPR cyclic_value()
Definition cyclic_value.h:325
void swap(cyclic_value< T, First, Last > &other)
Swaps the values.
Definition cyclic_value.h:552
ETL_CONSTEXPR14 void to_first()
Resets the value to the first in the range.
Definition cyclic_value.h:394
ETL_CONSTEXPR14 cyclic_value operator--(int)
– operator.
Definition cyclic_value.h:216
ETL_CONSTEXPR14 cyclic_value & operator=(const cyclic_value &other)
= operator.
Definition cyclic_value.h:517
ETL_CONSTEXPR T first() const
Gets the first value.
Definition cyclic_value.h:536
static ETL_CONSTEXPR T last()
Gets the last value.
Definition cyclic_value.h:263
ETL_CONSTEXPR cyclic_value()
Definition cyclic_value.h:68
ETL_CONSTEXPR14 cyclic_value & operator=(T t)
= operator.
Definition cyclic_value.h:228
ETL_CONSTEXPR14 cyclic_value operator--(int)
– operator.
Definition cyclic_value.h:496
ETL_CONSTEXPR14 cyclic_value & operator=(T t)
= operator.
Definition cyclic_value.h:508
ETL_CONSTEXPR14 cyclic_value operator++(int)
++ operator.
Definition cyclic_value.h:187
ETL_CONSTEXPR14 cyclic_value & operator--()
– operator.
Definition cyclic_value.h:199
ETL_CONSTEXPR14 cyclic_value & operator--()
– operator.
Definition cyclic_value.h:479
ETL_CONSTEXPR cyclic_value(const cyclic_value< T, First, Last > &other)
Copy constructor.
Definition cyclic_value.h:86
static ETL_CONSTEXPR T first()
Gets the first value.
Definition cyclic_value.h:255
ETL_CONSTEXPR14 void set(T value_)
Definition cyclic_value.h:386
ETL_CONSTEXPR14 void to_last()
Resets the value to the last in the range.
Definition cyclic_value.h:402
ETL_CONSTEXPR14 cyclic_value(T first_, T last_, T initial)
Definition cyclic_value.h:352
ETL_CONSTEXPR14 cyclic_value & operator++()
++ operator.
Definition cyclic_value.h:170
ETL_CONSTEXPR cyclic_value(T first_, T last_)
Definition cyclic_value.h:338
ETL_CONSTEXPR cyclic_value(const cyclic_value &other)
Copy constructor.
Definition cyclic_value.h:362
ETL_CONSTEXPR14 void advance(int n)
Definition cyclic_value.h:131
Definition absolute.h:40
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:856