1// Copyright (C) 2020 The Qt Company Ltd.
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 QBASICTIMER_H
5#define QBASICTIMER_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qnamespace.h>
9
10#include <chrono>
11
12QT_BEGIN_NAMESPACE
13
14
15class QObject;
16
17class Q_CORE_EXPORT QBasicTimer
18{
19 Qt::TimerId m_id;
20 Q_DISABLE_COPY(QBasicTimer)
21
22public:
23 constexpr QBasicTimer() noexcept : m_id{Qt::TimerId::Invalid} {}
24 ~QBasicTimer() { if (isActive()) stop(); }
25
26 QBasicTimer(QBasicTimer &&other) noexcept
27 : m_id{std::exchange(obj&: other.m_id, new_val: Qt::TimerId::Invalid)}
28 {}
29
30 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QBasicTimer)
31
32 void swap(QBasicTimer &other) noexcept { std::swap(a&: m_id, b&: other.m_id); }
33
34 bool isActive() const noexcept { return m_id != Qt::TimerId::Invalid; }
35 int timerId() const noexcept { return qToUnderlying(e: id()); }
36 Qt::TimerId id() const noexcept { return m_id; }
37 QT_CORE_INLINE_SINCE(6, 5)
38 void start(int msec, QObject *obj);
39 QT_CORE_INLINE_SINCE(6, 5)
40 void start(int msec, Qt::TimerType timerType, QObject *obj);
41 void start(std::chrono::milliseconds duration, QObject *obj);
42 void start(std::chrono::milliseconds duration, Qt::TimerType timerType, QObject *obj);
43 void stop();
44};
45Q_DECLARE_TYPEINFO(QBasicTimer, Q_RELOCATABLE_TYPE);
46
47#if QT_CORE_INLINE_IMPL_SINCE(6, 5)
48void QBasicTimer::start(int msec, QObject *obj)
49{
50 start(duration: std::chrono::milliseconds{msec}, obj);
51}
52
53void QBasicTimer::start(int msec, Qt::TimerType t, QObject *obj)
54{
55 start(duration: std::chrono::milliseconds{msec}, timerType: t, obj);
56}
57#endif
58
59inline void swap(QBasicTimer &lhs, QBasicTimer &rhs) noexcept { lhs.swap(other&: rhs); }
60
61QT_END_NAMESPACE
62
63#endif // QBASICTIMER_H
64

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

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