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 15 Milliseconds timestamp() const noexcept 16 { 17 return ClockFake::toTimestamp(now); 18 } 19 20 void advance(Milliseconds delta) noexcept 21 { 22 now += delta; 23 } 24 25 void set(Milliseconds timeSinceEpoch) noexcept 26 { 27 now = time_point{timeSinceEpoch}; 28 } 29 30 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> 40 static Milliseconds toTimestamp(TimePoint tp) 41 { 42 return std::chrono::time_point_cast<Milliseconds>(tp) 43 .time_since_epoch(); 44 } 45 46 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; 64 }; 65