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
qtconcurrentrunbase.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 QTCONCURRENT_RUNBASE_H
6#define QTCONCURRENT_RUNBASE_H
7
8#include <QtConcurrent/qtconcurrent_global.h>
9
10#ifndef QT_NO_CONCURRENT
11
12#include <QtCore/qfuture.h>
13#include <QtCore/qrunnable.h>
14#include <QtCore/qthreadpool.h>
15
16#include <type_traits>
17#include <utility>
18
19QT_BEGIN_NAMESPACE
20
21
22#ifndef Q_QDOC
23
24namespace QtConcurrent {
25
26template <typename T>
28{
29 template <class Normal, class Void>
30 struct Type { typedef Normal type; };
31};
32
33template <>
35{
36 template <class Normal, class Void>
37 struct Type { typedef Void type; };
38};
39
45
46template <typename T>
48{
49public:
51 {
52 return start(TaskStartParameters());
53 }
54
55 QFuture<T> start(const TaskStartParameters &parameters)
56 {
57 promise.setThreadPool(parameters.threadPool);
58 promise.setRunnable(this);
59 promise.reportStarted();
60 QFuture<T> theFuture = promise.future();
61
62 if (parameters.threadPool) {
63 parameters.threadPool->start(this, parameters.priority);
64 } else {
65 promise.reportCanceled();
66 promise.reportFinished();
67 delete this;
68 }
69 return theFuture;
70 }
71
73 {
74 if (!promise.isFinished()) {
75 promise.reportCanceled();
76 promise.reportFinished();
77 }
78 }
79
80 // For backward compatibility
81 QFuture<T> start(QThreadPool *pool) { return start({pool, 0}); }
82
84 {
85 if (promise.isCanceled()) {
86 promise.reportFinished();
87 return;
88 }
89#ifndef QT_NO_EXCEPTIONS
90 try {
91#endif
93#ifndef QT_NO_EXCEPTIONS
94 } catch (QException &e) {
95 promise.reportException(e);
96 } catch (...) {
97 promise.reportException(QUnhandledException(std::current_exception()));
98 }
99#endif
100 promise.reportFinished();
101 }
102
103protected:
104 virtual void runFunctor() = 0;
105
107};
108
109} //namespace QtConcurrent
110
111#endif //Q_QDOC
112
113QT_END_NAMESPACE
114
115#endif // QT_NO_CONCURRENT
116
117#endif
void run() override
Implement this pure virtual function in your subclass.
QFuture< T > start(QThreadPool *pool)
QFuture< T > start(const TaskStartParameters &parameters)
\inmodule QtConcurrent