Stride Reference Manual  1.0
Stopwatch.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * any later version.
7  * The software is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License
12  * along with the software. If not, see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2017, Willem L, Kuylen E, Stijven S & Broeckhove J
15  */
16 
22 #include <chrono>
23 #include <string>
24 #include "TimeToString.h"
25 
26 namespace stride {
27 namespace util {
28 
32 template<typename T = std::chrono::system_clock>
33 class Stopwatch {
34 public:
35  typedef T TClock;
36 
38  Stopwatch(std::string name = "stopwatch", bool running = false)
39  : m_accumulated(T::duration::zero()), m_name(name), m_running(running) {
40  if (m_running) {
41  m_last_start = T::now();
42  }
43  }
44 
47  if (!m_running) {
48  m_running = true;
49  m_last_start = T::now();
50  }
51  return *this;
52  }
53 
56  if (m_running) {
57  m_accumulated += (T::now() - m_last_start);
58  m_running = false;
59  }
60  return *this;
61  }
62 
65  m_accumulated = T::duration::zero();
66  m_running = false;
67  return *this;
68  }
69 
71  bool isRunning() const {
72  return (m_running);
73  }
74 
76  std::string getName() const {
77  return m_name;
78  }
79 
81  typename T::duration get() const {
82  auto fTemp = m_accumulated;
83  if (m_running) {
84  fTemp += (T::now() - m_last_start);
85  }
86  return fTemp;
87  }
88 
90  std::string toString() const {
91  using namespace std;
92  using namespace std::chrono;
93 
94  string colon_string;
95  typedef typename TClock::period TPeriod;
96  if (ratio_less_equal<TPeriod, micro>::value) {
97  microseconds d = duration_cast<microseconds>(get());
98  colon_string = TimeToString::toColonString(d);
99  } else if (ratio_less_equal<TPeriod, milli>::value) {
100  milliseconds d = duration_cast<milliseconds>(get());
101  colon_string = TimeToString::toColonString(d);
102  } else {
103  seconds d = duration_cast<seconds>(get());
104  colon_string = TimeToString::toColonString(d);
105  }
106  return colon_string;
107  }
108 
109 private:
110  typename T::duration m_accumulated;
111  typename T::time_point m_last_start;
112  std::string m_name;
113  bool m_running;
114 };
115 
119 template<typename T>
120 std::ostream& operator<<(std::ostream& oss, Stopwatch<T> const& stopwatch) {
121  return (oss << stopwatch.toString());
122 }
123 
124 }
125 }
126 
Stopwatch & reset()
Resets stopwatch i.e. stopwatch is stopped and time accumulator is cleared.
Definition: Stopwatch.h:64
Time Dependent Person DataType.
Definition: NoBehaviour.h:17
bool isRunning() const
Reports whether stopwatch has been started.
Definition: Stopwatch.h:71
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Definition: Stopwatch.h:33
Stopwatch & stop()
Stops the stopwatch if it was running.
Definition: Stopwatch.h:55
Stopwatch(std::string name="stopwatch", bool running=false)
Constructor initializes stopwatch.
Definition: Stopwatch.h:38
Utilities to tag clocks and reformat clock readout to string.
STL namespace.
std::string getName() const
Return name of this stopwatch.
Definition: Stopwatch.h:76
Stopwatch & start()
Starts stopwatch if it was stopped.
Definition: Stopwatch.h:46
T::duration m_accumulated
Definition: Stopwatch.h:110
T::time_point m_last_start
Definition: Stopwatch.h:111
std::string toString() const
Returns string representation of readout.
Definition: Stopwatch.h:90
static std::string toColonString(std::chrono::seconds d)
Produce string in hh:mm:ss format.
Definition: TimeToString.h:36