diff options
author | Dan Vrátil <dvratil@redhat.com> | 2015-05-15 16:00:32 +0200 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2015-05-15 16:22:28 +0200 |
commit | 5b78e0da1d64b6096829f54b29f14ec5643b5ece (patch) | |
tree | 2091795596f9721bc1d9d8cb9965103d5a32df7a | |
parent | 3bbee6c42683db5f85b01e4eb6dab8135e199f6c (diff) | |
download | sink-5b78e0da1d64b6096829f54b29f14ec5643b5ece.tar.gz sink-5b78e0da1d64b6096829f54b29f14ec5643b5ece.zip |
Async: rename Async namespace to KAsync
-rw-r--r-- | async/autotests/asynctest.cpp | 192 | ||||
-rw-r--r-- | async/autotests/kjobtest.cpp | 4 | ||||
-rw-r--r-- | async/src/async.cpp | 18 | ||||
-rw-r--r-- | async/src/async.h | 134 | ||||
-rw-r--r-- | async/src/async_impl.h | 20 | ||||
-rw-r--r-- | async/src/debug.cpp | 16 | ||||
-rw-r--r-- | async/src/debug.h | 16 | ||||
-rw-r--r-- | async/src/future.cpp | 4 | ||||
-rw-r--r-- | async/src/future.h | 52 |
9 files changed, 231 insertions, 225 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 9bc9f6b..ffc732c 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp | |||
@@ -82,7 +82,7 @@ private: | |||
82 | template<typename T> | 82 | template<typename T> |
83 | class AsyncSimulator { | 83 | class AsyncSimulator { |
84 | public: | 84 | public: |
85 | AsyncSimulator(Async::Future<T> &future, const T &result) | 85 | AsyncSimulator(KAsync::Future<T> &future, const T &result) |
86 | : mFuture(future) | 86 | : mFuture(future) |
87 | , mResult(result) | 87 | , mResult(result) |
88 | { | 88 | { |
@@ -99,7 +99,7 @@ private: | |||
99 | mTimer.start(200); | 99 | mTimer.start(200); |
100 | } | 100 | } |
101 | 101 | ||
102 | AsyncSimulator(Async::Future<T> &future, std::function<void(Async::Future<T>&)> callback) | 102 | AsyncSimulator(KAsync::Future<T> &future, std::function<void(KAsync::Future<T>&)> callback) |
103 | : mFuture(future) | 103 | : mFuture(future) |
104 | , mCallback(callback) | 104 | , mCallback(callback) |
105 | { | 105 | { |
@@ -116,8 +116,8 @@ private: | |||
116 | } | 116 | } |
117 | 117 | ||
118 | private: | 118 | private: |
119 | Async::Future<T> mFuture; | 119 | KAsync::Future<T> mFuture; |
120 | std::function<void(Async::Future<T>&)> mCallback; | 120 | std::function<void(KAsync::Future<T>&)> mCallback; |
121 | T mResult; | 121 | T mResult; |
122 | QTimer mTimer; | 122 | QTimer mTimer; |
123 | }; | 123 | }; |
@@ -127,7 +127,7 @@ private: | |||
127 | template<> | 127 | template<> |
128 | class AsyncTest::AsyncSimulator<void> { | 128 | class AsyncTest::AsyncSimulator<void> { |
129 | public: | 129 | public: |
130 | AsyncSimulator(Async::Future<void> &future) | 130 | AsyncSimulator(KAsync::Future<void> &future) |
131 | : mFuture(future) | 131 | : mFuture(future) |
132 | { | 132 | { |
133 | QObject::connect(&mTimer, &QTimer::timeout, | 133 | QObject::connect(&mTimer, &QTimer::timeout, |
@@ -143,7 +143,7 @@ public: | |||
143 | } | 143 | } |
144 | 144 | ||
145 | private: | 145 | private: |
146 | Async::Future<void> mFuture; | 146 | KAsync::Future<void> mFuture; |
147 | QTimer mTimer; | 147 | QTimer mTimer; |
148 | }; | 148 | }; |
149 | 149 | ||
@@ -151,24 +151,24 @@ private: | |||
151 | 151 | ||
152 | void AsyncTest::testSyncPromises() | 152 | void AsyncTest::testSyncPromises() |
153 | { | 153 | { |
154 | auto baseJob = Async::start<int>( | 154 | auto baseJob = KAsync::start<int>( |
155 | [](Async::Future<int> &f) { | 155 | [](KAsync::Future<int> &f) { |
156 | f.setValue(42); | 156 | f.setValue(42); |
157 | f.setFinished(); | 157 | f.setFinished(); |
158 | }) | 158 | }) |
159 | .then<QString, int>( | 159 | .then<QString, int>( |
160 | [](int v, Async::Future<QString> &f) { | 160 | [](int v, KAsync::Future<QString> &f) { |
161 | f.setValue("Result is " + QString::number(v)); | 161 | f.setValue(QLatin1String("Result is ") + QString::number(v)); |
162 | f.setFinished(); | 162 | f.setFinished(); |
163 | }); | 163 | }); |
164 | 164 | ||
165 | auto job = baseJob.then<QString, QString>( | 165 | auto job = baseJob.then<QString, QString>( |
166 | [](const QString &v, Async::Future<QString> &f) { | 166 | [](const QString &v, KAsync::Future<QString> &f) { |
167 | f.setValue(v.toUpper()); | 167 | f.setValue(v.toUpper()); |
168 | f.setFinished(); | 168 | f.setFinished(); |
169 | }); | 169 | }); |
170 | 170 | ||
171 | Async::Future<QString> future = job.exec(); | 171 | KAsync::Future<QString> future = job.exec(); |
172 | 172 | ||
173 | QVERIFY(future.isFinished()); | 173 | QVERIFY(future.isFinished()); |
174 | QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); | 174 | QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); |
@@ -176,12 +176,12 @@ void AsyncTest::testSyncPromises() | |||
176 | 176 | ||
177 | void AsyncTest::testAsyncPromises() | 177 | void AsyncTest::testAsyncPromises() |
178 | { | 178 | { |
179 | auto job = Async::start<int>( | 179 | auto job = KAsync::start<int>( |
180 | [](Async::Future<int> &future) { | 180 | [](KAsync::Future<int> &future) { |
181 | new AsyncSimulator<int>(future, 42); | 181 | new AsyncSimulator<int>(future, 42); |
182 | }); | 182 | }); |
183 | 183 | ||
184 | Async::Future<int> future = job.exec(); | 184 | KAsync::Future<int> future = job.exec(); |
185 | 185 | ||
186 | future.waitForFinished(); | 186 | future.waitForFinished(); |
187 | QCOMPARE(future.value(), 42); | 187 | QCOMPARE(future.value(), 42); |
@@ -191,11 +191,11 @@ void AsyncTest::testAsyncPromises2() | |||
191 | { | 191 | { |
192 | bool done = false; | 192 | bool done = false; |
193 | 193 | ||
194 | auto job = Async::start<int>( | 194 | auto job = KAsync::start<int>( |
195 | [](Async::Future<int> &future) { | 195 | [](KAsync::Future<int> &future) { |
196 | new AsyncSimulator<int>(future, 42); | 196 | new AsyncSimulator<int>(future, 42); |
197 | } | 197 | } |
198 | ).then<int, int>([&done](int result, Async::Future<int> &future) { | 198 | ).then<int, int>([&done](int result, KAsync::Future<int> &future) { |
199 | done = true; | 199 | done = true; |
200 | future.setValue(result); | 200 | future.setValue(result); |
201 | future.setFinished(); | 201 | future.setFinished(); |
@@ -210,17 +210,17 @@ void AsyncTest::testNestedAsync() | |||
210 | { | 210 | { |
211 | bool done = false; | 211 | bool done = false; |
212 | 212 | ||
213 | auto job = Async::start<int>( | 213 | auto job = KAsync::start<int>( |
214 | [](Async::Future<int> &future) { | 214 | [](KAsync::Future<int> &future) { |
215 | auto innerJob = Async::start<int>([](Async::Future<int> &innerFuture) { | 215 | auto innerJob = KAsync::start<int>([](KAsync::Future<int> &innerFuture) { |
216 | new AsyncSimulator<int>(innerFuture, 42); | 216 | new AsyncSimulator<int>(innerFuture, 42); |
217 | }).then<void>([&future](Async::Future<void> &innerThenFuture) { | 217 | }).then<void>([&future](KAsync::Future<void> &innerThenFuture) { |
218 | future.setFinished(); | 218 | future.setFinished(); |
219 | innerThenFuture.setFinished(); | 219 | innerThenFuture.setFinished(); |
220 | }); | 220 | }); |
221 | innerJob.exec().waitForFinished(); | 221 | innerJob.exec().waitForFinished(); |
222 | } | 222 | } |
223 | ).then<int, int>([&done](int result, Async::Future<int> &future) { | 223 | ).then<int, int>([&done](int result, KAsync::Future<int> &future) { |
224 | done = true; | 224 | done = true; |
225 | future.setValue(result); | 225 | future.setValue(result); |
226 | future.setFinished(); | 226 | future.setFinished(); |
@@ -232,8 +232,8 @@ void AsyncTest::testNestedAsync() | |||
232 | 232 | ||
233 | void AsyncTest::testStartValue() | 233 | void AsyncTest::testStartValue() |
234 | { | 234 | { |
235 | auto job = Async::start<int, int>( | 235 | auto job = KAsync::start<int, int>( |
236 | [](int in, Async::Future<int> &future) { | 236 | [](int in, KAsync::Future<int> &future) { |
237 | future.setValue(in); | 237 | future.setValue(in); |
238 | future.setFinished(); | 238 | future.setFinished(); |
239 | }); | 239 | }); |
@@ -249,8 +249,8 @@ void AsyncTest::testStartValue() | |||
249 | 249 | ||
250 | void AsyncTest::testAsyncThen() | 250 | void AsyncTest::testAsyncThen() |
251 | { | 251 | { |
252 | auto job = Async::start<int>( | 252 | auto job = KAsync::start<int>( |
253 | [](Async::Future<int> &future) { | 253 | [](KAsync::Future<int> &future) { |
254 | new AsyncSimulator<int>(future, 42); | 254 | new AsyncSimulator<int>(future, 42); |
255 | }); | 255 | }); |
256 | 256 | ||
@@ -264,7 +264,7 @@ void AsyncTest::testAsyncThen() | |||
264 | 264 | ||
265 | void AsyncTest::testSyncThen() | 265 | void AsyncTest::testSyncThen() |
266 | { | 266 | { |
267 | auto job = Async::start<int>( | 267 | auto job = KAsync::start<int>( |
268 | []() -> int { | 268 | []() -> int { |
269 | return 42; | 269 | return 42; |
270 | }) | 270 | }) |
@@ -280,13 +280,13 @@ void AsyncTest::testSyncThen() | |||
280 | 280 | ||
281 | void AsyncTest::testJoinedThen() | 281 | void AsyncTest::testJoinedThen() |
282 | { | 282 | { |
283 | auto job1 = Async::start<int, int>( | 283 | auto job1 = KAsync::start<int, int>( |
284 | [](int in, Async::Future<int> &future) { | 284 | [](int in, KAsync::Future<int> &future) { |
285 | new AsyncSimulator<int>(future, in * 2); | 285 | new AsyncSimulator<int>(future, in * 2); |
286 | }); | 286 | }); |
287 | 287 | ||
288 | auto job2 = Async::start<int>( | 288 | auto job2 = KAsync::start<int>( |
289 | [](Async::Future<int> &future) { | 289 | [](KAsync::Future<int> &future) { |
290 | new AsyncSimulator<int>(future, 42); | 290 | new AsyncSimulator<int>(future, 42); |
291 | }) | 291 | }) |
292 | .then<int>(job1); | 292 | .then<int>(job1); |
@@ -302,13 +302,13 @@ void AsyncTest::testVoidThen() | |||
302 | { | 302 | { |
303 | int check = 0; | 303 | int check = 0; |
304 | 304 | ||
305 | auto job = Async::start<void>( | 305 | auto job = KAsync::start<void>( |
306 | [&check](Async::Future<void> &future) { | 306 | [&check](KAsync::Future<void> &future) { |
307 | new AsyncSimulator<void>(future); | 307 | new AsyncSimulator<void>(future); |
308 | ++check; | 308 | ++check; |
309 | }) | 309 | }) |
310 | .then<void>( | 310 | .then<void>( |
311 | [&check](Async::Future<void> &future) { | 311 | [&check](KAsync::Future<void> &future) { |
312 | new AsyncSimulator<void>(future); | 312 | new AsyncSimulator<void>(future); |
313 | ++check; | 313 | ++check; |
314 | }) | 314 | }) |
@@ -328,12 +328,12 @@ void AsyncTest::testVoidThen() | |||
328 | 328 | ||
329 | void AsyncTest::testAsyncEach() | 329 | void AsyncTest::testAsyncEach() |
330 | { | 330 | { |
331 | auto job = Async::start<QList<int>>( | 331 | auto job = KAsync::start<QList<int>>( |
332 | [](Async::Future<QList<int>> &future) { | 332 | [](KAsync::Future<QList<int>> &future) { |
333 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); | 333 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); |
334 | }) | 334 | }) |
335 | .each<QList<int>, int>( | 335 | .each<QList<int>, int>( |
336 | [](const int &v, Async::Future<QList<int>> &future) { | 336 | [](const int &v, KAsync::Future<QList<int>> &future) { |
337 | new AsyncSimulator<QList<int>>(future, { v + 1 }); | 337 | new AsyncSimulator<QList<int>>(future, { v + 1 }); |
338 | }); | 338 | }); |
339 | 339 | ||
@@ -347,7 +347,7 @@ void AsyncTest::testAsyncEach() | |||
347 | 347 | ||
348 | void AsyncTest::testSyncEach() | 348 | void AsyncTest::testSyncEach() |
349 | { | 349 | { |
350 | auto job = Async::start<QList<int>>( | 350 | auto job = KAsync::start<QList<int>>( |
351 | []() -> QList<int> { | 351 | []() -> QList<int> { |
352 | return { 1, 2, 3, 4 }; | 352 | return { 1, 2, 3, 4 }; |
353 | }) | 353 | }) |
@@ -356,7 +356,7 @@ void AsyncTest::testSyncEach() | |||
356 | return { v + 1 }; | 356 | return { v + 1 }; |
357 | }); | 357 | }); |
358 | 358 | ||
359 | Async::Future<QList<int>> future = job.exec(); | 359 | KAsync::Future<QList<int>> future = job.exec(); |
360 | 360 | ||
361 | const QList<int> expected({ 2, 3, 4, 5 }); | 361 | const QList<int> expected({ 2, 3, 4, 5 }); |
362 | QVERIFY(future.isFinished()); | 362 | QVERIFY(future.isFinished()); |
@@ -365,12 +365,12 @@ void AsyncTest::testSyncEach() | |||
365 | 365 | ||
366 | void AsyncTest::testJoinedEach() | 366 | void AsyncTest::testJoinedEach() |
367 | { | 367 | { |
368 | auto job1 = Async::start<QList<int>, int>( | 368 | auto job1 = KAsync::start<QList<int>, int>( |
369 | [](int v, Async::Future<QList<int>> &future) { | 369 | [](int v, KAsync::Future<QList<int>> &future) { |
370 | new AsyncSimulator<QList<int>>(future, { v * 2 }); | 370 | new AsyncSimulator<QList<int>>(future, { v * 2 }); |
371 | }); | 371 | }); |
372 | 372 | ||
373 | auto job = Async::start<QList<int>>( | 373 | auto job = KAsync::start<QList<int>>( |
374 | []() -> QList<int> { | 374 | []() -> QList<int> { |
375 | return { 1, 2, 3, 4 }; | 375 | return { 1, 2, 3, 4 }; |
376 | }) | 376 | }) |
@@ -387,7 +387,7 @@ void AsyncTest::testJoinedEach() | |||
387 | void AsyncTest::testVoidEachThen() | 387 | void AsyncTest::testVoidEachThen() |
388 | { | 388 | { |
389 | QList<int> check; | 389 | QList<int> check; |
390 | auto job = Async::start<QList<int>>( | 390 | auto job = KAsync::start<QList<int>>( |
391 | []() -> QList<int> { | 391 | []() -> QList<int> { |
392 | return { 1, 2, 3, 4 }; | 392 | return { 1, 2, 3, 4 }; |
393 | }).each<void, int>( | 393 | }).each<void, int>( |
@@ -406,14 +406,14 @@ void AsyncTest::testAsyncVoidEachThen() | |||
406 | { | 406 | { |
407 | bool completedJob = false; | 407 | bool completedJob = false; |
408 | QList<int> check; | 408 | QList<int> check; |
409 | auto job = Async::start<QList<int>>( | 409 | auto job = KAsync::start<QList<int>>( |
410 | [](Async::Future<QList<int> > &future) { | 410 | [](KAsync::Future<QList<int> > &future) { |
411 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); | 411 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); |
412 | }).each<void, int>( | 412 | }).each<void, int>( |
413 | [&check](const int &v, Async::Future<void> &future) { | 413 | [&check](const int &v, KAsync::Future<void> &future) { |
414 | check << v; | 414 | check << v; |
415 | new AsyncSimulator<void>(future); | 415 | new AsyncSimulator<void>(future); |
416 | }).then<void>([&completedJob](Async::Future<void> &future) { | 416 | }).then<void>([&completedJob](KAsync::Future<void> &future) { |
417 | completedJob = true; | 417 | completedJob = true; |
418 | future.setFinished(); | 418 | future.setFinished(); |
419 | }); | 419 | }); |
@@ -433,14 +433,14 @@ void AsyncTest::testAsyncVoidEachThen() | |||
433 | 433 | ||
434 | void AsyncTest::testAsyncReduce() | 434 | void AsyncTest::testAsyncReduce() |
435 | { | 435 | { |
436 | auto job = Async::start<QList<int>>( | 436 | auto job = KAsync::start<QList<int>>( |
437 | [](Async::Future<QList<int>> &future) { | 437 | [](KAsync::Future<QList<int>> &future) { |
438 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); | 438 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); |
439 | }) | 439 | }) |
440 | .reduce<int, QList<int>>( | 440 | .reduce<int, QList<int>>( |
441 | [](const QList<int> &list, Async::Future<int> &future) { | 441 | [](const QList<int> &list, KAsync::Future<int> &future) { |
442 | new AsyncSimulator<int>(future, | 442 | new AsyncSimulator<int>(future, |
443 | [list](Async::Future<int> &future) { | 443 | [list](KAsync::Future<int> &future) { |
444 | int sum = 0; | 444 | int sum = 0; |
445 | for (int i : list) sum += i; | 445 | for (int i : list) sum += i; |
446 | future.setValue(sum); | 446 | future.setValue(sum); |
@@ -449,7 +449,7 @@ void AsyncTest::testAsyncReduce() | |||
449 | ); | 449 | ); |
450 | }); | 450 | }); |
451 | 451 | ||
452 | Async::Future<int> future = job.exec(); | 452 | KAsync::Future<int> future = job.exec(); |
453 | future.waitForFinished(); | 453 | future.waitForFinished(); |
454 | 454 | ||
455 | QVERIFY(future.isFinished()); | 455 | QVERIFY(future.isFinished()); |
@@ -458,7 +458,7 @@ void AsyncTest::testAsyncReduce() | |||
458 | 458 | ||
459 | void AsyncTest::testSyncReduce() | 459 | void AsyncTest::testSyncReduce() |
460 | { | 460 | { |
461 | auto job = Async::start<QList<int>>( | 461 | auto job = KAsync::start<QList<int>>( |
462 | []() -> QList<int> { | 462 | []() -> QList<int> { |
463 | return { 1, 2, 3, 4 }; | 463 | return { 1, 2, 3, 4 }; |
464 | }) | 464 | }) |
@@ -469,7 +469,7 @@ void AsyncTest::testSyncReduce() | |||
469 | return sum; | 469 | return sum; |
470 | }); | 470 | }); |
471 | 471 | ||
472 | Async::Future<int> future = job.exec(); | 472 | KAsync::Future<int> future = job.exec(); |
473 | 473 | ||
474 | QVERIFY(future.isFinished()); | 474 | QVERIFY(future.isFinished()); |
475 | QCOMPARE(future.value(), 10); | 475 | QCOMPARE(future.value(), 10); |
@@ -478,14 +478,14 @@ void AsyncTest::testSyncReduce() | |||
478 | 478 | ||
479 | void AsyncTest::testJoinedReduce() | 479 | void AsyncTest::testJoinedReduce() |
480 | { | 480 | { |
481 | auto job1 = Async::start<int, QList<int>>( | 481 | auto job1 = KAsync::start<int, QList<int>>( |
482 | [](const QList<int> &list, Async::Future<int> &future) { | 482 | [](const QList<int> &list, KAsync::Future<int> &future) { |
483 | int sum = 0; | 483 | int sum = 0; |
484 | for (int i : list) sum += i; | 484 | for (int i : list) sum += i; |
485 | new AsyncSimulator<int>(future, sum); | 485 | new AsyncSimulator<int>(future, sum); |
486 | }); | 486 | }); |
487 | 487 | ||
488 | auto job = Async::start<QList<int>>( | 488 | auto job = KAsync::start<QList<int>>( |
489 | []() -> QList<int> { | 489 | []() -> QList<int> { |
490 | return { 1, 2, 3, 4 }; | 490 | return { 1, 2, 3, 4 }; |
491 | }) | 491 | }) |
@@ -502,7 +502,7 @@ void AsyncTest::testVoidReduce() | |||
502 | { | 502 | { |
503 | // This must not compile (reduce with void result makes no sense) | 503 | // This must not compile (reduce with void result makes no sense) |
504 | #ifdef TEST_BUILD_FAIL | 504 | #ifdef TEST_BUILD_FAIL |
505 | auto job = Async::start<QList<int>>( | 505 | auto job = KAsync::start<QList<int>>( |
506 | []() -> QList<int> { | 506 | []() -> QList<int> { |
507 | return { 1, 2, 3, 4 }; | 507 | return { 1, 2, 3, 4 }; |
508 | }) | 508 | }) |
@@ -522,8 +522,8 @@ void AsyncTest::testProgressReporting() | |||
522 | static int progress; | 522 | static int progress; |
523 | progress = 0; | 523 | progress = 0; |
524 | 524 | ||
525 | auto job = Async::start<void>( | 525 | auto job = KAsync::start<void>( |
526 | [](Async::Future<void> &f) { | 526 | [](KAsync::Future<void> &f) { |
527 | QTimer *timer = new QTimer(); | 527 | QTimer *timer = new QTimer(); |
528 | connect(timer, &QTimer::timeout, | 528 | connect(timer, &QTimer::timeout, |
529 | [&f, timer]() { | 529 | [&f, timer]() { |
@@ -538,8 +538,8 @@ void AsyncTest::testProgressReporting() | |||
538 | }); | 538 | }); |
539 | 539 | ||
540 | int progressCheck = 0; | 540 | int progressCheck = 0; |
541 | Async::FutureWatcher<void> watcher; | 541 | KAsync::FutureWatcher<void> watcher; |
542 | connect(&watcher, &Async::FutureWatcher<void>::futureProgress, | 542 | connect(&watcher, &KAsync::FutureWatcher<void>::futureProgress, |
543 | [&progressCheck](qreal progress) { | 543 | [&progressCheck](qreal progress) { |
544 | progressCheck++; | 544 | progressCheck++; |
545 | // FIXME: Don't use Q_ASSERT in unit tests | 545 | // FIXME: Don't use Q_ASSERT in unit tests |
@@ -556,9 +556,9 @@ void AsyncTest::testErrorHandler() | |||
556 | { | 556 | { |
557 | 557 | ||
558 | { | 558 | { |
559 | auto job = Async::start<int>( | 559 | auto job = KAsync::start<int>( |
560 | [](Async::Future<int> &f) { | 560 | [](KAsync::Future<int> &f) { |
561 | f.setError(1, "error"); | 561 | f.setError(1, QLatin1String("error")); |
562 | }); | 562 | }); |
563 | 563 | ||
564 | auto future = job.exec(); | 564 | auto future = job.exec(); |
@@ -569,9 +569,9 @@ void AsyncTest::testErrorHandler() | |||
569 | 569 | ||
570 | { | 570 | { |
571 | int error = 0; | 571 | int error = 0; |
572 | auto job = Async::start<int>( | 572 | auto job = KAsync::start<int>( |
573 | [](Async::Future<int> &f) { | 573 | [](KAsync::Future<int> &f) { |
574 | f.setError(1, "error"); | 574 | f.setError(1, QLatin1String("error")); |
575 | }, | 575 | }, |
576 | [&error](int errorCode, const QString &errorMessage) { | 576 | [&error](int errorCode, const QString &errorMessage) { |
577 | error += errorCode; | 577 | error += errorCode; |
@@ -590,12 +590,12 @@ void AsyncTest::testErrorPropagation() | |||
590 | { | 590 | { |
591 | int error = 0; | 591 | int error = 0; |
592 | bool called = false; | 592 | bool called = false; |
593 | auto job = Async::start<int>( | 593 | auto job = KAsync::start<int>( |
594 | [](Async::Future<int> &f) { | 594 | [](KAsync::Future<int> &f) { |
595 | f.setError(1, "error"); | 595 | f.setError(1, QLatin1String("error")); |
596 | }) | 596 | }) |
597 | .then<int, int>( | 597 | .then<int, int>( |
598 | [&called](int v, Async::Future<int> &f) { | 598 | [&called](int v, KAsync::Future<int> &f) { |
599 | called = true; | 599 | called = true; |
600 | f.setFinished(); | 600 | f.setFinished(); |
601 | }, | 601 | }, |
@@ -614,11 +614,11 @@ void AsyncTest::testErrorPropagation() | |||
614 | void AsyncTest::testErrorHandlerAsync() | 614 | void AsyncTest::testErrorHandlerAsync() |
615 | { | 615 | { |
616 | { | 616 | { |
617 | auto job = Async::start<int>( | 617 | auto job = KAsync::start<int>( |
618 | [](Async::Future<int> &f) { | 618 | [](KAsync::Future<int> &f) { |
619 | new AsyncSimulator<int>(f, | 619 | new AsyncSimulator<int>(f, |
620 | [](Async::Future<int> &f) { | 620 | [](KAsync::Future<int> &f) { |
621 | f.setError(1, "error"); | 621 | f.setError(1, QLatin1String("error")); |
622 | } | 622 | } |
623 | ); | 623 | ); |
624 | } | 624 | } |
@@ -634,11 +634,11 @@ void AsyncTest::testErrorHandlerAsync() | |||
634 | 634 | ||
635 | { | 635 | { |
636 | int error = 0; | 636 | int error = 0; |
637 | auto job = Async::start<int>( | 637 | auto job = KAsync::start<int>( |
638 | [](Async::Future<int> &f) { | 638 | [](KAsync::Future<int> &f) { |
639 | new AsyncSimulator<int>(f, | 639 | new AsyncSimulator<int>(f, |
640 | [](Async::Future<int> &f) { | 640 | [](KAsync::Future<int> &f) { |
641 | f.setError(1, "error"); | 641 | f.setError(1, QLatin1String("error")); |
642 | } | 642 | } |
643 | ); | 643 | ); |
644 | }, | 644 | }, |
@@ -661,16 +661,16 @@ void AsyncTest::testErrorPropagationAsync() | |||
661 | { | 661 | { |
662 | int error = 0; | 662 | int error = 0; |
663 | bool called = false; | 663 | bool called = false; |
664 | auto job = Async::start<int>( | 664 | auto job = KAsync::start<int>( |
665 | [](Async::Future<int> &f) { | 665 | [](KAsync::Future<int> &f) { |
666 | new AsyncSimulator<int>(f, | 666 | new AsyncSimulator<int>(f, |
667 | [](Async::Future<int> &f) { | 667 | [](KAsync::Future<int> &f) { |
668 | f.setError(1, "error"); | 668 | f.setError(1, QLatin1String("error")); |
669 | } | 669 | } |
670 | ); | 670 | ); |
671 | }) | 671 | }) |
672 | .then<int, int>( | 672 | .then<int, int>( |
673 | [&called](int v, Async::Future<int> &f) { | 673 | [&called](int v, KAsync::Future<int> &f) { |
674 | called = true; | 674 | called = true; |
675 | f.setFinished(); | 675 | f.setFinished(); |
676 | }, | 676 | }, |
@@ -692,9 +692,9 @@ void AsyncTest::testErrorPropagationAsync() | |||
692 | void AsyncTest::testNestedErrorPropagation() | 692 | void AsyncTest::testNestedErrorPropagation() |
693 | { | 693 | { |
694 | int error = 0; | 694 | int error = 0; |
695 | auto job = Async::start<void>([](){}) | 695 | auto job = KAsync::start<void>([](){}) |
696 | .then<void>(Async::error<void>(1, "error")) //Nested job that throws error | 696 | .then<void>(KAsync::error<void>(1, QLatin1String("error"))) //Nested job that throws error |
697 | .then<void>([](Async::Future<void> &future) { | 697 | .then<void>([](KAsync::Future<void> &future) { |
698 | //We should never get here | 698 | //We should never get here |
699 | Q_ASSERT(false); | 699 | Q_ASSERT(false); |
700 | }, | 700 | }, |
@@ -719,8 +719,8 @@ void AsyncTest::testChainingRunningJob() | |||
719 | { | 719 | { |
720 | int check = 0; | 720 | int check = 0; |
721 | 721 | ||
722 | auto job = Async::start<int>( | 722 | auto job = KAsync::start<int>( |
723 | [&check](Async::Future<int> &future) { | 723 | [&check](KAsync::Future<int> &future) { |
724 | QTimer *timer = new QTimer(); | 724 | QTimer *timer = new QTimer(); |
725 | QObject::connect(timer, &QTimer::timeout, | 725 | QObject::connect(timer, &QTimer::timeout, |
726 | [&future, &check]() { | 726 | [&future, &check]() { |
@@ -763,7 +763,7 @@ void AsyncTest::testChainingFinishedJob() | |||
763 | { | 763 | { |
764 | int check = 0; | 764 | int check = 0; |
765 | 765 | ||
766 | auto job = Async::start<int>( | 766 | auto job = KAsync::start<int>( |
767 | [&check]() -> int { | 767 | [&check]() -> int { |
768 | ++check; | 768 | ++check; |
769 | return 42; | 769 | return 42; |
@@ -799,7 +799,7 @@ void AsyncTest::testLifetimeWithoutHandle() | |||
799 | { | 799 | { |
800 | bool done = false; | 800 | bool done = false; |
801 | { | 801 | { |
802 | auto job = Async::start<void>([&done](Async::Future<void> &future) { | 802 | auto job = KAsync::start<void>([&done](KAsync::Future<void> &future) { |
803 | QTimer *timer = new QTimer(); | 803 | QTimer *timer = new QTimer(); |
804 | QObject::connect(timer, &QTimer::timeout, | 804 | QObject::connect(timer, &QTimer::timeout, |
805 | [&future, &done]() { | 805 | [&future, &done]() { |
@@ -823,9 +823,9 @@ void AsyncTest::testLifetimeWithoutHandle() | |||
823 | */ | 823 | */ |
824 | void AsyncTest::testLifetimeWithHandle() | 824 | void AsyncTest::testLifetimeWithHandle() |
825 | { | 825 | { |
826 | Async::Future<void> future; | 826 | KAsync::Future<void> future; |
827 | { | 827 | { |
828 | auto job = Async::start<void>([](Async::Future<void> &future) { | 828 | auto job = KAsync::start<void>([](KAsync::Future<void> &future) { |
829 | QTimer *timer = new QTimer(); | 829 | QTimer *timer = new QTimer(); |
830 | QObject::connect(timer, &QTimer::timeout, | 830 | QObject::connect(timer, &QTimer::timeout, |
831 | [&future]() { | 831 | [&future]() { |
@@ -844,7 +844,7 @@ void AsyncTest::testLifetimeWithHandle() | |||
844 | 844 | ||
845 | void AsyncTest::benchmarkSyncThenExecutor() | 845 | void AsyncTest::benchmarkSyncThenExecutor() |
846 | { | 846 | { |
847 | auto job = Async::start<int>( | 847 | auto job = KAsync::start<int>( |
848 | []() -> int { | 848 | []() -> int { |
849 | return 0; | 849 | return 0; |
850 | }); | 850 | }); |
diff --git a/async/autotests/kjobtest.cpp b/async/autotests/kjobtest.cpp index be92d68..15e50de 100644 --- a/async/autotests/kjobtest.cpp +++ b/async/autotests/kjobtest.cpp | |||
@@ -43,7 +43,7 @@ private Q_SLOTS: | |||
43 | 43 | ||
44 | void KJobTest::testSingleKJob() | 44 | void KJobTest::testSingleKJob() |
45 | { | 45 | { |
46 | auto job = Async::start<int, TestKJob, &TestKJob::result, int>(); | 46 | auto job = KAsync::start<int, TestKJob, &TestKJob::result, int>(); |
47 | 47 | ||
48 | auto future = job.exec(42); | 48 | auto future = job.exec(42); |
49 | future.waitForFinished(); | 49 | future.waitForFinished(); |
@@ -54,7 +54,7 @@ void KJobTest::testSingleKJob() | |||
54 | 54 | ||
55 | void KJobTest::testKJobChain() | 55 | void KJobTest::testKJobChain() |
56 | { | 56 | { |
57 | auto job = Async::start<int, TestKJob, &TestKJob::result, int>() | 57 | auto job = KAsync::start<int, TestKJob, &TestKJob::result, int>() |
58 | .then<int, TestKJob, &TestKJob::result, int>(); | 58 | .then<int, TestKJob, &TestKJob::result, int>(); |
59 | 59 | ||
60 | auto future = job.exec(42); | 60 | auto future = job.exec(42); |
diff --git a/async/src/async.cpp b/async/src/async.cpp index 0e86a84..c57c9ad 100644 --- a/async/src/async.cpp +++ b/async/src/async.cpp | |||
@@ -22,7 +22,7 @@ | |||
22 | #include <QEventLoop> | 22 | #include <QEventLoop> |
23 | #include <QTimer> | 23 | #include <QTimer> |
24 | 24 | ||
25 | using namespace Async; | 25 | using namespace KAsync; |
26 | 26 | ||
27 | Private::Execution::Execution(const Private::ExecutorBasePtr &executor) | 27 | Private::Execution::Execution(const Private::ExecutorBasePtr &executor) |
28 | : executor(executor) | 28 | : executor(executor) |
@@ -104,11 +104,11 @@ static void asyncWhile(const std::function<void(std::function<void(bool)>)> &bod | |||
104 | }); | 104 | }); |
105 | } | 105 | } |
106 | 106 | ||
107 | Job<void> Async::dowhile(Condition condition, ThenTask<void> body) | 107 | Job<void> KAsync::dowhile(Condition condition, ThenTask<void> body) |
108 | { | 108 | { |
109 | return Async::start<void>([body, condition](Async::Future<void> &future) { | 109 | return KAsync::start<void>([body, condition](KAsync::Future<void> &future) { |
110 | asyncWhile([condition, body](std::function<void(bool)> whileCallback) { | 110 | asyncWhile([condition, body](std::function<void(bool)> whileCallback) { |
111 | Async::start<void>(body).then<void>([whileCallback, condition]() { | 111 | KAsync::start<void>(body).then<void>([whileCallback, condition]() { |
112 | whileCallback(!condition()); | 112 | whileCallback(!condition()); |
113 | }).exec(); | 113 | }).exec(); |
114 | }, | 114 | }, |
@@ -118,11 +118,11 @@ Job<void> Async::dowhile(Condition condition, ThenTask<void> body) | |||
118 | }); | 118 | }); |
119 | } | 119 | } |
120 | 120 | ||
121 | Job<void> Async::dowhile(ThenTask<bool> body) | 121 | Job<void> KAsync::dowhile(ThenTask<bool> body) |
122 | { | 122 | { |
123 | return Async::start<void>([body](Async::Future<void> &future) { | 123 | return KAsync::start<void>([body](KAsync::Future<void> &future) { |
124 | asyncWhile([body](std::function<void(bool)> whileCallback) { | 124 | asyncWhile([body](std::function<void(bool)> whileCallback) { |
125 | Async::start<bool>(body).then<bool, bool>([whileCallback](bool result) { | 125 | KAsync::start<bool>(body).then<bool, bool>([whileCallback](bool result) { |
126 | whileCallback(!result); | 126 | whileCallback(!result); |
127 | //FIXME this return value is only required because .then<bool, void> doesn't work | 127 | //FIXME this return value is only required because .then<bool, void> doesn't work |
128 | return true; | 128 | return true; |
@@ -134,10 +134,10 @@ Job<void> Async::dowhile(ThenTask<bool> body) | |||
134 | }); | 134 | }); |
135 | } | 135 | } |
136 | 136 | ||
137 | Job<void> Async::wait(int delay) | 137 | Job<void> KAsync::wait(int delay) |
138 | { | 138 | { |
139 | auto timer = QSharedPointer<QTimer>::create(); | 139 | auto timer = QSharedPointer<QTimer>::create(); |
140 | return Async::start<void>([timer, delay](Async::Future<void> &future) { | 140 | return KAsync::start<void>([timer, delay](KAsync::Future<void> &future) { |
141 | timer->setSingleShot(true); | 141 | timer->setSingleShot(true); |
142 | QObject::connect(timer.data(), &QTimer::timeout, [&future]() { | 142 | QObject::connect(timer.data(), &QTimer::timeout, [&future]() { |
143 | future.setFinished(); | 143 | future.setFinished(); |
diff --git a/async/src/async.h b/async/src/async.h index b1f1121..152f98e 100644 --- a/async/src/async.h +++ b/async/src/async.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright 2014 Daniel Vrátil <dvratil@redhat.com> | 2 | * Copyright 2014 - 2015 Daniel Vrátil <dvratil@redhat.com> |
3 | * | 3 | * |
4 | * This library is free software; you can redistribute it and/or | 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 | 5 | * modify it under the terms of the GNU Library General Public License as |
@@ -15,8 +15,10 @@ | |||
15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. | 15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ | 16 | */ |
17 | 17 | ||
18 | #ifndef ASYNC_H | 18 | #ifndef KASYNC_H |
19 | #define ASYNC_H | 19 | #define KASYNC_H |
20 | |||
21 | #include "kasync_export.h" | ||
20 | 22 | ||
21 | #include <functional> | 23 | #include <functional> |
22 | #include <list> | 24 | #include <list> |
@@ -64,7 +66,7 @@ | |||
64 | * TODO: Possibility to abort a job through future (perhaps optional?) | 66 | * TODO: Possibility to abort a job through future (perhaps optional?) |
65 | * TODO: Support for timeout, specified during exec call, after which the error handler gets called with a defined errorCode. | 67 | * TODO: Support for timeout, specified during exec call, after which the error handler gets called with a defined errorCode. |
66 | */ | 68 | */ |
67 | namespace Async { | 69 | namespace KAsync { |
68 | 70 | ||
69 | template<typename PrevOut, typename Out, typename ... In> | 71 | template<typename PrevOut, typename Out, typename ... In> |
70 | class Executor; | 72 | class Executor; |
@@ -75,15 +77,15 @@ template<typename Out, typename ... In> | |||
75 | class Job; | 77 | class Job; |
76 | 78 | ||
77 | template<typename Out, typename ... In> | 79 | template<typename Out, typename ... In> |
78 | using ThenTask = typename detail::identity<std::function<void(In ..., Async::Future<Out>&)>>::type; | 80 | using ThenTask = typename detail::identity<std::function<void(In ..., KAsync::Future<Out>&)>>::type; |
79 | template<typename Out, typename ... In> | 81 | template<typename Out, typename ... In> |
80 | using SyncThenTask = typename detail::identity<std::function<Out(In ...)>>::type; | 82 | using SyncThenTask = typename detail::identity<std::function<Out(In ...)>>::type; |
81 | template<typename Out, typename In> | 83 | template<typename Out, typename In> |
82 | using EachTask = typename detail::identity<std::function<void(In, Async::Future<Out>&)>>::type; | 84 | using EachTask = typename detail::identity<std::function<void(In, KAsync::Future<Out>&)>>::type; |
83 | template<typename Out, typename In> | 85 | template<typename Out, typename In> |
84 | using SyncEachTask = typename detail::identity<std::function<Out(In)>>::type; | 86 | using SyncEachTask = typename detail::identity<std::function<Out(In)>>::type; |
85 | template<typename Out, typename In> | 87 | template<typename Out, typename In> |
86 | using ReduceTask = typename detail::identity<std::function<void(In, Async::Future<Out>&)>>::type; | 88 | using ReduceTask = typename detail::identity<std::function<void(In, KAsync::Future<Out>&)>>::type; |
87 | template<typename Out, typename In> | 89 | template<typename Out, typename In> |
88 | using SyncReduceTask = typename detail::identity<std::function<Out(In)>>::type; | 90 | using SyncReduceTask = typename detail::identity<std::function<Out(In)>>::type; |
89 | 91 | ||
@@ -96,15 +98,15 @@ namespace Private | |||
96 | class ExecutorBase; | 98 | class ExecutorBase; |
97 | typedef QSharedPointer<ExecutorBase> ExecutorBasePtr; | 99 | typedef QSharedPointer<ExecutorBase> ExecutorBasePtr; |
98 | 100 | ||
99 | struct Execution { | 101 | struct KASYNC_EXPORT Execution { |
100 | Execution(const ExecutorBasePtr &executor); | 102 | Execution(const ExecutorBasePtr &executor); |
101 | ~Execution(); | 103 | ~Execution(); |
102 | void setFinished(); | 104 | void setFinished(); |
103 | 105 | ||
104 | template<typename T> | 106 | template<typename T> |
105 | Async::Future<T>* result() const | 107 | KAsync::Future<T>* result() const |
106 | { | 108 | { |
107 | return static_cast<Async::Future<T>*>(resultBase); | 109 | return static_cast<KAsync::Future<T>*>(resultBase); |
108 | } | 110 | } |
109 | 111 | ||
110 | void releaseFuture(); | 112 | void releaseFuture(); |
@@ -125,16 +127,16 @@ struct Execution { | |||
125 | 127 | ||
126 | typedef QSharedPointer<Execution> ExecutionPtr; | 128 | typedef QSharedPointer<Execution> ExecutionPtr; |
127 | 129 | ||
128 | class ExecutorBase | 130 | class KASYNC_EXPORT ExecutorBase |
129 | { | 131 | { |
130 | template<typename PrevOut, typename Out, typename ... In> | 132 | template<typename PrevOut, typename Out, typename ... In> |
131 | friend class Executor; | 133 | friend class Executor; |
132 | 134 | ||
133 | template<typename Out, typename ... In> | 135 | template<typename Out, typename ... In> |
134 | friend class Async::Job; | 136 | friend class KAsync::Job; |
135 | 137 | ||
136 | friend class Execution; | 138 | friend class Execution; |
137 | friend class Async::Tracer; | 139 | friend class KAsync::Tracer; |
138 | 140 | ||
139 | public: | 141 | public: |
140 | virtual ~ExecutorBase(); | 142 | virtual ~ExecutorBase(); |
@@ -144,7 +146,7 @@ protected: | |||
144 | ExecutorBase(const ExecutorBasePtr &parent); | 146 | ExecutorBase(const ExecutorBasePtr &parent); |
145 | 147 | ||
146 | template<typename T> | 148 | template<typename T> |
147 | Async::Future<T>* createFuture(const ExecutionPtr &execution) const; | 149 | KAsync::Future<T>* createFuture(const ExecutionPtr &execution) const; |
148 | 150 | ||
149 | virtual bool hasErrorFunc() const = 0; | 151 | virtual bool hasErrorFunc() const = 0; |
150 | virtual bool handleError(const ExecutionPtr &execution) = 0; | 152 | virtual bool handleError(const ExecutionPtr &execution) = 0; |
@@ -193,7 +195,7 @@ public: | |||
193 | void run(const ExecutionPtr &execution); | 195 | void run(const ExecutionPtr &execution); |
194 | private: | 196 | private: |
195 | EachTask<Out, In> mFunc; | 197 | EachTask<Out, In> mFunc; |
196 | QVector<Async::FutureWatcher<Out>*> mFutureWatchers; | 198 | QVector<KAsync::FutureWatcher<Out>*> mFutureWatchers; |
197 | }; | 199 | }; |
198 | 200 | ||
199 | template<typename Out, typename In> | 201 | template<typename Out, typename In> |
@@ -234,8 +236,8 @@ public: | |||
234 | SyncEachExecutor(SyncEachTask<Out, In> each, ErrorHandler errorFunc, const ExecutorBasePtr &parent); | 236 | SyncEachExecutor(SyncEachTask<Out, In> each, ErrorHandler errorFunc, const ExecutorBasePtr &parent); |
235 | void run(const ExecutionPtr &execution); | 237 | void run(const ExecutionPtr &execution); |
236 | private: | 238 | private: |
237 | void run(Async::Future<Out> *future, const typename PrevOut::value_type &arg, std::false_type); // !std::is_void<Out> | 239 | void run(KAsync::Future<Out> *future, const typename PrevOut::value_type &arg, std::false_type); // !std::is_void<Out> |
238 | void run(Async::Future<Out> *future, const typename PrevOut::value_type &arg, std::true_type); // std::is_void<Out> | 240 | void run(KAsync::Future<Out> *future, const typename PrevOut::value_type &arg, std::true_type); // std::is_void<Out> |
239 | SyncEachTask<Out, In> mFunc; | 241 | SyncEachTask<Out, In> mFunc; |
240 | }; | 242 | }; |
241 | 243 | ||
@@ -244,11 +246,11 @@ private: | |||
244 | /** | 246 | /** |
245 | * Start an asynchronous job sequence. | 247 | * Start an asynchronous job sequence. |
246 | * | 248 | * |
247 | * Async::start() is your starting point to build a chain of jobs to be executed | 249 | * KAsync::start() is your starting point to build a chain of jobs to be executed |
248 | * asynchronously. | 250 | * asynchronously. |
249 | * | 251 | * |
250 | * @param func An asynchronous function to be executed. The function must have | 252 | * @param func An asynchronous function to be executed. The function must have |
251 | * void return type, and accept exactly one argument of type @p Async::Future<In>, | 253 | * void return type, and accept exactly one argument of type @p KAsync::Future<In>, |
252 | * where @p In is type of the result. | 254 | * where @p In is type of the result. |
253 | */ | 255 | */ |
254 | template<typename Out, typename ... In> | 256 | template<typename Out, typename ... In> |
@@ -267,14 +269,14 @@ Job<ReturnType, Args ...> start(); | |||
267 | * | 269 | * |
268 | * The loop continues while @param condition returns true. | 270 | * The loop continues while @param condition returns true. |
269 | */ | 271 | */ |
270 | Job<void> dowhile(Condition condition, ThenTask<void> func); | 272 | KASYNC_EXPORT Job<void> dowhile(Condition condition, ThenTask<void> func); |
271 | 273 | ||
272 | /** | 274 | /** |
273 | * Async while loop. | 275 | * Async while loop. |
274 | * | 276 | * |
275 | * Loop continues while body returns true. | 277 | * Loop continues while body returns true. |
276 | */ | 278 | */ |
277 | Job<void> dowhile(ThenTask<bool> body); | 279 | KASYNC_EXPORT Job<void> dowhile(ThenTask<bool> body); |
278 | 280 | ||
279 | /** | 281 | /** |
280 | * Iterate over a container. | 282 | * Iterate over a container. |
@@ -287,7 +289,7 @@ Job<Out> iterate(const Out &container); | |||
287 | /** | 289 | /** |
288 | * Async delay. | 290 | * Async delay. |
289 | */ | 291 | */ |
290 | Job<void> wait(int delay); | 292 | KASYNC_EXPORT Job<void> wait(int delay); |
291 | 293 | ||
292 | /** | 294 | /** |
293 | * A null job. | 295 | * A null job. |
@@ -307,7 +309,7 @@ Job<Out> null(); | |||
307 | template<typename Out> | 309 | template<typename Out> |
308 | Job<Out> error(int errorCode = 1, const QString &errorMessage = QString()); | 310 | Job<Out> error(int errorCode = 1, const QString &errorMessage = QString()); |
309 | 311 | ||
310 | class JobBase | 312 | class KASYNC_EXPORT JobBase |
311 | { | 313 | { |
312 | template<typename Out, typename ... In> | 314 | template<typename Out, typename ... In> |
313 | friend class Job; | 315 | friend class Job; |
@@ -324,18 +326,18 @@ protected: | |||
324 | * An Asynchronous job | 326 | * An Asynchronous job |
325 | * | 327 | * |
326 | * A single instance of Job represents a single method that will be executed | 328 | * A single instance of Job represents a single method that will be executed |
327 | * asynchrously. The Job is started by @p Job::exec(), which returns @p Async::Future | 329 | * asynchrously. The Job is started by @p Job::exec(), which returns @p KAsync::Future |
328 | * immediatelly. The Future will be set to finished state once the asynchronous | 330 | * immediatelly. The Future will be set to finished state once the asynchronous |
329 | * task has finished. You can use @p Async::Future::waitForFinished() to wait for | 331 | * task has finished. You can use @p KAsync::Future::waitForFinished() to wait for |
330 | * for the Future in blocking manner. | 332 | * for the Future in blocking manner. |
331 | * | 333 | * |
332 | * It is possible to chain multiple Jobs one after another in different fashion | 334 | * It is possible to chain multiple Jobs one after another in different fashion |
333 | * (sequential, parallel, etc.). Calling Job::exec() will then return a pending | 335 | * (sequential, parallel, etc.). Calling Job::exec() will then return a pending |
334 | * @p Async::Future, and will execute the entire chain of jobs. | 336 | * @p KAsync::Future, and will execute the entire chain of jobs. |
335 | * | 337 | * |
336 | * @code | 338 | * @code |
337 | * auto job = Job::start<QList<int>>( | 339 | * auto job = Job::start<QList<int>>( |
338 | * [](Async::Future<QList<int>> &future) { | 340 | * [](KAsync::Future<QList<int>> &future) { |
339 | * MyREST::PendingUsers *pu = MyREST::requestListOfUsers(); | 341 | * MyREST::PendingUsers *pu = MyREST::requestListOfUsers(); |
340 | * QObject::connect(pu, &PendingOperation::finished, | 342 | * QObject::connect(pu, &PendingOperation::finished, |
341 | * [&](PendingOperation *pu) { | 343 | * [&](PendingOperation *pu) { |
@@ -344,7 +346,7 @@ protected: | |||
344 | * }); | 346 | * }); |
345 | * }) | 347 | * }) |
346 | * .each<QList<MyREST::User>, int>( | 348 | * .each<QList<MyREST::User>, int>( |
347 | * [](const int &userId, Async::Future<QList<MyREST::User>> &future) { | 349 | * [](const int &userId, KAsync::Future<QList<MyREST::User>> &future) { |
348 | * MyREST::PendingUser *pu = MyREST::requestUserDetails(userId); | 350 | * MyREST::PendingUser *pu = MyREST::requestUserDetails(userId); |
349 | * QObject::connect(pu, &PendingOperation::finished, | 351 | * QObject::connect(pu, &PendingOperation::finished, |
350 | * [&](PendingOperation *pu) { | 352 | * [&](PendingOperation *pu) { |
@@ -353,7 +355,7 @@ protected: | |||
353 | * }); | 355 | * }); |
354 | * }); | 356 | * }); |
355 | * | 357 | * |
356 | * Async::Future<QList<MyREST::User>> usersFuture = job.exec(); | 358 | * KAsync::Future<QList<MyREST::User>> usersFuture = job.exec(); |
357 | * usersFuture.waitForFinished(); | 359 | * usersFuture.waitForFinished(); |
358 | * QList<MyRest::User> users = usersFuture.value(); | 360 | * QList<MyRest::User> users = usersFuture.value(); |
359 | * @endcode | 361 | * @endcode |
@@ -369,10 +371,10 @@ class Job : public JobBase | |||
369 | friend class Job; | 371 | friend class Job; |
370 | 372 | ||
371 | template<typename OutOther, typename ... InOther> | 373 | template<typename OutOther, typename ... InOther> |
372 | friend Job<OutOther, InOther ...> start(Async::ThenTask<OutOther, InOther ...> func, ErrorHandler errorFunc); | 374 | friend Job<OutOther, InOther ...> start(KAsync::ThenTask<OutOther, InOther ...> func, ErrorHandler errorFunc); |
373 | 375 | ||
374 | template<typename OutOther, typename ... InOther> | 376 | template<typename OutOther, typename ... InOther> |
375 | friend Job<OutOther, InOther ...> start(Async::SyncThenTask<OutOther, InOther ...> func, ErrorHandler errorFunc); | 377 | friend Job<OutOther, InOther ...> start(KAsync::SyncThenTask<OutOther, InOther ...> func, ErrorHandler errorFunc); |
376 | 378 | ||
377 | #ifdef WITH_KJOB | 379 | #ifdef WITH_KJOB |
378 | template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResultMethod)(), typename ... Args> | 380 | template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResultMethod)(), typename ... Args> |
@@ -454,7 +456,7 @@ public: | |||
454 | } | 456 | } |
455 | 457 | ||
456 | template<typename FirstIn> | 458 | template<typename FirstIn> |
457 | Async::Future<Out> exec(FirstIn in) | 459 | KAsync::Future<Out> exec(FirstIn in) |
458 | { | 460 | { |
459 | // Inject a fake sync executor that will return the initial value | 461 | // Inject a fake sync executor that will return the initial value |
460 | Private::ExecutorBasePtr first = mExecutor; | 462 | Private::ExecutorBasePtr first = mExecutor; |
@@ -474,10 +476,10 @@ public: | |||
474 | return result; | 476 | return result; |
475 | } | 477 | } |
476 | 478 | ||
477 | Async::Future<Out> exec() | 479 | KAsync::Future<Out> exec() |
478 | { | 480 | { |
479 | Private::ExecutionPtr execution = mExecutor->exec(mExecutor); | 481 | Private::ExecutionPtr execution = mExecutor->exec(mExecutor); |
480 | Async::Future<Out> result = *execution->result<Out>(); | 482 | KAsync::Future<Out> result = *execution->result<Out>(); |
481 | 483 | ||
482 | return result; | 484 | return result; |
483 | } | 485 | } |
@@ -499,15 +501,15 @@ private: | |||
499 | template<typename InOther> | 501 | template<typename InOther> |
500 | void reduceInvariants() | 502 | void reduceInvariants() |
501 | { | 503 | { |
502 | static_assert(Async::detail::isIterable<Out>::value, | 504 | static_assert(KAsync::detail::isIterable<Out>::value, |
503 | "The 'Result' task can only be connected to a job that returns a list or an array"); | 505 | "The 'Result' task can only be connected to a job that returns a list or an array"); |
504 | static_assert(std::is_same<typename Out::value_type, typename InOther::value_type>::value, | 506 | static_assert(std::is_same<typename Out::value_type, typename InOther::value_type>::value, |
505 | "The return type of previous task must be compatible with input type of this task"); | 507 | "The return type of previous task must be compatible with input type of this task"); |
506 | } | 508 | } |
507 | 509 | ||
508 | template<typename OutOther, typename ... InOther> | 510 | template<typename OutOther, typename ... InOther> |
509 | inline std::function<void(InOther ..., Async::Future<OutOther>&)> nestedJobWrapper(Job<OutOther, InOther ...> otherJob) { | 511 | inline std::function<void(InOther ..., KAsync::Future<OutOther>&)> nestedJobWrapper(Job<OutOther, InOther ...> otherJob) { |
510 | return [otherJob](InOther ... in, Async::Future<OutOther> &future) { | 512 | return [otherJob](InOther ... in, KAsync::Future<OutOther> &future) { |
511 | // copy by value is const | 513 | // copy by value is const |
512 | auto job = otherJob; | 514 | auto job = otherJob; |
513 | FutureWatcher<OutOther> *watcher = new FutureWatcher<OutOther>(); | 515 | FutureWatcher<OutOther> *watcher = new FutureWatcher<OutOther>(); |
@@ -518,7 +520,7 @@ private: | |||
518 | // in copyFutureValue() | 520 | // in copyFutureValue() |
519 | // copy by value is const | 521 | // copy by value is const |
520 | auto outFuture = future; | 522 | auto outFuture = future; |
521 | Async::detail::copyFutureValue(watcher->future(), outFuture); | 523 | KAsync::detail::copyFutureValue(watcher->future(), outFuture); |
522 | if (watcher->future().errorCode()) { | 524 | if (watcher->future().errorCode()) { |
523 | outFuture.setError(watcher->future().errorCode(), watcher->future().errorMessage()); | 525 | outFuture.setError(watcher->future().errorCode(), watcher->future().errorMessage()); |
524 | } else { | 526 | } else { |
@@ -531,12 +533,12 @@ private: | |||
531 | } | 533 | } |
532 | }; | 534 | }; |
533 | 535 | ||
534 | } // namespace Async | 536 | } // namespace KAsync |
535 | 537 | ||
536 | 538 | ||
537 | // ********** Out of line definitions **************** | 539 | // ********** Out of line definitions **************** |
538 | 540 | ||
539 | namespace Async { | 541 | namespace KAsync { |
540 | 542 | ||
541 | template<typename Out, typename ... In> | 543 | template<typename Out, typename ... In> |
542 | Job<Out, In ...> start(ThenTask<Out, In ...> func, ErrorHandler error) | 544 | Job<Out, In ...> start(ThenTask<Out, In ...> func, ErrorHandler error) |
@@ -557,7 +559,7 @@ template<typename ReturnType, typename KJobType, ReturnType (KJobType::*KJobResu | |||
557 | Job<ReturnType, Args ...> start() | 559 | Job<ReturnType, Args ...> start() |
558 | { | 560 | { |
559 | return Job<ReturnType, Args ...>(Private::ExecutorBasePtr( | 561 | return Job<ReturnType, Args ...>(Private::ExecutorBasePtr( |
560 | new Private::ThenExecutor<ReturnType, Args ...>([](const Args & ... args, Async::Future<ReturnType> &future) | 562 | new Private::ThenExecutor<ReturnType, Args ...>([](const Args & ... args, KAsync::Future<ReturnType> &future) |
561 | { | 563 | { |
562 | KJobType *job = new KJobType(args ...); | 564 | KJobType *job = new KJobType(args ...); |
563 | job->connect(job, &KJob::finished, | 565 | job->connect(job, &KJob::finished, |
@@ -578,8 +580,8 @@ Job<ReturnType, Args ...> start() | |||
578 | template<typename Out> | 580 | template<typename Out> |
579 | Job<Out> null() | 581 | Job<Out> null() |
580 | { | 582 | { |
581 | return Async::start<Out>( | 583 | return KAsync::start<Out>( |
582 | [](Async::Future<Out> &future) { | 584 | [](KAsync::Future<Out> &future) { |
583 | future.setFinished(); | 585 | future.setFinished(); |
584 | }); | 586 | }); |
585 | } | 587 | } |
@@ -587,8 +589,8 @@ Job<Out> null() | |||
587 | template<typename Out> | 589 | template<typename Out> |
588 | Job<Out> error(int errorCode, const QString &errorMessage) | 590 | Job<Out> error(int errorCode, const QString &errorMessage) |
589 | { | 591 | { |
590 | return Async::start<Out>( | 592 | return KAsync::start<Out>( |
591 | [errorCode, errorMessage](Async::Future<Out> &future) { | 593 | [errorCode, errorMessage](KAsync::Future<Out> &future) { |
592 | future.setError(errorCode, errorMessage); | 594 | future.setError(errorCode, errorMessage); |
593 | }); | 595 | }); |
594 | } | 596 | } |
@@ -596,7 +598,7 @@ Job<Out> error(int errorCode, const QString &errorMessage) | |||
596 | template<typename Out> | 598 | template<typename Out> |
597 | Job<Out> iterate(const Out &container) | 599 | Job<Out> iterate(const Out &container) |
598 | { | 600 | { |
599 | return Async::start<Out>( | 601 | return KAsync::start<Out>( |
600 | [container]() { | 602 | [container]() { |
601 | return container; | 603 | return container; |
602 | }); | 604 | }); |
@@ -606,9 +608,9 @@ Job<Out> iterate(const Out &container) | |||
606 | namespace Private { | 608 | namespace Private { |
607 | 609 | ||
608 | template<typename T> | 610 | template<typename T> |
609 | Async::Future<T>* ExecutorBase::createFuture(const ExecutionPtr &execution) const | 611 | KAsync::Future<T>* ExecutorBase::createFuture(const ExecutionPtr &execution) const |
610 | { | 612 | { |
611 | return new Async::Future<T>(execution); | 613 | return new KAsync::Future<T>(execution); |
612 | } | 614 | } |
613 | 615 | ||
614 | template<typename PrevOut, typename Out, typename ... In> | 616 | template<typename PrevOut, typename Out, typename ... In> |
@@ -625,8 +627,8 @@ ExecutionPtr Executor<PrevOut, Out, In ...>::exec(const ExecutorBasePtr &self) | |||
625 | execution->prevExecution = mPrev ? mPrev->exec(mPrev) : ExecutionPtr(); | 627 | execution->prevExecution = mPrev ? mPrev->exec(mPrev) : ExecutionPtr(); |
626 | 628 | ||
627 | execution->resultBase = ExecutorBase::createFuture<Out>(execution); | 629 | execution->resultBase = ExecutorBase::createFuture<Out>(execution); |
628 | auto fw = new Async::FutureWatcher<Out>(); | 630 | auto fw = new KAsync::FutureWatcher<Out>(); |
629 | QObject::connect(fw, &Async::FutureWatcher<Out>::futureReady, | 631 | QObject::connect(fw, &KAsync::FutureWatcher<Out>::futureReady, |
630 | [fw, execution, this]() { | 632 | [fw, execution, this]() { |
631 | handleError(execution); | 633 | handleError(execution); |
632 | execution->setFinished(); | 634 | execution->setFinished(); |
@@ -634,7 +636,7 @@ ExecutionPtr Executor<PrevOut, Out, In ...>::exec(const ExecutorBasePtr &self) | |||
634 | }); | 636 | }); |
635 | fw->setFuture(*execution->result<Out>()); | 637 | fw->setFuture(*execution->result<Out>()); |
636 | 638 | ||
637 | Async::Future<PrevOut> *prevFuture = execution->prevExecution ? execution->prevExecution->result<PrevOut>() : nullptr; | 639 | KAsync::Future<PrevOut> *prevFuture = execution->prevExecution ? execution->prevExecution->result<PrevOut>() : nullptr; |
638 | if (!prevFuture || prevFuture->isFinished()) { | 640 | if (!prevFuture || prevFuture->isFinished()) { |
639 | if (prevFuture) { // prevFuture implies execution->prevExecution | 641 | if (prevFuture) { // prevFuture implies execution->prevExecution |
640 | if (prevFuture->errorCode()) { | 642 | if (prevFuture->errorCode()) { |
@@ -655,8 +657,8 @@ ExecutionPtr Executor<PrevOut, Out, In ...>::exec(const ExecutorBasePtr &self) | |||
655 | execution->isRunning = true; | 657 | execution->isRunning = true; |
656 | run(execution); | 658 | run(execution); |
657 | } else { | 659 | } else { |
658 | auto prevFutureWatcher = new Async::FutureWatcher<PrevOut>(); | 660 | auto prevFutureWatcher = new KAsync::FutureWatcher<PrevOut>(); |
659 | QObject::connect(prevFutureWatcher, &Async::FutureWatcher<PrevOut>::futureReady, | 661 | QObject::connect(prevFutureWatcher, &KAsync::FutureWatcher<PrevOut>::futureReady, |
660 | [prevFutureWatcher, execution, this]() { | 662 | [prevFutureWatcher, execution, this]() { |
661 | auto prevFuture = prevFutureWatcher->future(); | 663 | auto prevFuture = prevFutureWatcher->future(); |
662 | assert(prevFuture.isFinished()); | 664 | assert(prevFuture.isFinished()); |
@@ -679,7 +681,7 @@ ExecutionPtr Executor<PrevOut, Out, In ...>::exec(const ExecutorBasePtr &self) | |||
679 | run(execution); | 681 | run(execution); |
680 | }); | 682 | }); |
681 | 683 | ||
682 | prevFutureWatcher->setFuture(*static_cast<Async::Future<PrevOut>*>(prevFuture)); | 684 | prevFutureWatcher->setFuture(*static_cast<KAsync::Future<PrevOut>*>(prevFuture)); |
683 | } | 685 | } |
684 | 686 | ||
685 | return execution; | 687 | return execution; |
@@ -712,7 +714,7 @@ ThenExecutor<Out, In ...>::ThenExecutor(ThenTask<Out, In ...> then, ErrorHandler | |||
712 | template<typename Out, typename ... In> | 714 | template<typename Out, typename ... In> |
713 | void ThenExecutor<Out, In ...>::run(const ExecutionPtr &execution) | 715 | void ThenExecutor<Out, In ...>::run(const ExecutionPtr &execution) |
714 | { | 716 | { |
715 | Async::Future<typename detail::prevOut<In ...>::type> *prevFuture = nullptr; | 717 | KAsync::Future<typename detail::prevOut<In ...>::type> *prevFuture = nullptr; |
716 | if (execution->prevExecution) { | 718 | if (execution->prevExecution) { |
717 | prevFuture = execution->prevExecution->result<typename detail::prevOut<In ...>::type>(); | 719 | prevFuture = execution->prevExecution->result<typename detail::prevOut<In ...>::type>(); |
718 | assert(prevFuture->isFinished()); | 720 | assert(prevFuture->isFinished()); |
@@ -744,17 +746,17 @@ void EachExecutor<PrevOut, Out, In>::run(const ExecutionPtr &execution) | |||
744 | 746 | ||
745 | for (auto arg : prevFuture->value()) { | 747 | for (auto arg : prevFuture->value()) { |
746 | //We have to manually manage the lifetime of these temporary futures | 748 | //We have to manually manage the lifetime of these temporary futures |
747 | Async::Future<Out> *future = new Async::Future<Out>(); | 749 | KAsync::Future<Out> *future = new KAsync::Future<Out>(); |
748 | EachExecutor<PrevOut, Out, In>::mFunc(arg, *future); | 750 | EachExecutor<PrevOut, Out, In>::mFunc(arg, *future); |
749 | auto fw = new Async::FutureWatcher<Out>(); | 751 | auto fw = new KAsync::FutureWatcher<Out>(); |
750 | mFutureWatchers.append(fw); | 752 | mFutureWatchers.append(fw); |
751 | QObject::connect(fw, &Async::FutureWatcher<Out>::futureReady, | 753 | QObject::connect(fw, &KAsync::FutureWatcher<Out>::futureReady, |
752 | [out, fw, this, future]() { | 754 | [out, fw, this, future]() { |
753 | assert(fw->future().isFinished()); | 755 | assert(fw->future().isFinished()); |
754 | const int index = mFutureWatchers.indexOf(fw); | 756 | const int index = mFutureWatchers.indexOf(fw); |
755 | assert(index > -1); | 757 | assert(index > -1); |
756 | mFutureWatchers.removeAt(index); | 758 | mFutureWatchers.removeAt(index); |
757 | Async::detail::aggregateFutureValue<Out>(fw->future(), *out); | 759 | KAsync::detail::aggregateFutureValue<Out>(fw->future(), *out); |
758 | if (mFutureWatchers.isEmpty()) { | 760 | if (mFutureWatchers.isEmpty()) { |
759 | out->setFinished(); | 761 | out->setFinished(); |
760 | } | 762 | } |
@@ -794,19 +796,19 @@ void SyncThenExecutor<Out, In ...>::run(const ExecutionPtr &execution) | |||
794 | template<typename Out, typename ... In> | 796 | template<typename Out, typename ... In> |
795 | void SyncThenExecutor<Out, In ...>::run(const ExecutionPtr &execution, std::false_type) | 797 | void SyncThenExecutor<Out, In ...>::run(const ExecutionPtr &execution, std::false_type) |
796 | { | 798 | { |
797 | Async::Future<typename detail::prevOut<In ...>::type> *prevFuture = | 799 | KAsync::Future<typename detail::prevOut<In ...>::type> *prevFuture = |
798 | execution->prevExecution | 800 | execution->prevExecution |
799 | ? execution->prevExecution->result<typename detail::prevOut<In ...>::type>() | 801 | ? execution->prevExecution->result<typename detail::prevOut<In ...>::type>() |
800 | : nullptr; | 802 | : nullptr; |
801 | (void) prevFuture; // silence 'set but not used' warning | 803 | (void) prevFuture; // silence 'set but not used' warning |
802 | Async::Future<Out> *future = execution->result<Out>(); | 804 | KAsync::Future<Out> *future = execution->result<Out>(); |
803 | future->setValue(SyncThenExecutor<Out, In...>::mFunc(prevFuture ? prevFuture->value() : In() ...)); | 805 | future->setValue(SyncThenExecutor<Out, In...>::mFunc(prevFuture ? prevFuture->value() : In() ...)); |
804 | } | 806 | } |
805 | 807 | ||
806 | template<typename Out, typename ... In> | 808 | template<typename Out, typename ... In> |
807 | void SyncThenExecutor<Out, In ...>::run(const ExecutionPtr &execution, std::true_type) | 809 | void SyncThenExecutor<Out, In ...>::run(const ExecutionPtr &execution, std::true_type) |
808 | { | 810 | { |
809 | Async::Future<typename detail::prevOut<In ...>::type> *prevFuture = | 811 | KAsync::Future<typename detail::prevOut<In ...>::type> *prevFuture = |
810 | execution->prevExecution | 812 | execution->prevExecution |
811 | ? execution->prevExecution->result<typename detail::prevOut<In ...>::type>() | 813 | ? execution->prevExecution->result<typename detail::prevOut<In ...>::type>() |
812 | : nullptr; | 814 | : nullptr; |
@@ -842,13 +844,13 @@ void SyncEachExecutor<PrevOut, Out, In>::run(const ExecutionPtr &execution) | |||
842 | } | 844 | } |
843 | 845 | ||
844 | template<typename PrevOut, typename Out, typename In> | 846 | template<typename PrevOut, typename Out, typename In> |
845 | void SyncEachExecutor<PrevOut, Out, In>::run(Async::Future<Out> *out, const typename PrevOut::value_type &arg, std::false_type) | 847 | void SyncEachExecutor<PrevOut, Out, In>::run(KAsync::Future<Out> *out, const typename PrevOut::value_type &arg, std::false_type) |
846 | { | 848 | { |
847 | out->setValue(out->value() + SyncEachExecutor<PrevOut, Out, In>::mFunc(arg)); | 849 | out->setValue(out->value() + SyncEachExecutor<PrevOut, Out, In>::mFunc(arg)); |
848 | } | 850 | } |
849 | 851 | ||
850 | template<typename PrevOut, typename Out, typename In> | 852 | template<typename PrevOut, typename Out, typename In> |
851 | void SyncEachExecutor<PrevOut, Out, In>::run(Async::Future<Out> * /* unused */, const typename PrevOut::value_type &arg, std::true_type) | 853 | void SyncEachExecutor<PrevOut, Out, In>::run(KAsync::Future<Out> * /* unused */, const typename PrevOut::value_type &arg, std::true_type) |
852 | { | 854 | { |
853 | SyncEachExecutor<PrevOut, Out, In>::mFunc(arg); | 855 | SyncEachExecutor<PrevOut, Out, In>::mFunc(arg); |
854 | } | 856 | } |
@@ -863,10 +865,10 @@ SyncReduceExecutor<Out, In>::SyncReduceExecutor(SyncReduceTask<Out, In> reduce, | |||
863 | 865 | ||
864 | } // namespace Private | 866 | } // namespace Private |
865 | 867 | ||
866 | } // namespace Async | 868 | } // namespace KAsync |
867 | 869 | ||
868 | 870 | ||
869 | 871 | ||
870 | #endif // ASYNC_H | 872 | #endif // KASYNC_H |
871 | 873 | ||
872 | 874 | ||
diff --git a/async/src/async_impl.h b/async/src/async_impl.h index 8c74193..5b4e393 100644 --- a/async/src/async_impl.h +++ b/async/src/async_impl.h | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright 2014 Daniel Vrátil <dvratil@redhat.com> | 2 | * Copyright 2014 - 2015 Daniel Vrátil <dvratil@redhat.com> |
3 | * | 3 | * |
4 | * This library is free software; you can redistribute it and/or | 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 | 5 | * modify it under the terms of the GNU Library General Public License as |
@@ -15,13 +15,13 @@ | |||
15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. | 15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ | 16 | */ |
17 | 17 | ||
18 | #ifndef ASYNC_IMPL_H | 18 | #ifndef KASYNC_IMPL_H |
19 | #define ASYNC_IMPL_H | 19 | #define KASYNC_IMPL_H |
20 | 20 | ||
21 | #include "async.h" | 21 | #include "async.h" |
22 | #include <type_traits> | 22 | #include <type_traits> |
23 | 23 | ||
24 | namespace Async { | 24 | namespace KAsync { |
25 | 25 | ||
26 | namespace detail { | 26 | namespace detail { |
27 | 27 | ||
@@ -48,34 +48,34 @@ struct prevOut { | |||
48 | 48 | ||
49 | template<typename T> | 49 | template<typename T> |
50 | inline typename std::enable_if<!std::is_void<T>::value, void>::type | 50 | inline typename std::enable_if<!std::is_void<T>::value, void>::type |
51 | copyFutureValue(const Async::Future<T> &in, Async::Future<T> &out) | 51 | copyFutureValue(const KAsync::Future<T> &in, KAsync::Future<T> &out) |
52 | { | 52 | { |
53 | out.setValue(in.value()); | 53 | out.setValue(in.value()); |
54 | } | 54 | } |
55 | 55 | ||
56 | template<typename T> | 56 | template<typename T> |
57 | inline typename std::enable_if<std::is_void<T>::value, void>::type | 57 | inline typename std::enable_if<std::is_void<T>::value, void>::type |
58 | copyFutureValue(const Async::Future<T> &in, Async::Future<T> &out) | 58 | copyFutureValue(const KAsync::Future<T> &in, KAsync::Future<T> &out) |
59 | { | 59 | { |
60 | // noop | 60 | // noop |
61 | } | 61 | } |
62 | 62 | ||
63 | template<typename T> | 63 | template<typename T> |
64 | inline typename std::enable_if<!std::is_void<T>::value, void>::type | 64 | inline typename std::enable_if<!std::is_void<T>::value, void>::type |
65 | aggregateFutureValue(const Async::Future<T> &in, Async::Future<T> &out) | 65 | aggregateFutureValue(const KAsync::Future<T> &in, KAsync::Future<T> &out) |
66 | { | 66 | { |
67 | out.setValue(out.value() + in.value()); | 67 | out.setValue(out.value() + in.value()); |
68 | } | 68 | } |
69 | 69 | ||
70 | template<typename T> | 70 | template<typename T> |
71 | inline typename std::enable_if<std::is_void<T>::value, void>::type | 71 | inline typename std::enable_if<std::is_void<T>::value, void>::type |
72 | aggregateFutureValue(const Async::Future<T> &in, Async::Future<T> &out) | 72 | aggregateFutureValue(const KAsync::Future<T> &in, KAsync::Future<T> &out) |
73 | { | 73 | { |
74 | // noop | 74 | // noop |
75 | } | 75 | } |
76 | 76 | ||
77 | } // namespace Detail | 77 | } // namespace Detail |
78 | 78 | ||
79 | } // namespace Async | 79 | } // namespace KAsync |
80 | 80 | ||
81 | #endif // ASYNC_IMPL_H | 81 | #endif // KASYNC_IMPL_H |
diff --git a/async/src/debug.cpp b/async/src/debug.cpp index fdf2fa1..64a3a3b 100644 --- a/async/src/debug.cpp +++ b/async/src/debug.cpp | |||
@@ -25,7 +25,7 @@ | |||
25 | #include <memory> | 25 | #include <memory> |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | namespace Async | 28 | namespace KAsync |
29 | { | 29 | { |
30 | 30 | ||
31 | Q_LOGGING_CATEGORY(Debug, "org.kde.async", QtWarningMsg); | 31 | Q_LOGGING_CATEGORY(Debug, "org.kde.async", QtWarningMsg); |
@@ -37,15 +37,15 @@ QString demangleName(const char *name) | |||
37 | int status = 1; // uses -3 to 0 error codes | 37 | int status = 1; // uses -3 to 0 error codes |
38 | std::unique_ptr<char, void(*)(void*)> demangled(abi::__cxa_demangle(name, 0, 0, &status), std::free); | 38 | std::unique_ptr<char, void(*)(void*)> demangled(abi::__cxa_demangle(name, 0, 0, &status), std::free); |
39 | if (status == 0) { | 39 | if (status == 0) { |
40 | return QString(demangled.get()); | 40 | return QString::fromLatin1(demangled.get()); |
41 | } | 41 | } |
42 | #endif | 42 | #endif |
43 | return QString(name); | 43 | return QString::fromLatin1(name); |
44 | } | 44 | } |
45 | 45 | ||
46 | } | 46 | } |
47 | 47 | ||
48 | using namespace Async; | 48 | using namespace KAsync; |
49 | 49 | ||
50 | int Tracer::lastId = 0; | 50 | int Tracer::lastId = 0; |
51 | 51 | ||
@@ -53,12 +53,12 @@ Tracer::Tracer(Private::Execution *execution) | |||
53 | : mId(lastId++) | 53 | : mId(lastId++) |
54 | , mExecution(execution) | 54 | , mExecution(execution) |
55 | { | 55 | { |
56 | msg(Async::Tracer::Start); | 56 | msg(KAsync::Tracer::Start); |
57 | } | 57 | } |
58 | 58 | ||
59 | Tracer::~Tracer() | 59 | Tracer::~Tracer() |
60 | { | 60 | { |
61 | msg(Async::Tracer::End); | 61 | msg(KAsync::Tracer::End); |
62 | // FIXME: Does this work on parallel executions? | 62 | // FIXME: Does this work on parallel executions? |
63 | --lastId; | 63 | --lastId; |
64 | --mId; | 64 | --mId; |
@@ -68,8 +68,8 @@ void Tracer::msg(Tracer::MsgType msgType) | |||
68 | { | 68 | { |
69 | #ifndef QT_NO_DEBUG | 69 | #ifndef QT_NO_DEBUG |
70 | qCDebug(Trace).nospace() << (QString().fill(QLatin1Char(' '), mId * 2) % | 70 | qCDebug(Trace).nospace() << (QString().fill(QLatin1Char(' '), mId * 2) % |
71 | (msgType == Async::Tracer::Start ? " START " : " END ") % | 71 | (msgType == KAsync::Tracer::Start ? QStringLiteral(" START ") : QStringLiteral(" END ")) % |
72 | QString::number(mId) % " " % | 72 | QString::number(mId) % QStringLiteral(" ") % |
73 | mExecution->executor->mExecutorName); | 73 | mExecution->executor->mExecutorName); |
74 | #endif | 74 | #endif |
75 | } | 75 | } |
diff --git a/async/src/debug.h b/async/src/debug.h index c453eb3..b2b2ff7 100644 --- a/async/src/debug.h +++ b/async/src/debug.h | |||
@@ -15,8 +15,10 @@ | |||
15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. | 15 | * along with this library. If not, see <http://www.gnu.org/licenses/>. |
16 | */ | 16 | */ |
17 | 17 | ||
18 | #ifndef ASYNC_DEBUG_H | 18 | #ifndef KASYNC_DEBUG_H |
19 | #define ASYNC_DEBUG_H | 19 | #define KASYNC_DEBUG_H |
20 | |||
21 | #include "kasync_export.h" | ||
20 | 22 | ||
21 | #include <QLoggingCategory> | 23 | #include <QLoggingCategory> |
22 | #include <QStringBuilder> | 24 | #include <QStringBuilder> |
@@ -25,20 +27,20 @@ | |||
25 | #include <typeinfo> | 27 | #include <typeinfo> |
26 | #endif | 28 | #endif |
27 | 29 | ||
28 | namespace Async | 30 | namespace KAsync |
29 | { | 31 | { |
30 | 32 | ||
31 | Q_DECLARE_LOGGING_CATEGORY(Debug) | 33 | Q_DECLARE_LOGGING_CATEGORY(Debug) |
32 | Q_DECLARE_LOGGING_CATEGORY(Trace) | 34 | Q_DECLARE_LOGGING_CATEGORY(Trace) |
33 | 35 | ||
34 | QString demangleName(const char *name); | 36 | KASYNC_EXPORT QString demangleName(const char *name); |
35 | 37 | ||
36 | namespace Private | 38 | namespace Private |
37 | { | 39 | { |
38 | class Execution; | 40 | class Execution; |
39 | } | 41 | } |
40 | 42 | ||
41 | class Tracer | 43 | class KASYNC_EXPORT Tracer |
42 | { | 44 | { |
43 | public: | 45 | public: |
44 | Tracer(Private::Execution *execution); | 46 | Tracer(Private::Execution *execution); |
@@ -62,7 +64,7 @@ private: | |||
62 | #ifndef QT_NO_DEBUG | 64 | #ifndef QT_NO_DEBUG |
63 | template<typename T> | 65 | template<typename T> |
64 | QString storeExecutorNameExpanded() { | 66 | QString storeExecutorNameExpanded() { |
65 | return Async::demangleName(typeid(T).name()); | 67 | return KAsync::demangleName(typeid(T).name()); |
66 | } | 68 | } |
67 | 69 | ||
68 | template<typename T, typename ... Tail> | 70 | template<typename T, typename ... Tail> |
@@ -77,4 +79,4 @@ private: | |||
77 | #define STORE_EXECUTOR_NAME(...) | 79 | #define STORE_EXECUTOR_NAME(...) |
78 | #endif | 80 | #endif |
79 | 81 | ||
80 | #endif // ASYNC_DEBUG_H \ No newline at end of file | 82 | #endif // KASYNC_DEBUG_H \ No newline at end of file |
diff --git a/async/src/future.cpp b/async/src/future.cpp index 4f3cd80..9281cc8 100644 --- a/async/src/future.cpp +++ b/async/src/future.cpp | |||
@@ -18,7 +18,7 @@ | |||
18 | #include "future.h" | 18 | #include "future.h" |
19 | #include "async.h" | 19 | #include "async.h" |
20 | 20 | ||
21 | using namespace Async; | 21 | using namespace KAsync; |
22 | 22 | ||
23 | FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution) | 23 | FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution) |
24 | : finished(false) | 24 | : finished(false) |
@@ -53,7 +53,7 @@ FutureBase::FutureBase(FutureBase::PrivateBase *dd) | |||
53 | { | 53 | { |
54 | } | 54 | } |
55 | 55 | ||
56 | FutureBase::FutureBase(const Async::FutureBase &other) | 56 | FutureBase::FutureBase(const KAsync::FutureBase &other) |
57 | : d(other.d) | 57 | : d(other.d) |
58 | { | 58 | { |
59 | } | 59 | } |
diff --git a/async/src/future.h b/async/src/future.h index ff199ef..b2b723e 100644 --- a/async/src/future.h +++ b/async/src/future.h | |||
@@ -18,6 +18,8 @@ | |||
18 | #ifndef FUTURE_H | 18 | #ifndef FUTURE_H |
19 | #define FUTURE_H | 19 | #define FUTURE_H |
20 | 20 | ||
21 | #include "kasync_export.h" | ||
22 | |||
21 | class QEventLoop; | 23 | class QEventLoop; |
22 | 24 | ||
23 | #include <type_traits> | 25 | #include <type_traits> |
@@ -27,7 +29,7 @@ class QEventLoop; | |||
27 | #include <QVector> | 29 | #include <QVector> |
28 | #include <QEventLoop> | 30 | #include <QEventLoop> |
29 | 31 | ||
30 | namespace Async { | 32 | namespace KAsync { |
31 | 33 | ||
32 | class FutureWatcherBase; | 34 | class FutureWatcherBase; |
33 | template<typename T> | 35 | template<typename T> |
@@ -40,9 +42,9 @@ class ExecutorBase; | |||
40 | typedef QSharedPointer<Execution> ExecutionPtr; | 42 | typedef QSharedPointer<Execution> ExecutionPtr; |
41 | } // namespace Private | 43 | } // namespace Private |
42 | 44 | ||
43 | class FutureBase | 45 | class KASYNC_EXPORT FutureBase |
44 | { | 46 | { |
45 | friend class Async::Private::Execution; | 47 | friend class KAsync::Private::Execution; |
46 | friend class FutureWatcherBase; | 48 | friend class FutureWatcherBase; |
47 | 49 | ||
48 | public: | 50 | public: |
@@ -61,7 +63,7 @@ protected: | |||
61 | class PrivateBase : public QSharedData | 63 | class PrivateBase : public QSharedData |
62 | { | 64 | { |
63 | public: | 65 | public: |
64 | PrivateBase(const Async::Private::ExecutionPtr &execution); | 66 | PrivateBase(const KAsync::Private::ExecutionPtr &execution); |
65 | virtual ~PrivateBase(); | 67 | virtual ~PrivateBase(); |
66 | 68 | ||
67 | void releaseExecution(); | 69 | void releaseExecution(); |
@@ -72,14 +74,14 @@ protected: | |||
72 | 74 | ||
73 | QVector<QPointer<FutureWatcherBase>> watchers; | 75 | QVector<QPointer<FutureWatcherBase>> watchers; |
74 | private: | 76 | private: |
75 | QWeakPointer<Async::Private::Execution> mExecution; | 77 | QWeakPointer<KAsync::Private::Execution> mExecution; |
76 | }; | 78 | }; |
77 | 79 | ||
78 | FutureBase(); | 80 | FutureBase(); |
79 | FutureBase(FutureBase::PrivateBase *dd); | 81 | FutureBase(FutureBase::PrivateBase *dd); |
80 | FutureBase(const FutureBase &other); | 82 | FutureBase(const FutureBase &other); |
81 | 83 | ||
82 | void addWatcher(Async::FutureWatcherBase *watcher); | 84 | void addWatcher(KAsync::FutureWatcherBase *watcher); |
83 | void releaseExecution(); | 85 | void releaseExecution(); |
84 | 86 | ||
85 | protected: | 87 | protected: |
@@ -105,14 +107,14 @@ public: | |||
105 | } | 107 | } |
106 | FutureWatcher<T> watcher; | 108 | FutureWatcher<T> watcher; |
107 | QEventLoop eventLoop; | 109 | QEventLoop eventLoop; |
108 | QObject::connect(&watcher, &Async::FutureWatcher<T>::futureReady, | 110 | QObject::connect(&watcher, &KAsync::FutureWatcher<T>::futureReady, |
109 | &eventLoop, &QEventLoop::quit); | 111 | &eventLoop, &QEventLoop::quit); |
110 | watcher.setFuture(*static_cast<const Async::Future<T>*>(this)); | 112 | watcher.setFuture(*static_cast<const KAsync::Future<T>*>(this)); |
111 | eventLoop.exec(); | 113 | eventLoop.exec(); |
112 | } | 114 | } |
113 | 115 | ||
114 | protected: | 116 | protected: |
115 | FutureGeneric(const Async::Private::ExecutionPtr &execution) | 117 | FutureGeneric(const KAsync::Private::ExecutionPtr &execution) |
116 | : FutureBase(new Private(execution)) | 118 | : FutureBase(new Private(execution)) |
117 | {} | 119 | {} |
118 | 120 | ||
@@ -124,7 +126,7 @@ protected: | |||
124 | class Private : public FutureBase::PrivateBase | 126 | class Private : public FutureBase::PrivateBase |
125 | { | 127 | { |
126 | public: | 128 | public: |
127 | Private(const Async::Private::ExecutionPtr &execution) | 129 | Private(const KAsync::Private::ExecutionPtr &execution) |
128 | : FutureBase::PrivateBase(execution) | 130 | : FutureBase::PrivateBase(execution) |
129 | {} | 131 | {} |
130 | 132 | ||
@@ -137,14 +139,14 @@ protected: | |||
137 | template<typename T> | 139 | template<typename T> |
138 | class Future : public FutureGeneric<T> | 140 | class Future : public FutureGeneric<T> |
139 | { | 141 | { |
140 | friend class Async::Private::ExecutorBase; | 142 | friend class KAsync::Private::ExecutorBase; |
141 | 143 | ||
142 | template<typename T_> | 144 | template<typename T_> |
143 | friend class Async::FutureWatcher; | 145 | friend class KAsync::FutureWatcher; |
144 | 146 | ||
145 | public: | 147 | public: |
146 | Future() | 148 | Future() |
147 | : FutureGeneric<T>(Async::Private::ExecutionPtr()) | 149 | : FutureGeneric<T>(KAsync::Private::ExecutionPtr()) |
148 | {} | 150 | {} |
149 | 151 | ||
150 | Future(const Future<T> &other) | 152 | Future(const Future<T> &other) |
@@ -162,7 +164,7 @@ public: | |||
162 | } | 164 | } |
163 | 165 | ||
164 | protected: | 166 | protected: |
165 | Future(const Async::Private::ExecutionPtr &execution) | 167 | Future(const KAsync::Private::ExecutionPtr &execution) |
166 | : FutureGeneric<T>(execution) | 168 | : FutureGeneric<T>(execution) |
167 | {} | 169 | {} |
168 | 170 | ||
@@ -171,11 +173,11 @@ protected: | |||
171 | template<> | 173 | template<> |
172 | class Future<void> : public FutureGeneric<void> | 174 | class Future<void> : public FutureGeneric<void> |
173 | { | 175 | { |
174 | friend class Async::Private::ExecutorBase; | 176 | friend class KAsync::Private::ExecutorBase; |
175 | 177 | ||
176 | public: | 178 | public: |
177 | Future() | 179 | Future() |
178 | : FutureGeneric<void>(Async::Private::ExecutionPtr()) | 180 | : FutureGeneric<void>(KAsync::Private::ExecutionPtr()) |
179 | {} | 181 | {} |
180 | 182 | ||
181 | Future(const Future<void> &other) | 183 | Future(const Future<void> &other) |
@@ -183,7 +185,7 @@ public: | |||
183 | {} | 185 | {} |
184 | 186 | ||
185 | protected: | 187 | protected: |
186 | Future(const Async::Private::ExecutionPtr &execution) | 188 | Future(const KAsync::Private::ExecutionPtr &execution) |
187 | : FutureGeneric<void>(execution) | 189 | : FutureGeneric<void>(execution) |
188 | {} | 190 | {} |
189 | }; | 191 | }; |
@@ -192,7 +194,7 @@ protected: | |||
192 | 194 | ||
193 | 195 | ||
194 | 196 | ||
195 | class FutureWatcherBase : public QObject | 197 | class KASYNC_EXPORT FutureWatcherBase : public QObject |
196 | { | 198 | { |
197 | Q_OBJECT | 199 | Q_OBJECT |
198 | 200 | ||
@@ -209,12 +211,12 @@ protected: | |||
209 | void futureReadyCallback(); | 211 | void futureReadyCallback(); |
210 | void futureProgressCallback(qreal progress); | 212 | void futureProgressCallback(qreal progress); |
211 | 213 | ||
212 | void setFutureImpl(const Async::FutureBase &future); | 214 | void setFutureImpl(const KAsync::FutureBase &future); |
213 | 215 | ||
214 | protected: | 216 | protected: |
215 | class Private { | 217 | class Private { |
216 | public: | 218 | public: |
217 | Async::FutureBase future; | 219 | KAsync::FutureBase future; |
218 | }; | 220 | }; |
219 | 221 | ||
220 | Private * const d; | 222 | Private * const d; |
@@ -226,7 +228,7 @@ private: | |||
226 | template<typename T> | 228 | template<typename T> |
227 | class FutureWatcher : public FutureWatcherBase | 229 | class FutureWatcher : public FutureWatcherBase |
228 | { | 230 | { |
229 | friend class Async::FutureGeneric<T>; | 231 | friend class KAsync::FutureGeneric<T>; |
230 | 232 | ||
231 | public: | 233 | public: |
232 | FutureWatcher(QObject *parent = nullptr) | 234 | FutureWatcher(QObject *parent = nullptr) |
@@ -236,14 +238,14 @@ public: | |||
236 | ~FutureWatcher() | 238 | ~FutureWatcher() |
237 | {} | 239 | {} |
238 | 240 | ||
239 | void setFuture(const Async::Future<T> &future) | 241 | void setFuture(const KAsync::Future<T> &future) |
240 | { | 242 | { |
241 | setFutureImpl(*static_cast<const Async::FutureBase*>(&future)); | 243 | setFutureImpl(*static_cast<const KAsync::FutureBase*>(&future)); |
242 | } | 244 | } |
243 | 245 | ||
244 | Async::Future<T> future() const | 246 | KAsync::Future<T> future() const |
245 | { | 247 | { |
246 | return *static_cast<Async::Future<T>*>(&d->future); | 248 | return *static_cast<KAsync::Future<T>*>(&d->future); |
247 | } | 249 | } |
248 | 250 | ||
249 | private: | 251 | private: |