Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_timer.hpp
1#pragma once
2
3#include "structure/system/time/spk_duration.hpp"
4#include "structure/system/time/spk_timestamp.hpp"
5
6#include <iostream>
7
8namespace spk
9{
23 class Timer
24 {
25 public:
29 enum class State
30 {
31 Idle,
32 Running,
33 Paused,
34 TimedOut
35 };
36
37 private:
38 Duration _currentRunDuration() const;
39 void _updateTimedOutState() const;
40
41 mutable State _state;
42 Duration _expectedDuration;
43 Timestamp _startTime;
44 Duration _accumulatedTime;
45
46 public:
51 explicit Timer(const Duration &p_expectedDuration);
55 Timer();
56
61 State state() const;
66 Duration elapsed() const;
76 float elapsedRatio() const;
81 bool hasTimedOut() const;
82
86 void start();
90 void stop();
94 void pause();
98 void resume();
99 };
100
106 inline const char *toString(Timer::State p_state);
112 inline const wchar_t *toWstring(Timer::State p_state);
113}
114
121std::ostream &operator<<(std::ostream &p_os, const spk::Timer::State &p_state);
128std::wostream &operator<<(std::wostream &p_wos, const spk::Timer::State &p_state);
Strongly-typed time span with cached conversions between ns/ms/s.
Definition spk_duration.hpp:15
bool hasTimedOut() const
Indicates whether the timer exceeded its expected duration.
Definition spk_timer.cpp:48
void resume()
Resumes counting after a pause.
Definition spk_timer.cpp:80
Timer(const Duration &p_expectedDuration)
Builds a timer with the expected duration preconfigured.
Definition spk_timer.cpp:9
Timer()
Builds an idle timer with zero expected duration.
Definition spk_timer.cpp:17
State
Lifecycle state of the countdown.
Definition spk_timer.hpp:30
State state() const
Reports the current timer state.
Definition spk_timer.cpp:22
float elapsedRatio() const
Ratio of elapsed time over expected duration in [0,1] or above when overdue.
Definition spk_timer.cpp:33
Duration expectedDuration() const
Returns the configured timeout duration.
Definition spk_timer.cpp:28
Duration elapsed() const
Measures time accumulated since last start, excluding pauses.
Definition spk_timer.cpp:89
void pause()
Suspends the timer without discarding elapsed time.
Definition spk_timer.cpp:71
void stop()
Stops the timer and clears accumulated time.
Definition spk_timer.cpp:64
void start()
Starts or restarts the timer from zero.
Definition spk_timer.cpp:54
Point in time with conversion helpers and arithmetic with Duration.
Definition spk_timestamp.hpp:16