blob: 81192d6cc724a64fff1494b73399157612bb1051 (
plain)
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
|
#pragma once
#include <boost/signals2.hpp>
#include <chrono>
using namespace std::chrono_literals;
using clock_type = std::chrono::high_resolution_clock;
class Timer
{
public:
Timer(std::chrono::milliseconds interval, bool cyclic) : m_start_time(clock_type::now()), m_interval(interval), m_running(false), m_cyclic(cyclic)
{}
// connect to this signal
boost::signals2::signal<void()> elapsed;
void start()
{
m_running = true;
m_start_time = clock_type::now();
}
void stop()
{
m_running = false;
}
bool is_elapsed()
{
return m_start_time + m_interval < clock_type::now();
}
void update()
{
if (m_running && is_elapsed()) {
elapsed();
if (m_cyclic) {
start();
} else {
stop();
}
}
}
private:
std::chrono::time_point<clock_type> m_start_time;
std::chrono::milliseconds m_interval;
bool m_running;
bool m_cyclic;
};
|