Time(时间点)基类,实现了基础运算接口。HivaTime、WallTime、SteadyTime均继承BaseTime,支持BaseTime的所有运算操作。
BaseDuration类的定义请参见Hiva::Duration类型定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | namespace Hiva { /* Base class for Time implementations. */ class BaseTime { public: BaseTime(); BaseTime(const uint32_t secVal, const uint32_t nsecVal); explicit BaseTime(const double doubleSec); virtual ~BaseTime() = default; double ToSec() const; bool FromSec(const double doubleSec); bool FromTimespec(const timespec &timeVal); uint64_t ToNSec() const; bool FromNSec(const uint64_t uint64Nsec); bool IsZero() const; BaseDuration operator-(const BaseTime &rhs) const; BaseTime operator+(const BaseDuration &rhs) const; BaseTime operator-(const BaseDuration &rhs) const; BaseTime &operator+=(const BaseDuration &rhs); BaseTime &operator-=(const BaseDuration &rhs); bool operator==(const BaseTime &rhs) const; bool operator!=(const BaseTime &rhs) const; bool operator<(const BaseTime &rhs) const; bool operator>(const BaseTime &rhs) const; bool operator<=(const BaseTime &rhs) const; bool operator>=(const BaseTime &rhs) const; uint32_t sec; uint32_t nsec; }; std::ostream &operator<<(std::ostream &outStream, const BaseTime &rhs); } |
继承BaseTime,支持BaseTime的所有运算操作。在使能仿真时间的时候为仿真时间,在不使能仿真时间的时候为WallTime(墙上时间)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | namespace Hiva { /* Time may either represent wall clock time or simulation time. */ class HivaTime : public BaseTime { public: HivaTime(); HivaTime(const uint32_t secVal, const uint32_t nsecVal) noexcept; explicit HivaTime(const double doubleSec) noexcept; HivaTime(const BaseTime &timeVal); ~HivaTime() override = default; static bool UseSystemTime(); static bool IsSimTime(); static bool IsSystemTime(); static HivaTime Now(); static void SetNow(const HivaTime &newSimTime); static void Init(); static void Shutdown(); static bool GetState(); static bool IsValid(); static bool WaitForValid(); static bool WaitForValid(const Hiva::WallDuration &timeout); static bool SleepUntil(const HivaTime &endTime); private: static bool stopped; static std::mutex simTimeMutex; static bool initialized; static bool useSimTime; static HivaTime simTime; }; } |
墙上时间,即CLOCK_REALTIME,继承BaseTime,支持BaseTime的所有运算操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 | namespace Hiva { class WallTime : public BaseTime { public: WallTime(); WallTime(const uint32_t secVal, const uint32_t nsecVal); explicit WallTime(const double doubleSec); WallTime(const BaseTime &timeVal); ~WallTime() override = default; static WallTime Now(); static bool SleepUntil(const WallTime &endTime); static bool IsSystemTime(); }; } |
单独递增时间,即CLOCK_MONOTONIC,不受时间跳变影响。继承BaseTime,支持BaseTime的所有运算操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 | namespace Hiva { class SteadyTime : public BaseTime { public: SteadyTime(); SteadyTime(const uint32_t secVal, const uint32_t nsecVal); explicit SteadyTime(const double doubleSec); SteadyTime(const BaseTime &timeVal); ~SteadyTime() override = default; static SteadyTime Now(); static bool SleepUntil(const SteadyTime &endTime); static bool IsSystemTime(); }; } |