flatten 20260225
This commit is contained in:
11
tjp/core/timer/Timer.h
Normal file
11
tjp/core/timer/Timer.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
class Timer;
|
||||
|
||||
} // namespace
|
||||
96
tjp/core/timer/Timer.hpp
Normal file
96
tjp/core/timer/Timer.hpp
Normal file
@@ -0,0 +1,96 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Timer.h"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "../containers/Optional.hpp"
|
||||
#include "../assert/debug_assert.h"
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
class Timer
|
||||
{
|
||||
public:
|
||||
typedef double Time;
|
||||
typedef double Duration;
|
||||
|
||||
Duration accumulator = 0;
|
||||
Optional<std::chrono::time_point<std::chrono::high_resolution_clock>> then;
|
||||
|
||||
Timer()
|
||||
{
|
||||
start();
|
||||
}
|
||||
|
||||
Timer(bool start_)
|
||||
{
|
||||
if (start_)
|
||||
start();
|
||||
}
|
||||
|
||||
auto now() const
|
||||
{
|
||||
return std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
debug_assert(!then);
|
||||
|
||||
accumulator = 0;
|
||||
then = now();
|
||||
}
|
||||
|
||||
void restart()
|
||||
{
|
||||
then.reset();
|
||||
start();
|
||||
}
|
||||
|
||||
bool running ()
|
||||
{
|
||||
return then.has_value();
|
||||
}
|
||||
|
||||
Duration elapsed() const
|
||||
{
|
||||
if (!then)
|
||||
return accumulator;
|
||||
|
||||
auto now_ = now();
|
||||
auto duration = std::chrono::duration<double>(now_ - *then).count();
|
||||
|
||||
return duration + accumulator;
|
||||
}
|
||||
|
||||
void pause()
|
||||
{
|
||||
if (!then)
|
||||
return ;
|
||||
|
||||
auto now_ = now();
|
||||
auto partial = std::chrono::duration<double>(now_ - *then).count();
|
||||
then.reset();
|
||||
accumulator += partial;
|
||||
}
|
||||
|
||||
Duration stop()
|
||||
{
|
||||
pause();
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
void resume()
|
||||
{
|
||||
debug_assert(!then);
|
||||
|
||||
then = now();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
30
tjp/core/timer/_tests/Timer.cpp
Executable file
30
tjp/core/timer/_tests/Timer.cpp
Executable file
@@ -0,0 +1,30 @@
|
||||
// License: Modified MIT (NON-AI)
|
||||
// See the LICENSE file in the root directory for license information.
|
||||
// Copyright 2025 Timothy Prepscius
|
||||
|
||||
#include <tjp/core/timer/Timer.hpp>
|
||||
#include <tjp/core/testing/catch.hpp>
|
||||
#include <tjp/core/algorithm/unused.hpp>
|
||||
|
||||
#include <tjp/core/containers/Vector.hpp>
|
||||
|
||||
namespace tjp::core {
|
||||
namespace {
|
||||
|
||||
SCENARIO("core::timer::Timer")
|
||||
{
|
||||
GIVEN("a timer")
|
||||
{
|
||||
Timer timer;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
auto elapsed = timer.elapsed();
|
||||
|
||||
REQUIRE(elapsed != 0.1);
|
||||
REQUIRE(elapsed < 0.15);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user