2010-01-25 14:01:22 +01:00
|
|
|
#ifndef __TIMER_HH__
|
|
|
|
#define __TIMER_HH__
|
|
|
|
|
|
|
|
class TimeoutHandler;
|
|
|
|
|
|
|
|
class TimerController
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TimerController();
|
|
|
|
|
|
|
|
int arm(TimeoutHandler *which, int ms);
|
|
|
|
|
|
|
|
void disarm(TimeoutHandler *which);
|
|
|
|
|
|
|
|
void tick();
|
|
|
|
|
2010-08-15 14:11:23 +02:00
|
|
|
/* Singleton */
|
|
|
|
static TimerController *controller;
|
|
|
|
static void init();
|
|
|
|
|
2010-01-25 14:01:22 +01:00
|
|
|
private:
|
|
|
|
int n_handlers;
|
|
|
|
TimeoutHandler **handlers;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TimeoutHandler
|
|
|
|
{
|
|
|
|
friend class TimerController;
|
|
|
|
public:
|
|
|
|
TimeoutHandler()
|
|
|
|
{
|
|
|
|
this->timeout = 0;
|
|
|
|
this->timer_id = -1;
|
|
|
|
}
|
|
|
|
|
2010-01-30 19:43:33 +01:00
|
|
|
~TimeoutHandler();
|
|
|
|
|
2010-01-25 14:01:22 +01:00
|
|
|
void tick()
|
|
|
|
{
|
|
|
|
this->timeout--;
|
|
|
|
if (this->timeout == 0)
|
|
|
|
this->timeoutCallback();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void timeoutCallback() = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int timeout;
|
|
|
|
int timer_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* __TIMER_HH__ */
|