Timer.h revision 2745a763
12745a763SShuo Chen// excerpts from http://code.google.com/p/muduo/
22745a763SShuo Chen//
32745a763SShuo Chen// Use of this source code is governed by a BSD-style license
42745a763SShuo Chen// that can be found in the License file.
52745a763SShuo Chen//
62745a763SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
72745a763SShuo Chen
82745a763SShuo Chen#ifndef MUDUO_NET_TIMER_H
92745a763SShuo Chen#define MUDUO_NET_TIMER_H
102745a763SShuo Chen
112745a763SShuo Chen#include <boost/noncopyable.hpp>
122745a763SShuo Chen
132745a763SShuo Chen#include "datetime/Timestamp.h"
142745a763SShuo Chen#include "Callbacks.h"
152745a763SShuo Chen
162745a763SShuo Chennamespace muduo
172745a763SShuo Chen{
182745a763SShuo Chen
192745a763SShuo Chen///
202745a763SShuo Chen/// Internal class for timer event.
212745a763SShuo Chen///
222745a763SShuo Chenclass Timer : boost::noncopyable
232745a763SShuo Chen{
242745a763SShuo Chen public:
252745a763SShuo Chen  Timer(const TimerCallback& cb, Timestamp when, double interval)
262745a763SShuo Chen    : callback_(cb),
272745a763SShuo Chen      expiration_(when),
282745a763SShuo Chen      interval_(interval),
292745a763SShuo Chen      repeat_(interval > 0.0)
302745a763SShuo Chen  { }
312745a763SShuo Chen
322745a763SShuo Chen  void run() const
332745a763SShuo Chen  {
342745a763SShuo Chen    callback_();
352745a763SShuo Chen  }
362745a763SShuo Chen
372745a763SShuo Chen  Timestamp expiration() const  { return expiration_; }
382745a763SShuo Chen  bool repeat() const { return repeat_; }
392745a763SShuo Chen
402745a763SShuo Chen  void restart(Timestamp now);
412745a763SShuo Chen
422745a763SShuo Chen private:
432745a763SShuo Chen  const TimerCallback callback_;
442745a763SShuo Chen  Timestamp expiration_;
452745a763SShuo Chen  const double interval_;
462745a763SShuo Chen  const bool repeat_;
472745a763SShuo Chen};
482745a763SShuo Chen
492745a763SShuo Chen}
502745a763SShuo Chen#endif  // MUDUO_NET_TIMER_H
51