Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timer.h
Go to the documentation of this file.
1 
18 #ifndef ION_PORT_TIMER_H_
19 #define ION_PORT_TIMER_H_
20 
21 #include <chrono> // NOLINT
22 #include <type_traits>
23 
24 #include "base/integral_types.h"
25 
26 namespace ion {
27 namespace port {
28 
29 class Timer {
30  public:
31 #if defined(ION_PLATFORM_WINDOWS)
32  struct steady_clock {
35  typedef int64 rep;
36  typedef std::nano period;
37  typedef std::chrono::duration<rep, period> duration;
38  typedef std::chrono::time_point<steady_clock> time_point;
39  static const bool is_steady = true;
40 
41  static time_point now();
42  };
43 
44 #else
45  typedef std::conditional<std::chrono::high_resolution_clock::is_steady,
48  std::chrono::high_resolution_clock,
49  std::chrono::steady_clock>::type steady_clock;
50 #endif
51 
53 
54  Timer() { Reset(); }
55 
57  void Reset();
58 
60  Clock::duration Get() const;
61 
64  double GetInS() const;
65 
68  double GetInMs() const;
69 
71  static void SleepNSeconds(unsigned int seconds);
72 
74  static void SleepNMilliseconds(unsigned int milliseconds);
75 
76  private:
77  steady_clock::time_point start_;
78 };
79 
80 } // namespace port
81 } // namespace ion
82 
83 #endif // ION_PORT_TIMER_H_
double GetInS() const
Returns the elapsed time since construction or the last Reset() in seconds.
Definition: timer.cc:87
std::string type
Definition: printer.cc:353
steady_clock Clock
Definition: timer.h:52
double GetInMs() const
Returns the elapsed time since construction or the last Reset() in milliseconds.
Definition: timer.cc:92
static void SleepNSeconds(unsigned int seconds)
Sleeps for the passed number of seconds.
Definition: timer.cc:71
static void SleepNMilliseconds(unsigned int milliseconds)
Sleeps for n milliseconds.
Definition: timer.cc:74
std::conditional< std::chrono::high_resolution_clock::is_steady, std::chrono::high_resolution_clock, std::chrono::steady_clock >::type steady_clock
Use the high_resolution_clock if it is steady, otherwise use the steady_clock.
Definition: timer.h:49
void Reset()
Resets the timer.
Definition: timer.cc:83
Clock::duration Get() const
Returns the elapsed time since construction or the last Reset().
Definition: timer.cc:85