25 enum class ForeverConstant { Forever };
26 static constexpr ForeverConstant Forever = ForeverConstant::Forever;
28 constexpr QDeadlineTimer()
noexcept =
default;
29 constexpr explicit QDeadlineTimer(Qt::TimerType type_)
noexcept
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;
35 void swap(QDeadlineTimer &other)
noexcept
36 { std::swap(t1, other.t1); std::swap(type, other.type); }
38 constexpr bool isForever()
const noexcept
39 {
return t1 == (std::numeric_limits<qint64>::max)(); }
40 bool hasExpired()
const noexcept;
42 Qt::TimerType timerType()
const noexcept
43 {
return Qt::TimerType(type & 0xff); }
44 void setTimerType(Qt::TimerType type);
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;
52 Q_DECL_PURE_FUNCTION qint64 deadline()
const noexcept;
53 Q_DECL_PURE_FUNCTION qint64 deadlineNSecs()
const noexcept;
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;
58 Q_DECL_PURE_FUNCTION
static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs)
noexcept;
59 static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer)
noexcept;
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; }
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; }
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);
85 template <
class Clock,
class Duration =
typename Clock::duration>
86 std::chrono::time_point<Clock, Duration> deadline()
const;
88 template <
class Rep,
class Period>
89 QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
91 { setRemainingTime(remaining, type_); }
93 template <
class Rep,
class Period>
94 QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining)
95 { setRemainingTime(remaining);
return *
this; }
97 template <
class Rep,
class Period>
98 void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
100 using namespace std::chrono;
101 if (remaining == remaining.max())
102 *
this = QDeadlineTimer(Forever, type_);
104 setPreciseRemainingTime(0, ceil<nanoseconds>(remaining).count(), type_);
107 std::chrono::nanoseconds remainingTimeAsDuration()
const noexcept
110 return std::chrono::nanoseconds::max();
111 qint64 nsecs = rawRemainingTimeNSecs();
113 return std::chrono::nanoseconds::zero();
114 return std::chrono::nanoseconds(nsecs);
117 template <
class Rep,
class Period>
118 friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration<Rep, Period> value)
119 {
return QDeadlineTimer::addNSecs(dt, 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; }
128 friend bool comparesEqual(
const QDeadlineTimer &lhs,
129 const QDeadlineTimer &rhs)
noexcept
131 return lhs.t1 == rhs.t1;
133 friend Qt::strong_ordering compareThreeWay(
const QDeadlineTimer &lhs,
134 const QDeadlineTimer &rhs)
noexcept
136 return Qt::compareThreeWay(lhs.t1, rhs.t1);
138 Q_DECLARE_STRONGLY_ORDERED(QDeadlineTimer)
141#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
144 unsigned type = Qt::CoarseTimer;
146 qint64 rawRemainingTimeNSecs()
const noexcept;
150std::chrono::time_point<Clock, Duration> QDeadlineTimer::deadline()
const
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);
157 auto val = nanoseconds(rawRemainingTimeNSecs()) + Clock::now();
158 return time_point_cast<Duration>(val);
163void QDeadlineTimer::setDeadline(std::chrono::time_point<Clock, Duration> tp, Qt::TimerType type_)
165 using namespace std::chrono;
166 if (tp == tp.max()) {
169 }
else if constexpr (std::is_same_v<Clock, steady_clock>) {
170 setPreciseDeadline(0,
171 duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
174 setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - Clock::now()).count(), type_);