1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
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 QFUTUREWATCHER_H |
41 | #define QFUTUREWATCHER_H |
42 | |
43 | #include <QtCore/qfuture.h> |
44 | #include <QtCore/qobject.h> |
45 | |
46 | QT_REQUIRE_CONFIG(future); |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | |
51 | class QEvent; |
52 | |
53 | class QFutureWatcherBasePrivate; |
54 | class Q_CORE_EXPORT QFutureWatcherBase : public QObject |
55 | { |
56 | Q_OBJECT |
57 | Q_DECLARE_PRIVATE(QFutureWatcherBase) |
58 | |
59 | public: |
60 | explicit QFutureWatcherBase(QObject *parent = nullptr); |
61 | // de-inline dtor |
62 | |
63 | int progressValue() const; |
64 | int progressMinimum() const; |
65 | int progressMaximum() const; |
66 | QString progressText() const; |
67 | |
68 | bool isStarted() const; |
69 | bool isFinished() const; |
70 | bool isRunning() const; |
71 | bool isCanceled() const; |
72 | #if QT_DEPRECATED_SINCE(6, 0) |
73 | QT_DEPRECATED_VERSION_X_6_0("Use isSuspending() or isSuspended() instead." ) |
74 | bool isPaused() const; |
75 | #endif |
76 | bool isSuspending() const; |
77 | bool isSuspended() const; |
78 | |
79 | void waitForFinished(); |
80 | |
81 | void setPendingResultsLimit(int limit); |
82 | |
83 | bool event(QEvent *event) override; |
84 | |
85 | Q_SIGNALS: |
86 | void started(); |
87 | void finished(); |
88 | void canceled(); |
89 | #if QT_DEPRECATED_SINCE(6, 0) |
90 | QT_DEPRECATED_VERSION_X_6_0("Use suspending() instead." ) |
91 | void paused(); |
92 | #endif |
93 | void suspending(); |
94 | void suspended(); |
95 | void resumed(); |
96 | void resultReadyAt(int resultIndex); |
97 | void resultsReadyAt(int beginIndex, int endIndex); |
98 | void progressRangeChanged(int minimum, int maximum); |
99 | void progressValueChanged(int progressValue); |
100 | void progressTextChanged(const QString &progressText); |
101 | |
102 | public Q_SLOTS: |
103 | void cancel(); |
104 | void setSuspended(bool suspend); |
105 | void suspend(); |
106 | void resume(); |
107 | void toggleSuspended(); |
108 | |
109 | #if QT_DEPRECATED_SINCE(6, 0) |
110 | QT_DEPRECATED_VERSION_X_6_0("Use setSuspended() instead." ) |
111 | void setPaused(bool paused); |
112 | |
113 | QT_DEPRECATED_VERSION_X_6_0("Use suspended() instead." ) |
114 | void pause(); |
115 | |
116 | QT_DEPRECATED_VERSION_X_6_0("Use toggleSuspended() instead." ) |
117 | void togglePaused(); |
118 | #endif |
119 | |
120 | protected: |
121 | void connectNotify (const QMetaMethod &signal) override; |
122 | void disconnectNotify (const QMetaMethod &signal) override; |
123 | |
124 | // called from setFuture() implemented in template sub-classes |
125 | void connectOutputInterface(); |
126 | void disconnectOutputInterface(bool pendingAssignment = false); |
127 | |
128 | private: |
129 | // implemented in the template sub-classes |
130 | virtual const QFutureInterfaceBase &futureInterface() const = 0; |
131 | virtual QFutureInterfaceBase &futureInterface() = 0; |
132 | }; |
133 | |
134 | template <typename T> |
135 | class QFutureWatcher : public QFutureWatcherBase |
136 | { |
137 | public: |
138 | explicit QFutureWatcher(QObject *_parent = nullptr) |
139 | : QFutureWatcherBase(_parent) |
140 | { } |
141 | ~QFutureWatcher() |
142 | { disconnectOutputInterface(); } |
143 | |
144 | void setFuture(const QFuture<T> &future); |
145 | QFuture<T> future() const |
146 | { return m_future; } |
147 | |
148 | template<typename U = T, typename = QtPrivate::EnableForNonVoid<U>> |
149 | T result() const { return m_future.result(); } |
150 | |
151 | template<typename U = T, typename = QtPrivate::EnableForNonVoid<U>> |
152 | T resultAt(int index) const { return m_future.resultAt(index); } |
153 | |
154 | #ifdef Q_QDOC |
155 | int progressValue() const; |
156 | int progressMinimum() const; |
157 | int progressMaximum() const; |
158 | QString progressText() const; |
159 | |
160 | bool isStarted() const; |
161 | bool isFinished() const; |
162 | bool isRunning() const; |
163 | bool isCanceled() const; |
164 | #if QT_DEPRECATED_SINCE(6, 0) |
165 | bool isPaused() const; |
166 | #endif |
167 | bool isSuspending() const; |
168 | bool isSuspended() const; |
169 | |
170 | void waitForFinished(); |
171 | |
172 | void setPendingResultsLimit(int limit); |
173 | |
174 | Q_SIGNALS: |
175 | void started(); |
176 | void finished(); |
177 | void canceled(); |
178 | #if QT_DEPRECATED_SINCE(6, 0) |
179 | void paused(); |
180 | #endif |
181 | void suspending(); |
182 | void suspended(); |
183 | void resumed(); |
184 | void resultReadyAt(int resultIndex); |
185 | void resultsReadyAt(int beginIndex, int endIndex); |
186 | void progressRangeChanged(int minimum, int maximum); |
187 | void progressValueChanged(int progressValue); |
188 | void progressTextChanged(const QString &progressText); |
189 | |
190 | public Q_SLOTS: |
191 | void cancel(); |
192 | void setSuspended(bool suspend); |
193 | void suspend(); |
194 | void resume(); |
195 | void toggleSuspended(); |
196 | #if QT_DEPRECATED_SINCE(6, 0) |
197 | void setPaused(bool paused); |
198 | void pause(); |
199 | void togglePaused(); |
200 | #endif // QT_DEPRECATED_SINCE(6, 0) |
201 | |
202 | #endif // Q_QDOC |
203 | |
204 | private: |
205 | QFuture<T> m_future; |
206 | const QFutureInterfaceBase &futureInterface() const override { return m_future.d; } |
207 | QFutureInterfaceBase &futureInterface() override { return m_future.d; } |
208 | }; |
209 | |
210 | template <typename T> |
211 | Q_INLINE_TEMPLATE void QFutureWatcher<T>::setFuture(const QFuture<T> &_future) |
212 | { |
213 | if (_future == m_future) |
214 | return; |
215 | |
216 | disconnectOutputInterface(true); |
217 | m_future = _future; |
218 | connectOutputInterface(); |
219 | } |
220 | |
221 | QT_END_NAMESPACE |
222 | |
223 | #endif // QFUTUREWATCHER_H |
224 | |