blob: 6d0ccb41d751c3e812e71c6d18d6f6ab8712c59f (
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
55
56
57
|
#include <mbgl/util/timer.hpp>
#include <mbgl/util/run_loop.hpp>
#include <memory>
#include "timer_impl.hpp"
namespace mbgl {
namespace util {
Timer::Impl::Impl() {
#if QT_VERSION >= 0x050000
timer.setTimerType(Qt::PreciseTimer);
#endif
connect(&timer, SIGNAL(timeout()), this, SLOT(timerFired()));
}
void Timer::Impl::start(uint64_t timeout, uint64_t repeat_, std::function<void ()>&& cb) {
repeat = repeat_;
callback = std::move(cb);
timer.setSingleShot(true);
timer.start(timeout);
}
void Timer::Impl::stop() {
timer.stop();
}
void Timer::Impl::timerFired() {
if (repeat) {
timer.setSingleShot(false);
timer.start(repeat);
}
callback();
}
Timer::Timer()
: impl(std::make_unique<Impl>()) {
}
Timer::~Timer() = default;
void Timer::start(Duration timeout, Duration repeat, std::function<void()>&& cb) {
impl->start(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count(),
std::chrono::duration_cast<std::chrono::milliseconds>(repeat).count(),
std::move(cb));
}
void Timer::stop() {
impl->stop();
}
}
}
|