SPH
Timer.h
Go to the documentation of this file.
1 #pragma once
2 
7 
11 #include <chrono>
12 #include <functional>
13 
15 
16 enum class TimerFlags {
18  PERIODIC = 1 << 0,
19 
21  START_EXPIRED = 1 << 1
22 };
23 
25 
27 class Timer {
28 protected:
29  using Clock = std::chrono::system_clock;
30  using TimePoint = std::chrono::time_point<Clock>;
31 
33  int64_t interval;
35 
36 public:
44  Timer(const int64_t interval = 0, const Flags<TimerFlags> flags = EMPTY_FLAGS);
45 
47  void restart();
48 
50  int64_t elapsed(const TimerUnit unit) const;
51 
53  bool isExpired() const {
55  }
56 
57  bool isPeriodic() const {
59  }
60 };
61 
66 SharedPtr<Timer> makeTimer(const int64_t interval,
67  const std::function<void(void)>& callback,
68  const Flags<TimerFlags> flags = EMPTY_FLAGS);
69 
70 
72 class StoppableTimer : public Timer {
73 protected:
75  bool isStopped = false;
76 
77  auto elapsedImpl() const {
78  if (!isStopped) {
79  return Clock::now() - started;
80  } else {
81  return stopped - started;
82  }
83  }
84 
85 public:
87  void stop();
88 
90  void resume();
91 
93  int64_t elapsed(const TimerUnit unit) const;
94 };
95 
99 std::string getFormattedTime(const int64_t time);
100 
Generic dynamically allocated resizable storage.
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
Wrapper over enum allowing setting (and querying) individual bits of the stored value.
const EmptyFlags EMPTY_FLAGS
Definition: Flags.h:16
#define NAMESPACE_SPH_END
Definition: Object.h:12
@ SECOND
Quantity with 1st and 2nd derivative.
SharedPtr< Timer > makeTimer(const int64_t interval, const std::function< void(void)> &callback, const Flags< TimerFlags > flags=EMPTY_FLAGS)
Creates timer with given interval and callback when time interval is finished.
Definition: Timer.cpp:97
TimerFlags
Definition: Timer.h:16
@ START_EXPIRED
Creates expired timer, calling elapsed immediately after creating will return the timer interval.
@ PERIODIC
Timer will execute callback periodically.
std::string getFormattedTime(const int64_t time)
Returns the human-readable formatted time in suitable units.
Definition: Timer.cpp:169
TimerUnit
Definition: Timer.h:24
constexpr INLINE bool has(const TEnum flag) const
Checks if the object has a given flag.
Definition: Flags.h:77
Simple extension of Timer allowing to pause and continue timer.
Definition: Timer.h:72
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
Definition: Timer.cpp:85
auto elapsedImpl() const
Definition: Timer.h:77
TimePoint stopped
Definition: Timer.h:74
void stop()
Stops the timer. Function getElapsed() will report the same value from now on.
Definition: Timer.cpp:70
bool isStopped
Definition: Timer.h:75
void resume()
Resumes stopped timer.
Definition: Timer.cpp:77
Basic time-measuring tool. Starts automatically when constructed.
Definition: Timer.h:27
std::chrono::system_clock Clock
Definition: Timer.h:29
bool isExpired() const
Checks if the interval has already passed.
Definition: Timer.h:53
std::chrono::time_point< Clock > TimePoint
Definition: Timer.h:30
int64_t interval
Definition: Timer.h:33
bool isPeriodic() const
Definition: Timer.h:57
int64_t elapsed(const TimerUnit unit) const
Returns elapsed time in timer units. Does not reset the timer.
Definition: Timer.cpp:55
void restart()
Reset elapsed duration to zero.
Definition: Timer.cpp:51
Timer(const int64_t interval=0, const Flags< TimerFlags > flags=EMPTY_FLAGS)
Creates timer with given expiration duration.
Definition: Timer.cpp:40
Flags< TimerFlags > flags
Definition: Timer.h:34
TimePoint started
Definition: Timer.h:32
@ PERIODIC
Periodic boundary conditions.