summaryrefslogtreecommitdiffstats
path: root/async/src/future.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'async/src/future.cpp')
-rw-r--r--async/src/future.cpp92
1 files changed, 82 insertions, 10 deletions
diff --git a/async/src/future.cpp b/async/src/future.cpp
index 50a326a..970060b 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,77 @@ 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;
52} 103}
53 104
54 105
106void FutureBase::addWatcher(FutureWatcherBase* watcher)
107{
108 d->watchers.append(QPointer<FutureWatcherBase>(watcher));
109}
110
111
112
113
55 114
56FutureWatcherBase::FutureWatcherBase(QObject *parent) 115FutureWatcherBase::FutureWatcherBase(QObject *parent)
57 : QObject(parent) 116 : QObject(parent)
117 , d(new FutureWatcherBase::Private)
58{ 118{
59} 119}
60 120
61FutureWatcherBase::~FutureWatcherBase() 121FutureWatcherBase::~FutureWatcherBase()
62{ 122{
123 delete d;
63} 124}
64 125
126void FutureWatcherBase::futureReadyCallback()
127{
128 Q_EMIT futureReady();
129}
65 130
66#include "future.moc" 131void FutureWatcherBase::setFutureImpl(const FutureBase &future)
132{
133 d->future = future;
134 d->future.addWatcher(this);
135 if (future.isFinished()) {
136 futureReadyCallback();
137 }
138}