Upgrade FbTk::Timer so it can take an arbitrary functor as a parameter

This commit is contained in:
Pavel Labath 2011-08-02 12:08:58 +02:00
parent ae252c62cb
commit e67b9a6f83
2 changed files with 20 additions and 2 deletions

View file

@ -285,11 +285,15 @@ Command<void> *DelayedCmd::parse(const std::string &command,
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);
}

View file

@ -66,6 +66,9 @@ public:
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 @@ private:
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;
};