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