diff options
Diffstat (limited to 'async/src/async.cpp')
-rw-r--r-- | async/src/async.cpp | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/async/src/async.cpp b/async/src/async.cpp deleted file mode 100644 index c57c9ad..0000000 --- a/async/src/async.cpp +++ /dev/null | |||
@@ -1,148 +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 | #include "async.h" | ||
19 | |||
20 | #include <QCoreApplication> | ||
21 | #include <QDebug> | ||
22 | #include <QEventLoop> | ||
23 | #include <QTimer> | ||
24 | |||
25 | using namespace KAsync; | ||
26 | |||
27 | Private::Execution::Execution(const Private::ExecutorBasePtr &executor) | ||
28 | : executor(executor) | ||
29 | , resultBase(nullptr) | ||
30 | , isRunning(false) | ||
31 | , isFinished(false) | ||
32 | { | ||
33 | } | ||
34 | |||
35 | Private::Execution::~Execution() | ||
36 | { | ||
37 | if (resultBase) { | ||
38 | resultBase->releaseExecution(); | ||
39 | delete resultBase; | ||
40 | } | ||
41 | prevExecution.reset(); | ||
42 | } | ||
43 | |||
44 | void Private::Execution::setFinished() | ||
45 | { | ||
46 | isFinished = true; | ||
47 | //executor.clear(); | ||
48 | #ifndef QT_NO_DEBUG | ||
49 | if (tracer) { | ||
50 | delete tracer; | ||
51 | } | ||
52 | #endif | ||
53 | } | ||
54 | |||
55 | void Private::Execution::releaseFuture() | ||
56 | { | ||
57 | resultBase = 0; | ||
58 | } | ||
59 | |||
60 | bool Private::Execution::errorWasHandled() const | ||
61 | { | ||
62 | Execution *exec = const_cast<Execution*>(this); | ||
63 | while (exec) { | ||
64 | if (exec->executor->hasErrorFunc()) { | ||
65 | return true; | ||
66 | } | ||
67 | exec = exec->prevExecution.data(); | ||
68 | } | ||
69 | return false; | ||
70 | } | ||
71 | |||
72 | |||
73 | |||
74 | |||
75 | |||
76 | Private::ExecutorBase::ExecutorBase(const ExecutorBasePtr &parent) | ||
77 | : mPrev(parent) | ||
78 | { | ||
79 | } | ||
80 | |||
81 | Private::ExecutorBase::~ExecutorBase() | ||
82 | { | ||
83 | } | ||
84 | |||
85 | |||
86 | |||
87 | |||
88 | JobBase::JobBase(const Private::ExecutorBasePtr &executor) | ||
89 | : mExecutor(executor) | ||
90 | { | ||
91 | } | ||
92 | |||
93 | JobBase::~JobBase() | ||
94 | { | ||
95 | } | ||
96 | |||
97 | static void asyncWhile(const std::function<void(std::function<void(bool)>)> &body, const std::function<void()> &completionHandler) { | ||
98 | body([body, completionHandler](bool complete) { | ||
99 | if (complete) { | ||
100 | completionHandler(); | ||
101 | } else { | ||
102 | asyncWhile(body, completionHandler); | ||
103 | } | ||
104 | }); | ||
105 | } | ||
106 | |||
107 | Job<void> KAsync::dowhile(Condition condition, ThenTask<void> body) | ||
108 | { | ||
109 | return KAsync::start<void>([body, condition](KAsync::Future<void> &future) { | ||
110 | asyncWhile([condition, body](std::function<void(bool)> whileCallback) { | ||
111 | KAsync::start<void>(body).then<void>([whileCallback, condition]() { | ||
112 | whileCallback(!condition()); | ||
113 | }).exec(); | ||
114 | }, | ||
115 | [&future]() { //while complete | ||
116 | future.setFinished(); | ||
117 | }); | ||
118 | }); | ||
119 | } | ||
120 | |||
121 | Job<void> KAsync::dowhile(ThenTask<bool> body) | ||
122 | { | ||
123 | return KAsync::start<void>([body](KAsync::Future<void> &future) { | ||
124 | asyncWhile([body](std::function<void(bool)> whileCallback) { | ||
125 | KAsync::start<bool>(body).then<bool, bool>([whileCallback](bool result) { | ||
126 | whileCallback(!result); | ||
127 | //FIXME this return value is only required because .then<bool, void> doesn't work | ||
128 | return true; | ||
129 | }).exec(); | ||
130 | }, | ||
131 | [&future]() { //while complete | ||
132 | future.setFinished(); | ||
133 | }); | ||
134 | }); | ||
135 | } | ||
136 | |||
137 | Job<void> KAsync::wait(int delay) | ||
138 | { | ||
139 | auto timer = QSharedPointer<QTimer>::create(); | ||
140 | return KAsync::start<void>([timer, delay](KAsync::Future<void> &future) { | ||
141 | timer->setSingleShot(true); | ||
142 | QObject::connect(timer.data(), &QTimer::timeout, [&future]() { | ||
143 | future.setFinished(); | ||
144 | }); | ||
145 | timer->start(delay); | ||
146 | }); | ||
147 | } | ||
148 | |||