From bb62d7fb614e260c1695ef9161d4334d520fb0b7 Mon Sep 17 00:00:00 2001 From: Alexandre Tolstenko Date: Wed, 12 Jun 2024 12:21:47 -0400 Subject: [PATCH] fix(timer): add timer class --- .../RecursiveBacktrackerExample.cpp | 1 + modules/time/Time.cpp | 6 +- modules/time/Time.h | 4 +- modules/time/Timer.cpp | 39 +++++++++++++ modules/time/Timer.h | 56 +++++++++++++++++++ 5 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 modules/time/Timer.cpp create mode 100644 modules/time/Timer.h diff --git a/examples/maze/generators/RecursiveBacktrackerExample.cpp b/examples/maze/generators/RecursiveBacktrackerExample.cpp index 778dae13..b200999f 100644 --- a/examples/maze/generators/RecursiveBacktrackerExample.cpp +++ b/examples/maze/generators/RecursiveBacktrackerExample.cpp @@ -18,6 +18,7 @@ void RecursiveBacktrackerExample::Clear(World* world) { } } } + Point2D RecursiveBacktrackerExample::randomStartPoint(World* world) { auto sideOver2 = world->GetSize() / 2; diff --git a/modules/time/Time.cpp b/modules/time/Time.cpp index dc3a1850..2f884ebf 100644 --- a/modules/time/Time.cpp +++ b/modules/time/Time.cpp @@ -6,7 +6,11 @@ #include "TimeDefs.h" #include "Time.h" namespace MoBaGEn::TimeManager { - Duration Time::GetElapsedTime() { return Now() - m_startTime; } + Duration Time::GetElapsedTime() { return Now() - startTime(); } TimePoint Time::Now() { return std::chrono::high_resolution_clock::now(); } + TimePoint Time::startTime() { + static const auto time = Now(); + return time; + } } // namespace MoBaGEn::TimeManager \ No newline at end of file diff --git a/modules/time/Time.h b/modules/time/Time.h index 215ee9c6..e65b6d77 100644 --- a/modules/time/Time.h +++ b/modules/time/Time.h @@ -12,18 +12,16 @@ namespace MoBaGEn::TimeManager { public: /** * @brief Get the current wall time - * * @return TimePoint */ static TimePoint Now(); private: - static inline TimePoint m_startTime = Now(); + static TimePoint startTime(); public: /** * @brief Get the Elapsed Time since the start of the program - * * @return Duration */ static Duration GetElapsedTime(); diff --git a/modules/time/Timer.cpp b/modules/time/Timer.cpp new file mode 100644 index 00000000..d4d792b4 --- /dev/null +++ b/modules/time/Timer.cpp @@ -0,0 +1,39 @@ +// +// Created by Alexandre Tolstenko Nogueira on 2024.06.12. +// + +#include "TimeDefs.h" +#include "Time.h" +#include "Timer.h" +#include + +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 \ No newline at end of file diff --git a/modules/time/Timer.h b/modules/time/Timer.h new file mode 100644 index 00000000..b39d4a50 --- /dev/null +++ b/modules/time/Timer.h @@ -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