diff options
Diffstat (limited to 'async/src/future.h')
-rw-r--r-- | async/src/future.h | 255 |
1 files changed, 0 insertions, 255 deletions
diff --git a/async/src/future.h b/async/src/future.h deleted file mode 100644 index ff199ef..0000000 --- a/async/src/future.h +++ /dev/null | |||
@@ -1,255 +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 | class QEventLoop; | ||
22 | |||
23 | #include <type_traits> | ||
24 | |||
25 | #include <QSharedDataPointer> | ||
26 | #include <QPointer> | ||
27 | #include <QVector> | ||
28 | #include <QEventLoop> | ||
29 | |||
30 | namespace Async { | ||
31 | |||
32 | class FutureWatcherBase; | ||
33 | template<typename T> | ||
34 | class FutureWatcher; | ||
35 | |||
36 | namespace Private { | ||
37 | class Execution; | ||
38 | class ExecutorBase; | ||
39 | |||
40 | typedef QSharedPointer<Execution> ExecutionPtr; | ||
41 | } // namespace Private | ||
42 | |||
43 | class FutureBase | ||
44 | { | ||
45 | friend class Async::Private::Execution; | ||
46 | friend class FutureWatcherBase; | ||
47 | |||
48 | public: | ||
49 | virtual ~FutureBase(); | ||
50 | |||
51 | void setFinished(); | ||
52 | bool isFinished() const; | ||
53 | void setError(int code = 1, const QString &message = QString()); | ||
54 | int errorCode() const; | ||
55 | QString errorMessage() const; | ||
56 | |||
57 | void setProgress(qreal progress); | ||
58 | void setProgress(int processed, int total); | ||
59 | |||
60 | protected: | ||
61 | class PrivateBase : public QSharedData | ||
62 | { | ||
63 | public: | ||
64 | PrivateBase(const Async::Private::ExecutionPtr &execution); | ||
65 | virtual ~PrivateBase(); | ||
66 | |||
67 | void releaseExecution(); | ||
68 | |||
69 | bool finished; | ||
70 | int errorCode; | ||
71 | QString errorMessage; | ||
72 | |||
73 | QVector<QPointer<FutureWatcherBase>> watchers; | ||
74 | private: | ||
75 | QWeakPointer<Async::Private::Execution> mExecution; | ||
76 | }; | ||
77 | |||
78 | FutureBase(); | ||
79 | FutureBase(FutureBase::PrivateBase *dd); | ||
80 | FutureBase(const FutureBase &other); | ||
81 | |||
82 | void addWatcher(Async::FutureWatcherBase *watcher); | ||
83 | void releaseExecution(); | ||
84 | |||
85 | protected: | ||
86 | QExplicitlySharedDataPointer<PrivateBase> d; | ||
87 | }; | ||
88 | |||
89 | template<typename T> | ||
90 | class FutureWatcher; | ||
91 | |||
92 | template<typename T> | ||
93 | class Future; | ||
94 | |||
95 | template<typename T> | ||
96 | class FutureGeneric : public FutureBase | ||
97 | { | ||
98 | friend class FutureWatcher<T>; | ||
99 | |||
100 | public: | ||
101 | void waitForFinished() const | ||
102 | { | ||
103 | if (isFinished()) { | ||
104 | return; | ||
105 | } | ||
106 | FutureWatcher<T> watcher; | ||
107 | QEventLoop eventLoop; | ||
108 | QObject::connect(&watcher, &Async::FutureWatcher<T>::futureReady, | ||
109 | &eventLoop, &QEventLoop::quit); | ||
110 | watcher.setFuture(*static_cast<const Async::Future<T>*>(this)); | ||
111 | eventLoop.exec(); | ||
112 | } | ||
113 | |||
114 | protected: | ||
115 | FutureGeneric(const Async::Private::ExecutionPtr &execution) | ||
116 | : FutureBase(new Private(execution)) | ||
117 | {} | ||
118 | |||
119 | FutureGeneric(const FutureGeneric<T> &other) | ||
120 | : FutureBase(other) | ||
121 | {} | ||
122 | |||
123 | protected: | ||
124 | class Private : public FutureBase::PrivateBase | ||
125 | { | ||
126 | public: | ||
127 | Private(const Async::Private::ExecutionPtr &execution) | ||
128 | : FutureBase::PrivateBase(execution) | ||
129 | {} | ||
130 | |||
131 | typename std::conditional<std::is_void<T>::value, int /* dummy */, T>::type | ||
132 | value; | ||
133 | }; | ||
134 | }; | ||
135 | |||
136 | |||
137 | template<typename T> | ||
138 | class Future : public FutureGeneric<T> | ||
139 | { | ||
140 | friend class Async::Private::ExecutorBase; | ||
141 | |||
142 | template<typename T_> | ||
143 | friend class Async::FutureWatcher; | ||
144 | |||
145 | public: | ||
146 | Future() | ||
147 | : FutureGeneric<T>(Async::Private::ExecutionPtr()) | ||
148 | {} | ||
149 | |||
150 | Future(const Future<T> &other) | ||
151 | : FutureGeneric<T>(other) | ||
152 | {} | ||
153 | |||
154 | void setValue(const T &value) | ||
155 | { | ||
156 | static_cast<typename FutureGeneric<T>::Private*>(this->d.data())->value = value; | ||
157 | } | ||
158 | |||
159 | T value() const | ||
160 | { | ||
161 | return static_cast<typename FutureGeneric<T>::Private*>(this->d.data())->value; | ||
162 | } | ||
163 | |||
164 | protected: | ||
165 | Future(const Async::Private::ExecutionPtr &execution) | ||
166 | : FutureGeneric<T>(execution) | ||
167 | {} | ||
168 | |||
169 | }; | ||
170 | |||
171 | template<> | ||
172 | class Future<void> : public FutureGeneric<void> | ||
173 | { | ||
174 | friend class Async::Private::ExecutorBase; | ||
175 | |||
176 | public: | ||
177 | Future() | ||
178 | : FutureGeneric<void>(Async::Private::ExecutionPtr()) | ||
179 | {} | ||
180 | |||
181 | Future(const Future<void> &other) | ||
182 | : FutureGeneric<void>(other) | ||
183 | {} | ||
184 | |||
185 | protected: | ||
186 | Future(const Async::Private::ExecutionPtr &execution) | ||
187 | : FutureGeneric<void>(execution) | ||
188 | {} | ||
189 | }; | ||
190 | |||
191 | |||
192 | |||
193 | |||
194 | |||
195 | class FutureWatcherBase : public QObject | ||
196 | { | ||
197 | Q_OBJECT | ||
198 | |||
199 | friend class FutureBase; | ||
200 | |||
201 | Q_SIGNALS: | ||
202 | void futureReady(); | ||
203 | void futureProgress(qreal progress); | ||
204 | |||
205 | protected: | ||
206 | FutureWatcherBase(QObject *parent = nullptr); | ||
207 | virtual ~FutureWatcherBase(); | ||
208 | |||
209 | void futureReadyCallback(); | ||
210 | void futureProgressCallback(qreal progress); | ||
211 | |||
212 | void setFutureImpl(const Async::FutureBase &future); | ||
213 | |||
214 | protected: | ||
215 | class Private { | ||
216 | public: | ||
217 | Async::FutureBase future; | ||
218 | }; | ||
219 | |||
220 | Private * const d; | ||
221 | |||
222 | private: | ||
223 | Q_DISABLE_COPY(FutureWatcherBase); | ||
224 | }; | ||
225 | |||
226 | template<typename T> | ||
227 | class FutureWatcher : public FutureWatcherBase | ||
228 | { | ||
229 | friend class Async::FutureGeneric<T>; | ||
230 | |||
231 | public: | ||
232 | FutureWatcher(QObject *parent = nullptr) | ||
233 | : FutureWatcherBase(parent) | ||
234 | {} | ||
235 | |||
236 | ~FutureWatcher() | ||
237 | {} | ||
238 | |||
239 | void setFuture(const Async::Future<T> &future) | ||
240 | { | ||
241 | setFutureImpl(*static_cast<const Async::FutureBase*>(&future)); | ||
242 | } | ||
243 | |||
244 | Async::Future<T> future() const | ||
245 | { | ||
246 | return *static_cast<Async::Future<T>*>(&d->future); | ||
247 | } | ||
248 | |||
249 | private: | ||
250 | Q_DISABLE_COPY(FutureWatcher<T>); | ||
251 | }; | ||
252 | |||
253 | } // namespace Async | ||
254 | |||
255 | #endif // FUTURE_H | ||