Files
moloapp/src/operability/adapters/include/supervisor/common/clock.hpp
T

60 lines
1.8 KiB
C++
Raw Normal View History

// ADAPTER: standalone shim for missing base header "supervisor/common/clock.hpp".
// Production build uses the real base header from the base tree; this file is
// only on the include path for standalone verification (see operability.mk,
// OPERABILITY_STANDALONE=1). Signatures mirror the seam spec.
#pragma once
#include <chrono>
#include <cstdint>
#include <thread>
namespace molo::common {
// Minimal clock abstraction. Production Clock has identical member names.
class Clock {
public:
virtual ~Clock() = default;
virtual std::int64_t wall_ms() const = 0;
virtual std::int64_t steady_ms() const = 0;
virtual void sleep_ms(std::int64_t ms) = 0;
};
// Frozen clock for deterministic tests.
class FrozenClock : public Clock {
public:
explicit FrozenClock(std::int64_t start_ms = 0) : now_(start_ms) {}
std::int64_t wall_ms() const override { return now_; }
std::int64_t steady_ms() const override { return now_; }
void sleep_ms(std::int64_t ms) override {
if (ms > 0) now_ += ms;
}
void advance(std::int64_t ms) {
if (ms > 0) now_ += ms;
}
void set(std::int64_t ms) { now_ = ms; }
private:
std::int64_t now_;
};
// System clock for non-test standalone runs.
class SystemClock : public Clock {
public:
std::int64_t wall_ms() const override {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
}
std::int64_t steady_ms() const override {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
.count();
}
void sleep_ms(std::int64_t ms) override {
if (ms > 0) std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
};
} // namespace molo::common