Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-20 08:58:48

0001 // Copyright (C) 2020 The Qt Company Ltd.
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 QBASICTIMER_H
0005 #define QBASICTIMER_H
0006 
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qabstracteventdispatcher.h>
0009 #include <QtCore/qnamespace.h>
0010 
0011 #include <chrono>
0012 
0013 QT_BEGIN_NAMESPACE
0014 
0015 
0016 class QObject;
0017 
0018 class Q_CORE_EXPORT QBasicTimer
0019 {
0020     Qt::TimerId m_id;
0021     Q_DISABLE_COPY(QBasicTimer)
0022 
0023 public:
0024     // use the same duration type
0025     using Duration = QAbstractEventDispatcher::Duration;
0026 
0027     constexpr QBasicTimer() noexcept : m_id{Qt::TimerId::Invalid} {}
0028     ~QBasicTimer() { if (isActive()) stop(); }
0029 
0030     QBasicTimer(QBasicTimer &&other) noexcept
0031         : m_id{std::exchange(other.m_id, Qt::TimerId::Invalid)}
0032     {}
0033 
0034     QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QBasicTimer)
0035 
0036     void swap(QBasicTimer &other) noexcept { std::swap(m_id, other.m_id); }
0037 
0038     bool isActive() const noexcept { return m_id != Qt::TimerId::Invalid; }
0039     int timerId() const noexcept { return qToUnderlying(id()); }
0040     Qt::TimerId id() const noexcept { return m_id; }
0041     QT_CORE_INLINE_SINCE(6, 5)
0042     void start(int msec, QObject *obj);
0043     QT_CORE_INLINE_SINCE(6, 5)
0044     void start(int msec, Qt::TimerType timerType, QObject *obj);
0045 
0046 #if QT_CORE_REMOVED_SINCE(6, 9)
0047     void start(std::chrono::milliseconds duration, QObject *obj);
0048     void start(std::chrono::milliseconds duration, Qt::TimerType timerType, QObject *obj);
0049 #endif
0050     void start(Duration duration, QObject *obj)
0051     { start(duration, Qt::CoarseTimer, obj); }
0052     void start(Duration duration, Qt::TimerType timerType, QObject *obj);
0053     void stop();
0054 };
0055 Q_DECLARE_TYPEINFO(QBasicTimer, Q_RELOCATABLE_TYPE);
0056 
0057 #if QT_CORE_INLINE_IMPL_SINCE(6, 5)
0058 void QBasicTimer::start(int msec, QObject *obj)
0059 {
0060     start(std::chrono::milliseconds{msec}, obj);
0061 }
0062 
0063 void QBasicTimer::start(int msec, Qt::TimerType t, QObject *obj)
0064 {
0065     start(std::chrono::milliseconds{msec}, t, obj);
0066 }
0067 #endif
0068 
0069 inline void swap(QBasicTimer &lhs, QBasicTimer &rhs) noexcept { lhs.swap(rhs); }
0070 
0071 QT_END_NAMESPACE
0072 
0073 #endif // QBASICTIMER_H