-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeriodicTimer.h
More file actions
62 lines (50 loc) · 1.58 KB
/
PeriodicTimer.h
File metadata and controls
62 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef __PERIODIC_TIMER_H__
#define __PERIODIC_TIMER_H__
#include <functional>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "constants.h"
#include "DestroyGuard.h"
class PeriodicTimer {
using Error = boost::system::error_code;
using Duration = boost::posix_time::time_duration;
public:
PeriodicTimer(const PeriodicTimer&) = delete;
const PeriodicTimer& operator=(const PeriodicTimer&) = delete;
template<class Callback>
PeriodicTimer( const Duration& duration
, boost::asio::io_service& ios
, Callback&& callback)
: _timer(ios)
, _duration(duration)
, _callback(callback)
{
auto destroyed = _destroy_guard.indicator();
// Do first tick as soon as possible.
ios.post([this, destroyed]() {
if (destroyed) return;
on_timeout();
});
}
Duration duration() const { return _duration; }
void set_duration(Duration duration) { _duration = duration; }
private:
void on_timeout() {
using namespace boost::posix_time;
// Carefull, the callback may destroy this object.
auto destroyed = _destroy_guard.indicator();
auto callback_copy = _callback;
callback_copy();
if (destroyed) return;
_timer.expires_from_now(_duration);
_timer.async_wait([this, destroyed](Error) {
if (destroyed) return;
on_timeout();
});
}
private:
DestroyGuard _destroy_guard;
boost::asio::deadline_timer _timer;
Duration _duration;
std::function<void()> _callback;
};
#endif // ifndef __PERIODIC_TIMER_H__