-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Created by Alexandre Tolstenko Nogueira on 2024.06.12. | ||
// | ||
|
||
#include "TimeDefs.h" | ||
#include "Time.h" | ||
#include "Timer.h" | ||
#include <stdexcept> | ||
|
||
namespace MoBaGEn::TimeManager { | ||
Timer::Timer(bool startOnCreation) { | ||
if (startOnCreation) { | ||
Start(); | ||
} | ||
} | ||
|
||
void Timer::Start() { | ||
if (!m_running) { | ||
m_startTime = Time::Now(); | ||
m_running = true; | ||
} else { | ||
throw std::runtime_error("Timer already running"); | ||
} | ||
} | ||
|
||
Duration Timer::Stop() { | ||
if (m_running) { | ||
m_elapsedTime += Time::Now() - m_startTime; | ||
m_running = false; | ||
} | ||
return m_elapsedTime; | ||
} | ||
|
||
void Timer::Reset() { | ||
m_elapsedTime = Duration(0); | ||
Start(); | ||
} | ||
|
||
} // namespace MoBaGEn::TimeManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// Created by Alexandre Tolstenko Nogueira on 2024.06.12. | ||
// | ||
|
||
#ifndef MOBAGEN_TIMER_H | ||
#define MOBAGEN_TIMER_H | ||
|
||
#include "TimeDefs.h" | ||
#include "Time.h" | ||
|
||
namespace MoBaGEn::TimeManager { | ||
/** | ||
* @brief Timer class | ||
* @details A class to measure wall clock time | ||
*/ | ||
class Timer { | ||
private: | ||
TimePoint m_startTime = Time::Now(); | ||
Duration m_elapsedTime = Duration(0); | ||
bool m_running = false; | ||
|
||
public: | ||
/** | ||
* @brief Construct a new Timer object, by default it starts the timer | ||
* @param startOnCreation Start the timer on creation | ||
*/ | ||
explicit Timer(bool startOnCreation = true); | ||
~Timer() = default; | ||
|
||
// delete all implicit copy and move constructors | ||
Timer(const Timer&) = delete; | ||
Timer(Timer&&) = delete; | ||
Timer& operator=(const Timer&) = delete; | ||
Timer& operator=(Timer&&) = delete; | ||
|
||
/** | ||
* @brief Start or resume the timer | ||
* @details It will update the start time and mark the timer as running | ||
*/ | ||
void Start(); | ||
/** | ||
* @brief Stop or pause the timer | ||
* @return Duration The elapsed time since the timer started | ||
* @details It will update the elapsed time and mark the timer as not running, and return | ||
*/ | ||
Duration Stop(); | ||
|
||
/** | ||
* @brief Reset the timer and restart it | ||
* @details It will reset the elapsed time to 0 and start the timer | ||
*/ | ||
void Reset(); | ||
}; | ||
} // namespace MoBaGEn::TimeManager | ||
|
||
#endif // MOBAGEN_TIMER_H |