Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
Stopwatch.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This file is part of the gobelijn software.
4  * Gobelijn is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or any later
7  * version. Gobelijn is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for details. You should have received
11  * a copy of the GNU General Public License along with the software. If not,
12  * see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2012, Jan Broeckhove, Dirk De Vos.
15  */
21 #include "timekeeper/Utils.h"
22 #include <chrono>
23 #include <string>
24 
25 namespace UA_CoMP {
26 namespace Timekeeper {
27 
32 template <typename T = std::chrono::system_clock>
33 class Stopwatch
34 {
35 public:
36  typedef T TClock;
37 
39  Stopwatch(std::string name = "stopwatch", bool running = false)
40  : m_accumulated(T::duration::zero()), m_name(name), m_running(running)
41  {
42  if (m_running) {
43  m_last_start = T::now();
44  }
45  }
46 
49  {
50  if (!m_running) {
51  m_running = true;
52  m_last_start = T::now();
53  }
54  return *this;
55  }
56 
59  {
60  if (m_running) {
61  m_accumulated += (T::now() - m_last_start);
62  m_running = false;
63  }
64  return *this;
65  }
66 
70  {
71  m_accumulated = T::duration::zero();
72  m_running = false;
73  return *this;
74  }
75 
77  bool IsRunning() const { return (m_running); }
78 
80  std::string GetName() const { return m_name; }
81 
83  typename T::duration Get() const
84  {
85  auto fTemp = m_accumulated;
86  if (m_running) {
87  fTemp += (T::now() - m_last_start);
88  }
89  return fTemp;
90  }
91 
93  std::string ToString() const
94  {
95  using namespace std;
96  using namespace std::chrono;
97 
98  string colon_string;
99  typedef typename TClock::period TPeriod;
100  if (ratio_less_equal<TPeriod, micro>::value) {
101  microseconds d = duration_cast<microseconds>(Get());
102  colon_string = Utils::ToColonString(d);
103  } else if (ratio_less_equal<TPeriod, milli>::value) {
104  milliseconds d = duration_cast<milliseconds>(Get());
105  colon_string = Utils::ToColonString(d);
106  } else {
107  seconds d = duration_cast<seconds>(Get());
108  colon_string = Utils::ToColonString(d);
109  }
110  return colon_string;
111  }
112 
113 private:
114  typename T::duration m_accumulated;
115  typename T::time_point m_last_start;
116  std::string m_name;
117  bool m_running;
118 };
119 
123 template <typename T>
124 std::ostream& operator<<(std::ostream& oss, Stopwatch<T> const& stopwatch)
125 {
126  return (oss << stopwatch.ToString());
127 }
128 
129 } // namespace Timekeeper
130 } // namespace UA_CoMP
std::string ToString() const
Returns string representation of readout.
Definition: Stopwatch.h:93
Stopwatch(std::string name="stopwatch", bool running=false)
Constructor initializes stopwatch.
Definition: Stopwatch.h:39
Utilities to tag clocks and reformat clock readout to string.
std::string GetName() const
Return name of this stopwatch.
Definition: Stopwatch.h:80
Stopwatch & Reset()
Resets stopwatch i.e.
Definition: Stopwatch.h:69
T::duration Get() const
Returns the accumulated value without altering the stopwatch state.
Definition: Stopwatch.h:83
Stopwatch & Stop()
Stops the stopwatch if it was running.
Definition: Stopwatch.h:58
static std::string ToColonString(std::chrono::seconds d)
Procude string in hh:mm:ss format.
Definition: Utils.h:36
bool IsRunning() const
Reports whether stopwatch has been started.
Definition: Stopwatch.h:77
Stopwatch & Start()
Starts stopwatch if it was stopped.
Definition: Stopwatch.h:48
Provides a stopwatch interface to time: it accumulates time between start/stop pairs.
Definition: Stopwatch.h:33