1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtConcurrent module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTCONCURRENT_RUNBASE_H
41#define QTCONCURRENT_RUNBASE_H
42
43#include <QtConcurrent/qtconcurrent_global.h>
44
45#ifndef QT_NO_CONCURRENT
46
47#include <QtCore/qfuture.h>
48#include <QtCore/qrunnable.h>
49#include <QtCore/qthreadpool.h>
50
51QT_BEGIN_NAMESPACE
52
53
54#ifndef Q_QDOC
55
56namespace QtConcurrent {
57
58template <typename T>
59struct SelectSpecialization
60{
61 template <class Normal, class Void>
62 struct Type { typedef Normal type; };
63};
64
65template <>
66struct SelectSpecialization<void>
67{
68 template <class Normal, class Void>
69 struct Type { typedef Void type; };
70};
71
72template <typename T>
73class RunFunctionTaskBase : public QFutureInterface<T> , public QRunnable
74{
75public:
76 QFuture<T> start()
77 {
78 return start(QThreadPool::globalInstance());
79 }
80
81 QFuture<T> start(QThreadPool *pool)
82 {
83 this->setThreadPool(pool);
84 this->setRunnable(this);
85 this->reportStarted();
86 QFuture<T> theFuture = this->future();
87 if (pool) {
88 pool->start(this, /*m_priority*/ 0);
89 } else {
90 this->reportCanceled();
91 this->reportFinished();
92 delete this;
93 }
94 return theFuture;
95 }
96
97 void run() override {}
98 virtual void runFunctor() = 0;
99};
100
101template <typename T>
102class RunFunctionTask : public RunFunctionTaskBase<T>
103{
104public:
105 void run() override
106 {
107 if (this->isCanceled()) {
108 this->reportFinished();
109 return;
110 }
111#ifndef QT_NO_EXCEPTIONS
112 try {
113#endif
114 this->runFunctor();
115#ifndef QT_NO_EXCEPTIONS
116 } catch (QException &e) {
117 QFutureInterface<T>::reportException(e);
118 } catch (...) {
119 QFutureInterface<T>::reportException(QUnhandledException());
120 }
121#endif
122
123 this->reportResult(result);
124 this->reportFinished();
125 }
126 T result;
127};
128
129template <>
130class RunFunctionTask<void> : public RunFunctionTaskBase<void>
131{
132public:
133 void run() override
134 {
135 if (this->isCanceled()) {
136 this->reportFinished();
137 return;
138 }
139#ifndef QT_NO_EXCEPTIONS
140 try {
141#endif
142 this->runFunctor();
143#ifndef QT_NO_EXCEPTIONS
144 } catch (QException &e) {
145 QFutureInterface<void>::reportException(e);
146 } catch (...) {
147 QFutureInterface<void>::reportException(e: QUnhandledException());
148 }
149#endif
150 this->reportFinished();
151 }
152};
153
154} //namespace QtConcurrent
155
156#endif //Q_QDOC
157
158QT_END_NAMESPACE
159
160#endif // QT_NO_CONCURRENT
161
162#endif
163

source code of qtbase/src/concurrent/qtconcurrentrunbase.h