Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdeadlinetimer.h
Go to the documentation of this file.
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
21
22class Q_CORE_EXPORT QDeadlineTimer
23{
24public:
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_) {}
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(t1, other.t1); std::swap(type, 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;
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; }
65 { return dt + (-msecs); }
67 { return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); }
69 { *this = *this + msecs; return *this; }
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,
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(0, ceil<nanoseconds>(remaining).count(), 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, 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
127private:
128 friend bool comparesEqual(const QDeadlineTimer &lhs,
129 const QDeadlineTimer &rhs) noexcept
130 {
131 return lhs.t1 == rhs.t1;
132 }
134 const QDeadlineTimer &rhs) noexcept
135 {
136 return Qt::compareThreeWay(lhs.t1, rhs.t1);
137 }
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
149template<class Clock, class Duration>
150std::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
162template<class Clock, class Duration>
163void 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(0,
171 duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
172 type_);
173 } else {
174 setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - Clock::now()).count(), type_);
175 }
176}
177
178Q_DECLARE_SHARED(QDeadlineTimer)
179
181
183
184#endif // QDEADLINETIMER_H
\inmodule QtCore
void setDeadline(qint64 msecs, Qt::TimerType timerType=Qt::CoarseTimer) noexcept
Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point,...
friend QDeadlineTimer operator+(std::chrono::duration< Rep, Period > value, QDeadlineTimer dt)
QDeadlineTimer(std::chrono::duration< Rep, Period > remaining, Qt::TimerType type_=Qt::CoarseTimer)
Constructs a QDeadlineTimer object with a remaining time of remaining.
friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration< Rep, Period > value)
static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION
Returns a QDeadlineTimer object whose deadline is extended from dt's deadline by nsecs nanoseconds.
QDeadlineTimer & operator=(std::chrono::duration< Rep, Period > remaining)
Sets this deadline timer to the remaining time.
QDeadlineTimer(std::chrono::time_point< Clock, Duration > deadline_, Qt::TimerType type_=Qt::CoarseTimer)
Constructs a QDeadlineTimer object with a deadline at deadline time point, converting from the clock ...
ForeverConstant
\value Forever Used when creating a QDeadlineTimer to indicate the deadline should not expire
constexpr QDeadlineTimer(ForeverConstant, Qt::TimerType type_=Qt::CoarseTimer) noexcept
QDeadlineTimer objects created with ForeverConstant never expire.
QDeadlineTimer & operator=(std::chrono::time_point< Clock, Duration > deadline_)
Assigns deadline_ to this deadline timer.
friend Qt::strong_ordering compareThreeWay(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) noexcept
constexpr bool isForever() const noexcept
Returns true if this QDeadlineTimer object never expires, false otherwise.
friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2)
constexpr QDeadlineTimer() noexcept=default
void swap(QDeadlineTimer &other) noexcept
Swaps this deadline timer with the other deadline timer.
friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs)
Returns a QDeadlineTimer object whose deadline is msecs before the deadline stored in dt.
friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration< Rep, Period > value)
friend bool comparesEqual(const QDeadlineTimer &lhs, const QDeadlineTimer &rhs) noexcept
std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
Returns the time remaining before the deadline.
QDeadlineTimer & operator-=(qint64 msecs)
Shortens this QDeadlineTimer object by msecs milliseconds and returns itself.
QDeadlineTimer & operator+=(qint64 msecs)
Extends this QDeadlineTimer object by msecs milliseconds and returns itself.
void setRemainingTime(std::chrono::duration< Rep, Period > remaining, Qt::TimerType type_=Qt::CoarseTimer)
This is an overloaded member function, provided for convenience. It differs from the above function o...
\inmodule QtCore \title Classes and helpers for defining comparison operators \keyword qtcompare
Definition qcompare.h:400
Combined button and popup list for selecting options.
Definition qcompare.h:63
TimerType
@ CoarseTimer
constexpr Qt::strong_ordering compareThreeWay(LeftInt lhs, RightInt rhs) noexcept
#define Q_DECLARE_STRONGLY_ORDERED(...)
#define Q_DECL_PURE_FUNCTION
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define QT_DECL_METATYPE_EXTERN(TYPE, EXPORT)
Definition qmetatype.h:1388
GLenum GLenum GLsizei count
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLenum type
GLuint GLfloat * val
#define t2
#define t1
long long qint64
Definition qtypes.h:60
#define explicit
return lhs deadlineNSecs()
[7]
QDeadlineTimer deadline(30s)
deadline setRemainingTime(250ms)
QSharedPointer< T > other(t)
[5]