Upgrade FbTk::Timer so it can take an arbitrary functor as a parameter
Pavel Labath pavelo@centrum.sk
2 files changed,
20 insertions(+),
2 deletions(-)
M
src/FbTk/Timer.cc
→
src/FbTk/Timer.cc
@@ -285,11 +285,15 @@
REGISTER_COMMAND_PARSER(delay, DelayedCmd::parse, void); DelayedCmd::DelayedCmd(const RefCount<Slot<void> > &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); }
M
src/FbTk/Timer.hh
→
src/FbTk/Timer.hh
@@ -66,6 +66,9 @@ /// set timeout
void setTimeout(const timeval &val); void setTimeout(unsigned int secs, unsigned int usecs); void setCommand(const RefCount<Slot<void> > &cmd); + template<typename Functor> + void setFunctor(const Functor &functor) + { setCommand(RefCount<Slot<void> >(new SlotImpl<Functor, void>(functor))); } void setInterval(int val) { m_interval = val; } /// start timing void start();@@ -111,10 +114,21 @@ /// executes a command after a specified timeout
class DelayedCmd: public Command<void> { public: DelayedCmd(const RefCount<Slot<void> > &cmd, unsigned int timeout = 200000); + + // this constructor has inverted order of parameters to avoid ambiguity with the previous + // constructor + template<typename Functor> + DelayedCmd(unsigned int timeout, const Functor &functor) { + initTimer(timeout); + m_timer.setFunctor(functor); + } + void execute(); static Command<void> *parse(const std::string &command, const std::string &args, bool trusted); private: + void initTimer(unsigned int timeout); + Timer m_timer; };