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 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | |
54 | #ifndef Q_QDOC |
55 | |
56 | namespace QtConcurrent { |
57 | |
58 | template <typename T> |
59 | struct SelectSpecialization |
60 | { |
61 | template <class Normal, class Void> |
62 | struct Type { typedef Normal type; }; |
63 | }; |
64 | |
65 | template <> |
66 | struct SelectSpecialization<void> |
67 | { |
68 | template <class Normal, class Void> |
69 | struct Type { typedef Void type; }; |
70 | }; |
71 | |
72 | template <typename T> |
73 | class RunFunctionTaskBase : public QFutureInterface<T> , public QRunnable |
74 | { |
75 | public: |
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 | pool->start(this, /*m_priority*/ 0); |
88 | return theFuture; |
89 | } |
90 | |
91 | void run() override {} |
92 | virtual void runFunctor() = 0; |
93 | }; |
94 | |
95 | template <typename T> |
96 | class RunFunctionTask : public RunFunctionTaskBase<T> |
97 | { |
98 | public: |
99 | void run() override |
100 | { |
101 | if (this->isCanceled()) { |
102 | this->reportFinished(); |
103 | return; |
104 | } |
105 | #ifndef QT_NO_EXCEPTIONS |
106 | try { |
107 | #endif |
108 | this->runFunctor(); |
109 | #ifndef QT_NO_EXCEPTIONS |
110 | } catch (QException &e) { |
111 | QFutureInterface<T>::reportException(e); |
112 | } catch (...) { |
113 | QFutureInterface<T>::reportException(QUnhandledException()); |
114 | } |
115 | #endif |
116 | |
117 | this->reportResult(result); |
118 | this->reportFinished(); |
119 | } |
120 | T result; |
121 | }; |
122 | |
123 | template <> |
124 | class RunFunctionTask<void> : public RunFunctionTaskBase<void> |
125 | { |
126 | public: |
127 | void run() override |
128 | { |
129 | if (this->isCanceled()) { |
130 | this->reportFinished(); |
131 | return; |
132 | } |
133 | #ifndef QT_NO_EXCEPTIONS |
134 | try { |
135 | #endif |
136 | this->runFunctor(); |
137 | #ifndef QT_NO_EXCEPTIONS |
138 | } catch (QException &e) { |
139 | QFutureInterface<void>::reportException(e); |
140 | } catch (...) { |
141 | QFutureInterface<void>::reportException(QUnhandledException()); |
142 | } |
143 | #endif |
144 | this->reportFinished(); |
145 | } |
146 | }; |
147 | |
148 | } //namespace QtConcurrent |
149 | |
150 | #endif //Q_QDOC |
151 | |
152 | QT_END_NAMESPACE |
153 | |
154 | #endif // QT_NO_CONCURRENT |
155 | |
156 | #endif |
157 | |