#pragma once #include #include #include #include #include namespace Void::Time { 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. class Stopwatch { public: using Clock = std::chrono::steady_clock; using TimePoint = Clock::time_point; using Duration = std::chrono::nanoseconds; 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 { Stopwatch sw; sw.start(); return sw; } [[nodiscard]] static Stopwatch started(const MonotonicClock& clock, std::size_t max_laps = kDefaultMaxLaps) noexcept { Stopwatch sw{clock, max_laps}; sw.start(); return sw; } /// 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 try_lap() { if (!running_) { return std::unexpected{StopwatchError::not_running}; } if (laps_.size() >= max_laps_) { return std::unexpected{StopwatchError::lap_limit_reached}; } const auto now = clock_->now(); const auto d = std::chrono::duration_cast(now - last_lap_); laps_.push_back(d); last_lap_ = now; return d; } /// 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 accumulated_; } const auto now = clock_->now(); accumulated_ += std::chrono::duration_cast(now - start_); running_ = false; return accumulated_; } /// Total elapsed time (accumulated + active interval if running). [[nodiscard]] Duration elapsed() const noexcept { if (running_) { return accumulated_ + std::chrono::duration_cast( clock_->now() - start_); } return accumulated_; } /// Elapsed time cast to a specific duration type. template [[nodiscard]] D elapsed_as() const noexcept { return std::chrono::duration_cast(elapsed()); } /// Elapsed in milliseconds as double. [[nodiscard]] double elapsed_ms() const noexcept { return std::chrono::duration(elapsed()) .count(); } /// Split: sample the clock once, return elapsed, and restart at the /// exact same instant. [[nodiscard]] Duration split() noexcept { const auto now = clock_->now(); const Duration total = running_ ? accumulated_ + std::chrono::duration_cast( 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& laps() const noexcept { return laps_; } [[nodiscard]] std::size_t lap_count() const noexcept { return laps_.size(); } /// Stop and clear all recorded state. void reset() noexcept { running_ = false; accumulated_ = Duration::zero(); laps_.clear(); } private: const MonotonicClock* clock_{&SystemMonotonicClock::instance()}; std::size_t max_laps_{kDefaultMaxLaps}; TimePoint start_{}; TimePoint last_lap_{}; Duration accumulated_{}; std::vector laps_; bool running_{false}; }; /// RAII timer that records elapsed into a provided Duration on destruction. class ScopedTimer { public: using Clock = Stopwatch::Clock; using Duration = Stopwatch::Duration; 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(clock_->now() - start_); } ScopedTimer(const ScopedTimer&) = delete; ScopedTimer& operator=(const ScopedTimer&) = delete; private: Duration& out_; const MonotonicClock* clock_; Clock::time_point start_; }; } // namespace Void::Time