Listing 3
class TimeEvent; //forward declaration.
class TimeEventSource : public EventSource <TimeEvent>
{
public:
TimeEventSource() {};
~TimeEventSource() {};
};
//call back declaration with specific signature.
typedef void (*timerCallback)(time_t timer);
class TimeEvent : public Event <TimeEventSource>
{
public:
explicit TimeEvent(unsigned int timer, timerCallback cb = NULL, bool
periodic = false):m_timer(timer), m_callBack(cb)
{ setRestore(periodic);
activateEvent();
}
void eventProcess(); // impl of virtual,
bool eventTrigged();
bool activateEvent();
void deactivateEvent() {};
time_t getTimer() const { return m_timer;}
time_t getExpireTime() const { return m_expireTime;}
void setExpireTime(time_t t) {m_expireTime = t;}
~TimeEvent () {}
private:
unsigned int m_timer;
time_t m_expireTime;
timerCallback m_callBack;
};
TimeEventSource Event<TimeEventSource>::theEventSource;
bool TimeEvent::activateEvent()
{ time_t current_time = time(NULL);
setExpireTime(current_time + getTimer());
setTimeStamp(getExpireTime());//for position in dispatch queue
theEventSource.addEvent(this);//put it on polling queue
return true;
}
void TimeEvent::eventProcess()
{ if (m_callBack != NULL) (*m_callBack)(m_timer);
if (IsRestore())
activateEvent(); //re-activated
else
delete this; //all objects of this class are created with
//"new" operator, so self delete is valid.
}
bool TimeEvent::eventTrigged()
{ time_t current_time = time(NULL);
time_t time_remaind = getExpireTime() - current_time;
if (time_remaind <= 0)
return true;
else
return false;
}