ROSE  0.11.2.0
Stopwatch.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_Stopwatch_H
9 #define Sawyer_Stopwatch_H
10 
11 #include <Sawyer/Sawyer.h>
12 
13 #ifdef SAWYER_HAVE_BOOST_CHRONO
14 # include <boost/chrono/duration.hpp>
15 # include <boost/chrono/system_clocks.hpp>
16 #endif
17 
18 namespace Sawyer {
19 
41 class SAWYER_EXPORT Stopwatch {
42 public:
43 #ifdef SAWYER_HAVE_BOOST_CHRONO
44  typedef boost::chrono::high_resolution_clock::time_point TimePoint;
45  typedef boost::chrono::duration<double> Duration;
46 #else
47  typedef double TimePoint;
48  typedef double Duration;
49 #endif
50 
51 private:
52 #include <Sawyer/WarningsOff.h>
53  mutable TimePoint begin_; // time that this stopwatch (re)started
54  mutable Duration elapsed_; // sum of elapsed run time in seconds
55  bool running_; // state of the stopwatch: running or not
56 #include <Sawyer/WarningsRestore.h>
57 
58 public:
62  explicit Stopwatch(bool start=true): elapsed_(0), running_(false) {
63  start && this->start();
64  }
65 
69  double clear(double value=0.0);
70 
77  double start();
78  double start(double value);
84  double restart();
85 
91  double stop(bool clear=false);
92 
97  double report(bool clear=false) const;
98 
102  bool isRunning() const { return running_; }
103 };
104 
105 
106 SAWYER_EXPORT std::ostream& operator<<(std::ostream&, const Stopwatch&);
107 
108 } // namespace
109 #endif
void clear(SharedPointer< T > &ptr)
Make pointer point to nothing.
bool isRunning() const
Query state of stopwatch.
Definition: Stopwatch.h:102
Name space for the entire library.
Simple elapsed time.
Definition: Stopwatch.h:41
Stopwatch(bool start=true)
Construct and optionally start a timer.
Definition: Stopwatch.h:62