Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_Lollipop.h
Go to the documentation of this file.
1
25class Lollipop {
26private:
27 uint16_t val;
28 uint16_t max;
29
30public:
31 Lollipop(uint16_t m) : val(0), max(m) {}
32 Lollipop(uint16_t v, uint16_t m) : val(v), max(m) {}
33 Lollipop(const Lollipop& l) = default;
34
35 // strict comparison: this < other
36 bool operator<(const Lollipop& other) {
37 if (max != other.max) {
38 return false;
39 }
40 if (other.val == 0) {
41 return false;
42 }
43 if (val == 0) {
44 return true;
45 }
46
47 bool reverse = val > max / 2;
48
49 // TODO: what is m and M ?
50 int m = val - (reverse ? max / 2 : 0);
51 int M = val + (reverse ? 0 : max / 2);
52
53 return (m < other.val && other.val <= M) ^ reverse;
54 }
55
61 static uint16_t next(uint16_t currentValue, uint16_t maxValue) {
62 return ++currentValue >= maxValue ? 1 : currentValue;
63 }
64
71 static bool isNewer(uint16_t previousValue, uint16_t currentValue, uint16_t maxValue) {
72 return Lollipop(previousValue, maxValue) < Lollipop(currentValue, maxValue);
73 }
74
75 // pre-fix increment operator
77 val = next(val, max);
78 return *this;
79 }
80
81 // post-fix increment operator
83 Lollipop copy(*this);
84 val = next(val, max);
85 return copy;
86 }
87};
Increment administration object with 'none' value default.
Definition: cs_Lollipop.h:25
Lollipop(const Lollipop &l)=default
static bool isNewer(uint16_t previousValue, uint16_t currentValue, uint16_t maxValue)
Returns true when current value is newer than previous value.
Definition: cs_Lollipop.h:71
bool operator<(const Lollipop &other)
Definition: cs_Lollipop.h:36
Lollipop & operator++()
Definition: cs_Lollipop.h:76
uint16_t max
Definition: cs_Lollipop.h:28
static uint16_t next(uint16_t currentValue, uint16_t maxValue)
Return the next value given the current value.
Definition: cs_Lollipop.h:61
Lollipop(uint16_t m)
Definition: cs_Lollipop.h:31
Lollipop(uint16_t v, uint16_t m)
Definition: cs_Lollipop.h:32
Lollipop operator++(int)
Definition: cs_Lollipop.h:82
uint16_t val
Definition: cs_Lollipop.h:27