NAP
timer.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "datetime.h"
9 
10 namespace nap
11 {
19  template<typename Clock>
20  class Timer
21  {
22  public:
23  // Construction / Destruction
24  Timer() = default;
25  virtual ~Timer() = default;
26 
30  void start();
31 
36  std::chrono::time_point<Clock> getStartTime() const;
37 
45  void stop();
46 
50  void reset();
51 
55  double getElapsedTime() const;
56 
60  float getElapsedTimeFloat() const;
61 
65  uint32_t getTicks() const;
66 
70  NanoSeconds getNanos() const { return get<NanoSeconds>(); }
71 
75  MicroSeconds getMicros() const { return get<MicroSeconds>(); }
76 
80  Milliseconds getMillis() const { return get<Milliseconds>(); }
81 
85  Seconds getSeconds() const { return get<Seconds>(); }
86 
90  Minutes getMinutes() const { return get<Minutes>(); }
91 
95  Hours getHours() const { return get<Hours>(); }
96 
102  template<typename T>
103  T get() const;
104 
105  private:
106  // Members
107  std::chrono::time_point<Clock> mStart;
108  };
109 
110 
117 
118 
125 
126 
133 
134 
136  // Template Definitions
138 
139  template<typename Clock>
141  {
142  mStart = Clock::now();
143  }
144 
145 
146  template<typename Clock>
147  std::chrono::time_point<Clock> Timer<Clock>::getStartTime() const
148  {
149  return mStart;
150  }
151 
152 
153  // Stop timer
154  template<typename Clock>
156  {
157  mStart = std::chrono::time_point<Clock>(Milliseconds(0));
158  }
159 
160 
161  // Reset the timer
162  template<typename Clock>
164  {
165  start();
166  }
167 
168 
169  // Return number of ticks in milliseconds
170  template<typename Clock>
171  uint32_t Timer<Clock>::getTicks() const
172  {
173  return getMillis().count();
174  }
175 
176 
177  template<typename Clock>
178  template<typename T>
180  {
181  return std::chrono::duration_cast<T>(Clock::now() - mStart);
182  }
183 
184 
185  // Return elapsed time in seconds
186  template<typename Clock>
188  {
189  return std::chrono::duration<double>(Clock::now() - mStart).count();
190  }
191 
192 
193  // Elapsed time in seconds
194  template<typename Clock>
196  {
197  return std::chrono::duration<float>(Clock::now() - mStart).count();
198  }
199 }
nap::Timer::stop
void stop()
Definition: timer.h:155
nap::Milliseconds
std::chrono::milliseconds Milliseconds
Milliseconds type definition.
Definition: datetime.h:22
nap::Timer::getTicks
uint32_t getTicks() const
Definition: timer.h:171
nap::Minutes
std::chrono::minutes Minutes
Minutes type definition.
Definition: datetime.h:26
nap::Seconds
std::chrono::seconds Seconds
Seconds type definition.
Definition: datetime.h:25
nap::Timer::getNanos
NanoSeconds getNanos() const
Definition: timer.h:70
nap::Timer::getHours
Hours getHours() const
Definition: timer.h:95
nap::Timer::get
T get() const
Definition: timer.h:179
nap::Timer::Timer
Timer()=default
nap::Timer::getMinutes
Minutes getMinutes() const
Definition: timer.h:90
nap::Timer::getElapsedTime
double getElapsedTime() const
Definition: timer.h:187
nap::NanoSeconds
std::chrono::nanoseconds NanoSeconds
Nanoseconds type definition.
Definition: datetime.h:24
nap::Timer::getMicros
MicroSeconds getMicros() const
Definition: timer.h:75
nap::Timer
Definition: timer.h:20
nap::Timer::getStartTime
std::chrono::time_point< Clock > getStartTime() const
Definition: timer.h:147
nap::Timer::getSeconds
Seconds getSeconds() const
Definition: timer.h:85
nap::MicroSeconds
std::chrono::microseconds MicroSeconds
Microseconds type definition.
Definition: datetime.h:23
nap::Timer::getMillis
Milliseconds getMillis() const
Definition: timer.h:80
nap::Timer::~Timer
virtual ~Timer()=default
nap
Definition: templateapp.h:17
nap::Hours
std::chrono::hours Hours
Hours type definition.
Definition: datetime.h:27
nap::Timer::getElapsedTimeFloat
float getElapsedTimeFloat() const
Definition: timer.h:195
nap::Timer::reset
void reset()
Definition: timer.h:163
nap::Timer::start
void start()
Definition: timer.h:140