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