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