1/****************************************************************************
2**
3** Copyright (C) 2016 Intel Corporation.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QDEADLINETIMER_H
41#define QDEADLINETIMER_H
42
43#include <QtCore/qelapsedtimer.h>
44#include <QtCore/qmetatype.h>
45#include <QtCore/qnamespace.h>
46#include <QtCore/qpair.h>
47
48#ifdef max
49// un-pollute the namespace. We need std::numeric_limits::max() and std::chrono::duration::max()
50# undef max
51#endif
52
53#include <limits>
54
55#if __has_include(<chrono>)
56# include <chrono>
57#endif
58
59QT_BEGIN_NAMESPACE
60
61class Q_CORE_EXPORT QDeadlineTimer
62{
63public:
64 enum ForeverConstant { Forever };
65
66 Q_DECL_CONSTEXPR QDeadlineTimer(Qt::TimerType type_ = Qt::CoarseTimer) noexcept
67 : t1(0), t2(0), type(type_) {}
68 Q_DECL_CONSTEXPR QDeadlineTimer(ForeverConstant, Qt::TimerType type_ = Qt::CoarseTimer) noexcept
69 : t1(std::numeric_limits<qint64>::max()), t2(0), type(type_) {}
70 explicit QDeadlineTimer(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) noexcept;
71
72 void swap(QDeadlineTimer &other) noexcept
73 { qSwap(value1&: t1, value2&: other.t1); qSwap(value1&: t2, value2&: other.t2); qSwap(value1&: type, value2&: other.type); }
74
75 Q_DECL_CONSTEXPR bool isForever() const noexcept
76 { return t1 == (std::numeric_limits<qint64>::max)(); }
77 bool hasExpired() const noexcept;
78
79 Qt::TimerType timerType() const noexcept
80 { return Qt::TimerType(type & 0xff); }
81 void setTimerType(Qt::TimerType type);
82
83 qint64 remainingTime() const noexcept;
84 qint64 remainingTimeNSecs() const noexcept;
85 void setRemainingTime(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) noexcept;
86 void setPreciseRemainingTime(qint64 secs, qint64 nsecs = 0,
87 Qt::TimerType type = Qt::CoarseTimer) noexcept;
88
89 qint64 deadline() const noexcept Q_DECL_PURE_FUNCTION;
90 qint64 deadlineNSecs() const noexcept Q_DECL_PURE_FUNCTION;
91 void setDeadline(qint64 msecs, Qt::TimerType timerType = Qt::CoarseTimer) noexcept;
92 void setPreciseDeadline(qint64 secs, qint64 nsecs = 0,
93 Qt::TimerType type = Qt::CoarseTimer) noexcept;
94
95 static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION;
96 static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer) noexcept;
97
98 friend bool operator==(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
99 { return d1.t1 == d2.t1 && d1.t2 == d2.t2; }
100 friend bool operator!=(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
101 { return !(d1 == d2); }
102 friend bool operator<(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
103 { return d1.t1 < d2.t1 || (d1.t1 == d2.t1 && d1.t2 < d2.t2); }
104 friend bool operator<=(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
105 { return d1 == d2 || d1 < d2; }
106 friend bool operator>(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
107 { return d2 < d1; }
108 friend bool operator>=(QDeadlineTimer d1, QDeadlineTimer d2) noexcept
109 { return !(d1 < d2); }
110
111 friend Q_CORE_EXPORT QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs);
112 friend QDeadlineTimer operator+(qint64 msecs, QDeadlineTimer dt)
113 { return dt + msecs; }
114 friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs)
115 { return dt + (-msecs); }
116 friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2)
117 { return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); }
118 QDeadlineTimer &operator+=(qint64 msecs)
119 { *this = *this + msecs; return *this; }
120 QDeadlineTimer &operator-=(qint64 msecs)
121 { *this = *this + (-msecs); return *this; }
122
123#if __has_include(<chrono>) || defined(Q_CLANG_QDOC)
124 template <class Clock, class Duration>
125 QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_,
126 Qt::TimerType type_ = Qt::CoarseTimer) : t2(0)
127 { setDeadline(deadline_, type_); }
128 template <class Clock, class Duration>
129 QDeadlineTimer &operator=(std::chrono::time_point<Clock, Duration> deadline_)
130 { setDeadline(deadline_); return *this; }
131
132 template <class Clock, class Duration>
133 void setDeadline(std::chrono::time_point<Clock, Duration> deadline_,
134 Qt::TimerType type_ = Qt::CoarseTimer)
135 { setRemainingTime(deadline_ == deadline_.max() ? Duration::max() : deadline_ - Clock::now(), type_); }
136
137 template <class Clock, class Duration = typename Clock::duration>
138 std::chrono::time_point<Clock, Duration> deadline() const
139 {
140 auto val = std::chrono::nanoseconds(rawRemainingTimeNSecs()) + Clock::now();
141 return std::chrono::time_point_cast<Duration>(val);
142 }
143
144 template <class Rep, class Period>
145 QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
146 : t2(0)
147 { setRemainingTime(remaining, type_); }
148
149 template <class Rep, class Period>
150 QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining)
151 { setRemainingTime(remaining); return *this; }
152
153 template <class Rep, class Period>
154 void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
155 {
156 if (remaining == remaining.max())
157 *this = QDeadlineTimer(Forever, type_);
158 else
159 setPreciseRemainingTime(secs: 0, nsecs: std::chrono::nanoseconds(remaining).count(), type: type_);
160 }
161
162 std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
163 {
164 if (isForever())
165 return std::chrono::nanoseconds::max();
166 qint64 nsecs = rawRemainingTimeNSecs();
167 if (nsecs <= 0)
168 return std::chrono::nanoseconds::zero();
169 return std::chrono::nanoseconds(nsecs);
170 }
171
172 template <class Rep, class Period>
173 friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration<Rep, Period> value)
174 { return QDeadlineTimer::addNSecs(dt, nsecs: std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()); }
175 template <class Rep, class Period>
176 friend QDeadlineTimer operator+(std::chrono::duration<Rep, Period> value, QDeadlineTimer dt)
177 { return dt + value; }
178 template <class Rep, class Period>
179 friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration<Rep, Period> value)
180 { return dt = dt + value; }
181#endif
182
183private:
184 qint64 t1;
185 unsigned t2;
186 unsigned type;
187
188 qint64 rawRemainingTimeNSecs() const noexcept;
189
190public:
191 // This is not a public function, it's here only for Qt's internal convenience...
192 QPair<qint64, unsigned> _q_data() const { return qMakePair(x: t1, y: t2); }
193};
194
195Q_DECLARE_SHARED(QDeadlineTimer)
196
197QT_END_NAMESPACE
198
199Q_DECLARE_METATYPE(QDeadlineTimer)
200
201#endif // QDEADLINETIMER_H
202

source code of qtbase/src/corelib/kernel/qdeadlinetimer.h