Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfuturesynchronizer.h
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
5#ifndef QFUTURESYNCHRONIZER_H
6#define QFUTURESYNCHRONIZER_H
7
8#include <QtCore/qfuture.h>
9
11
12QT_BEGIN_NAMESPACE
13
14
15template <typename T>
16class QFutureSynchronizer
17{
18 Q_DISABLE_COPY(QFutureSynchronizer)
19
20public:
21 Q_NODISCARD_CTOR QFutureSynchronizer() : m_cancelOnWait(false) { }
22 Q_NODISCARD_CTOR_X("Use future.waitForFinished() instead.")
23 explicit QFutureSynchronizer(QFuture<T> future)
24 : m_cancelOnWait(false)
25 { addFuture(std::move(future)); }
26 ~QFutureSynchronizer() { waitForFinished(); }
27
28 void setFuture(QFuture<T> future)
29 {
30 waitForFinished();
31 m_futures.clear();
32 addFuture(std::move(future));
33 }
34
35 void addFuture(QFuture<T> future)
36 {
37 m_futures.append(std::move(future));
38 }
39
40 void waitForFinished()
41 {
42 if (m_cancelOnWait) {
43 for (int i = 0; i < m_futures.size(); ++i) {
44 m_futures[i].cancel();
45 }
46 }
47
48 for (int i = 0; i < m_futures.size(); ++i) {
49 m_futures[i].waitForFinished();
50 }
51 }
52
53 void clearFutures()
54 {
55 m_futures.clear();
56 }
57
58 QList<QFuture<T> > futures() const
59 {
60 return m_futures;
61 }
62
63 void setCancelOnWait(bool enabled)
64 {
65 m_cancelOnWait = enabled;
66 }
67
68 bool cancelOnWait() const
69 {
70 return m_cancelOnWait;
71 }
72
73protected:
74 QList<QFuture<T>> m_futures;
75 bool m_cancelOnWait;
76};
77
78QT_END_NAMESPACE
79
80#endif // QFUTURESYNCHRONIZER_H
QFuture< void > future
[5]