| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QELAPSEDTIMER_H |
| 5 | #define QELAPSEDTIMER_H |
| 6 | |
| 7 | #include <QtCore/qcompare.h> |
| 8 | #include <QtCore/qglobal.h> |
| 9 | |
| 10 | #include <chrono> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | class Q_CORE_EXPORT QElapsedTimer |
| 15 | { |
| 16 | public: |
| 17 | enum ClockType { |
| 18 | SystemTime, |
| 19 | MonotonicClock, |
| 20 | TickCounter Q_DECL_ENUMERATOR_DEPRECATED_X( |
| 21 | "Not supported anymore. Use PerformanceCounter instead." ), |
| 22 | MachAbsoluteTime, |
| 23 | PerformanceCounter |
| 24 | }; |
| 25 | |
| 26 | // similar to std::chrono::*_clock |
| 27 | using Duration = std::chrono::nanoseconds; |
| 28 | using TimePoint = std::chrono::time_point<std::chrono::steady_clock, Duration>; |
| 29 | |
| 30 | constexpr QElapsedTimer() = default; |
| 31 | |
| 32 | static ClockType clockType() noexcept; |
| 33 | static bool isMonotonic() noexcept; |
| 34 | |
| 35 | void start() noexcept; |
| 36 | qint64 restart() noexcept; |
| 37 | void invalidate() noexcept; |
| 38 | bool isValid() const noexcept; |
| 39 | |
| 40 | Duration durationElapsed() const noexcept; |
| 41 | qint64 nsecsElapsed() const noexcept; |
| 42 | qint64 elapsed() const noexcept; |
| 43 | bool hasExpired(qint64 timeout) const noexcept; |
| 44 | |
| 45 | qint64 msecsSinceReference() const noexcept; |
| 46 | Duration durationTo(const QElapsedTimer &other) const noexcept; |
| 47 | qint64 msecsTo(const QElapsedTimer &other) const noexcept; |
| 48 | qint64 secsTo(const QElapsedTimer &other) const noexcept; |
| 49 | friend bool Q_CORE_EXPORT operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept; |
| 50 | |
| 51 | private: |
| 52 | friend bool comparesEqual(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept |
| 53 | { |
| 54 | return lhs.t1 == rhs.t1 && lhs.t2 == rhs.t2; |
| 55 | } |
| 56 | Q_DECLARE_EQUALITY_COMPARABLE(QElapsedTimer) |
| 57 | |
| 58 | friend Qt::strong_ordering compareThreeWay(const QElapsedTimer &lhs, |
| 59 | const QElapsedTimer &rhs) noexcept |
| 60 | { |
| 61 | return Qt::compareThreeWay(lhs: lhs.t1, rhs: rhs.t1); |
| 62 | } |
| 63 | |
| 64 | #if defined(__cpp_lib_three_way_comparison) |
| 65 | friend std::strong_ordering |
| 66 | operator<=>(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept |
| 67 | { |
| 68 | return compareThreeWay(lhs, rhs); |
| 69 | } |
| 70 | #else |
| 71 | friend bool operator>(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept |
| 72 | { |
| 73 | return is_gt(o: compareThreeWay(lhs, rhs)); |
| 74 | } |
| 75 | friend bool operator<=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept |
| 76 | { |
| 77 | return is_lteq(o: compareThreeWay(lhs, rhs)); |
| 78 | } |
| 79 | friend bool operator>=(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept |
| 80 | { |
| 81 | return is_gteq(o: compareThreeWay(lhs, rhs)); |
| 82 | } |
| 83 | #endif // defined(__cpp_lib_three_way_comparison) |
| 84 | qint64 t1 = Q_INT64_C(0x8000000000000000); |
| 85 | qint64 t2 = Q_INT64_C(0x8000000000000000); |
| 86 | }; |
| 87 | |
| 88 | QT_END_NAMESPACE |
| 89 | |
| 90 | #endif // QELAPSEDTIMER_H |
| 91 | |