File indexing completed on 2026-04-01 08:27:59
0001
0002
0003
0004
0005 #ifndef QTCONCURRENT_RUNBASE_H
0006 #define QTCONCURRENT_RUNBASE_H
0007
0008 #include <QtConcurrent/qtconcurrent_global.h>
0009
0010 #ifndef QT_NO_CONCURRENT
0011
0012 #include <QtCore/qfuture.h>
0013 #include <QtCore/qrunnable.h>
0014 #include <QtCore/qthreadpool.h>
0015
0016 #include <type_traits>
0017 #include <utility>
0018
0019 QT_BEGIN_NAMESPACE
0020
0021
0022 #ifndef Q_QDOC
0023
0024 namespace QtConcurrent {
0025
0026 template <typename T>
0027 struct SelectSpecialization
0028 {
0029 template <class Normal, class Void>
0030 struct Type { typedef Normal type; };
0031 };
0032
0033 template <>
0034 struct SelectSpecialization<void>
0035 {
0036 template <class Normal, class Void>
0037 struct Type { typedef Void type; };
0038 };
0039
0040 struct TaskStartParameters
0041 {
0042 QThreadPool *threadPool = QThreadPool::globalInstance();
0043 int priority = 0;
0044 };
0045
0046 template <typename T>
0047 class RunFunctionTaskBase : public QRunnable
0048 {
0049 public:
0050 QFuture<T> start()
0051 {
0052 return start(TaskStartParameters());
0053 }
0054
0055 QFuture<T> start(const TaskStartParameters ¶meters)
0056 {
0057 promise.setThreadPool(parameters.threadPool);
0058 promise.setRunnable(this);
0059 promise.reportStarted();
0060 QFuture<T> theFuture = promise.future();
0061
0062 if (parameters.threadPool) {
0063 parameters.threadPool->start(this, parameters.priority);
0064 } else {
0065 promise.reportCanceled();
0066 promise.reportFinished();
0067 delete this;
0068 }
0069 return theFuture;
0070 }
0071
0072
0073 QFuture<T> start(QThreadPool *pool) { return start({pool, 0}); }
0074
0075 void run() override
0076 {
0077 if (promise.isCanceled()) {
0078 promise.reportFinished();
0079 return;
0080 }
0081 #ifndef QT_NO_EXCEPTIONS
0082 try {
0083 #endif
0084 runFunctor();
0085 #ifndef QT_NO_EXCEPTIONS
0086 } catch (QException &e) {
0087 promise.reportException(e);
0088 } catch (...) {
0089 promise.reportException(QUnhandledException(std::current_exception()));
0090 }
0091 #endif
0092 promise.reportFinished();
0093 }
0094
0095 protected:
0096 virtual void runFunctor() = 0;
0097
0098 QFutureInterface<T> promise;
0099 };
0100
0101 }
0102
0103 #endif
0104
0105 QT_END_NAMESPACE
0106
0107 #endif
0108
0109 #endif