Austin Schuh | 2dc8c7d | 2021-07-01 17:41:28 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |
| 2 | #define AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |
| 3 | |
| 4 | #include <iostream> |
| 5 | |
| 6 | #include "aos/time/time.h" |
| 7 | |
| 8 | namespace aos::logger { |
| 9 | |
| 10 | // Simple class representing which boot and what monotonic time in that boot. |
| 11 | // Boots are assumed to be sequential, and the monotonic clock resets on reboot |
| 12 | // for all the compare operations. |
| 13 | struct BootTimestamp { |
| 14 | // Boot number for this timestamp. |
| 15 | size_t boot = 0u; |
| 16 | // Monotonic time in that boot. |
| 17 | monotonic_clock::time_point time = monotonic_clock::min_time; |
| 18 | |
| 19 | static constexpr BootTimestamp min_time() { |
| 20 | return BootTimestamp{.boot = 0u, .time = monotonic_clock::min_time}; |
| 21 | } |
| 22 | static constexpr BootTimestamp max_time() { |
| 23 | return BootTimestamp{.boot = std::numeric_limits<size_t>::max(), |
| 24 | .time = monotonic_clock::max_time}; |
| 25 | } |
| 26 | |
| 27 | // Compare operators. These are implemented such that earlier boots always |
| 28 | // compare less than later boots, and the times are only compared in a single |
| 29 | // boot. |
| 30 | bool operator<(const BootTimestamp &m2) const; |
| 31 | bool operator<=(const BootTimestamp &m2) const; |
| 32 | bool operator>=(const BootTimestamp &m2) const; |
| 33 | bool operator>(const BootTimestamp &m2) const; |
| 34 | bool operator==(const BootTimestamp &m2) const; |
| 35 | bool operator!=(const BootTimestamp &m2) const; |
| 36 | }; |
| 37 | |
| 38 | std::ostream &operator<<(std::ostream &os, |
| 39 | const struct BootTimestamp ×tamp); |
| 40 | |
| 41 | inline bool BootTimestamp::operator<(const BootTimestamp &m2) const { |
| 42 | if (boot != m2.boot) { |
| 43 | return boot < m2.boot; |
| 44 | } |
| 45 | |
| 46 | return time < m2.time; |
| 47 | } |
| 48 | inline bool BootTimestamp::operator<=(const BootTimestamp &m2) const { |
| 49 | if (boot != m2.boot) { |
| 50 | return boot <= m2.boot; |
| 51 | } |
| 52 | |
| 53 | return time <= m2.time; |
| 54 | } |
| 55 | inline bool BootTimestamp::operator>=(const BootTimestamp &m2) const { |
| 56 | if (boot != m2.boot) { |
| 57 | return boot >= m2.boot; |
| 58 | } |
| 59 | |
| 60 | return time >= m2.time; |
| 61 | } |
| 62 | inline bool BootTimestamp::operator>(const BootTimestamp &m2) const { |
| 63 | if (boot != m2.boot) { |
| 64 | return boot > m2.boot; |
| 65 | } |
| 66 | |
| 67 | return time > m2.time; |
| 68 | } |
| 69 | |
| 70 | inline bool BootTimestamp::operator==(const BootTimestamp &m2) const { |
| 71 | return boot == m2.boot && time == m2.time; |
| 72 | } |
| 73 | inline bool BootTimestamp::operator!=(const BootTimestamp &m2) const { |
| 74 | return boot != m2.boot || time != m2.time; |
| 75 | } |
| 76 | |
| 77 | } // namespace aos::logger |
| 78 | |
| 79 | #endif // AOS_EVENTS_LOGGING_BOOT_TIMESTAMP_H_ |