File indexing completed on 2025-09-16 09:04:24
0001
0002
0003
0004 #ifndef QELAPSEDTIMER_H
0005 #define QELAPSEDTIMER_H
0006
0007 #include <QtCore/qcompare.h>
0008 #include <QtCore/qglobal.h>
0009
0010 #include <chrono>
0011
0012 QT_BEGIN_NAMESPACE
0013
0014 class Q_CORE_EXPORT QElapsedTimer
0015 {
0016 public:
0017 enum ClockType {
0018 SystemTime,
0019 MonotonicClock,
0020 TickCounter Q_DECL_ENUMERATOR_DEPRECATED_X(
0021 "Not supported anymore. Use PerformanceCounter instead."),
0022 MachAbsoluteTime,
0023 PerformanceCounter
0024 };
0025
0026
0027 using Duration = std::chrono::nanoseconds;
0028 using TimePoint = std::chrono::time_point<std::chrono::steady_clock, Duration>;
0029
0030 constexpr QElapsedTimer() = default;
0031
0032 static ClockType clockType() noexcept;
0033 static bool isMonotonic() noexcept;
0034
0035 void start() noexcept;
0036 qint64 restart() noexcept;
0037 void invalidate() noexcept;
0038 bool isValid() const noexcept;
0039
0040 Duration durationElapsed() const noexcept;
0041 qint64 nsecsElapsed() const noexcept;
0042 qint64 elapsed() const noexcept;
0043 bool hasExpired(qint64 timeout) const noexcept;
0044
0045 qint64 msecsSinceReference() const noexcept;
0046 Duration durationTo(const QElapsedTimer &other) const noexcept;
0047 qint64 msecsTo(const QElapsedTimer &other) const noexcept;
0048 qint64 secsTo(const QElapsedTimer &other) const noexcept;
0049 friend bool Q_CORE_EXPORT operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept;
0050
0051 private:
0052 friend bool comparesEqual(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
0053 {
0054 return lhs.t1 == rhs.t1 && lhs.t2 == rhs.t2;
0055 }
0056 Q_DECLARE_EQUALITY_COMPARABLE(QElapsedTimer)
0057
0058 friend Qt::strong_ordering compareThreeWay(const QElapsedTimer &lhs,
0059 const QElapsedTimer &rhs) noexcept
0060 {
0061 return Qt::compareThreeWay(lhs.t1, rhs.t1);
0062 }
0063
0064 #if defined(__cpp_lib_three_way_comparison)
0065 friend std::strong_ordering
0066 operator<=>(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
0067 {
0068 return compareThreeWay(lhs, rhs);
0069 }
0070 #else
0071 friend bool operator>(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
0072 {
0073 return is_gt(compareThreeWay(lhs, rhs));
0074 }
0075 friend bool operator<=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
0076 {
0077 return is_lteq(compareThreeWay(lhs, rhs));
0078 }
0079 friend bool operator>=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
0080 {
0081 return is_gteq(compareThreeWay(lhs, rhs));
0082 }
0083 #endif
0084 qint64 t1 = Q_INT64_C(0x8000000000000000);
0085 qint64 t2 = Q_INT64_C(0x8000000000000000);
0086 };
0087
0088 QT_END_NAMESPACE
0089
0090 #endif