VPTissue Reference Manual
Stopwatch.h
Go to the documentation of this file.
1 #ifndef UTIL_CLOCK_MAN_STOPWATCH_H_INCLUDED
2 #define UTIL_CLOCK_MAN_STOPWATCH_H_INCLUDED
3 /*
4  * Copyright 2011-2016 Universiteit Antwerpen
5  *
6  * Licensed under the EUPL, Version 1.1 or as soon they
7  * will be approved by the European Commission - subsequent
8  * versions of the EUPL (the "Licence");
9  * You may not use this work except in compliance with the
10  * Licence. You may obtain a copy of the Licence at:
11  *
12  * http://ec.europa.eu/idabc/eupl5
13  *
14  * Unless required by applicable law or agreed to in
15  * writing, software distributed under the Licence is
16  * distributed on an "AS IS" basis,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
18  * express or implied.
19  * See the Licence for the specific language governing
20  * permissions and limitations under the Licence.
21  */
27 #include "Utils.h"
28 
29 #include <chrono>
30 #include <string>
31 
32 namespace SimPT_Sim {
33 namespace ClockMan {
34 
38 template<typename T = std::chrono::system_clock>
39 class Stopwatch
40 {
41 public:
42  typedef T TClock;
43 
45  Stopwatch(std::string name = "stopwatch", bool running = false)
46  : m_accumulated(T::duration::zero()), m_name(name), m_running(running)
47  {
48  if (m_running) {
49  m_last_start = T::now();
50  }
51  }
52 
55  {
56  if (!m_running) {
57  m_running = true;
58  m_last_start = T::now();
59  }
60  return *this;
61  }
62 
65  {
66  if (m_running) {
67  m_accumulated += (T::now() - m_last_start);
68  m_running = false;
69  }
70  return *this;
71  }
72 
75  {
76  m_accumulated = T::duration::zero();
77  m_running = false;
78  return *this;
79  }
80 
82  bool IsRunning() const
83  {
84  return (m_running);
85  }
86 
88  std::string GetName() const
89  {
90  return m_name;
91  }
92 
94  typename T::duration Get() const
95  {
96  auto fTemp = m_accumulated;
97  if (m_running) {
98  fTemp += (T::now() - m_last_start);
99  }
100  return fTemp;
101  }
102 
104  std::string ToString() const
105  {
106  using namespace std;
107  using namespace std::chrono;
108 
109  string colon_string;
110  typedef typename TClock::period TPeriod;
111  if (ratio_less_equal<TPeriod, micro>::value) {
112  microseconds d = duration_cast < microseconds > (Get());
113  colon_string = Utils::ToColonString(d);
114  } else if (ratio_less_equal<TPeriod, milli>::value) {
115  milliseconds d = duration_cast < milliseconds > (Get());
116  colon_string = Utils::ToColonString(d);
117  } else {
118  seconds d = duration_cast < seconds > (Get());
119  colon_string = Utils::ToColonString(d);
120  }
121  return colon_string;
122  }
123 
124 private:
125  typename T::duration m_accumulated;
126  typename T::time_point m_last_start;
127  std::string m_name;
128  bool m_running;
129 };
130 
134 template<typename T>
135 std::ostream&
136 operator<<(std::ostream& oss, Stopwatch<T> const& stopwatch) {
137  return (oss << stopwatch.ToString());
138 }
139 
140 } // namespace
141 } // namespace
142 
143 #endif // end of include guard
bool IsRunning() const
Reports whether stopwatch has been started.
Definition: Stopwatch.h:82
STL namespace.
std::string GetName() const
Return name of this stopwatch.
Definition: Stopwatch.h:88
Interface for TimeKeeper::Utils.
static std::string ToColonString(std::chrono::seconds d)
Procude string in hh:mm:ss format.
Definition: Utils.h:42
Namespace for the core simulator.
Stopwatch & Stop()
Stops the stopwatch if it was running.
Definition: Stopwatch.h:64
Stopwatch(std::string name="stopwatch", bool running=false)
Constructor initializes stopwatch.
Definition: Stopwatch.h:45
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
Definition: Stopwatch.h:94
std::string ToString() const
Returns string representation of readout.
Definition: Stopwatch.h:104
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Definition: Stopwatch.h:39
Stopwatch & Reset()
Resets stopwatch i.e. stopwatch is stopped and time accumulator is cleared.
Definition: Stopwatch.h:74
Stopwatch & Start()
Starts stopwatch if it was stopped.
Definition: Stopwatch.h:54