1// excerpts from http://code.google.com/p/muduo/
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the License file.
5//
6// Author: Shuo Chen (chenshuo at chenshuo dot com)
7
8#ifndef MUDUO_NET_TIMER_H
9#define MUDUO_NET_TIMER_H
10
11#include <boost/noncopyable.hpp>
12
13#include "datetime/Timestamp.h"
14#include "thread/Atomic.h"
15#include "Callbacks.h"
16
17namespace muduo
18{
19
20///
21/// Internal class for timer event.
22///
23class Timer : boost::noncopyable
24{
25 public:
26  Timer(const TimerCallback& cb, Timestamp when, double interval)
27    : callback_(cb),
28      expiration_(when),
29      interval_(interval),
30      repeat_(interval > 0.0),
31      sequence_(s_numCreated_.incrementAndGet())
32  {
33  }
34
35  void run() const
36  {
37    callback_();
38  }
39
40  Timestamp expiration() const  { return expiration_; }
41  bool repeat() const { return repeat_; }
42  int64_t sequence() const { return sequence_; }
43
44  void restart(Timestamp now);
45
46 private:
47  const TimerCallback callback_;
48  Timestamp expiration_;
49  const double interval_;
50  const bool repeat_;
51  const int64_t sequence_;
52
53  static AtomicInt64 s_numCreated_;
54};
55
56}
57#endif  // MUDUO_NET_TIMER_H
58