From e67b9a6f833fd8af3be105ef411f8743d94e25d9 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 2 Aug 2011 12:08:58 +0200 Subject: Upgrade FbTk::Timer so it can take an arbitrary functor as a parameter --- src/FbTk/Timer.cc | 8 ++++++-- src/FbTk/Timer.hh | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/FbTk/Timer.cc b/src/FbTk/Timer.cc index 13bfc05..9b3fe8c 100644 --- a/src/FbTk/Timer.cc +++ b/src/FbTk/Timer.cc @@ -285,11 +285,15 @@ Command *DelayedCmd::parse(const std::string &command, REGISTER_COMMAND_PARSER(delay, DelayedCmd::parse, void); DelayedCmd::DelayedCmd(const RefCount > &cmd, unsigned int timeout) { - timeval to; // defaults to 200ms + initTimer(timeout); + m_timer.setCommand(cmd); +} + +void DelayedCmd::initTimer(unsigned int timeout) { + timeval to; to.tv_sec = timeout/1000000; to.tv_usec = timeout % 1000000; m_timer.setTimeout(to); - m_timer.setCommand(cmd); m_timer.fireOnce(true); } diff --git a/src/FbTk/Timer.hh b/src/FbTk/Timer.hh index 4b5a10e..376eac0 100644 --- a/src/FbTk/Timer.hh +++ b/src/FbTk/Timer.hh @@ -66,6 +66,9 @@ public: void setTimeout(const timeval &val); void setTimeout(unsigned int secs, unsigned int usecs); void setCommand(const RefCount > &cmd); + template + void setFunctor(const Functor &functor) + { setCommand(RefCount >(new SlotImpl(functor))); } void setInterval(int val) { m_interval = val; } /// start timing void start(); @@ -111,10 +114,21 @@ private: class DelayedCmd: public Command { public: DelayedCmd(const RefCount > &cmd, unsigned int timeout = 200000); + + // this constructor has inverted order of parameters to avoid ambiguity with the previous + // constructor + template + DelayedCmd(unsigned int timeout, const Functor &functor) { + initTimer(timeout); + m_timer.setFunctor(functor); + } + void execute(); static Command *parse(const std::string &command, const std::string &args, bool trusted); private: + void initTimer(unsigned int timeout); + Timer m_timer; }; -- cgit v0.11.2