Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qdeadlinetimer.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2016 Intel Corporation.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QDEADLINETIMER_H
0005 #define QDEADLINETIMER_H
0006 
0007 #include <QtCore/qelapsedtimer.h>
0008 #include <QtCore/qmetatype.h>
0009 #include <QtCore/qnamespace.h>
0010 
0011 #ifdef max
0012 // un-pollute the namespace. We need std::numeric_limits::max() and std::chrono::duration::max()
0013 #  undef max
0014 #endif
0015 
0016 #include <limits>
0017 
0018 #include <chrono>
0019 
0020 QT_BEGIN_NAMESPACE
0021 
0022 class Q_CORE_EXPORT QDeadlineTimer
0023 {
0024 public:
0025     enum class ForeverConstant { Forever };
0026     static constexpr ForeverConstant Forever = ForeverConstant::Forever;
0027 
0028     constexpr QDeadlineTimer() noexcept = default;
0029     constexpr explicit QDeadlineTimer(Qt::TimerType type_) noexcept
0030         : type(type_) {}
0031     constexpr QDeadlineTimer(ForeverConstant, Qt::TimerType type_ = Qt::CoarseTimer) noexcept
0032         : t1((std::numeric_limits<qint64>::max)()), type(type_) {}
0033     explicit QDeadlineTimer(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) noexcept;
0034 
0035     void swap(QDeadlineTimer &other) noexcept
0036     { std::swap(t1, other.t1); std::swap(type, other.type); }
0037 
0038     constexpr bool isForever() const noexcept
0039     { return t1 == (std::numeric_limits<qint64>::max)(); }
0040     bool hasExpired() const noexcept;
0041 
0042     Qt::TimerType timerType() const noexcept
0043     { return Qt::TimerType(type & 0xff); }
0044     void setTimerType(Qt::TimerType type);
0045 
0046     qint64 remainingTime() const noexcept;
0047     qint64 remainingTimeNSecs() const noexcept;
0048     void setRemainingTime(qint64 msecs, Qt::TimerType type = Qt::CoarseTimer) noexcept;
0049     void setPreciseRemainingTime(qint64 secs, qint64 nsecs = 0,
0050                                  Qt::TimerType type = Qt::CoarseTimer) noexcept;
0051 
0052     qint64 deadline() const noexcept Q_DECL_PURE_FUNCTION;
0053     qint64 deadlineNSecs() const noexcept Q_DECL_PURE_FUNCTION;
0054     void setDeadline(qint64 msecs, Qt::TimerType timerType = Qt::CoarseTimer) noexcept;
0055     void setPreciseDeadline(qint64 secs, qint64 nsecs = 0,
0056                             Qt::TimerType type = Qt::CoarseTimer) noexcept;
0057 
0058     static QDeadlineTimer addNSecs(QDeadlineTimer dt, qint64 nsecs) noexcept Q_DECL_PURE_FUNCTION;
0059     static QDeadlineTimer current(Qt::TimerType timerType = Qt::CoarseTimer) noexcept;
0060 
0061     friend Q_CORE_EXPORT QDeadlineTimer operator+(QDeadlineTimer dt, qint64 msecs);
0062     friend QDeadlineTimer operator+(qint64 msecs, QDeadlineTimer dt)
0063     { return dt + msecs; }
0064     friend QDeadlineTimer operator-(QDeadlineTimer dt, qint64 msecs)
0065     { return dt + (-msecs); }
0066     friend qint64 operator-(QDeadlineTimer dt1, QDeadlineTimer dt2)
0067     { return (dt1.deadlineNSecs() - dt2.deadlineNSecs()) / (1000 * 1000); }
0068     QDeadlineTimer &operator+=(qint64 msecs)
0069     { *this = *this + msecs; return *this; }
0070     QDeadlineTimer &operator-=(qint64 msecs)
0071     { *this = *this + (-msecs); return *this; }
0072 
0073     template <class Clock, class Duration = typename Clock::duration>
0074     QDeadlineTimer(std::chrono::time_point<Clock, Duration> deadline_,
0075                    Qt::TimerType type_ = Qt::CoarseTimer) : t2(0)
0076     { setDeadline(deadline_, type_); }
0077     template <class Clock, class Duration = typename Clock::duration>
0078     QDeadlineTimer &operator=(std::chrono::time_point<Clock, Duration> deadline_)
0079     { setDeadline(deadline_); return *this; }
0080 
0081     template <class Clock, class Duration = typename Clock::duration>
0082     void setDeadline(std::chrono::time_point<Clock, Duration> tp,
0083                      Qt::TimerType type_ = Qt::CoarseTimer);
0084 
0085     template <class Clock, class Duration = typename Clock::duration>
0086     std::chrono::time_point<Clock, Duration> deadline() const;
0087 
0088     template <class Rep, class Period>
0089     QDeadlineTimer(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
0090         : t2(0)
0091     { setRemainingTime(remaining, type_); }
0092 
0093     template <class Rep, class Period>
0094     QDeadlineTimer &operator=(std::chrono::duration<Rep, Period> remaining)
0095     { setRemainingTime(remaining); return *this; }
0096 
0097     template <class Rep, class Period>
0098     void setRemainingTime(std::chrono::duration<Rep, Period> remaining, Qt::TimerType type_ = Qt::CoarseTimer)
0099     {
0100         using namespace std::chrono;
0101         if (remaining == remaining.max())
0102             *this = QDeadlineTimer(Forever, type_);
0103         else
0104             setPreciseRemainingTime(0, ceil<nanoseconds>(remaining).count(), type_);
0105     }
0106 
0107     std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
0108     {
0109         if (isForever())
0110             return std::chrono::nanoseconds::max();
0111         qint64 nsecs = rawRemainingTimeNSecs();
0112         if (nsecs <= 0)
0113             return std::chrono::nanoseconds::zero();
0114         return std::chrono::nanoseconds(nsecs);
0115     }
0116 
0117     template <class Rep, class Period>
0118     friend QDeadlineTimer operator+(QDeadlineTimer dt, std::chrono::duration<Rep, Period> value)
0119     { return QDeadlineTimer::addNSecs(dt, std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()); }
0120     template <class Rep, class Period>
0121     friend QDeadlineTimer operator+(std::chrono::duration<Rep, Period> value, QDeadlineTimer dt)
0122     { return dt + value; }
0123     template <class Rep, class Period>
0124     friend QDeadlineTimer operator+=(QDeadlineTimer &dt, std::chrono::duration<Rep, Period> value)
0125     { return dt = dt + value; }
0126 
0127 private:
0128     friend bool comparesEqual(const QDeadlineTimer &lhs,
0129                               const QDeadlineTimer &rhs) noexcept
0130     {
0131         return lhs.t1 == rhs.t1;
0132     }
0133     friend Qt::strong_ordering compareThreeWay(const QDeadlineTimer &lhs,
0134                                                const QDeadlineTimer &rhs) noexcept
0135     {
0136         return Qt::compareThreeWay(lhs.t1, rhs.t1);
0137     }
0138     Q_DECLARE_STRONGLY_ORDERED(QDeadlineTimer)
0139 
0140     qint64 t1 = 0;
0141 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
0142     unsigned t2 = 0;
0143 #endif
0144     unsigned type = Qt::CoarseTimer;
0145 
0146     qint64 rawRemainingTimeNSecs() const noexcept;
0147 };
0148 
0149 template<class Clock, class Duration>
0150 std::chrono::time_point<Clock, Duration> QDeadlineTimer::deadline() const
0151 {
0152     using namespace std::chrono;
0153     if constexpr (std::is_same_v<Clock, steady_clock>) {
0154         auto val = duration_cast<Duration>(nanoseconds(deadlineNSecs()));
0155         return time_point<Clock, Duration>(val);
0156     } else {
0157         auto val = nanoseconds(rawRemainingTimeNSecs()) + Clock::now();
0158         return time_point_cast<Duration>(val);
0159     }
0160 }
0161 
0162 template<class Clock, class Duration>
0163 void QDeadlineTimer::setDeadline(std::chrono::time_point<Clock, Duration> tp, Qt::TimerType type_)
0164 {
0165     using namespace std::chrono;
0166     if (tp == tp.max()) {
0167         *this = Forever;
0168         type = type_;
0169     } else if constexpr (std::is_same_v<Clock, steady_clock>) {
0170         setPreciseDeadline(0,
0171                            duration_cast<nanoseconds>(tp.time_since_epoch()).count(),
0172                            type_);
0173     } else {
0174         setPreciseRemainingTime(0, duration_cast<nanoseconds>(tp - Clock::now()).count(), type_);
0175     }
0176 }
0177 
0178 Q_DECLARE_SHARED(QDeadlineTimer)
0179 
0180 QT_END_NAMESPACE
0181 
0182 QT_DECL_METATYPE_EXTERN(QDeadlineTimer, Q_CORE_EXPORT)
0183 
0184 #endif // QDEADLINETIMER_H