executing a command on timeout instead of calling a TimeoutHandler
fluxgen fluxgen
2 files changed,
27 insertions(+),
22 deletions(-)
M
src/FbTk/Timer.cc
→
src/FbTk/Timer.cc
@@ -24,6 +24,8 @@ // DEALINGS IN THE SOFTWARE.
#include "Timer.hh" +#include "Command.hh" + //use GNU extensions #ifndef _GNU_SOURCE #define _GNU_SOURCE@@ -42,8 +44,12 @@ namespace FbTk {
Timer::TimerList Timer::m_timerlist; -Timer::Timer(TimeoutHandler *h): - m_handler(h), +Timer::Timer():m_timing(false), m_once(false) { + +} + +Timer::Timer(RefCount<Command> &handler): + m_handler(handler), m_timing(false), m_once(false) { }@@ -67,6 +73,9 @@ m_timeout.tv_sec = t.tv_sec;
m_timeout.tv_usec = t.tv_usec; } +void Timer::setCommand(RefCount<Command> &cmd) { + m_handler = cmd; +} void Timer::start() { gettimeofday(&m_start, 0);@@ -85,7 +94,8 @@ }
void Timer::fireTimeout() { - if (m_handler) m_handler->timeout(); + if (*m_handler) + m_handler->execute(); } void Timer::updateTimers(int fd) {
M
src/FbTk/Timer.hh
→
src/FbTk/Timer.hh
@@ -25,6 +25,8 @@
#ifndef FBTK_TIMER_HH #define FBTK_TIMER_HH +#include "RefCount.hh" + #include <ctime> #include <list>@@ -44,36 +46,23 @@ #include <unistd.h>
namespace FbTk { -/// Handles timeouts -/** - Inherit this to have a timed object, that calls - timeout function when the time is out -*/ -class TimeoutHandler { -public: - /// called when the time is out - virtual void timeout() = 0; -}; +class Command; /** - Handles TimeoutHandles + Handles Timeout */ class Timer { public: - explicit Timer(TimeoutHandler *handler); + Timer(); + explicit Timer(RefCount<Command> &handler); virtual ~Timer(); - inline int isTiming() const { return m_timing; } - inline int doOnce() const { return m_once; } - - inline const timeval &getTimeout() const { return m_timeout; } - inline const timeval &getStartTime() const { return m_start; } - inline void fireOnce(bool once) { m_once = once; } /// set timeout void setTimeout(long val); /// set timeout void setTimeout(timeval val); + void setCommand(RefCount<Command> &cmd); /// start timing void start(); /// stop timing@@ -81,6 +70,12 @@ void stop();
/// update all timers static void updateTimers(int file_descriptor); + inline int isTiming() const { return m_timing; } + inline int doOnce() const { return m_once; } + + inline const timeval &getTimeout() const { return m_timeout; } + inline const timeval &getStartTime() const { return m_start; } + protected: /// force a timeout void fireTimeout();@@ -94,7 +89,7 @@
typedef std::list<Timer *> TimerList; static TimerList m_timerlist; ///< list of all timers - TimeoutHandler *m_handler; ///< handler + RefCount<Command> m_handler; ///< what to do on a timeout bool m_timing; ///< clock running? bool m_once; ///< do timeout only once?