// Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QTCONCURRENT_RUNBASE_H #define QTCONCURRENT_RUNBASE_H #include #ifndef QT_NO_CONCURRENT #include #include #include #include #include QT_BEGIN_NAMESPACE #ifndef Q_QDOC namespace QtConcurrent { template struct SelectSpecialization { template struct Type { typedef Normal type; }; }; template <> struct SelectSpecialization { template struct Type { typedef Void type; }; }; struct TaskStartParameters { QThreadPool *threadPool = QThreadPool::globalInstance(); int priority = 0; }; template class RunFunctionTaskBase : public QRunnable { public: QFuture start() { return start(TaskStartParameters()); } QFuture start(const TaskStartParameters ¶meters) { promise.setThreadPool(parameters.threadPool); promise.setRunnable(this); promise.reportStarted(); QFuture theFuture = promise.future(); if (parameters.threadPool) { parameters.threadPool->start(this, parameters.priority); } else { promise.reportCanceled(); promise.reportFinished(); delete this; } return theFuture; } // For backward compatibility QFuture start(QThreadPool *pool) { return start({pool, 0}); } void run() override { if (promise.isCanceled()) { promise.reportFinished(); return; } #ifndef QT_NO_EXCEPTIONS try { #endif runFunctor(); #ifndef QT_NO_EXCEPTIONS } catch (QException &e) { promise.reportException(e); } catch (...) { promise.reportException(QUnhandledException(std::current_exception())); } #endif promise.reportFinished(); } protected: virtual void runFunctor() = 0; QFutureInterface promise; }; } //namespace QtConcurrent #endif //Q_QDOC QT_END_NAMESPACE #endif // QT_NO_CONCURRENT #endif