Devel Lib
Timer.h
1 #pragma once
2 
3 #include "Core/Typedef.h"
4 
5 #include <chrono>
6 
9 namespace Devel {
43  class CTimer {
44  public:
47  explicit CTimer(const bool i_fAutoStart = false) {
48  if (i_fAutoStart) {
49  this->start();
50  } else {
51  this->clear();
52  }
53  }
54 
56  ~CTimer() = default;
57 
58  public:
60  void start();
62  void restart() { return this->start(); }
63 
64  public:
68  [[nodiscard]] bool hasExpired(const uint64 i_nTimeMs) const {
69  return this->isStarted() && (this->elapsed() >= i_nTimeMs);
70  }
71 
74  [[nodiscard]] bool isStarted() const { return this->m_fIsStarted; }
75 
76  public:
79  [[nodiscard]] uint64 elapsed() const;
80 
81  public:
84  void clear() { return this->reset(); }
85 
88  void reset() { this->m_fIsStarted = false; }
89 
90  private:
95  bool m_fIsStarted{};
96 
97 #ifdef _WIN32
103  std::chrono::time_point<std::chrono::steady_clock> m_oStartTime;
104 #else
110  std::chrono::time_point<std::chrono::system_clock> m_oStartTime;
111 #endif
112  };
113 }
This file contains a series of type definitions for the codebase.
unsigned long long uint64
A 64-bit unsigned integer type.
Definition: Typedef.h:11
A high-precision timer class.
Definition: Timer.h:43
void clear()
Resets the timer. The timer is stopped and the elapsed time is set to zero.
Definition: Timer.h:84
void restart()
Restarts the timer.
Definition: Timer.h:62
bool isStarted() const
Checks if the timer is running.
Definition: Timer.h:74
void reset()
Resets the timer. The timer is stopped and the elapsed time is set to zero.
Definition: Timer.h:88
CTimer(const bool i_fAutoStart=false)
Constructor.
Definition: Timer.h:47
uint64 elapsed() const
Gets the elapsed time since the timer was started.
Definition: Timer.cpp:8
void start()
Starts or restarts the timer.
Definition: Timer.cpp:3
bool hasExpired(const uint64 i_nTimeMs) const
Checks if a certain amount of time has passed since the timer was started.
Definition: Timer.h:68
~CTimer()=default
Default destructor.
A namespace for development related classes and functions.