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 {
12  // Forward declares
13  template<typename Clock>
14  class Timer;
15 
17  // Timers
19 
26 
27 
34 
35 
42 
43 
45  // Implementation
47 
55  template<typename Clock>
56  class Timer
57  {
58  public:
59  // Construction / Destruction
60  Timer() = default;
61  virtual ~Timer() = default;
62 
66  void start() { mStart = Clock::now(); }
67 
72  void start(std::chrono::time_point<Clock> time) { mStart = time; }
73 
78  std::chrono::time_point<Clock> getStartTime() const { return mStart; }
79 
83  void reset() { start(); }
84 
88  double getElapsedTime() const { return std::chrono::duration<double>(Clock::now() - mStart).count(); }
89 
93  float getElapsedTimeFloat() const { return std::chrono::duration<float>(Clock::now() - mStart).count(); }
94 
98  uint32_t getTicks() const { return getMillis().count(); }
99 
103  NanoSeconds getNanos() const { return get<NanoSeconds>(); }
104 
108  MicroSeconds getMicros() const { return get<MicroSeconds>(); }
109 
113  Milliseconds getMillis() const { return get<Milliseconds>(); }
114 
118  Seconds getSeconds() const { return get<Seconds>(); }
119 
123  Minutes getMinutes() const { return get<Minutes>(); }
124 
128  Hours getHours() const { return get<Hours>(); }
129 
135  template<typename T>
136  T get() const { return std::chrono::duration_cast<T>(Clock::now() - mStart); }
137 
138  private:
139  // Members
140  std::chrono::time_point<Clock> mStart;
141  };
142 }
nap::Milliseconds
std::chrono::milliseconds Milliseconds
Milliseconds type definition.
Definition: datetime.h:22
nap::Timer::getTicks
uint32_t getTicks() const
Definition: timer.h:98
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:103
nap::Timer::start
void start(std::chrono::time_point< Clock > time)
Definition: timer.h:72
nap::Timer::getHours
Hours getHours() const
Definition: timer.h:128
nap::Timer::get
T get() const
Definition: timer.h:136
nap::Timer::Timer
Timer()=default
nap::Timer::getMinutes
Minutes getMinutes() const
Definition: timer.h:123
nap::Timer::getElapsedTime
double getElapsedTime() const
Definition: timer.h:88
nap::NanoSeconds
std::chrono::nanoseconds NanoSeconds
Nanoseconds type definition.
Definition: datetime.h:24
nap::Timer::getMicros
MicroSeconds getMicros() const
Definition: timer.h:108
nap::Timer
Definition: timer.h:14
nap::Timer::getSeconds
Seconds getSeconds() const
Definition: timer.h:118
nap::MicroSeconds
std::chrono::microseconds MicroSeconds
Microseconds type definition.
Definition: datetime.h:23
nap::Timer::getMillis
Milliseconds getMillis() const
Definition: timer.h:113
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:93
nap::Timer::reset
void reset()
Definition: timer.h:83
nap::Timer::getStartTime
std::chrono::time_point< Clock > getStartTime() const
Definition: timer.h:78
nap::Timer::start
void start()
Definition: timer.h:66