Skip to content

Commit 88ac7eb

Browse files
committed
Merge branch 'mc/foreach'
* mc/foreach: src:arduino-timer: timer_foreach_task helpers
2 parents 302bcb9 + 0a4c8df commit 88ac7eb

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/arduino-timer.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
#define TIMER_MAX_TASKS 0x10
4646
#endif
4747

48+
#define _timer_foreach_task(T, task) \
49+
for (T task = tasks; task < tasks + max_tasks; ++task)
50+
51+
#define timer_foreach_task(T) \
52+
_timer_foreach_task(struct task *, T)
53+
54+
#define timer_foreach_const_task(T) \
55+
_timer_foreach_task(const struct task *, T)
56+
4857
template <
4958
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
5059
unsigned long (*time_func)() = millis, /* time function for timer */
@@ -84,9 +93,7 @@ class Timer {
8493
{
8594
if (!task) return;
8695

87-
for (size_t i = 0; i < max_tasks; ++i) {
88-
struct task * const t = &tasks[i];
89-
96+
timer_foreach_task(t) {
9097
if (t->handler && (t->id ^ task) == (uintptr_t)t) {
9198
remove(t);
9299
break;
@@ -100,8 +107,7 @@ class Timer {
100107
void
101108
cancel()
102109
{
103-
for (size_t i = 0; i < max_tasks; ++i) {
104-
struct task * const t = &tasks[i];
110+
timer_foreach_task(t) {
105111
remove(t);
106112
}
107113
}
@@ -117,9 +123,7 @@ class Timer {
117123
template <typename R> void
118124
tick()
119125
{
120-
for (size_t i = 0; i < max_tasks; ++i) {
121-
struct task * const task = &tasks[i];
122-
126+
timer_foreach_task(task) {
123127
if (task->handler) {
124128
const unsigned long t = time_func();
125129
const unsigned long duration = t - task->start;
@@ -141,9 +145,7 @@ class Timer {
141145
unsigned long ticks = (unsigned long)-1, elapsed;
142146
const unsigned long start = time_func();
143147

144-
for (size_t i = 0; i < max_tasks; ++i) {
145-
const struct task * const task = &tasks[i];
146-
148+
timer_foreach_const_task(task) {
147149
if (task->handler) {
148150
const unsigned long t = time_func();
149151
const unsigned long duration = t - task->start;
@@ -204,8 +206,7 @@ class Timer {
204206
struct task *
205207
next_task_slot()
206208
{
207-
for (size_t i = 0; i < max_tasks; ++i) {
208-
struct task * const slot = &tasks[i];
209+
timer_foreach_task(slot) {
209210
if (slot->handler == NULL) return slot;
210211
}
211212

@@ -241,4 +242,8 @@ timer_create_default()
241242
return Timer<>();
242243
}
243244

245+
#undef _timer_foreach_task
246+
#undef timer_foreach_task
247+
#undef timer_foreach_const_task
248+
244249
#endif

0 commit comments

Comments
 (0)