Embedded Template Library 1.0
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 //*************************************************************************
108 //*************************************************************************
109 void set(T value_)
110 {
111 if (value_ > LAST)
112 {
113 value_ = LAST;
114 }
115 else if (value_ < FIRST)
116 {
117 value_ = FIRST;
118 }
119
120 value = value_;
121 }
122
123 //*************************************************************************
125 //*************************************************************************
126 void to_first()
127 {
128 value = FIRST;
129 }
130
131 //*************************************************************************
133 //*************************************************************************
134 void to_last()
135 {
136 value = LAST;
137 }
138
139 //*************************************************************************
142 //*************************************************************************
143 void advance(int n)
144 {
145 if (n > 0)
146 {
147 for (int i = 0; i < n; ++i)
148 {
149 operator ++();
150 }
151 }
152 else
153 {
154 for (int i = 0; i < -n; ++i)
155 {
156 operator --();
157 }
158 }
159 }
160
161 //*************************************************************************
164 //*************************************************************************
165 operator T()
166 {
167 return value;
168 }
169
170 //*************************************************************************
173 //*************************************************************************
174 operator const T() const
175 {
176 return value;
177 }
178
179 //*************************************************************************
181 //*************************************************************************
182 cyclic_value& operator ++()
183 {
184 if (value >= LAST) ETL_UNLIKELY
185 {
186 value = FIRST;
187 }
188 else
189 {
190 ++value;
191 }
192
193 return *this;
194 }
195
196 //*************************************************************************
198 //*************************************************************************
199 cyclic_value operator ++(int)
200 {
201 cyclic_value temp(*this);
202
203 operator++();
204
205 return temp;
206 }
207
208 //*************************************************************************
210 //*************************************************************************
211 cyclic_value& operator --()
212 {
213 if (value <= FIRST) ETL_UNLIKELY
214 {
215 value = LAST;
216 }
217 else
218 {
219 --value;
220 }
221
222 return *this;
223 }
224
225 //*************************************************************************
227 //*************************************************************************
228 cyclic_value operator --(int)
229 {
230 cyclic_value temp(*this);
231
232 operator--();
233
234 return temp;
235 }
236
237 //*************************************************************************
239 //*************************************************************************
240 cyclic_value& operator =(T t)
241 {
242 set(t);
243 return *this;
244 }
245
246 //*************************************************************************
248 //*************************************************************************
249 template <const T FIRST2, const T LAST2>
251 {
252 set(other.get());
253 return *this;
254 }
255
256 //*************************************************************************
258 //*************************************************************************
259 T get() const
260 {
261 return value;
262 }
263
264 //*************************************************************************
266 //*************************************************************************
267 const T first() const
268 {
269 return FIRST;
270 }
271
272 //*************************************************************************
274 //*************************************************************************
275 const T last() const
276 {
277 return LAST;
278 }
279
280 //*************************************************************************
282 //*************************************************************************
284 {
285 using ETL_OR_STD::swap; // Allow ADL
286
287 swap(value, other.value);
288 }
289
290 //*************************************************************************
292 //*************************************************************************
294 {
295 lhs.swap(rhs);
296 }
297
298 //*************************************************************************
300 //*************************************************************************
302 {
303 return lhs.value == rhs.value;
304 }
305
306 //*************************************************************************
308 //*************************************************************************
310 {
311 return !(lhs == rhs);
312 }
313
314 private:
315
316 T value;
317 };
318
319 //***************************************************************************
326 //***************************************************************************
327 template <typename T, T FIRST, T LAST>
328 class cyclic_value<T, FIRST, LAST, true>
329 {
330 public:
331
332 //*************************************************************************
336 //*************************************************************************
338 : value(FIRST),
339 first_value(FIRST),
340 last_value(LAST)
341 {
342 }
343
344 //*************************************************************************
349 //*************************************************************************
350 cyclic_value(T first_, T last_)
351 : value(first_),
352 first_value(first_),
353 last_value(last_)
354 {
355 }
356
357 //*************************************************************************
363 //*************************************************************************
364 cyclic_value(T first_, T last_, T initial)
365 : first_value(first_)
366 , last_value(last_)
367 {
368 set(initial);
369 }
370
371 //*************************************************************************
373 //*************************************************************************
375 : value(other.value),
376 first_value(other.first_value),
377 last_value(other.last_value)
378 {
379 }
380
381 //*************************************************************************
386 //*************************************************************************
387 void set(T first_, T last_)
388 {
389 first_value = first_;
390 last_value = last_;
391 value = first_;
392 }
393
394 //*************************************************************************
397 //*************************************************************************
398 void set(T value_)
399 {
400 if (value_ > last_value)
401 {
402 value_ = last_value;
403 }
404 else if (value_ < first_value)
405 {
406 value_ = first_value;
407 }
408
409 value = value_;
410 }
411
412 //*************************************************************************
414 //*************************************************************************
415 void to_first()
416 {
417 value = first_value;
418 }
419
420 //*************************************************************************
422 //*************************************************************************
423 void to_last()
424 {
425 value = last_value;
426 }
427
428 //*************************************************************************
431 //*************************************************************************
432 void advance(int n)
433 {
434 if (n > 0)
435 {
436 for (int i = 0; i < n; ++i)
437 {
438 operator ++();
439 }
440 }
441 else
442 {
443 for (int i = 0; i < -n; ++i)
444 {
445 operator --();
446 }
447 }
448 }
449
450 //*************************************************************************
453 //*************************************************************************
454 operator T()
455 {
456 return value;
457 }
458
459 //*************************************************************************
462 //*************************************************************************
463 operator const T() const
464 {
465 return value;
466 }
467
468 //*************************************************************************
470 //*************************************************************************
471 cyclic_value& operator ++()
472 {
473 if (value >= last_value)
474 {
475 value = first_value;
476 }
477 else
478 {
479 ++value;
480 }
481
482 return *this;
483 }
484
485 //*************************************************************************
487 //*************************************************************************
488 cyclic_value operator ++(int)
489 {
490 cyclic_value temp(*this);
491
492 operator++();
493
494 return temp;
495 }
496
497 //*************************************************************************
499 //*************************************************************************
500 cyclic_value& operator --()
501 {
502 if (value <= first_value)
503 {
504 value = last_value;
505 }
506 else
507 {
508 --value;
509 }
510
511 return *this;
512 }
513
514 //*************************************************************************
516 //*************************************************************************
517 cyclic_value operator --(int)
518 {
519 cyclic_value temp(*this);
520
521 operator--();
522
523 return temp;
524 }
525
526 //*************************************************************************
528 //*************************************************************************
529 cyclic_value& operator =(T t)
530 {
531 set(t);
532 return *this;
533 }
534
535 //*************************************************************************
537 //*************************************************************************
538 cyclic_value& operator =(const cyclic_value& other)
539 {
540 value = other.value;
541 first_value = other.first_value;
542 last_value = other.last_value;
543 return *this;
544 }
545
546 //*************************************************************************
548 //*************************************************************************
549 T get() const
550 {
551 return value;
552 }
553
554 //*************************************************************************
556 //*************************************************************************
557 T first() const
558 {
559 return first_value;
560 }
561
562 //*************************************************************************
564 //*************************************************************************
565 T last() const
566 {
567 return last_value;
568 }
569
570 //*************************************************************************
572 //*************************************************************************
574 {
575 using ETL_OR_STD::swap; // Allow ADL
576
577 swap(first_value, other.first_value);
578 swap(last_value, other.last_value);
579 swap(value, other.value);
580 }
581
582 //*************************************************************************
584 //*************************************************************************
586 {
587 lhs.swap(rhs);
588 }
589
590 //*************************************************************************
592 //*************************************************************************
594 {
595 return (lhs.value == rhs.value) &&
596 (lhs.first_value == rhs.first_value) &&
597 (lhs.last_value == rhs.last_value);
598 }
599
600 //*************************************************************************
602 //*************************************************************************
604 {
605 return !(lhs == rhs);
606 }
607
608 private:
609
610 T value;
611 T first_value;
612 T last_value;
613 };
614}
615
616#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:2502
T first() const
Gets the first value.
Definition: cyclic_value.h:557
void advance(int n)
Definition: cyclic_value.h:143
const T last() const
Gets the last value.
Definition: cyclic_value.h:275
cyclic_value(T first_, T last_)
Definition: cyclic_value.h:350
cyclic_value(T first_, T last_, T initial)
Definition: cyclic_value.h:364
T get() const
Gets the value.
Definition: cyclic_value.h:259
T last() const
Gets the last value.
Definition: cyclic_value.h:565
cyclic_value(T initial)
Definition: cyclic_value.h:82
void set(T value_)
Definition: cyclic_value.h:109
void to_first()
Resets the value to the first in the range.
Definition: cyclic_value.h:415
cyclic_value()
Definition: cyclic_value.h:72
T get() const
Gets the value.
Definition: cyclic_value.h:549
void to_last()
Resets the value to the last in the range.
Definition: cyclic_value.h:423
void swap(cyclic_value< T, FIRST, LAST > &other)
Swaps the values.
Definition: cyclic_value.h:573
cyclic_value(const cyclic_value &other)
Copy constructor.
Definition: cyclic_value.h:374
cyclic_value()
Definition: cyclic_value.h:337
void advance(int n)
Definition: cyclic_value.h:432
const T first() const
Gets the first value.
Definition: cyclic_value.h:267
friend void swap(cyclic_value< T, FIRST, LAST > &lhs, cyclic_value< T, FIRST, LAST > &rhs)
Swaps the values.
Definition: cyclic_value.h:293
cyclic_value(const cyclic_value< T, FIRST, LAST > &other)
Copy constructor.
Definition: cyclic_value.h:90
void set(T value_)
Definition: cyclic_value.h:398
void set(T first_, T last_)
Definition: cyclic_value.h:387
void swap(cyclic_value< T, FIRST, LAST > &other)
Swaps the values.
Definition: cyclic_value.h:283
void to_first()
Resets the value to the first in the range.
Definition: cyclic_value.h:126
void to_last()
Resets the value to the last in the range.
Definition: cyclic_value.h:134
bitset_ext
Definition: absolute.h:38
bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:645
void swap(etl::array< T, SIZE > &lhs, etl::array< T, SIZE > &rhs)
Template deduction guides.
Definition: array.h:621
bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition: array.h:633