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 QT_HAS_INCLUDE(<chrono>) |

56 | # include <chrono> |

57 | #endif |

58 | |

59 | QT_BEGIN_NAMESPACE |

60 | |

61 | class Q_CORE_EXPORT QDeadlineTimer |

62 | { |

63 | public: |

64 | enum ForeverConstant { Forever }; |

65 | |

66 | Q_DECL_CONSTEXPR QDeadlineTimer(Qt::TimerType type_ = Qt::CoarseTimer) Q_DECL_NOTHROW |

67 | : t1(0), t2(0), type(type_) {} |

68 | Q_DECL_CONSTEXPR QDeadlineTimer(ForeverConstant, Qt::TimerType type_ = Qt::CoarseTimer) Q_DECL_NOTHROW |

69 | : t1(std::numeric_limits<qint64>::max()), t2(0), type(type_) {} |

70 | explicit QDeadlineTimer(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) Q_DECL_NOTHROW; |

71 | |

72 | void swap(QDeadlineTimer &other) Q_DECL_NOTHROW |

73 | { qSwap(t1, other.t1); qSwap(t2, other.t2); qSwap(type, other.type); } |

74 | |

75 | Q_DECL_CONSTEXPR bool isForever() const Q_DECL_NOTHROW |

76 | { return t1 == (std::numeric_limits<qint64>::max)(); } |

77 | bool hasExpired() const Q_DECL_NOTHROW; |

78 | |

79 | Qt::TimerType timerType() const Q_DECL_NOTHROW |

80 | { return Qt::TimerType(type & 0xff); } |

81 | void setTimerType(Qt::TimerType type); |

82 | |

83 | qint64 remainingTime() const Q_DECL_NOTHROW; |

84 | qint64 remainingTimeNSecs() const Q_DECL_NOTHROW; |

85 | void setRemainingTime(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) Q_DECL_NOTHROW; |

86 | void setPreciseRemainingTime(qint64 secs, qint64 nsecs = 0, |

87 | Qt::TimerType type = Qt::CoarseTimer) Q_DECL_NOTHROW; |

88 | |

89 | qint64 deadline() const Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION; |

90 | qint64 deadlineNSecs() const Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION; |

91 | void setDeadline(qint64 msecs, Qt::TimerType timerType = Qt::CoarseTimer) Q_DECL_NOTHROW; |

92 | void setPreciseDeadline(qint64 secs, qint64 nsecs = 0, |

93 | Qt::TimerType type = Qt::CoarseTimer) Q_DECL_NOTHROW; |

94 | |

95 | static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) Q_DECL_NOTHROW Q_DECL_PURE_FUNCTION; |

96 | static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer) Q_DECL_NOTHROW; |

97 | |

98 | friend bool operator==(QDeadlineTimer d1, QDeadlineTimer d2) Q_DECL_NOTHROW |

99 | { return d1.t1 == d2.t1 && d1.t2 == d2.t2; } |

100 | friend bool operator!=(QDeadlineTimer d1, QDeadlineTimer d2) Q_DECL_NOTHROW |

101 | { return !(d1 == d2); } |

102 | friend bool operator<(QDeadlineTimer d1, QDeadlineTimer d2) Q_DECL_NOTHROW |

103 | { return d1.t1 < d2.t1 || (d1.t1 == d2.t1 && d1.t2 < d2.t2); } |

104 | friend bool operator<=(QDeadlineTimer d1, QDeadlineTimer d2) Q_DECL_NOTHROW |

105 | { return d1 == d2 || d1 < d2; } |

106 | friend bool operator>(QDeadlineTimer d1, QDeadlineTimer d2) Q_DECL_NOTHROW |

107 | { return d2 < d1; } |

108 | friend bool operator>=(QDeadlineTimer d1, QDeadlineTimer d2) Q_DECL_NOTHROW |

109 | { return !(d1 < d2); } |

110 | |

111 | friend QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs) |

112 | { return QDeadlineTimer::addNSecs(dt, msecs * 1000 * 1000); } |

113 | friend QDeadlineTimer operator+(qint64 msecs, QDeadlineTimer dt) |

