feat(Time): add MonotonicClock/CivilClock/ManualClock and bounded Stopwatch (M-TIME-02)

This commit is contained in:
faumaray committed 2026-11-24 02:46:25 +03:00
1 parent 7f3395cb74
commit 4227348051
4 files changed
+471 -45

No files matched your search

+105 -45
View File
@@ -1,28 +1,22 @@
#pragma once
#include <Void/Time/Clock.hpp>
#include <chrono>
#include <cstddef>
#include <expected>
#include <vector>
namespace Void::Time
{
/// Wall-clock stopwatch with lap support, built on steady_clock.
enum class StopwatchError {
not_running,
lap_limit_reached
};
/// Wall-clock or injected-clock stopwatch with bounded lap history.
/// Single-writer; not thread-safe across threads.
///
/// Explicit style:
/// Stopwatch sw;
/// sw.start();
/// // ... work ...
/// sw.lap();
/// sw.stop();
/// sw.elapsed(); // total elapsed
/// sw.laps(); // lap durations
///
/// RAII style (auto-started):
/// auto sw = Stopwatch::started();
/// do_work();
/// auto ms = sw.elapsed_as<std::chrono::milliseconds>();
class Stopwatch
{
public:
@@ -31,9 +25,18 @@ class Stopwatch
using TimePoint = Clock::time_point;
using Duration = std::chrono::nanoseconds;
/// Constructed stopped with zero elapsed time.
static constexpr std::size_t kDefaultMaxLaps = 4096;
/// Constructed stopped with zero elapsed time using system monotonic clock.
Stopwatch() noexcept = default;
/// Constructed stopped using an injected MonotonicClock (must outlive *this).
explicit Stopwatch(const MonotonicClock& clock,
std::size_t max_laps = kDefaultMaxLaps) noexcept
: clock_(&clock), max_laps_(max_laps)
{
}
/// Convenience factory: a stopwatch already running.
[[nodiscard]] static Stopwatch started() noexcept
{
@@ -42,22 +45,49 @@ class Stopwatch
return sw;
}
/// Start (or restart) the timer. Clears recorded laps.
void start() noexcept
[[nodiscard]] static Stopwatch
started(const MonotonicClock& clock,
std::size_t max_laps = kDefaultMaxLaps) noexcept
{
running_ = true;
start_ = Clock::now();
laps_.clear();
last_lap_ = start_;
Stopwatch sw{clock, max_laps};
sw.start();
return sw;
}
/// Record a lap split without stopping. Returns the lap duration.
Duration lap()
/// Start (or restart) from zero, clearing accumulated time and laps.
void start() noexcept
{
const auto now = clock_->now();
running_ = true;
accumulated_ = Duration::zero();
start_ = now;
last_lap_ = now;
laps_.clear();
}
/// Resume accumulating from the previous stopped elapsed time without
/// clearing laps. No-op if already running.
void resume() noexcept
{
if (running_) {
return;
}
const auto now = clock_->now();
running_ = true;
start_ = now;
last_lap_ = now;
}
/// Record a lap split without stopping (checked against max_laps).
[[nodiscard]] std::expected<Duration, StopwatchError> try_lap()
{
if (!running_) {
return Duration::zero();
return std::unexpected{StopwatchError::not_running};
}
const auto now = Clock::now();
if (laps_.size() >= max_laps_) {
return std::unexpected{StopwatchError::lap_limit_reached};
}
const auto now = clock_->now();
const auto d =
std::chrono::duration_cast<Duration>(now - last_lap_);
laps_.push_back(d);
@@ -65,26 +95,34 @@ class Stopwatch
return d;
}
/// Stop the timer. Returns elapsed time since start.
/// Legacy compatibility lap wrapper; returns zero if stopped or at cap.
Duration lap()
{
auto res = try_lap();
return res ? *res : Duration::zero();
}
/// Stop the timer and freeze accumulated elapsed time.
Duration stop() noexcept
{
if (!running_) {
return elapsed_;
return accumulated_;
}
elapsed_ =
std::chrono::duration_cast<Duration>(Clock::now() - start_);
const auto now = clock_->now();
accumulated_ +=
std::chrono::duration_cast<Duration>(now - start_);
running_ = false;
return elapsed_;
return accumulated_;
}
/// Elapsed time (while running: from start to now; stopped: total).
/// Total elapsed time (accumulated + active interval if running).
[[nodiscard]] Duration elapsed() const noexcept
{
if (running_) {
return std::chrono::duration_cast<Duration>(Clock::now() -
start_);
return accumulated_ + std::chrono::duration_cast<Duration>(
clock_->now() - start_);
}
return elapsed_;
return accumulated_;
}
/// Elapsed time cast to a specific duration type.
@@ -101,16 +139,30 @@ class Stopwatch
.count();
}
/// Split: return elapsed and restart in one call.
/// Split: sample the clock once, return elapsed, and restart at the
/// exact same instant.
[[nodiscard]] Duration split() noexcept
{
const auto d = elapsed();
start();
return d;
const auto now = clock_->now();
const Duration total =
running_ ? accumulated_ + std::chrono::duration_cast<Duration>(
now - start_)
: accumulated_;
running_ = true;
accumulated_ = Duration::zero();
start_ = now;
last_lap_ = now;
laps_.clear();
return total;
}
[[nodiscard]] bool is_running() const noexcept { return running_; }
[[nodiscard]] std::size_t max_laps() const noexcept
{
return max_laps_;
}
[[nodiscard]] const std::vector<Duration>& laps() const noexcept
{
return laps_;
@@ -125,15 +177,17 @@ class Stopwatch
void reset() noexcept
{
running_ = false;
elapsed_ = Duration::zero();
accumulated_ = Duration::zero();
laps_.clear();
}
private:
const MonotonicClock* clock_{&SystemMonotonicClock::instance()};
std::size_t max_laps_{kDefaultMaxLaps};
TimePoint start_{};
TimePoint last_lap_{};
Duration elapsed_{};
Duration accumulated_{};
std::vector<Duration> laps_;
bool running_{false};
};
@@ -146,15 +200,20 @@ class ScopedTimer
using Clock = Stopwatch::Clock;
using Duration = Stopwatch::Duration;
explicit ScopedTimer(Duration& out)
: out_{out}, start_{Clock::now()}
explicit ScopedTimer(Duration& out) noexcept
: out_{out}, clock_{&SystemMonotonicClock::instance()},
start_{clock_->now()}
{
}
ScopedTimer(Duration& out, const MonotonicClock& clock) noexcept
: out_{out}, clock_{&clock}, start_{clock_->now()}
{
}
~ScopedTimer()
{
out_ =
std::chrono::duration_cast<Duration>(Clock::now() - start_);
out_ = std::chrono::duration_cast<Duration>(clock_->now() - start_);
}
ScopedTimer(const ScopedTimer&) = delete;
@@ -163,6 +222,7 @@ class ScopedTimer
private:
Duration& out_;
const MonotonicClock* clock_;
Clock::time_point start_;
};