Timer.h revision 65c497a3
165c497a3SShuo Chen// excerpts from http://code.google.com/p/muduo/ 265c497a3SShuo Chen// 365c497a3SShuo Chen// Use of this source code is governed by a BSD-style license 465c497a3SShuo Chen// that can be found in the License file. 565c497a3SShuo Chen// 665c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 765c497a3SShuo Chen 865c497a3SShuo Chen#ifndef MUDUO_NET_TIMER_H 965c497a3SShuo Chen#define MUDUO_NET_TIMER_H 1065c497a3SShuo Chen 1165c497a3SShuo Chen#include <boost/noncopyable.hpp> 1265c497a3SShuo Chen 1365c497a3SShuo Chen#include "datetime/Timestamp.h" 1465c497a3SShuo Chen#include "Callbacks.h" 1565c497a3SShuo Chen 1665c497a3SShuo Chennamespace muduo 1765c497a3SShuo Chen{ 1865c497a3SShuo Chen 1965c497a3SShuo Chen/// 2065c497a3SShuo Chen/// Internal class for timer event. 2165c497a3SShuo Chen/// 2265c497a3SShuo Chenclass Timer : boost::noncopyable 2365c497a3SShuo Chen{ 2465c497a3SShuo Chen public: 2565c497a3SShuo Chen Timer(const TimerCallback& cb, Timestamp when, double interval) 2665c497a3SShuo Chen : callback_(cb), 2765c497a3SShuo Chen expiration_(when), 2865c497a3SShuo Chen interval_(interval), 2965c497a3SShuo Chen repeat_(interval > 0.0) 3065c497a3SShuo Chen { } 3165c497a3SShuo Chen 3265c497a3SShuo Chen void run() const 3365c497a3SShuo Chen { 3465c497a3SShuo Chen callback_(); 3565c497a3SShuo Chen } 3665c497a3SShuo Chen 3765c497a3SShuo Chen Timestamp expiration() const { return expiration_; } 3865c497a3SShuo Chen bool repeat() const { return repeat_; } 3965c497a3SShuo Chen 4065c497a3SShuo Chen void restart(Timestamp now); 4165c497a3SShuo Chen 4265c497a3SShuo Chen private: 4365c497a3SShuo Chen const TimerCallback callback_; 4465c497a3SShuo Chen Timestamp expiration_; 4565c497a3SShuo Chen const double interval_; 4665c497a3SShuo Chen const bool repeat_; 4765c497a3SShuo Chen}; 4865c497a3SShuo Chen 4965c497a3SShuo Chen} 5065c497a3SShuo Chen#endif // MUDUO_NET_TIMER_H 51