114 | { return dt + msecs; } |

115 | friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs) |

116 | { return dt + (-msecs); } |

117 | friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2) |

118 | { return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); } |

119 | QDeadlineTimer &operator+=(qint64 msecs) |

120 | { *this = *this + msecs; return *this; } |

121 | QDeadlineTimer &operator-=(qint64 msecs) |

122 | { *this = *this + (-msecs); return *this; } |

123 | |

124 | #if QT_HAS_INCLUDE(<chrono>) || defined(Q_CLANG_QDOC) |

125 | template <class Clock, class Duration> |

126 | QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_, |

127 | Qt::TimerType type_ = Qt::CoarseTimer) : t2(0) |

128 | { setDeadline(deadline_, type_); } |

129 | template <class Clock, class Duration> |

130 | QDeadlineTimer &operator=(std::chrono::time_point<Clock, Duration> deadline_) |

131 | { setDeadline(deadline_); return *this; } |

132 | |

133 | template <class Clock, class Duration> |

134 | void setDeadline(std::chrono::time_point<Clock, Duration> deadline_, |

135 | Qt::TimerType type_ = Qt::CoarseTimer) |

136 | { setRemainingTime(deadline_ == deadline_.max() ? Duration::max() : deadline_ - Clock::now(), type_); } |

137 | |

138 | template <class Clock, class Duration = typename Clock::duration> |

139 | std::chrono::time_point<Clock, Duration> deadline() const |

140 | { |

141 | auto val = std::chrono::nanoseconds(rawRemainingTimeNSecs()) + Clock::now(); |

142 | return std::chrono::time_point_cast<Duration>(val); |

143 | } |

144 | |

145 | template <class Rep, class Period> |

146 | QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer) |

147 | : t2(0) |

148 | { setRemainingTime(remaining, type_); } |

149 | |

150 | template <class Rep, class Period> |

151 | QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining) |

152 | { setRemainingTime(remaining); return *this; } |

153 | |

154 | template <class Rep, class Period> |

155 | void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer) |

156 | { |

157 | if (remaining == remaining.max()) |

158 | *this = QDeadlineTimer(Forever, type_); |

159 | else |

160 | setPreciseRemainingTime(0, std::chrono::nanoseconds(remaining).count(), type_); |

161 | } |

162 | |

163 | std::chrono::nanoseconds remainingTimeAsDuration() const Q_DECL_NOTHROW |

164 | { |

165 | if (isForever()) |

166 | return std::chrono::nanoseconds::max(); |

167 | qint64 nsecs = rawRemainingTimeNSecs(); |

168 | if (nsecs <= 0) |

169 | return std::chrono::nanoseconds::zero(); |

170 | return std::chrono::nanoseconds(nsecs); |

171 | } |

172 | |

173 | template <class Rep, class Period> |

174 | friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration<Rep, Period> value) |

175 | { return QDeadlineTimer::addNSecs(dt, std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()); } |

176 | template <class Rep, class Period> |

177 | friend QDeadlineTimer operator+(std::chrono::duration<Rep, Period> value, QDeadlineTimer dt) |

178 | { return dt + value; } |

179 | template <class Rep, class Period> |

180 | friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration<Rep, Period> value) |

181 | { return dt = dt + value; } |

182 | #endif |

183 | |

184 | private: |

185 | qint64 t1; |

186 | unsigned t2; |

187 | unsigned type; |

188 | |

189 | qint64 rawRemainingTimeNSecs() const Q_DECL_NOTHROW; |

190 | |

191 | public: |

192 | // This is not a public function, it's here only for Qt's internal convenience... |

193 | QPair<qint64, unsigned> _q_data() const { return qMakePair(t1, t2); } |

194 | }; |

195 | |

196 | Q_DECLARE_SHARED(QDeadlineTimer) |

197 | |

198 | QT_END_NAMESPACE |

199 | |

200 | Q_DECLARE_METATYPE(QDeadlineTimer) |

201 | |

202 | #endif // QDEADLINETIMER_H |

203 |