mirror of
https://github.com/hyprwm/hyprlock.git
synced 2024-12-22 05:19:48 +01:00
Timer: add
This commit is contained in:
parent
13124c6a96
commit
12da91b9ca
2 changed files with 48 additions and 0 deletions
25
src/core/Timer.cpp
Normal file
25
src/core/Timer.cpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
#include "Timer.hpp"
|
||||
|
||||
CTimer::CTimer(std::chrono::system_clock::duration timeout, std::function<void(std::shared_ptr<CTimer> self, void* data)> cb_, void* data_) : cb(cb_), data(data_) {
|
||||
expires = std::chrono::system_clock::now() + timeout;
|
||||
}
|
||||
|
||||
bool CTimer::passed() {
|
||||
return std::chrono::system_clock::now() > expires;
|
||||
}
|
||||
|
||||
void CTimer::cancel() {
|
||||
wasCancelled = true;
|
||||
}
|
||||
|
||||
bool CTimer::cancelled() {
|
||||
return wasCancelled;
|
||||
}
|
||||
|
||||
void CTimer::call(std::shared_ptr<CTimer> self) {
|
||||
cb(self, data);
|
||||
}
|
||||
|
||||
float CTimer::leftMs() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(expires - std::chrono::system_clock::now()).count();
|
||||
}
|
23
src/core/Timer.hpp
Normal file
23
src/core/Timer.hpp
Normal file
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
|
||||
class CTimer {
|
||||
public:
|
||||
CTimer(std::chrono::system_clock::duration timeout, std::function<void(std::shared_ptr<CTimer> self, void* data)> cb_, void* data_);
|
||||
|
||||
void cancel();
|
||||
bool passed();
|
||||
|
||||
float leftMs();
|
||||
|
||||
bool cancelled();
|
||||
void call(std::shared_ptr<CTimer> self);
|
||||
|
||||
private:
|
||||
std::function<void(std::shared_ptr<CTimer> self, void* data)> cb;
|
||||
void* data = nullptr;
|
||||
std::chrono::system_clock::time_point expires;
|
||||
bool wasCancelled = false;
|
||||
};
|
Loading…
Reference in a new issue