📄 timer.h
字号:
#ifndef TIMER_H
#define TIMER_H
#include <list>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost;
using namespace boost::posix_time;
#include "common.h"
class EXPORT TimePointInterface
{
friend bool EXPORT operator < ( const shared_ptr<TimePointInterface>& __timepoint1, const shared_ptr<TimePointInterface>& __timepoint2 );
public:
TimePointInterface( const ptime& __alarm_time, const time_duration& __step_time, const int __repeat );
virtual ~TimePointInterface();
public:
virtual void alarm(void) =0;
bool operator <= ( const ptime& __time );
bool step(void);//return true to be delete
protected:
ptime _alarm_time;
time_duration _step_time;
int _repeat;
};
bool EXPORT operator < ( const shared_ptr<TimePointInterface>& __timepoint1, const shared_ptr<TimePointInterface>& __timepoint2 );
template< class Callback >
class EXPORT TimePoint : public TimePointInterface
{
public:
TimePoint( const ptime& __alarm_time, const time_duration& __step_time, Callback& __callback, const int __repeat )
: _callback(__callback), TimePointInterface( __alarm_time, __step_time, __repeat ) {}
~TimePoint() {}
public:
void alarm(void)
{
_callback.alarm();
}
protected:
Callback& _callback;
};
class EXPORT Timer
{
public:
Timer( float __speed= 1.0 );
~Timer();
public:
void breathe(void);
void setAlarm( shared_ptr<TimePointInterface>& __timepoint );
void getTimeNow( ptime& __time );
void setSpeed( float __speed );
float getSpeed(void) const;
protected:
void updateLastTime(void);
protected:
std::list< shared_ptr<TimePointInterface> > _times;
float _speed;
ptime _last_time_normal;
ptime _last_time_shift;
};
template< class Callback >
void EXPORT setAlarm( Timer& __timer, const ptime& __alarm_time, const time_duration& __step_time, Callback& __callback, const int __repeat= 0 )//循环相对定时
{
__timer.setAlarm( shared_ptr<TimePointInterface>( new TimePoint<Callback>( __alarm_time, __step_time, __callback, __repeat ) ) );
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -