24 enum class ForeverConstant { Forever };
25 static constexpr ForeverConstant Forever = ForeverConstant::Forever;
27 constexpr QDeadlineTimer()
noexcept =
default;
28 constexpr explicit QDeadlineTimer(Qt::TimerType type_)
noexcept
30 constexpr QDeadlineTimer(ForeverConstant, Qt::TimerType type_ = Qt::CoarseTimer)
noexcept
31 : t1((std::numeric_limits<qint64>::max)()), type(type_) {}
32 explicit QDeadlineTimer(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer)
noexcept;
34 void swap(QDeadlineTimer &other)
noexcept
35 { std::swap(t1, other.t1); std::swap(type, other.type); }
37 constexpr bool isForever()
const noexcept
38 {
return t1 == (std::numeric_limits<qint64>::max)(); }
39 bool hasExpired()
const noexcept;
41 Qt::TimerType timerType()
const noexcept
42 {
return Qt::TimerType(type & 0xff); }
43 void setTimerType(Qt::TimerType type);
45 qint64 remainingTime()
const noexcept;
46 qint64 remainingTimeNSecs()
const noexcept;
47 void setRemainingTime(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer)
noexcept;
48 void setPreciseRemainingTime(qint64 secs, qint64 nsecs = 0,
49 Qt::TimerType type = Qt::CoarseTimer)
noexcept;
51 Q_DECL_PURE_FUNCTION qint64 deadline()
const noexcept;
52 Q_DECL_PURE_FUNCTION qint64 deadlineNSecs()
const noexcept;
53 void setDeadline(qint64 msecs, Qt::TimerType timerType = Qt::CoarseTimer)
noexcept;
54 void setPreciseDeadline(qint64 secs, qint64 nsecs = 0,
55 Qt::TimerType type = Qt::CoarseTimer)
noexcept;
57 Q_DECL_PURE_FUNCTION
static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs)
noexcept;
58 static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer)
noexcept;
60 friend Q_CORE_EXPORT QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs);
61 friend QDeadlineTimer operator+(qint64 msecs, QDeadlineTimer dt)
62 {
return dt + msecs; }
63 friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs)
64 {
return dt + (-msecs); }
65 friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2)
66 {
return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); }
67 QDeadlineTimer &operator+=(qint64 msecs)
68 { *
this = *
this + msecs;
return *
this; }
69 QDeadlineTimer &operator-=(qint64 msecs)
70 { *
this = *
this + (-msecs);
return *
this; }
72 template <
class Clock,
class Duration =
typename Clock::duration>
73 QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_,
74 Qt::TimerType type_ = Qt::CoarseTimer) : t2(0)
75 { setDeadline(deadline_, type_); }
76 template <
class Clock,
class Duration =
typename Clock::duration>
77 QDeadlineTimer &operator=(std::chrono::time_point<Clock, Duration> deadline_)
78 { setDeadline(deadline_);
return *
this; }
80 template <
class Clock,
class Duration =
typename Clock::duration>
81 void setDeadline(std::chrono::time_point<Clock, Duration> tp,
82 Qt::TimerType type_ = Qt::CoarseTimer);
84 template <
class Clock,
class Duration =
typename Clock::duration>
85 std::chrono::time_point<Clock, Duration> deadline()
const;
87 template <
class Rep,
class Period>
88 QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
90 { setRemainingTime(remaining, type_); }
92 template <
class Rep,
class Period>
93 QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining)
94 { setRemainingTime(remaining);
return *
this; }
96 template <
class Rep,
class Period>
97 void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
99 using namespace std::chrono;
100 if (remaining == remaining.max())
101 *
this = QDeadlineTimer(Forever, type_);
103 setPreciseRemainingTime(0, ceil<nanoseconds>(remaining).count(), type_);
106 std::chrono::nanoseconds remainingTimeAsDuration()
const noexcept
109 return std::chrono::nanoseconds::max();
110 qint64 nsecs = rawRemainingTimeNSecs();
112 return std::chrono::nanoseconds::zero();
113 return std::chrono::nanoseconds(nsecs);
116 template <
class Rep,
class Period>
117 friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration<Rep, Period> value)
118 {
return QDeadlineTimer::addNSecs(dt, std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()); }
119 template <
class Rep,
class Period>
120 friend QDeadlineTimer operator+(std::chrono::duration<Rep, Period> value, QDeadlineTimer dt)
121 {
return dt + value; }
122 template <
class Rep,
class Period>
123 friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration<Rep, Period> value)
124 {
return dt = dt + value; }
127 friend bool comparesEqual(
const QDeadlineTimer &lhs,
128 const QDeadlineTimer &rhs)
noexcept
130 return lhs.t1 == rhs.t1;
132 friend Qt::strong_ordering compareThreeWay(
const QDeadlineTimer &lhs,
133 const QDeadlineTimer &rhs)
noexcept
135 return Qt::compareThreeWay(lhs.t1, rhs.t1);
137 Q_DECLARE_STRONGLY_ORDERED(QDeadlineTimer)
140#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
143 unsigned type = Qt::CoarseTimer;
145 qint64 rawRemainingTimeNSecs()
const noexcept;
149std::chrono::time_point<Clock, Duration> QDeadlineTimer::deadline()
const
151 using namespace std::chrono;
152 if constexpr (std::is_same_v<Clock, steady_clock>) {
153 auto val = duration_cast<Duration>(nanoseconds(deadlineNSecs()));
154 return time_point<Clock, Duration>(val);
156 auto val = nanoseconds(rawRemainingTimeNSecs()) + Clock::now();
157 return time_point_cast<Duration>(val);
162void QDeadlineTimer::setDeadline(std::chrono::time_point<Clock, Duration> tp, Qt::TimerType type_)
164 using namespace std::chrono;
165 if (tp == tp.max()) {
168 }
else if constexpr (std::is_same_v<Clock, steady_clock>) {
169 setPreciseDeadline(0,
170 duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
173 setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - Clock::now()).count(), type_);