summaryrefslogtreecommitdiffstats
path: root/async/src/future.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-12 16:02:56 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-04-12 16:02:56 +0200
commit7357673a7ee5a0976447d0285e3aa272e8626282 (patch)
tree667c7f7c8a780c88419f878ed5c381d54a7ed803 /async/src/future.cpp
parente85967518b9041e9943ec5f1765c6694bb153840 (diff)
parent73b8fe58c6fb985898d2bbf431926f0628e2b0a9 (diff)
downloadsink-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.cpp112
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
21using namespace Async; 21using namespace Async;
22 22
23FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution)
24 : finished(false)
25 , errorCode(0)
26 , mExecution(execution)
27{
28}
29
30FutureBase::PrivateBase::~PrivateBase()
31{
32 Private::ExecutionPtr executionPtr = mExecution.toStrongRef();
33 if (executionPtr) {
34 executionPtr->releaseFuture();
35 releaseExecution();
36 }
37}
38
39void FutureBase::PrivateBase::releaseExecution()
40{
41 mExecution.clear();
42}
43
44
45
23FutureBase::FutureBase() 46FutureBase::FutureBase()
47 : d(nullptr)
48{
49}
50
51FutureBase::FutureBase(FutureBase::PrivateBase *dd)
52 : d(dd)
24{ 53{
25} 54}
26 55
27FutureBase::FutureBase(const Async::FutureBase &other) 56FutureBase::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
35FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution) 65void FutureBase::releaseExecution()
36 : mExecution(execution)
37{ 66{
67 d->releaseExecution();
38} 68}
39 69
40FutureBase::PrivateBase::~PrivateBase() 70void 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
49void FutureBase::PrivateBase::releaseExecution() 83bool FutureBase::isFinished() const
50{ 84{
51 mExecution.clear(); 85 return d->finished;
86}
87
88void FutureBase::setError(int code, const QString &message)
89{
90 d->errorCode = code;
91 d->errorMessage = message;
92 setFinished();
93}
94
95int FutureBase::errorCode() const
96{
97 return d->errorCode;
98}
99
100QString FutureBase::errorMessage() const
101{
102 return d->errorMessage;
103}
104
105void FutureBase::setProgress(int processed, int total)
106{
107 setProgress((qreal) processed / total);
108}
109
110void 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
121void FutureBase::addWatcher(FutureWatcherBase* watcher)
122{
123 d->watchers.append(QPointer<FutureWatcherBase>(watcher));
124}
125
126
127
128
129
56FutureWatcherBase::FutureWatcherBase(QObject *parent) 130FutureWatcherBase::FutureWatcherBase(QObject *parent)
57 : QObject(parent) 131 : QObject(parent)
132 , d(new FutureWatcherBase::Private)
58{ 133{
59} 134}
60 135
61FutureWatcherBase::~FutureWatcherBase() 136FutureWatcherBase::~FutureWatcherBase()
62{ 137{
138 delete d;
63} 139}
64 140
141void FutureWatcherBase::futureReadyCallback()
142{
143 Q_EMIT futureReady();
144}
145
146void FutureWatcherBase::futureProgressCallback(qreal progress)
147{
148 Q_EMIT futureProgress(progress);
149}
65 150
66#include "future.moc" 151void FutureWatcherBase::setFutureImpl(const FutureBase &future)
152{
153 d->future = future;
154 d->future.addWatcher(this);
155 if (future.isFinished()) {
156 futureReadyCallback();
157 }
158}