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 "static_assert.h"
40#include "exception.h"
41#include "static_assert.h"
42#include "type_traits.h"
43#include "algorithm.h"
44
45#include <stddef.h>
46
47namespace etl
48{
49 //***************************************************************************
51 //***************************************************************************
52 template <typename T, T First = 0, T Last = 0, bool EtlRuntimeSpecialisation = ((First == 0) && (Last == 0))>
54
55 //***************************************************************************
62 //***************************************************************************
63 template <typename T, T First, T Last>
64 class cyclic_value<T, First, Last, false>
65 {
66 public:
67
68 //*************************************************************************
71 //*************************************************************************
73 : value(First)
74 {
75 }
76
77 //*************************************************************************
81 //*************************************************************************
82 explicit cyclic_value(T initial)
83 {
84 set(initial);
85 }
86
87 //*************************************************************************
89 //*************************************************************************
91 : value(other.value)
92 {
93 }
94
95 //*************************************************************************
97 //*************************************************************************
99 {
100 value = other.value;
101
102 return *this;
103 }
104
105 //*************************************************************************
109 //*************************************************************************
110 void set(T value_)
111 {
112 value = etl::clamp(value_, First, Last);
113 }
114
115 //*************************************************************************
117 //*************************************************************************
118 void to_first()
119 {
120 value = First;
121 }
122
123 //*************************************************************************
125 //*************************************************************************
126 void to_last()
127 {
128 value = Last;
129 }
130
131 //*************************************************************************
134 //*************************************************************************
135 void advance(int n)
136 {
137 if (n > 0)
138 {
139 for (int i = 0; i < n; ++i)
140 {
141 operator ++();
142 }
143 }
144 else
145 {
146 for (int i = 0; i < -n; ++i)
147 {
148 operator --();
149 }
150 }
151 }
152
153 //*************************************************************************
156 //*************************************************************************
157 operator T()
158 {
159 return value;
160 }
161
162 //*************************************************************************
165 //*************************************************************************
166 operator const T() const
167 {
168 return value;
169 }
170
171 //*************************************************************************
173 //*************************************************************************
174 cyclic_value& operator ++()
175 {
176 if (value >= Last) ETL_UNLIKELY
177 {
178 value = First;
179 }
180 else
181 {
182 ++value;
183 }
184
185 return *this;
186 }
187
188 //*************************************************************************
190 //*************************************************************************
191 cyclic_value operator ++(int)
192 {
193 cyclic_value temp(*this);
194
195 operator++();
196
197 return temp;
198 }
199
200 //*************************************************************************
202 //*************************************************************************
203 cyclic_value& operator --()
204 {
205 if (value <= First) ETL_UNLIKELY
206 {
207 value = Last;
208 }
209 else
210 {
211 --value;
212 }
213
214 return *this;
215 }
216
217 //*************************************************************************
219 //*************************************************************************
220 cyclic_value operator --(int)
221 {
222 cyclic_value temp(*this);
223
224 operator--();
225
226 return temp;
227 }
228
229 //*************************************************************************
231 //*************************************************************************
232 cyclic_value& operator =(T t)
233 {
234 set(t);
235 return *this;
236 }
237
238 //*************************************************************************
240 //*************************************************************************
241 template <const T FIRST2, const T LAST2>
243 {
244 set(other.get());
245 return *this;
246 }
247
248 //*************************************************************************
250 //*************************************************************************
251 T get() const
252 {
253 return value;
254 }
255
256 //*************************************************************************
258 //*************************************************************************
259 static ETL_CONSTEXPR T first()
260 {
261 return First;
262 }
263
264 //*************************************************************************
266 //*************************************************************************
267 static ETL_CONSTEXPR T last()
268 {
269 return Last;
270 }
271
272 //*************************************************************************
274 //*************************************************************************
276 {
277 using ETL_OR_STD::swap; // Allow ADL
278
279 swap(value, other.value);
280 }
281
282 //*************************************************************************
284 //*************************************************************************
286 {
287 lhs.swap(rhs);
288 }
289
290 //*************************************************************************
292 //*************************************************************************
294 {
295 return lhs.value == rhs.value;
296 }
297
298 //*************************************************************************
300 //*************************************************************************
302 {
303 return !(lhs == rhs);
304 }
305
306 private:
307
308 T value;
309 };
310
311 //***************************************************************************
318 //***************************************************************************
319 template <typename T, T First, T Last>
320 class cyclic_value<T, First, Last, true>
321 {
322 public:
323
324 //*************************************************************************
328 //*************************************************************************
330 : value(First)
331 , first_value(First)
332 , last_value(Last)
333 {
334 }
335
336 //*************************************************************************
341 //*************************************************************************
342 cyclic_value(T first_, T last_)
343 : value(first_)
344 , first_value(first_)
345 , last_value(last_)
346 {
347 }
348
349 //*************************************************************************
355 //*************************************************************************
356 cyclic_value(T first_, T last_, T initial)
357 : first_value(first_)
358 , last_value(last_)
359 {
360 set(initial);
361 }
362
363 //*************************************************************************
365 //*************************************************************************
367 : value(other.value)
368 , first_value(other.first_value)
369 , last_value(other.last_value)
370 {
371 }
372
373 //*************************************************************************
378 //*************************************************************************
379 void set(T first_, T last_)
380 {
381 first_value = first_;
382 last_value = last_;
383 value = first_;
384 }
385
386 //*************************************************************************
389 //*************************************************************************
390 void set(T value_)
391 {
392 value = etl::clamp(value_, first_value, last_value);
393 }
394
395 //*************************************************************************
397 //*************************************************************************
398 void to_first()
399 {
400 value = first_value;
401 }
402
403 //*************************************************************************
405 //*************************************************************************
406 void to_last()
407 {
408 value = last_value;
409 }
410
411 //*************************************************************************
414 //*************************************************************************
415 void advance(int n)
416 {
417 if (n > 0)
418 {
419 for (int i = 0; i < n; ++i)
420 {
421 operator ++();
422 }
423 }
424 else
425 {
426 for (int i = 0; i < -n; ++i)
427 {
428 operator --();
429 }
430 }
431 }
432
433 //*************************************************************************
436 //*************************************************************************
437 operator T()
438 {
439 return value;
440 }
441
442 //*************************************************************************
445 //*************************************************************************
446 operator const T() const
447 {
448 return value;
449 }
450
451 //*************************************************************************
453 //*************************************************************************
454 cyclic_value& operator ++()
455 {
456 if (value >= last_value)
457 {
458 value = first_value;
459 }
460 else
461 {
462 ++value;
463 }
464
465 return *this;
466 }
467
468 //*************************************************************************
470 //*************************************************************************
471 cyclic_value operator ++(int)
472 {
473 cyclic_value temp(*this);
474
475 operator++();
476
477 return temp;
478 }
479
480 //*************************************************************************
482 //*************************************************************************
483 cyclic_value& operator --()
484 {
485 if (value <= first_value)
486 {
487 value = last_value;
488 }
489 else
490 {
491 --value;
492 }
493
494 return *this;
495 }
496
497 //*************************************************************************
499 //*************************************************************************
500 cyclic_value operator --(int)
501 {
502 cyclic_value temp(*this);
503
504 operator--();
505
506 return temp;
507 }
508
509 //*************************************************************************
511 //*************************************************************************
512 cyclic_value& operator =(T t)
513 {
514 set(t);
515 return *this;
516 }
517
518 //*************************************************************************
520 //*************************************************************************
521 cyclic_value& operator =(const cyclic_value& other)
522 {
523 value = other.value;
524 first_value = other.first_value;
525 last_value = other.last_value;
526 return *this;
527 }
528
529 //*************************************************************************
531 //*************************************************************************
532 T get() const
533 {
534 return value;
535 }
536
537 //*************************************************************************
539 //*************************************************************************
540 T first() const
541 {
542 return first_value;
543 }
544
545 //*************************************************************************
547 //*************************************************************************
548 T last() const
549 {
550 return last_value;
551 }
552
553 //*************************************************************************
555 //*************************************************************************
557 {
558 using ETL_OR_STD::swap; // Allow ADL
559
560 swap(first_value, other.first_value);
561 swap(last_value, other.last_value);
562 swap(value, other.value);
563 }
564
565 //*************************************************************************
567 //*************************************************************************
569 {
570 lhs.swap(rhs);
571 }
572
573 //*************************************************************************
575 //*************************************************************************
577 {
578 return (lhs.value == rhs.value) &&
579 (lhs.first_value == rhs.first_value) &&
580 (lhs.last_value == rhs.last_value);
581 }
582
583 //*************************************************************************
585 //*************************************************************************
587 {
588 return !(lhs == rhs);
589 }
590
591 private:
592
593 T value;
594 T first_value;
595 T last_value;
596 };
597}
598
599#endif
Provides a value that cycles between two limits.
Definition cyclic_value.h:53
ETL_CONSTEXPR T clamp(const T &value, const T &low, const T &high, TCompare compare)
Definition algorithm.h:2268
void swap(cyclic_value< T, First, Last > &other)
Swaps the values.
Definition cyclic_value.h:275
cyclic_value(T first_, T last_)
Definition cyclic_value.h:342
void to_first()
Resets the value to the first in the range.
Definition cyclic_value.h:398
friend void swap(cyclic_value< T, First, Last > &lhs, cyclic_value< T, First, Last > &rhs)
Swaps the values.
Definition cyclic_value.h:285
cyclic_value(const cyclic_value< T, First, Last > &other)
Copy constructor.
Definition cyclic_value.h:90
cyclic_value & operator--()
– operator.
Definition cyclic_value.h:203
T last() const
Gets the last value.
Definition cyclic_value.h:548
void advance(int n)
Definition cyclic_value.h:415
void set(T value_)
Definition cyclic_value.h:110
void advance(int n)
Definition cyclic_value.h:135
void set(T first_, T last_)
Definition cyclic_value.h:379
void swap(cyclic_value< T, First, Last > &other)
Swaps the values.
Definition cyclic_value.h:556
cyclic_value(T initial)
Definition cyclic_value.h:82
cyclic_value & operator--()
– operator.
Definition cyclic_value.h:483
static ETL_CONSTEXPR T last()
Gets the last value.
Definition cyclic_value.h:267
cyclic_value & operator++()
++ operator.
Definition cyclic_value.h:454
cyclic_value(const cyclic_value &other)
Copy constructor.
Definition cyclic_value.h:366
T get() const
Gets the value.
Definition cyclic_value.h:532
T first() const
Gets the first value.
Definition cyclic_value.h:540
cyclic_value(T first_, T last_, T initial)
Definition cyclic_value.h:356
static ETL_CONSTEXPR T first()
Gets the first value.
Definition cyclic_value.h:259
cyclic_value()
Definition cyclic_value.h:329
cyclic_value & operator++()
++ operator.
Definition cyclic_value.h:174
void to_last()
Resets the value to the last in the range.
Definition cyclic_value.h:126
void to_first()
Resets the value to the first in the range.
Definition cyclic_value.h:118
void to_last()
Resets the value to the last in the range.
Definition cyclic_value.h:406
T get() const
Gets the value.
Definition cyclic_value.h:251
void set(T value_)
Definition cyclic_value.h:390
cyclic_value()
Definition cyclic_value.h:72
bitset_ext
Definition absolute.h:39
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:838
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1151
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1139