ROSE  0.11.96.11
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 
105  std::string toString() const;
106 
108  static std::string toString(double seconds);
109 };
110 
111 
112 SAWYER_EXPORT std::ostream& operator<<(std::ostream&, const Stopwatch&);
113 
114 } // namespace
115 #endif
Sawyer::Stopwatch::Stopwatch
Stopwatch(bool start=true)
Construct and optionally start a timer.
Definition: Stopwatch.h:62
Sawyer::clear
void clear(SharedPointer< T > &ptr)
Make pointer point to nothing.
Definition: SharedPointer.h:325
Sawyer::Stopwatch::isRunning
bool isRunning() const
Query state of stopwatch.
Definition: Stopwatch.h:102
Sawyer::Stopwatch
Simple elapsed time.
Definition: Stopwatch.h:41
Sawyer
Name space for the entire library.
Definition: Access.h:13