diff options
Diffstat (limited to 'async/src/future.h')
-rw-r--r-- | async/src/future.h | 257 |
1 files changed, 0 insertions, 257 deletions
diff --git a/async/src/future.h b/async/src/future.h deleted file mode 100644 index b2b723e..0000000 --- a/async/src/future.h +++ /dev/null | |||
@@ -1,257 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright 2014 Daniel Vrátil <dvratil@redhat.com> | ||
3 | * | ||
4 | * This library is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU Library General Public License as | ||
6 | * published by the Free Software Foundation; either version 2 of | ||
7 | * the License, or (at your option) any later version. | ||
8 | * | ||
9 | * This library is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU Library General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU Library General Public License | ||
15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | |||
18 | #ifndef FUTURE_H | ||
19 | #define FUTURE_H | ||
20 | |||
21 | #include "kasync_export.h" | ||
22 | |||
23 | class QEventLoop; | ||
24 | |||
25 | #include <type_traits> | ||
26 | |||
27 | #include <QSharedDataPointer> | ||
28 | #include <QPointer> | ||
29 | #include <QVector> | ||
30 | #include <QEventLoop> | ||
31 | |||
32 | namespace KAsync { | ||
33 | |||
34 | class FutureWatcherBase; | ||
35 | template<typename T> | ||
36 | class FutureWatcher; | ||
37 | |||
38 | namespace Private { | ||
39 | class Execution; | ||
40 | class ExecutorBase; | ||
41 | |||
42 | typedef QSharedPointer<Execution> ExecutionPtr; | ||
43 | } // namespace Private | ||
44 | |||
45 | class KASYNC_EXPORT FutureBase | ||
46 | { | ||
47 | friend class KAsync::Private::Execution; | ||
48 | friend class FutureWatcherBase; | ||
49 | |||
50 | public: | ||
51 | virtual ~FutureBase(); | ||
52 | |||
53 | void setFinished(); | ||
54 | bool isFinished() const; | ||
55 | void setError(int code = 1, const QString &message = QString()); | ||
56 | int errorCode() const; | ||
57 | QString errorMessage() const; | ||
58 | |||
59 | void setProgress(qreal progress); | ||
60 | void setProgress(int processed, int total); | ||
61 | |||
62 | protected: | ||
63 | class PrivateBase : public QSharedData | ||
64 | { | ||
65 | public: | ||
66 | PrivateBase(const KAsync::Private::ExecutionPtr &execution); | ||
67 | virtual ~PrivateBase(); | ||
68 | |||
69 | void releaseExecution(); | ||
70 | |||
71 | bool finished; | ||
72 | int errorCode; | ||
73 | QString errorMessage; | ||
74 | |||
75 | QVector<QPointer<FutureWatcherBase>> watchers; | ||
76 | private: | ||
77 | QWeakPointer<KAsync::Private::Execution> mExecution; | ||
78 | }; | ||
79 | |||
80 | FutureBase(); | ||
81 | FutureBase(FutureBase::PrivateBase *dd); | ||
82 | FutureBase(const FutureBase &other); | ||
83 | |||
84 | void addWatcher(KAsync::FutureWatcherBase *watcher); | ||
85 | void releaseExecution(); | ||
86 | |||
87 | protected: | ||
88 | QExplicitlySharedDataPointer<PrivateBase> d; | ||
89 | }; | ||
90 | |||
91 | template<typename T> | ||
92 | class FutureWatcher; | ||
93 | |||
94 | template<typename T> | ||
95 | class Future; | ||
96 | |||
97 | template<typename T> | ||
98 | class FutureGeneric : public FutureBase | ||
99 | { | ||
100 | friend class FutureWatcher<T>; | ||
101 | |||
102 | public: | ||
103 | void waitForFinished() const | ||
104 | { | ||
105 | if (isFinished()) { | ||
106 | return; | ||
107 | } | ||
108 | FutureWatcher<T> watcher; | ||
109 | QEventLoop eventLoop; | ||
110 | QObject::connect(&watcher, &KAsync::FutureWatcher<T>::futureReady, | ||
111 | &eventLoop, &QEventLoop::quit); | ||
112 | watcher.setFuture(*static_cast<const KAsync::Future<T>*>(this)); | ||
113 | eventLoop.exec(); | ||
114 | } | ||
115 | |||
116 | protected: | ||
117 | FutureGeneric(const KAsync::Private::ExecutionPtr &execution) | ||
118 | : FutureBase(new Private(execution)) | ||
119 | {} | ||
120 | |||
121 | FutureGeneric(const FutureGeneric<T> &other) | ||
122 | : FutureBase(other) | ||
123 | {} | ||
124 | |||
125 | protected: | ||
126 | class Private : public FutureBase::PrivateBase | ||
127 | { | ||
128 | public: | ||
129 | Private(const KAsync::Private::ExecutionPtr &execution) | ||
130 | : FutureBase::PrivateBase(execution) | ||
131 | {} | ||
132 | |||
133 | typename std::conditional<std::is_void<T>::value, int /* dummy */, T>::type | ||
134 | value; | ||
135 | }; | ||
136 | }; | ||
137 | |||
138 | |||
139 | template<typename T> | ||
140 | class Future : public FutureGeneric<T> | ||
141 | { | ||
142 | friend class KAsync::Private::ExecutorBase; | ||
143 | |||
144 | template<typename T_> | ||
145 | friend class KAsync::FutureWatcher; | ||
146 | |||
147 | public: | ||
148 | Future() | ||
149 | : FutureGeneric<T>(KAsync::Private::ExecutionPtr()) | ||
150 | {} | ||
151 | |||
152 | Future(const Future<T> &other) | ||
153 | : FutureGeneric<T>(other) | ||
154 | {} | ||
155 | |||
156 | void setValue(const T &value) | ||
157 | { | ||
158 | static_cast<typename FutureGeneric<T>::Private*>(this->d.data())->value = value; | ||
159 | } | ||
160 | |||
161 | T value() const | ||
162 | { | ||
163 | return static_cast<typename FutureGeneric<T>::Private*>(this->d.data())->value; | ||
164 | } | ||
165 | |||
166 | protected: | ||
167 | Future(const KAsync::Private::ExecutionPtr &execution) | ||
168 | : FutureGeneric<T>(execution) | ||
169 | {} | ||
170 | |||
171 | }; | ||
172 | |||
173 | template<> | ||
174 | class Future<void> : public FutureGeneric<void> | ||
175 | { | ||
176 | friend class KAsync::Private::ExecutorBase; | ||
177 | |||
178 | public: | ||
179 | Future() | ||
180 | : FutureGeneric<void>(KAsync::Private::ExecutionPtr()) | ||
181 | {} | ||
182 | |||
183 | Future(const Future<void> &other) | ||
184 | : FutureGeneric<void>(other) | ||
185 | {} | ||
186 | |||
187 | protected: | ||
188 | Future(const KAsync::Private::ExecutionPtr &execution) | ||
189 | : FutureGeneric<void>(execution) | ||
190 | {} | ||
191 | }; | ||
192 | |||
193 | |||
194 | |||
195 | |||
196 | |||
197 | class KASYNC_EXPORT FutureWatcherBase : public QObject | ||
198 | { | ||
199 | Q_OBJECT | ||
200 | |||
201 | friend class FutureBase; | ||
202 | |||
203 | Q_SIGNALS: | ||
204 | void futureReady(); | ||
205 | void futureProgress(qreal progress); | ||
206 | |||
207 | protected: | ||
208 | FutureWatcherBase(QObject *parent = nullptr); | ||
209 | virtual ~FutureWatcherBase(); | ||
210 | |||
211 | void futureReadyCallback(); | ||
212 | void futureProgressCallback(qreal progress); | ||
213 | |||
214 | void setFutureImpl(const KAsync::FutureBase &future); | ||
215 | |||
216 | protected: | ||
217 | class Private { | ||
218 | public: | ||
219 | KAsync::FutureBase future; | ||
220 | }; | ||
221 | |||
222 | Private * const d; | ||
223 | |||
224 | private: | ||
225 | Q_DISABLE_COPY(FutureWatcherBase); | ||
226 | }; | ||
227 | |||
228 | template<typename T> | ||
229 | class FutureWatcher : public FutureWatcherBase | ||
230 | { | ||
231 | friend class KAsync::FutureGeneric<T>; | ||
232 | |||
233 | public: | ||
234 | FutureWatcher(QObject *parent = nullptr) | ||
235 | : FutureWatcherBase(parent) | ||
236 | {} | ||
237 | |||
238 | ~FutureWatcher() | ||
239 | {} | ||
240 | |||
241 | void setFuture(const KAsync::Future<T> &future) | ||
242 | { | ||
243 | setFutureImpl(*static_cast<const KAsync::FutureBase*>(&future)); | ||
244 | } | ||
245 | |||
246 | KAsync::Future<T> future() const | ||
247 | { | ||
248 | return *static_cast<KAsync::Future<T>*>(&d->future); | ||
249 | } | ||
250 | |||
251 | private: | ||
252 | Q_DISABLE_COPY(FutureWatcher<T>); | ||
253 | }; | ||
254 | |||
255 | } // namespace Async | ||
256 | |||
257 | #endif // FUTURE_H | ||