-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCounter.h
53 lines (43 loc) · 1.15 KB
/
Counter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef _COUNTER_H_
#define _COUNTER_H_
#include <Arduino.h>
#include <string.h>
template <unsigned int max_digits>
class Counter {
public:
Counter() { reset(); }
void reset() {
memset(buffer, '0', max_digits);
buffer[max_digits] = '\0';
val = 0;
len = 1;
}
inline Counter &operator++() {
inc_string(buffer + max_digits - 1);
++val;
return *this;
}
inline operator unsigned int() const { return val; }
inline const char *c_str() const { return buffer + max_digits - len; }
inline size_t strlen() const { return len; }
protected:
inline void inc_string(char *c) {
// In theory, the line below should be uncommented to avoid writing outside the buffer. In practice however,
// with max_digits set to 10 or more, we can fit all possible unsigned 32-bit integers in the buffer.
// The check is skipped to gain a small extra speed improvement.
// if (c >= buffer) return;
if (*c < '9') {
*c += 1;
}
else {
*c = '0';
inc_string(c - 1);
len = max(max_digits - (c - buffer) + 1, len);
}
}
protected:
char buffer[max_digits + 1];
unsigned int val;
size_t len;
};
#endif