smats  0.0.1
Satisfability Modulo Arithmetic Theories Symbols
Loading...
Searching...
No Matches
timer.h
1
10#pragma once
11
12#include <chrono>
13#include <cstdint>
14#include <ratio>
15#include <type_traits>
16
17namespace smats {
18
19struct user_clock;
20
24template <typename T>
25class TimerBase {
26 public:
27 using clock = T;
28 typedef typename clock::duration duration;
29 typedef typename clock::time_point time_point;
30
32 TimerBase();
33
39 void start();
40
46 void pause();
47
53 void resume();
54
56 [[nodiscard]] bool is_running() const;
58 [[nodiscard]] duration elapsed() const;
60 [[nodiscard]] std::chrono::duration<double>::rep seconds() const;
61
62 TimerBase<T> &operator+=(const TimerBase<T> &other);
63 TimerBase<T> operator+(const TimerBase<T> &other) const;
64
65 protected:
67 [[nodiscard]] time_point now() const { return clock::now(); }
68
69 private:
70 bool running_{false};
71 time_point last_start_{};
72 duration elapsed_{};
73};
74
75// Use high_resolution clock if it's steady, otherwise use steady_clock.
76using chosen_steady_clock = std::conditional<std::chrono::high_resolution_clock::is_steady,
77 std::chrono::high_resolution_clock, std::chrono::steady_clock>::type;
78
79extern template class TimerBase<chosen_steady_clock>;
81
82struct user_clock { // Implements the Clock interface of std::chrono
83 typedef uint64_t rep;
84 typedef std::micro period;
85 typedef std::chrono::duration<rep, period> duration;
86 typedef std::chrono::time_point<user_clock> time_point;
87 const bool is_steady = false; // Not sure how this should be interpreted here
88 static time_point now();
89};
90
91extern template class TimerBase<user_clock>;
93
98 public:
109 TimerGuard(Timer *timer, bool enabled, bool start_timer = true);
110
111 TimerGuard(const TimerGuard &) = delete;
112 TimerGuard(TimerGuard &&) = delete;
113 TimerGuard &operator=(const TimerGuard &) = delete;
114 TimerGuard &operator=(TimerGuard &&) = delete;
115
120 ~TimerGuard();
121
123 void pause();
124
126 void resume();
127
128 private:
129 Timer *const timer_;
130 const bool enabled_{false};
131};
132
133} // namespace smats
Definition timer.h:25
time_point now() const
Definition timer.h:67
bool is_running() const
Definition timer.cpp:48
duration elapsed_
Elapsed time so far. This doesn't include the current fragment if it is running.
Definition timer.h:72
void pause()
Definition timer.cpp:32
bool running_
Whether the timer is running or not.
Definition timer.h:70
time_point last_start_
Last time_point when the timer is started or resumed.
Definition timer.h:71
void start()
Definition timer.cpp:24
std::chrono::duration< double >::rep seconds() const
Definition timer.cpp:59
duration elapsed() const
Definition timer.cpp:53
void resume()
Definition timer.cpp:40
TimerBase()
Definition timer.cpp:21
Definition timer.h:97
Timer *const timer_
The timer to be guarded.
Definition timer.h:129
Definition timer.h:80
Definition timer.h:92
ExpressionKind enum.
Definition timer.h:82