| 1 | // Copyright (C) 2016 Intel Corporation. |
| 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 QDEADLINETIMER_H |
| 5 | #define QDEADLINETIMER_H |
| 6 | |
| 7 | #include <QtCore/qelapsedtimer.h> |
| 8 | #include <QtCore/qmetatype.h> |
| 9 | #include <QtCore/qnamespace.h> |
| 10 | |
| 11 | #ifdef max |
| 12 | // un-pollute the namespace. We need std::numeric_limits::max() and std::chrono::duration::max() |
| 13 | # undef max |
| 14 | #endif |
| 15 | |
| 16 | #include <limits> |
| 17 | |
| 18 | #include <chrono> |
| 19 | |
| 20 | QT_BEGIN_NAMESPACE |
| 21 | |
| 22 | class Q_CORE_EXPORT QDeadlineTimer |
| 23 | { |
| 24 | public: |
| 25 | enum class ForeverConstant { Forever }; |
| 26 | static constexpr ForeverConstant Forever = ForeverConstant::Forever; |
| 27 | |
| 28 | constexpr QDeadlineTimer() noexcept = default; |
| 29 | constexpr explicit QDeadlineTimer(Qt::TimerType type_) noexcept |
| 30 | : type(type_) {} |
| 31 | constexpr QDeadlineTimer(ForeverConstant, Qt::TimerType type_ = Qt::CoarseTimer) noexcept |
| 32 | : t1((std::numeric_limits<qint64>::max)()), type(type_) {} |
| 33 | explicit QDeadlineTimer(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) noexcept; |
| 34 | |
| 35 | void swap(QDeadlineTimer &other) noexcept |
| 36 | { std::swap(a&: t1, b&: other.t1); std::swap(a&: type, b&: other.type); } |
| 37 | |
| 38 | constexpr bool isForever() const noexcept |
| 39 | { return t1 == (std::numeric_limits<qint64>::max)(); } |
| 40 | bool hasExpired() const noexcept; |
| 41 | |
| 42 | Qt::TimerType timerType() const noexcept |
| 43 | { return Qt::TimerType(type & 0xff); } |
| 44 | void setTimerType(Qt::TimerType type); |
| 45 | |
| 46 | qint64 remainingTime() const noexcept; |
| 47 | qint64 remainingTimeNSecs() const noexcept; |
| 48 | void setRemainingTime(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) noexcept; |
| 49 | void setPreciseRemainingTime(qint64 secs, qint64 nsecs = 0, |
| 50 | Qt::TimerType type = Qt::CoarseTimer) noexcept; |
| 51 | |
| 52 | qint64 deadline() const noexcept Q_DECL_PURE_FUNCTION; |
| 53 | qint64 deadlineNSecs() const noexcept Q_DECL_PURE_FUNCTION; |
| 54 | void setDeadline(qint64 msecs, Qt::TimerType timerType = Qt::CoarseTimer) noexcept; |
| 55 | void setPreciseDeadline(qint64 secs, qint64 nsecs = 0, |
| 56 | Qt::TimerType type = Qt::CoarseTimer) noexcept; |
| 57 | |
| 58 | static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION; |
| 59 | static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer) noexcept; |
| 60 | |
| 61 | friend Q_CORE_EXPORT QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs); |
| 62 | friend QDeadlineTimer operator+(qint64 msecs, QDeadlineTimer dt) |
| 63 | { return dt + msecs; } |
| 64 | friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs) |
| 65 | { return dt + (-msecs); } |
| 66 | friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2) |
| 67 | { return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); } |
| 68 | QDeadlineTimer &operator+=(qint64 msecs) |
| 69 | { *this = *this + msecs; return *this; } |
| 70 | QDeadlineTimer &operator-=(qint64 msecs) |
| 71 | { *this = *this + (-msecs); return *this; } |
| 72 | |
| 73 | template <class Clock, class Duration = typename Clock::duration> |
| 74 | QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_, |
| 75 | Qt::TimerType type_ = Qt::CoarseTimer) : t2(0) |
| 76 | { setDeadline(deadline_, type_); } |
| 77 | template <class Clock, class Duration = typename Clock::duration> |
| 78 | QDeadlineTimer &operator=(std::chrono::time_point<Clock, Duration> deadline_) |
| 79 | { setDeadline(deadline_); return *this; } |
| 80 | |
| 81 | template <class Clock, class Duration = typename Clock::duration> |
| 82 | void setDeadline(std::chrono::time_point<Clock, Duration> tp, |
| 83 | Qt::TimerType type_ = Qt::CoarseTimer); |
| 84 | |
| 85 | template <class Clock, class Duration = typename Clock::duration> |
| 86 | std::chrono::time_point<Clock, Duration> deadline() const; |
| 87 | |
| 88 | template <class Rep, class Period> |
| 89 | QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer) |
| 90 | : t2(0) |
| 91 | { setRemainingTime(remaining, type_); } |
| 92 | |
| 93 | template <class Rep, class Period> |
| 94 | QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining) |
| 95 | { setRemainingTime(remaining); return *this; } |
| 96 | |
| 97 | template <class Rep, class Period> |
| 98 | void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer) |
| 99 | { |
| 100 | using namespace std::chrono; |
| 101 | if (remaining == remaining.max()) |
| 102 | *this = QDeadlineTimer(Forever, type_); |
| 103 | else |
| 104 | setPreciseRemainingTime(secs: 0, nsecs: ceil<nanoseconds>(remaining).count(), type: type_); |
| 105 | } |
| 106 | |
| 107 | std::chrono::nanoseconds remainingTimeAsDuration() const noexcept |
| 108 | { |
| 109 | if (isForever()) |
| 110 | return std::chrono::nanoseconds::max(); |
| 111 | qint64 nsecs = rawRemainingTimeNSecs(); |
| 112 | if (nsecs <= 0) |
| 113 | return std::chrono::nanoseconds::zero(); |
| 114 | return std::chrono::nanoseconds(nsecs); |
| 115 | } |
| 116 | |
| 117 | template <class Rep, class Period> |
| 118 | friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration<Rep, Period> value) |
| 119 | { return QDeadlineTimer::addNSecs(dt, nsecs: std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()); } |
| 120 | template <class Rep, class Period> |
| 121 | friend QDeadlineTimer operator+(std::chrono::duration<Rep, Period> value, QDeadlineTimer dt) |
| 122 | { return dt + value; } |
| 123 | template <class Rep, class Period> |
| 124 | friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration<Rep, Period> value) |
| 125 | { return dt = dt + value; } |
| 126 | |
| 127 | private: |
| 128 | friend bool comparesEqual(const QDeadlineTimer &lhs, |
| 129 | const QDeadlineTimer &rhs) noexcept |
| 130 | { |
| 131 | return lhs.t1 == rhs.t1; |
| 132 | } |
| 133 | friend Qt::strong_ordering compareThreeWay(const QDeadlineTimer &lhs, |
| 134 | const QDeadlineTimer &rhs) noexcept |
| 135 | { |
| 136 | return Qt::compareThreeWay(lhs: lhs.t1, rhs: rhs.t1); |
| 137 | } |
| 138 | Q_DECLARE_STRONGLY_ORDERED(QDeadlineTimer) |
| 139 | |
| 140 | qint64 t1 = 0; |
| 141 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) |
| 142 | unsigned t2 = 0; |
| 143 | #endif |
| 144 | unsigned type = Qt::CoarseTimer; |
| 145 | |
| 146 | qint64 rawRemainingTimeNSecs() const noexcept; |
| 147 | }; |
| 148 | |
| 149 | template<class Clock, class Duration> |
| 150 | std::chrono::time_point<Clock, Duration> QDeadlineTimer::deadline() const |
| 151 | { |
| 152 | using namespace std::chrono; |
| 153 | if constexpr (std::is_same_v<Clock, steady_clock>) { |
| 154 | auto val = duration_cast<Duration>(nanoseconds(deadlineNSecs())); |
| 155 | return time_point<Clock, Duration>(val); |
| 156 | } else { |
| 157 | auto val = nanoseconds(rawRemainingTimeNSecs()) + Clock::now(); |
| 158 | return time_point_cast<Duration>(val); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | template<class Clock, class Duration> |
| 163 | void QDeadlineTimer::setDeadline(std::chrono::time_point<Clock, Duration> tp, Qt::TimerType type_) |
| 164 | { |
| 165 | using namespace std::chrono; |
| 166 | if (tp == tp.max()) { |
| 167 | *this = Forever; |
| 168 | type = type_; |
| 169 | } else if constexpr (std::is_same_v<Clock, steady_clock>) { |
| 170 | setPreciseDeadline(secs: 0, |
| 171 | nsecs: duration_cast<nanoseconds>(tp.time_since_epoch()).count(), |
| 172 | type: type_); |
| 173 | } else { |
| 174 | setPreciseRemainingTime(secs: 0, nsecs: duration_cast<nanoseconds>(tp - Clock::now()).count(), type: type_); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | Q_DECLARE_SHARED(QDeadlineTimer) |
| 179 | |
| 180 | QT_END_NAMESPACE |
| 181 | |
| 182 | QT_DECL_METATYPE_EXTERN(QDeadlineTimer, Q_CORE_EXPORT) |
| 183 | |
| 184 | #endif // QDEADLINETIMER_H |
| 185 | |