diff options
-rw-r--r-- | src/FbTk/Timer.cc | 16 | ||||
-rw-r--r-- | src/FbTk/Timer.hh | 33 |
2 files changed, 27 insertions, 22 deletions
diff --git a/src/FbTk/Timer.cc b/src/FbTk/Timer.cc index 4305658..1597fa3 100644 --- a/src/FbTk/Timer.cc +++ b/src/FbTk/Timer.cc | |||
@@ -24,6 +24,8 @@ | |||
24 | 24 | ||
25 | #include "Timer.hh" | 25 | #include "Timer.hh" |
26 | 26 | ||
27 | #include "Command.hh" | ||
28 | |||
27 | //use GNU extensions | 29 | //use GNU extensions |
28 | #ifndef _GNU_SOURCE | 30 | #ifndef _GNU_SOURCE |
29 | #define _GNU_SOURCE | 31 | #define _GNU_SOURCE |
@@ -42,8 +44,12 @@ namespace FbTk { | |||
42 | 44 | ||
43 | Timer::TimerList Timer::m_timerlist; | 45 | Timer::TimerList Timer::m_timerlist; |
44 | 46 | ||
45 | Timer::Timer(TimeoutHandler *h): | 47 | Timer::Timer():m_timing(false), m_once(false) { |
46 | m_handler(h), | 48 | |
49 | } | ||
50 | |||
51 | Timer::Timer(RefCount<Command> &handler): | ||
52 | m_handler(handler), | ||
47 | m_timing(false), | 53 | m_timing(false), |
48 | m_once(false) { | 54 | m_once(false) { |
49 | } | 55 | } |
@@ -67,6 +73,9 @@ void Timer::setTimeout(timeval t) { | |||
67 | m_timeout.tv_usec = t.tv_usec; | 73 | m_timeout.tv_usec = t.tv_usec; |
68 | } | 74 | } |
69 | 75 | ||
76 | void Timer::setCommand(RefCount<Command> &cmd) { | ||
77 | m_handler = cmd; | ||
78 | } | ||
70 | 79 | ||
71 | void Timer::start() { | 80 | void Timer::start() { |
72 | gettimeofday(&m_start, 0); | 81 | gettimeofday(&m_start, 0); |
@@ -85,7 +94,8 @@ void Timer::stop() { | |||
85 | 94 | ||
86 | 95 | ||
87 | void Timer::fireTimeout() { | 96 | void Timer::fireTimeout() { |
88 | if (m_handler) m_handler->timeout(); | 97 | if (*m_handler) |
98 | m_handler->execute(); | ||
89 | } | 99 | } |
90 | 100 | ||
91 | void Timer::updateTimers(int fd) { | 101 | void Timer::updateTimers(int fd) { |
diff --git a/src/FbTk/Timer.hh b/src/FbTk/Timer.hh index 6c40155..961d4d7 100644 --- a/src/FbTk/Timer.hh +++ b/src/FbTk/Timer.hh | |||
@@ -25,6 +25,8 @@ | |||
25 | #ifndef FBTK_TIMER_HH | 25 | #ifndef FBTK_TIMER_HH |
26 | #define FBTK_TIMER_HH | 26 | #define FBTK_TIMER_HH |
27 | 27 | ||
28 | #include "RefCount.hh" | ||
29 | |||
28 | #include <ctime> | 30 | #include <ctime> |
29 | #include <list> | 31 | #include <list> |
30 | 32 | ||
@@ -44,36 +46,23 @@ | |||
44 | 46 | ||
45 | namespace FbTk { | 47 | namespace FbTk { |
46 | 48 | ||
47 | /// Handles timeouts | 49 | class Command; |
48 | /** | ||
49 | Inherit this to have a timed object, that calls | ||
50 | timeout function when the time is out | ||
51 | */ | ||
52 | class TimeoutHandler { | ||
53 | public: | ||
54 | /// called when the time is out | ||
55 | virtual void timeout() = 0; | ||
56 | }; | ||
57 | 50 | ||
58 | /** | 51 | /** |
59 | Handles TimeoutHandles | 52 | Handles Timeout |
60 | */ | 53 | */ |
61 | class Timer { | 54 | class Timer { |
62 | public: | 55 | public: |
63 | explicit Timer(TimeoutHandler *handler); | 56 | Timer(); |
57 | explicit Timer(RefCount<Command> &handler); | ||
64 | virtual ~Timer(); | 58 | virtual ~Timer(); |
65 | 59 | ||
66 | inline int isTiming() const { return m_timing; } | ||
67 | inline int doOnce() const { return m_once; } | ||
68 | |||
69 | inline const timeval &getTimeout() const { return m_timeout; } | ||
70 | inline const timeval &getStartTime() const { return m_start; } | ||
71 | |||
72 | inline void fireOnce(bool once) { m_once = once; } | 60 | inline void fireOnce(bool once) { m_once = once; } |
73 | /// set timeout | 61 | /// set timeout |
74 | void setTimeout(long val); | 62 | void setTimeout(long val); |
75 | /// set timeout | 63 | /// set timeout |
76 | void setTimeout(timeval val); | 64 | void setTimeout(timeval val); |
65 | void setCommand(RefCount<Command> &cmd); | ||
77 | /// start timing | 66 | /// start timing |
78 | void start(); | 67 | void start(); |
79 | /// stop timing | 68 | /// stop timing |
@@ -81,6 +70,12 @@ public: | |||
81 | /// update all timers | 70 | /// update all timers |
82 | static void updateTimers(int file_descriptor); | 71 | static void updateTimers(int file_descriptor); |
83 | 72 | ||
73 | inline int isTiming() const { return m_timing; } | ||
74 | inline int doOnce() const { return m_once; } | ||
75 | |||
76 | inline const timeval &getTimeout() const { return m_timeout; } | ||
77 | inline const timeval &getStartTime() const { return m_start; } | ||
78 | |||
84 | protected: | 79 | protected: |
85 | /// force a timeout | 80 | /// force a timeout |
86 | void fireTimeout(); | 81 | void fireTimeout(); |
@@ -94,7 +89,7 @@ private: | |||
94 | typedef std::list<Timer *> TimerList; | 89 | typedef std::list<Timer *> TimerList; |
95 | static TimerList m_timerlist; ///< list of all timers | 90 | static TimerList m_timerlist; ///< list of all timers |
96 | 91 | ||
97 | TimeoutHandler *m_handler; ///< handler | 92 | RefCount<Command> m_handler; ///< what to do on a timeout |
98 | 93 | ||
99 | bool m_timing; ///< clock running? | 94 | bool m_timing; ///< clock running? |
100 | bool m_once; ///< do timeout only once? | 95 | bool m_once; ///< do timeout only once? |