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