Skip to content

Commit

Permalink
fix(timer): add timer class
Browse files Browse the repository at this point in the history
  • Loading branch information
tolstenko committed Jun 12, 2024
1 parent f0fe596 commit bb62d7f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/maze/generators/RecursiveBacktrackerExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void RecursiveBacktrackerExample::Clear(World* world) {
}
}
}

Point2D RecursiveBacktrackerExample::randomStartPoint(World* world) {
auto sideOver2 = world->GetSize() / 2;

Expand Down
6 changes: 5 additions & 1 deletion modules/time/Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions modules/time/Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 39 additions & 0 deletions modules/time/Timer.cpp
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
56 changes: 56 additions & 0 deletions modules/time/Timer.h
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

0 comments on commit bb62d7f

Please sign in to comment.