diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-12 16:02:56 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-04-12 16:02:56 +0200 |
commit | 7357673a7ee5a0976447d0285e3aa272e8626282 (patch) | |
tree | 667c7f7c8a780c88419f878ed5c381d54a7ed803 /async/src/future.cpp | |
parent | e85967518b9041e9943ec5f1765c6694bb153840 (diff) | |
parent | 73b8fe58c6fb985898d2bbf431926f0628e2b0a9 (diff) | |
download | sink-7357673a7ee5a0976447d0285e3aa272e8626282.tar.gz sink-7357673a7ee5a0976447d0285e3aa272e8626282.zip |
Merge remote-tracking branch 'origin/develop' into develop
Diffstat (limited to 'async/src/future.cpp')
-rw-r--r-- | async/src/future.cpp | 112 |
1 files changed, 102 insertions, 10 deletions
diff --git a/async/src/future.cpp b/async/src/future.cpp index 50a326a..4f3cd80 100644 --- a/async/src/future.cpp +++ b/async/src/future.cpp | |||
@@ -20,11 +20,41 @@ | |||
20 | 20 | ||
21 | using namespace Async; | 21 | using namespace Async; |
22 | 22 | ||
23 | FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution) | ||
24 | : finished(false) | ||
25 | , errorCode(0) | ||
26 | , mExecution(execution) | ||
27 | { | ||
28 | } | ||
29 | |||
30 | FutureBase::PrivateBase::~PrivateBase() | ||
31 | { | ||
32 | Private::ExecutionPtr executionPtr = mExecution.toStrongRef(); | ||
33 | if (executionPtr) { | ||
34 | executionPtr->releaseFuture(); | ||
35 | releaseExecution(); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | void FutureBase::PrivateBase::releaseExecution() | ||
40 | { | ||
41 | mExecution.clear(); | ||
42 | } | ||
43 | |||
44 | |||
45 | |||
23 | FutureBase::FutureBase() | 46 | FutureBase::FutureBase() |
47 | : d(nullptr) | ||
48 | { | ||
49 | } | ||
50 | |||
51 | FutureBase::FutureBase(FutureBase::PrivateBase *dd) | ||
52 | : d(dd) | ||
24 | { | 53 | { |
25 | } | 54 | } |
26 | 55 | ||
27 | FutureBase::FutureBase(const Async::FutureBase &other) | 56 | FutureBase::FutureBase(const Async::FutureBase &other) |
57 | : d(other.d) | ||
28 | { | 58 | { |
29 | } | 59 | } |
30 | 60 | ||
@@ -32,35 +62,97 @@ FutureBase::~FutureBase() | |||
32 | { | 62 | { |
33 | } | 63 | } |
34 | 64 | ||
35 | FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution) | 65 | void FutureBase::releaseExecution() |
36 | : mExecution(execution) | ||
37 | { | 66 | { |
67 | d->releaseExecution(); | ||
38 | } | 68 | } |
39 | 69 | ||
40 | FutureBase::PrivateBase::~PrivateBase() | 70 | void FutureBase::setFinished() |
41 | { | 71 | { |
42 | Private::ExecutionPtr executionPtr = mExecution.toStrongRef(); | 72 | if (isFinished()) { |
43 | if (executionPtr) { | 73 | return; |
44 | executionPtr->releaseFuture(); | 74 | } |
45 | releaseExecution(); | 75 | d->finished = true; |
76 | for (auto watcher : d->watchers) { | ||
77 | if (watcher) { | ||
78 | watcher->futureReadyCallback(); | ||
79 | } | ||
46 | } | 80 | } |
47 | } | 81 | } |
48 | 82 | ||
49 | void FutureBase::PrivateBase::releaseExecution() | 83 | bool FutureBase::isFinished() const |
50 | { | 84 | { |
51 | mExecution.clear(); | 85 | return d->finished; |
86 | } | ||
87 | |||
88 | void FutureBase::setError(int code, const QString &message) | ||
89 | { | ||
90 | d->errorCode = code; | ||
91 | d->errorMessage = message; | ||
92 | setFinished(); | ||
93 | } | ||
94 | |||
95 | int FutureBase::errorCode() const | ||
96 | { | ||
97 | return d->errorCode; | ||
98 | } | ||
99 | |||
100 | QString FutureBase::errorMessage() const | ||
101 | { | ||
102 | return d->errorMessage; | ||
103 | } | ||
104 | |||
105 | void FutureBase::setProgress(int processed, int total) | ||
106 | { | ||
107 | setProgress((qreal) processed / total); | ||
108 | } | ||
109 | |||
110 | void FutureBase::setProgress(qreal progress) | ||
111 | { | ||
112 | for (auto watcher : d->watchers) { | ||
113 | if (watcher) { | ||
114 | watcher->futureProgressCallback(progress); | ||
115 | } | ||
116 | } | ||
52 | } | 117 | } |
53 | 118 | ||
54 | 119 | ||
55 | 120 | ||
121 | void FutureBase::addWatcher(FutureWatcherBase* watcher) | ||
122 | { | ||
123 | d->watchers.append(QPointer<FutureWatcherBase>(watcher)); | ||
124 | } | ||
125 | |||
126 | |||
127 | |||
128 | |||
129 | |||
56 | FutureWatcherBase::FutureWatcherBase(QObject *parent) | 130 | FutureWatcherBase::FutureWatcherBase(QObject *parent) |
57 | : QObject(parent) | 131 | : QObject(parent) |
132 | , d(new FutureWatcherBase::Private) | ||
58 | { | 133 | { |
59 | } | 134 | } |
60 | 135 | ||
61 | FutureWatcherBase::~FutureWatcherBase() | 136 | FutureWatcherBase::~FutureWatcherBase() |
62 | { | 137 | { |
138 | delete d; | ||
63 | } | 139 | } |
64 | 140 | ||
141 | void FutureWatcherBase::futureReadyCallback() | ||
142 | { | ||
143 | Q_EMIT futureReady(); | ||
144 | } | ||
145 | |||
146 | void FutureWatcherBase::futureProgressCallback(qreal progress) | ||
147 | { | ||
148 | Q_EMIT futureProgress(progress); | ||
149 | } | ||
65 | 150 | ||
66 | #include "future.moc" | 151 | void FutureWatcherBase::setFutureImpl(const FutureBase &future) |
152 | { | ||
153 | d->future = future; | ||
154 | d->future.addWatcher(this); | ||
155 | if (future.isFinished()) { | ||
156 | futureReadyCallback(); | ||
157 | } | ||
158 | } | ||