blob: d4633b0d830ee462cb1db29a2bef125bb73734b6 (
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
|
#include "EventLoopTimer.hpp"
#include <limits>
#include "EventLoopManager.hpp"
#include "../../helpers/time/Time.hpp"
CEventLoopTimer::CEventLoopTimer(std::optional<Time::steady_dur> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_) : m_cb(cb_), m_data(data_) {
if (!timeout.has_value())
m_expires.reset();
else
m_expires = Time::steadyNow() + *timeout;
}
void CEventLoopTimer::updateTimeout(std::optional<Time::steady_dur> timeout) {
if (!timeout.has_value()) {
m_expires.reset();
g_pEventLoopManager->nudgeTimers();
return;
}
m_expires = Time::steadyNow() + *timeout;
g_pEventLoopManager->nudgeTimers();
}
bool CEventLoopTimer::passed() {
if (!m_expires.has_value())
return false;
return Time::steadyNow() > *m_expires;
}
void CEventLoopTimer::cancel() {
m_wasCancelled = true;
m_expires.reset();
}
bool CEventLoopTimer::cancelled() {
return m_wasCancelled;
}
void CEventLoopTimer::call(SP<CEventLoopTimer> self) {
m_expires.reset();
m_cb(self, m_data);
}
float CEventLoopTimer::leftUs() {
if (!m_expires.has_value())
return std::numeric_limits<float>::max();
return std::chrono::duration_cast<std::chrono::microseconds>(*m_expires - Time::steadyNow()).count();
}
bool CEventLoopTimer::armed() {
return m_expires.has_value();
}
|