SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_TIMER_H_
3 #define SYDEVS_TIMER_H_
4 
5 #include <sydevs/core/quantity.h>
6 #include <chrono>
7 
8 namespace sydevs {
9 
10 using clock = std::chrono::steady_clock;
11 using clock_time = std::chrono::time_point<clock>;
12 
13 
24 class timer
25 {
26 public:
30  timer();
31 
35  explicit timer(duration cumulative_dt);
36 
37  timer(const timer&) = delete;
38  timer& operator=(const timer&) = delete;
39  timer(timer&&) = default;
40  timer& operator=(timer&&) = default;
41  ~timer() = default;
42 
43  bool timing() const;
44 
46 
47  void start();
48  duration stop();
49 
50 private:
51  clock_time start_clk_t_;
52  duration cumulative_dt_;
53 };
54 
55 
56 inline timer::timer()
57  : start_clk_t_()
58  , cumulative_dt_(0)
59 {
60  cumulative_dt_ = cumulative_dt_.fixed_at(micro);
61 }
62 
63 
64 inline timer::timer(duration cumulative_dt)
65  : start_clk_t_()
66  , cumulative_dt_(cumulative_dt)
67 {
68  cumulative_dt_ = cumulative_dt_.fixed_at(micro);
69 }
70 
71 
72 inline bool timer::timing() const
73 {
74  return start_clk_t_ != clock_time();
75 }
76 
77 
79 {
80  return cumulative_dt_;
81 }
82 
83 
84 inline void timer::start()
85 {
86  if (timing()) {
87  throw std::runtime_error("Attempt to start timer that has already been started");
88  }
89  start_clk_t_ = clock::now();
90 }
91 
92 
94 {
95  auto stop_clk_t = clock::now();
96  if (!timing()) {
97  throw std::runtime_error("Attempt to stop timer that has not been started");
98  }
99  auto microsecond_count = std::chrono::duration_cast<std::chrono::microseconds>(stop_clk_t - start_clk_t_).count();
100  auto interval_dt = duration(microsecond_count, micro).fixed_at(micro);
101  cumulative_dt_ += interval_dt;
102  start_clk_t_ = clock_time();
103  return interval_dt;
104 }
105 
106 
107 } // namespace
108 
109 #endif
bool timing() const
Returns true if the timer has been started but not yet stopped.
Definition: timer.h:72
duration cumulative_duration() const
Returns the cumulative duration of measured time.
Definition: timer.h:78
duration stop()
Stops the timer and returns the measured interval of wallclock time.
Definition: timer.h:93
constexpr const quantity fixed_at(scale precision) const
Returns a new quantity value with the length precision changed and fixed.
Definition: quantity.h:610
timer()
Constructs a timer that has not yet accumuated any interval measurements.
Definition: timer.h:56
A class for measuring and accumulating intervals of wallclock time.
Definition: timer.h:24
constexpr scale micro
Definition: scale.h:161
std::chrono::time_point< clock > clock_time
Definition: timer.h:11
timer & operator=(const timer &)=delete
Copy assignment.
void start()
Starts the timer.
Definition: timer.h:84
quantity< seconds > duration
Definition: quantity.h:1006
~timer()=default
Destructor.
std::chrono::steady_clock clock
Definition: timer.h:10