Skip to content

Commit 0af91bd

Browse files
committed
src/timer: parameterize handler argument type
1 parent fd02d24 commit 0af91bd

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/timer.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,33 @@
4747

4848
template <
4949
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
50-
unsigned long (*time_func)() = millis /* time function for timer */
50+
unsigned long (*time_func)() = millis, /* time function for timer */
51+
typename T = void * /* handler argument type */
5152
>
5253
class Timer {
5354
public:
5455

5556
typedef uintptr_t Task; /* public task handle */
56-
typedef bool (*handler_t)(void *opaque); /* task handler func signature */
57+
typedef bool (*handler_t)(T opaque); /* task handler func signature */
5758

5859
/* Calls handler with opaque as argument in delay units of time */
5960
Task
60-
in(unsigned long delay, handler_t h, void *opaque = NULL)
61+
in(unsigned long delay, handler_t h, T opaque = T())
6162
{
6263
return task_id(add_task(time_func(), delay, h, opaque));
6364
}
6465

6566
/* Calls handler with opaque as argument at time */
6667
Task
67-
at(unsigned long time, handler_t h, void *opaque = NULL)
68+
at(unsigned long time, handler_t h, T opaque = T())
6869
{
6970
const unsigned long now = time_func();
7071
return task_id(add_task(now, time - now, h, opaque));
7172
}
7273

7374
/* Calls handler with opaque as argument every interval units of time */
7475
Task
75-
every(unsigned long interval, handler_t h, void *opaque = NULL)
76+
every(unsigned long interval, handler_t h, T opaque = T())
7677
{
7778
return task_id(add_task(time_func(), interval, h, opaque, interval));
7879
}
@@ -129,7 +130,7 @@ class Timer {
129130

130131
struct task {
131132
handler_t handler; /* task handler callback func */
132-
void *opaque; /* argument given to the callback handler */
133+
T opaque; /* argument given to the callback handler */
133134
unsigned long start,
134135
expires; /* when the task expires */
135136
size_t repeat, /* repeat task */
@@ -141,7 +142,7 @@ class Timer {
141142
remove(struct task *task)
142143
{
143144
task->handler = NULL;
144-
task->opaque = NULL;
145+
task->opaque = T();
145146
task->start = 0;
146147
task->expires = 0;
147148
task->repeat = 0;
@@ -172,7 +173,7 @@ class Timer {
172173
inline
173174
struct task *
174175
add_task(unsigned long start, unsigned long expires,
175-
handler_t h, void *opaque, bool repeat = 0)
176+
handler_t h, T opaque, bool repeat = 0)
176177
{
177178
struct task * const slot = next_task_slot();
178179

0 commit comments

Comments
 (0)