time: move to stl's clocks and move timer
This commit is contained in:
parent
0e521788bc
commit
877fb5b93a
43 changed files with 392 additions and 248 deletions
|
|
@ -1,55 +1,55 @@
|
|||
#include "EventLoopTimer.hpp"
|
||||
#include <limits>
|
||||
#include "EventLoopManager.hpp"
|
||||
#include "../../helpers/time/Time.hpp"
|
||||
|
||||
CEventLoopTimer::CEventLoopTimer(std::optional<std::chrono::steady_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_) :
|
||||
cb(cb_), data(data_) {
|
||||
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())
|
||||
expires.reset();
|
||||
m_expires.reset();
|
||||
else
|
||||
expires = std::chrono::steady_clock::now() + *timeout;
|
||||
m_expires = Time::steadyNow() + *timeout;
|
||||
}
|
||||
|
||||
void CEventLoopTimer::updateTimeout(std::optional<std::chrono::steady_clock::duration> timeout) {
|
||||
void CEventLoopTimer::updateTimeout(std::optional<Time::steady_dur> timeout) {
|
||||
if (!timeout.has_value()) {
|
||||
expires.reset();
|
||||
m_expires.reset();
|
||||
g_pEventLoopManager->nudgeTimers();
|
||||
return;
|
||||
}
|
||||
|
||||
expires = std::chrono::steady_clock::now() + *timeout;
|
||||
m_expires = Time::steadyNow() + *timeout;
|
||||
|
||||
g_pEventLoopManager->nudgeTimers();
|
||||
}
|
||||
|
||||
bool CEventLoopTimer::passed() {
|
||||
if (!expires.has_value())
|
||||
if (!m_expires.has_value())
|
||||
return false;
|
||||
return std::chrono::steady_clock::now() > *expires;
|
||||
return Time::steadyNow() > *m_expires;
|
||||
}
|
||||
|
||||
void CEventLoopTimer::cancel() {
|
||||
wasCancelled = true;
|
||||
expires.reset();
|
||||
m_wasCancelled = true;
|
||||
m_expires.reset();
|
||||
}
|
||||
|
||||
bool CEventLoopTimer::cancelled() {
|
||||
return wasCancelled;
|
||||
return m_wasCancelled;
|
||||
}
|
||||
|
||||
void CEventLoopTimer::call(SP<CEventLoopTimer> self) {
|
||||
expires.reset();
|
||||
cb(self, data);
|
||||
m_expires.reset();
|
||||
m_cb(self, m_data);
|
||||
}
|
||||
|
||||
float CEventLoopTimer::leftUs() {
|
||||
if (!expires.has_value())
|
||||
if (!m_expires.has_value())
|
||||
return std::numeric_limits<float>::max();
|
||||
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(*expires - std::chrono::steady_clock::now()).count();
|
||||
return std::chrono::duration_cast<std::chrono::microseconds>(*m_expires - Time::steadyNow()).count();
|
||||
}
|
||||
|
||||
bool CEventLoopTimer::armed() {
|
||||
return expires.has_value();
|
||||
return m_expires.has_value();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,14 +5,15 @@
|
|||
#include <optional>
|
||||
|
||||
#include "../../helpers/memory/Memory.hpp"
|
||||
#include "../../helpers/time/Time.hpp"
|
||||
|
||||
class CEventLoopTimer {
|
||||
public:
|
||||
CEventLoopTimer(std::optional<std::chrono::steady_clock::duration> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_);
|
||||
CEventLoopTimer(std::optional<Time::steady_dur> timeout, std::function<void(SP<CEventLoopTimer> self, void* data)> cb_, void* data_);
|
||||
|
||||
// if not specified, disarms.
|
||||
// if specified, arms.
|
||||
void updateTimeout(std::optional<std::chrono::steady_clock::duration> timeout);
|
||||
void updateTimeout(std::optional<Time::steady_dur> timeout);
|
||||
|
||||
void cancel();
|
||||
bool passed();
|
||||
|
|
@ -25,8 +26,8 @@ class CEventLoopTimer {
|
|||
void call(SP<CEventLoopTimer> self);
|
||||
|
||||
private:
|
||||
std::function<void(SP<CEventLoopTimer> self, void* data)> cb;
|
||||
void* data = nullptr;
|
||||
std::optional<std::chrono::steady_clock::time_point> expires;
|
||||
bool wasCancelled = false;
|
||||
std::function<void(SP<CEventLoopTimer> self, void* data)> m_cb;
|
||||
void* m_data = nullptr;
|
||||
std::optional<Time::steady_tp> m_expires;
|
||||
bool m_wasCancelled = false;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue