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 //*************************************************************************
111 {
112 if (value_ > Last)
113 {
114 value_ = Last;
115 }
116 else if (value_ < First)
117 {
118 value_ = First;
119 }
120
121 value = value_;
122 }
123
124 //*************************************************************************
126 //*************************************************************************
127 void to_first()
128 {
129 value = First;
130 }
131
132 //*************************************************************************
134 //*************************************************************************
135 void to_last()
136 {
137 value = Last;
138 }
139
140 //*************************************************************************
143 //*************************************************************************
144 void advance(int n)
145 {
146 if (n > 0)
147 {
148 for (int i = 0; i < n; ++i)
149 {
150 operator ++();
151 }
152 }
153 else
154 {
155 for (int i = 0; i < -n; ++i)
156 {
157 operator --();
158 }
159 }
160 }
161
162 //*************************************************************************
165 //*************************************************************************
166 operator T()
167 {
168 return value;
169 }
170
171 //*************************************************************************
174 //*************************************************************************
175 operator const T() const
176 {
177 return value;
178 }
179
180 //*************************************************************************
182 //*************************************************************************
184 {
185 if (value >= Last) ETL_UNLIKELY
186 {
187 value = First;
188 }
189 else
190 {
191 ++value;
192 }
193
194 return *this;
195 }
196
197 //*************************************************************************
199 //*************************************************************************
201 {
202 cyclic_value temp(*this);
203
204 operator++();
205
206 return temp;
207 }
208
209 //*************************************************************************
211 //*************************************************************************
213 {
214 if (value <= First) ETL_UNLIKELY
215 {
216 value = Last;
217 }
218 else
219 {
220 --value;
221 }
222
223 return *this;
224 }
225
226 //*************************************************************************
228 //*************************************************************************
230 {
231 cyclic_value temp(*this);
232
233 operator--();
234
235 return temp;
236 }
237
238 //*************************************************************************
240 //*************************************************************************
242 {
243 set(t);
244 return *this;
245 }
246
247 //*************************************************************************
249 //*************************************************************************
250 template <const T FIRST2, const T LAST2>
252 {
253 set(other.get());
254 return *this;
255 }
256
257 //*************************************************************************
259 //*************************************************************************
260 T get() const
261 {
262 return value;
263 }
264
265 //*************************************************************************
267 //*************************************************************************
268 static ETL_CONSTEXPR T first()
269 {
270 return First;
271 }
272
273 //*************************************************************************
275 //*************************************************************************
276 static ETL_CONSTEXPR T last()
277 {
278 return Last;
279 }
280
281 //*************************************************************************
283 //*************************************************************************
285 {
286 using ETL_OR_STD::swap; // Allow ADL
287
288 swap(value, other.value);
289 }
290
291 //*************************************************************************
293 //*************************************************************************
298
299 //*************************************************************************
301 //*************************************************************************
303 {
304 return lhs.value == rhs.value;
305 }
306
307 //*************************************************************************
309 //*************************************************************************
311 {
312 return !(lhs == rhs);
313 }
314
315 private:
316
317 T value;
318 };
319
320 //***************************************************************************
327 //***************************************************************************
328 template <typename T, T First, T Last>
329 class cyclic_value<T, First, Last, true>
330 {
331 public:
332
333 //*************************************************************************
337 //*************************************************************************
339 : value(First)
340 , first_value(First)
341 , last_value(Last)
342 {
343 }
344
345 //*************************************************************************
350 //*************************************************************************
352 : value(first_)
353 , first_value(first_)
354 , last_value(last_)
355 {
356 }
357
358 //*************************************************************************
364 //*************************************************************************
366 : first_value(first_)
367 , last_value(last_)
368 {
369 set(initial);
370 }
371
372 //*************************************************************************
374 //*************************************************************************
376 : value(other.value)
377 , first_value(other.first_value)
378 , last_value(other.last_value)
379 {
380 }
381
382 //*************************************************************************
387 //*************************************************************************
389 {
390 first_value = first_;
391 last_value = last_;
392 value = first_;
393 }
394
395 //*************************************************************************
398 //*************************************************************************
400 {
401 if (value_ > last_value)
402 {
403 value_ = last_value;
404 }
405 else if (value_ < first_value)
406 {
407 value_ = first_value;
408 }
409
410 value = value_;
411 }
412
413 //*************************************************************************
415 //*************************************************************************
416 void to_first()
417 {
418 value = first_value;
419 }
420
421 //*************************************************************************
423 //*************************************************************************
424 void to_last()
425 {
426 value = last_value;
427 }
428
429 //*************************************************************************
432 //*************************************************************************
433 void advance(int n)
434 {
435 if (n > 0)
436 {
437 for (int i = 0; i < n; ++i)
438 {
439 operator ++();
440 }
441 }
442 else
443 {
444 for (int i = 0; i < -n; ++i)
445 {
446 operator --();
447 }
448 }
449 }
450
451 //*************************************************************************
454 //*************************************************************************
455 operator T()
456 {
457 return value;
458 }
459
460 //*************************************************************************
463 //*************************************************************************
464 operator const T() const
465 {
466 return value;
467 }
468
469 //*************************************************************************
471 //*************************************************************************
473 {
474 if (value >= last_value)
475 {
476 value = first_value;
477 }
478 else
479 {
480 ++value;
481 }
482
483 return *this;
484 }
485
486 //*************************************************************************
488 //*************************************************************************
490 {
491 cyclic_value temp(*this);
492
493 operator++();
494
495 return temp;
496 }
497
498 //*************************************************************************
500 //*************************************************************************
502 {
503 if (value <= first_value)
504 {
505 value = last_value;
506 }
507 else
508 {
509 --value;
510 }
511
512 return *this;
513 }
514
515 //*************************************************************************
517 //*************************************************************************
519 {
520 cyclic_value temp(*this);
521
522 operator--();
523
524 return temp;
525 }
526
527 //*************************************************************************
529 //*************************************************************************
531 {
532 set(t);
533 return *this;
534 }
535
536 //*************************************************************************
538 //*************************************************************************
540 {
541 value = other.value;
542 first_value = other.first_value;
543 last_value = other.last_value;
544 return *this;
545 }
546
547 //*************************************************************************
549 //*************************************************************************
550 T get() const
551 {
552 return value;
553 }
554
555 //*************************************************************************
557 //*************************************************************************
558 T first() const
559 {
560 return first_value;
561 }
562
563 //*************************************************************************
565 //*************************************************************************
566 T last() const
567 {
568 return last_value;
569 }
570
571 //*************************************************************************
573 //*************************************************************************
575 {
576 using ETL_OR_STD::swap; // Allow ADL
577
578 swap(first_value, other.first_value);
579 swap(last_value, other.last_value);
580 swap(value, other.value);
581 }
582
583 //*************************************************************************
585 //*************************************************************************
590
591 //*************************************************************************
593 //*************************************************************************
595 {
596 return (lhs.value == rhs.value) &&
597 (lhs.first_value == rhs.first_value) &&
598 (lhs.last_value == rhs.last_value);
599 }
600
601 //*************************************************************************
603 //*************************************************************************
605 {
606 return !(lhs == rhs);
607 }
608
609 private:
610
611 T value;
612 T first_value;
613 T last_value;
614 };
615}
616
617#endif
Provides a value that cycles between two limits.
Definition cyclic_value.h:53
A templated set implementation that uses a fixed size buffer.
Definition set.h:2548
void swap(cyclic_value< T, First, Last > &other)
Swaps the values.
Definition cyclic_value.h:284
cyclic_value(T first_, T last_)
Definition cyclic_value.h:351
void to_first()
Resets the value to the first in the range.
Definition cyclic_value.h:416
friend void swap(cyclic_value< T, First, Last > &lhs, cyclic_value< T, First, Last > &rhs)
Swaps the values.
Definition cyclic_value.h:294
cyclic_value(const cyclic_value< T, First, Last > &other)
Copy constructor.
Definition cyclic_value.h:90
T last() const
Gets the last value.
Definition cyclic_value.h:566
void advance(int n)
Definition cyclic_value.h:433
void set(T value_)
Definition cyclic_value.h:110
void advance(int n)
Definition cyclic_value.h:144
void set(T first_, T last_)
Definition cyclic_value.h:388
void swap(cyclic_value< T, First, Last > &other)
Swaps the values.
Definition cyclic_value.h:574
cyclic_value(T initial)
Definition cyclic_value.h:82
static ETL_CONSTEXPR T last()
Gets the last value.
Definition cyclic_value.h:276
cyclic_value(const cyclic_value &other)
Copy constructor.
Definition cyclic_value.h:375
T get() const
Gets the value.
Definition cyclic_value.h:550
T first() const
Gets the first value.
Definition cyclic_value.h:558
cyclic_value(T first_, T last_, T initial)
Definition cyclic_value.h:365
static ETL_CONSTEXPR T first()
Gets the first value.
Definition cyclic_value.h:268
cyclic_value()
Definition cyclic_value.h:338
void to_last()
Resets the value to the last in the range.
Definition cyclic_value.h:135
void to_first()
Resets the value to the first in the range.
Definition cyclic_value.h:127
void to_last()
Resets the value to the last in the range.
Definition cyclic_value.h:424
T get() const
Gets the value.
Definition cyclic_value.h:260
void set(T value_)
Definition cyclic_value.h:399
cyclic_value()
Definition cyclic_value.h:72
bitset_ext
Definition absolute.h:38
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:654
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition array.h:630
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:642
pair holds two objects of arbitrary type
Definition utility.h:164