blob: b65b82f5717ec6fcbd0f2bed6f5e21daa8f56ca2 [file] [log] [blame]
Krzysztof Grobelny80697712021-03-04 09:49:27 +00001#pragma once
2
3#include "interfaces/clock.hpp"
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01004#include "types/duration_types.hpp"
Krzysztof Grobelny80697712021-03-04 09:49:27 +00005
6class ClockFake : public interfaces::Clock
7{
8 public:
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +01009 template <class ClockType>
10 struct InternalClock
Krzysztof Grobelny80697712021-03-04 09:49:27 +000011 {
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010012 using clock = ClockType;
13 using time_point = typename clock::time_point;
Krzysztof Grobelny80697712021-03-04 09:49:27 +000014
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010015 Milliseconds timestamp() const noexcept
16 {
17 return ClockFake::toTimestamp(now);
18 }
Krzysztof Grobelny80697712021-03-04 09:49:27 +000019
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010020 void advance(Milliseconds delta) noexcept
21 {
22 now += delta;
23 }
Krzysztof Grobelny80697712021-03-04 09:49:27 +000024
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010025 void set(Milliseconds timeSinceEpoch) noexcept
26 {
27 now = time_point{timeSinceEpoch};
28 }
Krzysztof Grobelny80697712021-03-04 09:49:27 +000029
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010030 void reset() noexcept
31 {
32 now = time_point{Milliseconds{0u}};
33 }
Lukasz Kazmierczak7e098e92021-09-16 15:59:56 +020034
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010035 private:
36 time_point now = clock::now();
37 };
Krzysztof Grobelny80697712021-03-04 09:49:27 +000038
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010039 template <class TimePoint>
40 static Milliseconds toTimestamp(TimePoint tp)
Krzysztof Grobelny80697712021-03-04 09:49:27 +000041 {
42 return std::chrono::time_point_cast<Milliseconds>(tp)
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010043 .time_since_epoch();
Krzysztof Grobelny80697712021-03-04 09:49:27 +000044 }
45
Krzysztof Grobelny51f0fd52021-12-28 16:32:08 +010046 Milliseconds steadyTimestamp() const noexcept override
47 {
48 return steady.timestamp();
49 }
50
51 Milliseconds systemTimestamp() const noexcept override
52 {
53 return system.timestamp();
54 }
55
56 void advance(Milliseconds delta) noexcept
57 {
58 steady.advance(delta);
59 system.advance(delta);
60 }
61
62 InternalClock<std::chrono::steady_clock> steady;
63 InternalClock<std::chrono::system_clock> system;
Krzysztof Grobelny80697712021-03-04 09:49:27 +000064};