Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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/qmetatype.h>
8#include <QtCore/qnamespace.h>
9
10#ifdef max
11// un-pollute the namespace. We need std::numeric_limits::max() and std::chrono::duration::max()
12# undef max
13#endif
14
15#include <limits>
16
17#include <chrono>
18
20
22{
23public:
24 enum class ForeverConstant { Forever };
25 static constexpr ForeverConstant Forever = ForeverConstant::Forever;
26
27 constexpr QDeadlineTimer() noexcept = default;
28 constexpr explicit QDeadlineTimer(Qt::TimerType type_) noexcept
29 : type(type_) {}
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;
33
34 void swap(QDeadlineTimer &other) noexcept
35 { std::swap(t1, other.t1); std::swap(type, other.type); }
36
37 constexpr bool isForever() const noexcept
38 { return t1 == (std::numeric_limits<qint64>::max)(); }
39 bool hasExpired() const noexcept;
40
41 Qt::TimerType timerType() const noexcept
42 { return Qt::TimerType(type & 0xff); }
43 void setTimerType(Qt::TimerType type);
44
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;
50
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;
56
57 Q_DECL_PURE_FUNCTION static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept;
58 static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer) noexcept;
59
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; }
71
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; }
79
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);
83
84 template <class Clock, class Duration = typename Clock::duration>
85 std::chrono::time_point<Clock, Duration> deadline() const;
86
87 template <class Rep, class Period>
88 QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
89 : t2(0)
90 { setRemainingTime(remaining, type_); }
91
92 template <class Rep, class Period>
93 QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining)
94 { setRemainingTime(remaining); return *this; }
95
96 template <class Rep, class Period>
97 void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
98 {
99 using namespace std::chrono;
100 if (remaining == remaining.max())
101 *this = QDeadlineTimer(Forever, type_);
102 else
103 setPreciseRemainingTime(0, ceil<nanoseconds>(remaining).count(), type_);
104 }
105
106 std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
107 {
108 if (isForever())
109 return std::chrono::nanoseconds::max();
110 qint64 nsecs = rawRemainingTimeNSecs();
111 if (nsecs <= 0)
112 return std::chrono::nanoseconds::zero();
113 return std::chrono::nanoseconds(nsecs);
114 }
115
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; }
125
126private:
127 friend bool comparesEqual(const QDeadlineTimer &lhs,
128 const QDeadlineTimer &rhs) noexcept
129 {
130 return lhs.t1 == rhs.t1;
131 }
132 friend Qt::strong_ordering compareThreeWay(const QDeadlineTimer &lhs,
133 const QDeadlineTimer &rhs) noexcept
134 {
135 return Qt::compareThreeWay(lhs.t1, rhs.t1);
136 }
137 Q_DECLARE_STRONGLY_ORDERED(QDeadlineTimer)
138
139 qint64 t1 = 0;
140#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
141 unsigned t2 = 0;
142#endif
143 unsigned type = Qt::CoarseTimer;
144
145 qint64 rawRemainingTimeNSecs() const noexcept;
146};
147
148template<class Clock, class Duration>
149std::chrono::time_point<Clock, Duration> QDeadlineTimer::deadline() const
150{
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);
155 } else {
156 auto val = nanoseconds(rawRemainingTimeNSecs()) + Clock::now();
157 return time_point_cast<Duration>(val);
158 }
159}
160
161template<class Clock, class Duration>
162void QDeadlineTimer::setDeadline(std::chrono::time_point<Clock, Duration> tp, Qt::TimerType type_)
163{
164 using namespace std::chrono;
165 if (tp == tp.max()) {
166 *this = Forever;
167 type = type_;
168 } else if constexpr (std::is_same_v<Clock, steady_clock>) {
169 setPreciseDeadline(0,
170 duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
171 type_);
172 } else {
173 setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - Clock::now()).count(), type_);
174 }
175}
176
178
179QT_END_NAMESPACE
180
181QT_DECL_METATYPE_EXTERN(QDeadlineTimer, Q_CORE_EXPORT)
182
183#endif // QDEADLINETIMER_H
\inmodule QtCore
ThreadFunctionResult whileThreadFunction()
virtual bool runIterations(Iterator, int, int, T *)
virtual bool runIteration(Iterator, int, T *)
IterateKernel(QThreadPool *pool, Iterator _begin, Iterator _end, U &&_defaultValue)
IterateKernel(QThreadPool *pool, Iterator _begin, Iterator _end)
ThreadFunctionResult threadFunction() override
DefaultValueContainer< ResultType > defaultValue
\inmodule QtConcurrent
bool selectIteration(std::bidirectional_iterator_tag)
bool selectIteration(std::random_access_iterator_tag)
bool selectIteration(std::forward_iterator_tag)
static qint64 getticks()
static double elapsed(qint64 after, qint64 before)