diff options
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/StringUtil.hh | 8 | ||||
-rw-r--r-- | src/FbTk/Timer.cc | 38 | ||||
-rw-r--r-- | src/FbTk/Timer.hh | 16 |
3 files changed, 58 insertions, 4 deletions
diff --git a/src/FbTk/StringUtil.hh b/src/FbTk/StringUtil.hh index bca48bc..416db0d 100644 --- a/src/FbTk/StringUtil.hh +++ b/src/FbTk/StringUtil.hh | |||
@@ -26,6 +26,8 @@ | |||
26 | 26 | ||
27 | #include <string> | 27 | #include <string> |
28 | 28 | ||
29 | #include "stringstream.hh" | ||
30 | |||
29 | namespace FbTk { | 31 | namespace FbTk { |
30 | 32 | ||
31 | namespace StringUtil { | 33 | namespace StringUtil { |
@@ -67,6 +69,12 @@ std::string::size_type removeTrailingWhitespace(std::string &str); | |||
67 | /// splits input at first non-leading whitespace and returns both parts | 69 | /// splits input at first non-leading whitespace and returns both parts |
68 | void getFirstWord(const std::string &in, std::string &first, std::string &rest); | 70 | void getFirstWord(const std::string &in, std::string &first, std::string &rest); |
69 | 71 | ||
72 | template <typename T> | ||
73 | void fromString(const char *in, T &out) { | ||
74 | FbTk_istringstream iss(in); | ||
75 | iss >> out; | ||
76 | } | ||
77 | |||
70 | template <typename Container> | 78 | template <typename Container> |
71 | static void stringTokensBetween(Container &container, const std::string &in, | 79 | static void stringTokensBetween(Container &container, const std::string &in, |
72 | std::string &rest, char first, char last, | 80 | std::string &rest, char first, char last, |
diff --git a/src/FbTk/Timer.cc b/src/FbTk/Timer.cc index 9492ac6..ff568dc 100644 --- a/src/FbTk/Timer.cc +++ b/src/FbTk/Timer.cc | |||
@@ -24,7 +24,8 @@ | |||
24 | 24 | ||
25 | #include "Timer.hh" | 25 | #include "Timer.hh" |
26 | 26 | ||
27 | #include "Command.hh" | 27 | #include "ObjectRegistry.hh" |
28 | #include "StringUtil.hh" | ||
28 | 29 | ||
29 | //use GNU extensions | 30 | //use GNU extensions |
30 | #ifndef _GNU_SOURCE | 31 | #ifndef _GNU_SOURCE |
@@ -246,6 +247,41 @@ void Timer::addTimer(Timer *timer) { | |||
246 | 247 | ||
247 | } | 248 | } |
248 | 249 | ||
250 | Command *DelayedCmd::parse(const std::string &command, | ||
251 | const std::string &args, bool trusted) { | ||
252 | |||
253 | std::string cmd_str; | ||
254 | int err = StringUtil::getStringBetween(cmd_str, args.c_str(), '{', '}'); | ||
255 | if (err == 0) | ||
256 | return 0; | ||
257 | |||
258 | RefCount<Command> cmd(ObjectRegistry<Command>::instance().parse(cmd_str, trusted)); | ||
259 | if (*cmd == 0) | ||
260 | return 0; | ||
261 | |||
262 | int delay = 200000; | ||
263 | StringUtil::fromString<int>(args.c_str() + err, delay); | ||
264 | |||
265 | return new DelayedCmd(cmd, delay); | ||
266 | } | ||
267 | |||
268 | REGISTER_OBJECT_PARSER(delay, DelayedCmd::parse, Command); | ||
269 | |||
270 | DelayedCmd::DelayedCmd(RefCount<Command> &cmd, unsigned int timeout) { | ||
271 | timeval to; // defaults to 200ms | ||
272 | to.tv_sec = timeout/1000000; | ||
273 | to.tv_usec = timeout % 1000000; | ||
274 | m_timer.setTimeout(to); | ||
275 | m_timer.setCommand(cmd); | ||
276 | m_timer.fireOnce(true); | ||
277 | } | ||
278 | |||
279 | void DelayedCmd::execute() { | ||
280 | if (m_timer.isTiming()) | ||
281 | m_timer.stop(); | ||
282 | m_timer.start(); | ||
283 | } | ||
284 | |||
249 | void Timer::removeTimer(Timer *timer) { | 285 | void Timer::removeTimer(Timer *timer) { |
250 | assert(timer); | 286 | assert(timer); |
251 | m_timerlist.remove(timer); | 287 | m_timerlist.remove(timer); |
diff --git a/src/FbTk/Timer.hh b/src/FbTk/Timer.hh index bd55435..4b5be95 100644 --- a/src/FbTk/Timer.hh +++ b/src/FbTk/Timer.hh | |||
@@ -26,6 +26,7 @@ | |||
26 | #define FBTK_TIMER_HH | 26 | #define FBTK_TIMER_HH |
27 | 27 | ||
28 | #include "RefCount.hh" | 28 | #include "RefCount.hh" |
29 | #include "Command.hh" | ||
29 | 30 | ||
30 | #ifdef HAVE_CTIME | 31 | #ifdef HAVE_CTIME |
31 | #include <ctime> | 32 | #include <ctime> |
@@ -33,6 +34,7 @@ | |||
33 | #include <time.h> | 34 | #include <time.h> |
34 | #endif | 35 | #endif |
35 | #include <list> | 36 | #include <list> |
37 | #include <string> | ||
36 | 38 | ||
37 | #ifdef HAVE_CONFIG_H | 39 | #ifdef HAVE_CONFIG_H |
38 | #include "config.h" | 40 | #include "config.h" |
@@ -49,8 +51,6 @@ | |||
49 | 51 | ||
50 | namespace FbTk { | 52 | namespace FbTk { |
51 | 53 | ||
52 | class Command; | ||
53 | |||
54 | /** | 54 | /** |
55 | Handles Timeout | 55 | Handles Timeout |
56 | */ | 56 | */ |
@@ -107,7 +107,17 @@ private: | |||
107 | timeval m_timeout; ///< time length | 107 | timeval m_timeout; ///< time length |
108 | }; | 108 | }; |
109 | 109 | ||
110 | /// executes a command after a specified timeout | ||
111 | class DelayedCmd: public Command { | ||
112 | public: | ||
113 | DelayedCmd(RefCount<Command> &cmd, unsigned int timeout = 200000); | ||
114 | void execute(); | ||
115 | static Command *parse(const std::string &command, | ||
116 | const std::string &args, bool trusted); | ||
117 | private: | ||
118 | Timer m_timer; | ||
119 | }; | ||
120 | |||
110 | } // end namespace FbTk | 121 | } // end namespace FbTk |
111 | 122 | ||
112 | #endif // FBTK_TIMER_HH | 123 | #endif // FBTK_TIMER_HH |
113 | |||