styyx-util 1.0
Utility Header for SKSE Plugin development
Loading...
Searching...
No Matches
st-timer.h
Go to the documentation of this file.
1#pragma once
2
3namespace StyyxUtil
4{
15 struct TimerUtil {
16
17
19 void Start()
20 {
21 if (!running) {
22 startTime = std::chrono::steady_clock::now();
23 running = true;
24 }
25 }
26
27 void Stop()
28 {
29 running = false;
30 }
31
32 void Reset()
33 {
34 startTime = std::chrono::steady_clock::now();
35 }
36
39 [[nodiscard]] double ElapsedSeconds() const
40 {
41 if (!running) {
42 return 0.0;
43 }
44 const auto now = std::chrono::steady_clock::now();
45 return std::chrono::duration<double>(now - startTime).count();
46 }
47
50 [[nodiscard]] bool IsRunning() const {
51 return running;
52 }
53
56 void StartLimited(double a_seconds) {
57 limitSeconds = a_seconds;
58 limited = true;
59 startTime = std::chrono::steady_clock::now();
60 running = true;
61 }
62
65 bool IsExpired()
66 {
67 if (!running || !limited) {
68 return false;
69 }
70
72 Stop();
73 return true;
74 }
75 return false;
76 }
77
80 [[nodiscard]] double RemainingSeconds() const
81 {
82 if (!running || !limited) {
83 return 0.0;
84 }
85
86 return std::max(0.0, limitSeconds - ElapsedSeconds());
87 }
88
89 private:
90 bool limited{ false };
91 double limitSeconds{0.0};
92 std::chrono::steady_clock::time_point startTime{};
93 bool running{false};
94 };
95}
Definition st-actor.h:7
Timer class used to do x after y time.
Definition st-timer.h:15
bool running
Definition st-timer.h:93
double ElapsedSeconds() const
Get the time the timer runs for.
Definition st-timer.h:39
double RemainingSeconds() const
Get the remaining seconds a limited timer runs for.
Definition st-timer.h:80
void StartLimited(double a_seconds)
Start a time for a limited amount of time.
Definition st-timer.h:56
bool limited
Definition st-timer.h:90
double limitSeconds
Definition st-timer.h:91
bool IsRunning() const
Check if the timer is running or not.
Definition st-timer.h:50
std::chrono::steady_clock::time_point startTime
Definition st-timer.h:92
void Stop()
Stop the timer.
Definition st-timer.h:27
void Start()
Start the timer.
Definition st-timer.h:19
void Reset()
Reset timer. Basically set it back to 0.
Definition st-timer.h:32
bool IsExpired()
Check if a limited timer has ran out or not.
Definition st-timer.h:65