1 #pragma once
2 
3 #include "interfaces/clock.hpp"
4 #include "types/duration_types.hpp"
5 
6 class ClockFake : public interfaces::Clock
7 {
8   public:
9     template <class ClockType>
10     struct InternalClock
11     {
12         using clock = ClockType;
13         using time_point = typename clock::time_point;
14 
timestampClockFake::InternalClock15         Milliseconds timestamp() const noexcept
16         {
17             return ClockFake::toTimestamp(now);
18         }
19 
advanceClockFake::InternalClock20         void advance(Milliseconds delta) noexcept
21         {
22             now += delta;
23         }
24 
setClockFake::InternalClock25         void set(Milliseconds timeSinceEpoch) noexcept
26         {
27             now = time_point{timeSinceEpoch};
28         }
29 
resetClockFake::InternalClock30         void reset() noexcept
31         {
32             now = time_point{Milliseconds{0u}};
33         }
34 
35       private:
36         time_point now = clock::now();
37     };
38 
39     template <class TimePoint>
toTimestamp(TimePoint tp)40     static Milliseconds toTimestamp(TimePoint tp)
41     {
42         return std::chrono::time_point_cast<Milliseconds>(tp)
43             .time_since_epoch();
44     }
45 
steadyTimestamp() const46     Milliseconds steadyTimestamp() const noexcept override
47     {
48         return steady.timestamp();
49     }
50 
systemTimestamp() const51     Milliseconds systemTimestamp() const noexcept override
52     {
53         return system.timestamp();
54     }
55 
advance(Milliseconds delta)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;
64 };
65