Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "interfaces/clock.hpp" |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 4 | #include "types/duration_types.hpp" |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 5 | |
| 6 | class ClockFake : public interfaces::Clock |
| 7 | { |
| 8 | public: |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 9 | template <class ClockType> |
| 10 | struct InternalClock |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 11 | { |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 12 | using clock = ClockType; |
| 13 | using time_point = typename clock::time_point; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 14 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 15 | Milliseconds timestamp() const noexcept |
| 16 | { |
| 17 | return ClockFake::toTimestamp(now); |
| 18 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 19 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 20 | void advance(Milliseconds delta) noexcept |
| 21 | { |
| 22 | now += delta; |
| 23 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 24 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 25 | void set(Milliseconds timeSinceEpoch) noexcept |
| 26 | { |
| 27 | now = time_point{timeSinceEpoch}; |
| 28 | } |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 29 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 30 | void reset() noexcept |
| 31 | { |
| 32 | now = time_point{Milliseconds{0u}}; |
| 33 | } |
Lukasz Kazmierczak | 7e098e9 | 2021-09-16 15:59:56 +0200 | [diff] [blame] | 34 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 35 | private: |
| 36 | time_point now = clock::now(); |
| 37 | }; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 38 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 39 | template <class TimePoint> |
| 40 | static Milliseconds toTimestamp(TimePoint tp) |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 41 | { |
| 42 | return std::chrono::time_point_cast<Milliseconds>(tp) |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 43 | .time_since_epoch(); |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Krzysztof Grobelny | 51f0fd5 | 2021-12-28 16:32:08 +0100 | [diff] [blame] | 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; |
Krzysztof Grobelny | 8069771 | 2021-03-04 09:49:27 +0000 | [diff] [blame] | 64 | }; |