From 5b78e0da1d64b6096829f54b29f14ec5643b5ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Fri, 15 May 2015 16:00:32 +0200 Subject: Async: rename Async namespace to KAsync --- async/autotests/asynctest.cpp | 192 +++++++++++++++++++++--------------------- async/autotests/kjobtest.cpp | 4 +- async/src/async.cpp | 18 ++-- async/src/async.h | 134 ++++++++++++++--------------- async/src/async_impl.h | 20 ++--- async/src/debug.cpp | 16 ++-- async/src/debug.h | 16 ++-- async/src/future.cpp | 4 +- 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: template class AsyncSimulator { public: - AsyncSimulator(Async::Future &future, const T &result) + AsyncSimulator(KAsync::Future &future, const T &result) : mFuture(future) , mResult(result) { @@ -99,7 +99,7 @@ private: mTimer.start(200); } - AsyncSimulator(Async::Future &future, std::function&)> callback) + AsyncSimulator(KAsync::Future &future, std::function&)> callback) : mFuture(future) , mCallback(callback) { @@ -116,8 +116,8 @@ private: } private: - Async::Future mFuture; - std::function&)> mCallback; + KAsync::Future mFuture; + std::function&)> mCallback; T mResult; QTimer mTimer; }; @@ -127,7 +127,7 @@ private: template<> class AsyncTest::AsyncSimulator { public: - AsyncSimulator(Async::Future &future) + AsyncSimulator(KAsync::Future &future) : mFuture(future) { QObject::connect(&mTimer, &QTimer::timeout, @@ -143,7 +143,7 @@ public: } private: - Async::Future mFuture; + KAsync::Future mFuture; QTimer mTimer; }; @@ -151,24 +151,24 @@ private: void AsyncTest::testSyncPromises() { - auto baseJob = Async::start( - [](Async::Future &f) { + auto baseJob = KAsync::start( + [](KAsync::Future &f) { f.setValue(42); f.setFinished(); }) .then( - [](int v, Async::Future &f) { - f.setValue("Result is " + QString::number(v)); + [](int v, KAsync::Future &f) { + f.setValue(QLatin1String("Result is ") + QString::number(v)); f.setFinished(); }); auto job = baseJob.then( - [](const QString &v, Async::Future &f) { + [](const QString &v, KAsync::Future &f) { f.setValue(v.toUpper()); f.setFinished(); }); - Async::Future future = job.exec(); + KAsync::Future future = job.exec(); QVERIFY(future.isFinished()); QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); @@ -176,12 +176,12 @@ void AsyncTest::testSyncPromises() void AsyncTest::testAsyncPromises() { - auto job = Async::start( - [](Async::Future &future) { + auto job = KAsync::start( + [](KAsync::Future &future) { new AsyncSimulator(future, 42); }); - Async::Future future = job.exec(); + KAsync::Future future = job.exec(); future.waitForFinished(); QCOMPARE(future.value(), 42); @@ -191,11 +191,11 @@ void AsyncTest::testAsyncPromises2() { bool done = false; - auto job = Async::start( - [](Async::Future &future) { + auto job = KAsync::start( + [](KAsync::Future &future) { new AsyncSimulator(future, 42); } - ).then([&done](int result, Async::Future &future) { + ).then([&done](int result, KAsync::Future &future) { done = true; future.setValue(result); future.setFinished(); @@ -210,17 +210,17 @@ void AsyncTest::testNestedAsync() { bool done = false; - auto job = Async::start( - [](Async::Future &future) { - auto innerJob = Async::start([](Async::Future &innerFuture) { + auto job = KAsync::start( + [](KAsync::Future &future) { + auto innerJob = KAsync::start([](KAsync::Future &innerFuture) { new AsyncSimulator(innerFuture, 42); - }).then([&future](Async::Future &innerThenFuture) { + }).then([&future](KAsync::Future &innerThenFuture) { future.setFinished(); innerThenFuture.setFinished(); }); innerJob.exec().waitForFinished(); } - ).then([&done](int result, Async::Future &future) { + ).then([&done](int result, KAsync::Future &future) { done = true; future.setValue(result); future.setFinished(); @@ -232,8 +232,8 @@ void AsyncTest::testNestedAsync() void AsyncTest::testStartValue() { - auto job = Async::start( - [](int in, Async::Future &future) { + auto job = KAsync::start( + [](int in, KAsync::Future &future) { future.setValue(in); future.setFinished(); }); @@ -249,8 +249,8 @@ void AsyncTest::testStartValue() void AsyncTest::testAsyncThen() { - auto job = Async::start( - [](Async::Future &future) { + auto job = KAsync::start( + [](KAsync::Future &future) { new AsyncSimulator(future, 42); }); @@ -264,7 +264,7 @@ void AsyncTest::testAsyncThen() void AsyncTest::testSyncThen() { - auto job = Async::start( + auto job = KAsync::start( []() -> int { return 42; }) @@ -280,13 +280,13 @@ void AsyncTest::testSyncThen() void AsyncTest::testJoinedThen() { - auto job1 = Async::start( - [](int in, Async::Future &future) { + auto job1 = KAsync::start( + [](int in, KAsync::Future &future) { new AsyncSimulator(future, in * 2); }); - auto job2 = Async::start( - [](Async::Future &future) { + auto job2 = KAsync::start( + [](KAsync::Future &future) { new AsyncSimulator(future, 42); }) .then(job1); @@ -302,13 +302,13 @@ void AsyncTest::testVoidThen() { int check = 0; - auto job = Async::start( - [&check](Async::Future &future) { + auto job = KAsync::start( + [&check](KAsync::Future &future) { new AsyncSimulator(future); ++check; }) .then( - [&check](Async::Future &future) { + [&check](KAsync::Future &future) { new AsyncSimulator(future); ++check; }) @@ -328,12 +328,12 @@ void AsyncTest::testVoidThen() void AsyncTest::testAsyncEach() { - auto job = Async::start>( - [](Async::Future> &future) { + auto job = KAsync::start>( + [](KAsync::Future> &future) { new AsyncSimulator>(future, { 1, 2, 3, 4 }); }) .each, int>( - [](const int &v, Async::Future> &future) { + [](const int &v, KAsync::Future> &future) { new AsyncSimulator>(future, { v + 1 }); }); @@ -347,7 +347,7 @@ void AsyncTest::testAsyncEach() void AsyncTest::testSyncEach() { - auto job = Async::start>( + auto job = KAsync::start>( []() -> QList { return { 1, 2, 3, 4 }; }) @@ -356,7 +356,7 @@ void AsyncTest::testSyncEach() return { v + 1 }; }); - Async::Future> future = job.exec(); + KAsync::Future> future = job.exec(); const QList expected({ 2, 3, 4, 5 }); QVERIFY(future.isFinished()); @@ -365,12 +365,12 @@ void AsyncTest::testSyncEach() void AsyncTest::testJoinedEach() { - auto job1 = Async::start, int>( - [](int v, Async::Future> &future) { + auto job1 = KAsync::start, int>( + [](int v, KAsync::Future> &future) { new AsyncSimulator>(future, { v * 2 }); }); - auto job = Async::start>( + auto job = KAsync::start>( []() -> QList { return { 1, 2, 3, 4 }; }) @@ -387,7 +387,7 @@ void AsyncTest::testJoinedEach() void AsyncTest::testVoidEachThen() { QList check; - auto job = Async::start>( + auto job = KAsync::start>( []() -> QList { return { 1, 2, 3, 4 }; }).each( @@ -406,14 +406,14 @@ void AsyncTest::testAsyncVoidEachThen() { bool completedJob = false; QList check; - auto job = Async::start>( - [](Async::Future > &future) { + auto job = KAsync::start>( + [](KAsync::Future > &future) { new AsyncSimulator>(future, { 1, 2, 3, 4 }); }).each( - [&check](const int &v, Async::Future &future) { + [&check](const int &v, KAsync::Future &future) { check << v; new AsyncSimulator(future); - }).then([&completedJob](Async::Future &future) { + }).then([&completedJob](KAsync::Future &future) { completedJob = true; future.setFinished(); }); @@ -433,14 +433,14 @@ void AsyncTest::testAsyncVoidEachThen() void AsyncTest::testAsyncReduce() { - auto job = Async::start>( - [](Async::Future> &future) { + auto job = KAsync::start>( + [](KAsync::Future> &future) { new AsyncSimulator>(future, { 1, 2, 3, 4 }); }) .reduce>( - [](const QList &list, Async::Future &future) { + [](const QList &list, KAsync::Future &future) { new AsyncSimulator(future, - [list](Async::Future &future) { + [list](KAsync::Future &future) { int sum = 0; for (int i : list) sum += i; future.setValue(sum); @@ -449,7 +449,7 @@ void AsyncTest::testAsyncReduce() ); }); - Async::Future future = job.exec(); + KAsync::Future future = job.exec(); future.waitForFinished(); QVERIFY(future.isFinished()); @@ -458,7 +458,7 @@ void AsyncTest::testAsyncReduce() void AsyncTest::testSyncReduce() { - auto job = Async::start>( + auto job = KAsync::start>( []() -> QList { return { 1, 2, 3, 4 }; }) @@ -469,7 +469,7 @@ void AsyncTest::testSyncReduce() return sum; }); - Async::Future future = job.exec(); + KAsync::Future future = job.exec(); QVERIFY(future.isFinished()); QCOMPARE(future.value(), 10); @@ -478,14 +478,14 @@ void AsyncTest::testSyncReduce() void AsyncTest::testJoinedReduce() { - auto job1 = Async::start>( - [](const QList &list, Async::Future &future) { + auto job1 = KAsync::start>( + [](const QList &list, KAsync::Future &future) { int sum = 0; for (int i : list) sum += i; new AsyncSimulator(future, sum); }); - auto job = Async::start>( + auto job = KAsync::start>( []() -> QList { return { 1, 2, 3, 4 }; }) @@ -502,7 +502,7 @@ void AsyncTest::testVoidReduce() { // This must not compile (reduce with void result makes no sense) #ifdef TEST_BUILD_FAIL - auto job = Async::start>( + auto job = KAsync::start>( []() -> QList { return { 1, 2, 3, 4 }; }) @@ -522,8 +522,8 @@ void AsyncTest::testProgressReporting() static int progress; progress = 0; - auto job = Async::start( - [](Async::Future &f) { + auto job = KAsync::start( + [](KAsync::Future &f) { QTimer *timer = new QTimer(); connect(timer, &QTimer::timeout, [&f, timer]() { @@ -538,8 +538,8 @@ void AsyncTest::testProgressReporting() }); int progressCheck = 0; - Async::FutureWatcher watcher; - connect(&watcher, &Async::FutureWatcher::futureProgress, + KAsync::FutureWatcher watcher; + connect(&watcher, &KAsync::FutureWatcher::futureProgress, [&progressCheck](qreal progress) { progressCheck++; // FIXME: Don't use Q_ASSERT in unit tests @@ -556,9 +556,9 @@ void AsyncTest::testErrorHandler() { { - auto job = Async::start( - [](Async::Future &f) { - f.setError(1, "error"); + auto job = KAsync::start( + [](KAsync::Future &f) { + f.setError(1, QLatin1String("error")); }); auto future = job.exec(); @@ -569,9 +569,9 @@ void AsyncTest::testErrorHandler() { int error = 0; - auto job = Async::start( - [](Async::Future &f) { - f.setError(1, "error"); + auto job = KAsync::start( + [](KAsync::Future &f) { + f.setError(1, QLatin1String("error")); }, [&error](int errorCode, const QString &errorMessage) { error += errorCode; @@ -590,12 +590,12 @@ void AsyncTest::testErrorPropagation() { int error = 0; bool called = false; - auto job = Async::start( - [](Async::Future &f) { - f.setError(1, "error"); + auto job = KAsync::start( + [](KAsync::Future &f) { + f.setError(1, QLatin1String("error")); }) .then( - [&called](int v, Async::Future &f) { + [&called](int v, KAsync::Future &f) { called = true; f.setFinished(); }, @@ -614,11 +614,11 @@ void AsyncTest::testErrorPropagation() void AsyncTest::testErrorHandlerAsync() { { - auto job = Async::start( - [](Async::Future &f) { + auto job = KAsync::start( + [](KAsync::Future &f) { new AsyncSimulator(f, - [](Async::Future &f) { - f.setError(1, "error"); + [](KAsync::Future &f) { + f.setError(1, QLatin1String("error")); } ); } @@ -634,11 +634,11 @@ void AsyncTest::testErrorHandlerAsync() { int error = 0; - auto job = Async::start( - [](Async::Future &f) { + auto job = KAsync::start( + [](KAsync::Future &f) { new AsyncSimulator(f, - [](Async::Future &f) { - f.setError(1, "error"); + [](KAsync::Future &f) { + f.setError(1, QLatin1String("error")); } ); }, @@ -661,16 +661,16 @@ void AsyncTest::testErrorPropagationAsync() { int error = 0; bool called = false; - auto job = Async::start( - [](Async::Future &f) { + auto job = KAsync::start( + [](KAsync::Future &f) { new AsyncSimulator(f, - [](Async::Future &f) { - f.setError(1, "error"); + [](KAsync::Future &f) { + f.setError(1, QLatin1String("error")); } ); }) .then( - [&called](int v, Async::Future &f) { + [&called](int v, KAsync::Future &f) { called = true; f.setFinished(); }, @@ -692,9 +692,9 @@ void AsyncTest::testErrorPropagationAsync() void AsyncTest::testNestedErrorPropagation() { int error = 0; - auto job = Async::start([](){}) - .then(Async::error(1, "error")) //Nested job that throws error - .then([](Async::Future &future) { + auto job = KAsync::start([](){}) + .then(KAsync::error(1, QLatin1String("error"))) //Nested job that throws error + .then([](KAsync::Future &future) { //We should never get here Q_ASSERT(false); }, @@ -719,8 +719,8 @@ void AsyncTest::testChainingRunningJob() { int check = 0; - auto job = Async::start( - [&check](Async::Future &future) { + auto job = KAsync::start( + [&check](KAsync::Future &future) { QTimer *timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&future, &check]() { @@ -763,7 +763,7 @@ void AsyncTest::testChainingFinishedJob() { int check = 0; - auto job = Async::start( + auto job = KAsync::start( [&check]() -> int { ++check; return 42; @@ -799,7 +799,7 @@ void AsyncTest::testLifetimeWithoutHandle() { bool done = false; { - auto job = Async::start([&done](Async::Future &future) { + auto job = KAsync::start([&done](KAsync::Future &future) { QTimer *timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&future, &done]() { @@ -823,9 +823,9 @@ void AsyncTest::testLifetimeWithoutHandle() */ void AsyncTest::testLifetimeWithHandle() { - Async::Future future; + KAsync::Future future; { - auto job = Async::start([](Async::Future &future) { + auto job = KAsync::start([](KAsync::Future &future) { QTimer *timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [&future]() { @@ -844,7 +844,7 @@ void AsyncTest::testLifetimeWithHandle() void AsyncTest::benchmarkSyncThenExecutor() { - auto job = Async::start( + auto job = KAsync::start( []() -> int { return 0; }); 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: void KJobTest::testSingleKJob() { - auto job = Async::start(); + auto job = KAsync::start(); auto future = job.exec(42); future.waitForFinished(); @@ -54,7 +54,7 @@ void KJobTest::testSingleKJob() void KJobTest::testKJobChain() { - auto job = Async::start() + auto job = KAsync::start() .then(); 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 @@ #include #include -using namespace Async; +using namespace KAsync; Private::Execution::Execution(const Private::ExecutorBasePtr &executor) : executor(executor) @@ -104,11 +104,11 @@ static void asyncWhile(const std::function)> &bod }); } -Job Async::dowhile(Condition condition, ThenTask body) +Job KAsync::dowhile(Condition condition, ThenTask body) { - return Async::start([body, condition](Async::Future &future) { + return KAsync::start([body, condition](KAsync::Future &future) { asyncWhile([condition, body](std::function whileCallback) { - Async::start(body).then([whileCallback, condition]() { + KAsync::start(body).then([whileCallback, condition]() { whileCallback(!condition()); }).exec(); }, @@ -118,11 +118,11 @@ Job Async::dowhile(Condition condition, ThenTask body) }); } -Job Async::dowhile(ThenTask body) +Job KAsync::dowhile(ThenTask body) { - return Async::start([body](Async::Future &future) { + return KAsync::start([body](KAsync::Future &future) { asyncWhile([body](std::function whileCallback) { - Async::start(body).then([whileCallback](bool result) { + KAsync::start(body).then([whileCallback](bool result) { whileCallback(!result); //FIXME this return value is only required because .then doesn't work return true; @@ -134,10 +134,10 @@ Job Async::dowhile(ThenTask body) }); } -Job Async::wait(int delay) +Job KAsync::wait(int delay) { auto timer = QSharedPointer::create(); - return Async::start([timer, delay](Async::Future &future) { + return KAsync::start([timer, delay](KAsync::Future &future) { timer->setSingleShot(true); QObject::connect(timer.data(), &QTimer::timeout, [&future]() { 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 @@ /* - * Copyright 2014 Daniel Vrátil + * Copyright 2014 - 2015 Daniel Vrátil * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License as @@ -15,8 +15,10 @@ * along with this library. If not, see . */ -#ifndef ASYNC_H -#define ASYNC_H +#ifndef KASYNC_H +#define KASYNC_H + +#include "kasync_export.h" #include #include @@ -64,7 +66,7 @@ * TODO: Possibility to abort a job through future (perhaps optional?) * TODO: Support for timeout, specified during exec call, after which the error handler gets called with a defined errorCode. */ -namespace Async { +namespace KAsync { template class Executor; @@ -75,15 +77,15 @@ template class Job; template -using ThenTask = typename detail::identity&)>>::type; +using ThenTask = typename detail::identity&)>>::type; template using SyncThenTask = typename detail::identity>::type; template -using EachTask = typename detail::identity&)>>::type; +using EachTask = typename detail::identity&)>>::type; template using SyncEachTask = typename detail::identity>::type; template -using ReduceTask = typename detail::identity&)>>::type; +using ReduceTask = typename detail::identity&)>>::type; template using SyncReduceTask = typename detail::identity>::type; @@ -96,15 +98,15 @@ namespace Private class ExecutorBase; typedef QSharedPointer ExecutorBasePtr; -struct Execution { +struct KASYNC_EXPORT Execution { Execution(const ExecutorBasePtr &executor); ~Execution(); void setFinished(); template - Async::Future* result() const + KAsync::Future* result() const { - return static_cast*>(resultBase); + return static_cast*>(resultBase); } void releaseFuture(); @@ -125,16 +127,16 @@ struct Execution { typedef QSharedPointer ExecutionPtr; -class ExecutorBase +class KASYNC_EXPORT ExecutorBase { template friend class Executor; template - friend class Async::Job; + friend class KAsync::Job; friend class Execution; - friend class Async::Tracer; + friend class KAsync::Tracer; public: virtual ~ExecutorBase(); @@ -144,7 +146,7 @@ protected: ExecutorBase(const ExecutorBasePtr &parent); template - Async::Future* createFuture(const ExecutionPtr &execution) const; + KAsync::Future* createFuture(const ExecutionPtr &execution) const; virtual bool hasErrorFunc() const = 0; virtual bool handleError(const ExecutionPtr &execution) = 0; @@ -193,7 +195,7 @@ public: void run(const ExecutionPtr &execution); private: EachTask mFunc; - QVector*> mFutureWatchers; + QVector*> mFutureWatchers; }; template @@ -234,8 +236,8 @@ public: SyncEachExecutor(SyncEachTask each, ErrorHandler errorFunc, const ExecutorBasePtr &parent); void run(const ExecutionPtr &execution); private: - void run(Async::Future *future, const typename PrevOut::value_type &arg, std::false_type); // !std::is_void - void run(Async::Future *future, const typename PrevOut::value_type &arg, std::true_type); // std::is_void + void run(KAsync::Future *future, const typename PrevOut::value_type &arg, std::false_type); // !std::is_void + void run(KAsync::Future *future, const typename PrevOut::value_type &arg, std::true_type); // std::is_void SyncEachTask mFunc; }; @@ -244,11 +246,11 @@ private: /** * Start an asynchronous job sequence. * - * Async::start() is your starting point to build a chain of jobs to be executed + * KAsync::start() is your starting point to build a chain of jobs to be executed * asynchronously. * * @param func An asynchronous function to be executed. The function must have - * void return type, and accept exactly one argument of type @p Async::Future, + * void return type, and accept exactly one argument of type @p KAsync::Future, * where @p In is type of the result. */ template @@ -267,14 +269,14 @@ Job start(); * * The loop continues while @param condition returns true. */ -Job dowhile(Condition condition, ThenTask func); +KASYNC_EXPORT Job dowhile(Condition condition, ThenTask func); /** * Async while loop. * * Loop continues while body returns true. */ -Job dowhile(ThenTask body); +KASYNC_EXPORT Job dowhile(ThenTask body); /** * Iterate over a container. @@ -287,7 +289,7 @@ Job iterate(const Out &container); /** * Async delay. */ -Job wait(int delay); +KASYNC_EXPORT Job wait(int delay); /** * A null job. @@ -307,7 +309,7 @@ Job null(); template Job error(int errorCode = 1, const QString &errorMessage = QString()); -class JobBase +class KASYNC_EXPORT JobBase { template friend class Job; @@ -324,18 +326,18 @@ protected: * An Asynchronous job * * A single instance of Job represents a single method that will be executed - * asynchrously. The Job is started by @p Job::exec(), which returns @p Async::Future + * asynchrously. The Job is started by @p Job::exec(), which returns @p KAsync::Future * immediatelly. The Future will be set to finished state once the asynchronous - * task has finished. You can use @p Async::Future::waitForFinished() to wait for + * task has finished. You can use @p KAsync::Future::waitForFinished() to wait for * for the Future in blocking manner. * * It is possible to chain multiple Jobs one after another in different fashion * (sequential, parallel, etc.). Calling Job::exec() will then return a pending - * @p Async::Future, and will execute the entire chain of jobs. + * @p KAsync::Future, and will execute the entire chain of jobs. * * @code * auto job = Job::start>( - * [](Async::Future> &future) { + * [](KAsync::Future> &future) { * MyREST::PendingUsers *pu = MyREST::requestListOfUsers(); * QObject::connect(pu, &PendingOperation::finished, * [&](PendingOperation *pu) { @@ -344,7 +346,7 @@ protected: * }); * }) * .each, int>( - * [](const int &userId, Async::Future> &future) { + * [](const int &userId, KAsync::Future> &future) { * MyREST::PendingUser *pu = MyREST::requestUserDetails(userId); * QObject::connect(pu, &PendingOperation::finished, * [&](PendingOperation *pu) { @@ -353,7 +355,7 @@ protected: * }); * }); * - * Async::Future> usersFuture = job.exec(); + * KAsync::Future> usersFuture = job.exec(); * usersFuture.waitForFinished(); * QList users = usersFuture.value(); * @endcode @@ -369,10 +371,10 @@ class Job : public JobBase friend class Job; template - friend Job start(Async::ThenTask func, ErrorHandler errorFunc); + friend Job start(KAsync::ThenTask func, ErrorHandler errorFunc); template - friend Job start(Async::SyncThenTask func, ErrorHandler errorFunc); + friend Job start(KAsync::SyncThenTask func, ErrorHandler errorFunc); #ifdef WITH_KJOB template @@ -454,7 +456,7 @@ public: } template - Async::Future exec(FirstIn in) + KAsync::Future exec(FirstIn in) { // Inject a fake sync executor that will return the initial value Private::ExecutorBasePtr first = mExecutor; @@ -474,10 +476,10 @@ public: return result; } - Async::Future exec() + KAsync::Future exec() { Private::ExecutionPtr execution = mExecutor->exec(mExecutor); - Async::Future result = *execution->result(); + KAsync::Future result = *execution->result(); return result; } @@ -499,15 +501,15 @@ private: template void reduceInvariants() { - static_assert(Async::detail::isIterable::value, + static_assert(KAsync::detail::isIterable::value, "The 'Result' task can only be connected to a job that returns a list or an array"); static_assert(std::is_same::value, "The return type of previous task must be compatible with input type of this task"); } template - inline std::function&)> nestedJobWrapper(Job otherJob) { - return [otherJob](InOther ... in, Async::Future &future) { + inline std::function&)> nestedJobWrapper(Job otherJob) { + return [otherJob](InOther ... in, KAsync::Future &future) { // copy by value is const auto job = otherJob; FutureWatcher *watcher = new FutureWatcher(); @@ -518,7 +520,7 @@ private: // in copyFutureValue() // copy by value is const auto outFuture = future; - Async::detail::copyFutureValue(watcher->future(), outFuture); + KAsync::detail::copyFutureValue(watcher->future(), outFuture); if (watcher->future().errorCode()) { outFuture.setError(watcher->future().errorCode(), watcher->future().errorMessage()); } else { @@ -531,12 +533,12 @@ private: } }; -} // namespace Async +} // namespace KAsync // ********** Out of line definitions **************** -namespace Async { +namespace KAsync { template Job start(ThenTask func, ErrorHandler error) @@ -557,7 +559,7 @@ template start() { return Job(Private::ExecutorBasePtr( - new Private::ThenExecutor([](const Args & ... args, Async::Future &future) + new Private::ThenExecutor([](const Args & ... args, KAsync::Future &future) { KJobType *job = new KJobType(args ...); job->connect(job, &KJob::finished, @@ -578,8 +580,8 @@ Job start() template Job null() { - return Async::start( - [](Async::Future &future) { + return KAsync::start( + [](KAsync::Future &future) { future.setFinished(); }); } @@ -587,8 +589,8 @@ Job null() template Job error(int errorCode, const QString &errorMessage) { - return Async::start( - [errorCode, errorMessage](Async::Future &future) { + return KAsync::start( + [errorCode, errorMessage](KAsync::Future &future) { future.setError(errorCode, errorMessage); }); } @@ -596,7 +598,7 @@ Job error(int errorCode, const QString &errorMessage) template Job iterate(const Out &container) { - return Async::start( + return KAsync::start( [container]() { return container; }); @@ -606,9 +608,9 @@ Job iterate(const Out &container) namespace Private { template -Async::Future* ExecutorBase::createFuture(const ExecutionPtr &execution) const +KAsync::Future* ExecutorBase::createFuture(const ExecutionPtr &execution) const { - return new Async::Future(execution); + return new KAsync::Future(execution); } template @@ -625,8 +627,8 @@ ExecutionPtr Executor::exec(const ExecutorBasePtr &self) execution->prevExecution = mPrev ? mPrev->exec(mPrev) : ExecutionPtr(); execution->resultBase = ExecutorBase::createFuture(execution); - auto fw = new Async::FutureWatcher(); - QObject::connect(fw, &Async::FutureWatcher::futureReady, + auto fw = new KAsync::FutureWatcher(); + QObject::connect(fw, &KAsync::FutureWatcher::futureReady, [fw, execution, this]() { handleError(execution); execution->setFinished(); @@ -634,7 +636,7 @@ ExecutionPtr Executor::exec(const ExecutorBasePtr &self) }); fw->setFuture(*execution->result()); - Async::Future *prevFuture = execution->prevExecution ? execution->prevExecution->result() : nullptr; + KAsync::Future *prevFuture = execution->prevExecution ? execution->prevExecution->result() : nullptr; if (!prevFuture || prevFuture->isFinished()) { if (prevFuture) { // prevFuture implies execution->prevExecution if (prevFuture->errorCode()) { @@ -655,8 +657,8 @@ ExecutionPtr Executor::exec(const ExecutorBasePtr &self) execution->isRunning = true; run(execution); } else { - auto prevFutureWatcher = new Async::FutureWatcher(); - QObject::connect(prevFutureWatcher, &Async::FutureWatcher::futureReady, + auto prevFutureWatcher = new KAsync::FutureWatcher(); + QObject::connect(prevFutureWatcher, &KAsync::FutureWatcher::futureReady, [prevFutureWatcher, execution, this]() { auto prevFuture = prevFutureWatcher->future(); assert(prevFuture.isFinished()); @@ -679,7 +681,7 @@ ExecutionPtr Executor::exec(const ExecutorBasePtr &self) run(execution); }); - prevFutureWatcher->setFuture(*static_cast*>(prevFuture)); + prevFutureWatcher->setFuture(*static_cast*>(prevFuture)); } return execution; @@ -712,7 +714,7 @@ ThenExecutor::ThenExecutor(ThenTask then, ErrorHandler template void ThenExecutor::run(const ExecutionPtr &execution) { - Async::Future::type> *prevFuture = nullptr; + KAsync::Future::type> *prevFuture = nullptr; if (execution->prevExecution) { prevFuture = execution->prevExecution->result::type>(); assert(prevFuture->isFinished()); @@ -744,17 +746,17 @@ void EachExecutor::run(const ExecutionPtr &execution) for (auto arg : prevFuture->value()) { //We have to manually manage the lifetime of these temporary futures - Async::Future *future = new Async::Future(); + KAsync::Future *future = new KAsync::Future(); EachExecutor::mFunc(arg, *future); - auto fw = new Async::FutureWatcher(); + auto fw = new KAsync::FutureWatcher(); mFutureWatchers.append(fw); - QObject::connect(fw, &Async::FutureWatcher::futureReady, + QObject::connect(fw, &KAsync::FutureWatcher::futureReady, [out, fw, this, future]() { assert(fw->future().isFinished()); const int index = mFutureWatchers.indexOf(fw); assert(index > -1); mFutureWatchers.removeAt(index); - Async::detail::aggregateFutureValue(fw->future(), *out); + KAsync::detail::aggregateFutureValue(fw->future(), *out); if (mFutureWatchers.isEmpty()) { out->setFinished(); } @@ -794,19 +796,19 @@ void SyncThenExecutor::run(const ExecutionPtr &execution) template void SyncThenExecutor::run(const ExecutionPtr &execution, std::false_type) { - Async::Future::type> *prevFuture = + KAsync::Future::type> *prevFuture = execution->prevExecution ? execution->prevExecution->result::type>() : nullptr; (void) prevFuture; // silence 'set but not used' warning - Async::Future *future = execution->result(); + KAsync::Future *future = execution->result(); future->setValue(SyncThenExecutor::mFunc(prevFuture ? prevFuture->value() : In() ...)); } template void SyncThenExecutor::run(const ExecutionPtr &execution, std::true_type) { - Async::Future::type> *prevFuture = + KAsync::Future::type> *prevFuture = execution->prevExecution ? execution->prevExecution->result::type>() : nullptr; @@ -842,13 +844,13 @@ void SyncEachExecutor::run(const ExecutionPtr &execution) } template -void SyncEachExecutor::run(Async::Future *out, const typename PrevOut::value_type &arg, std::false_type) +void SyncEachExecutor::run(KAsync::Future *out, const typename PrevOut::value_type &arg, std::false_type) { out->setValue(out->value() + SyncEachExecutor::mFunc(arg)); } template -void SyncEachExecutor::run(Async::Future * /* unused */, const typename PrevOut::value_type &arg, std::true_type) +void SyncEachExecutor::run(KAsync::Future * /* unused */, const typename PrevOut::value_type &arg, std::true_type) { SyncEachExecutor::mFunc(arg); } @@ -863,10 +865,10 @@ SyncReduceExecutor::SyncReduceExecutor(SyncReduceTask reduce, } // namespace Private -} // namespace Async +} // namespace KAsync -#endif // ASYNC_H +#endif // KASYNC_H 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 @@ /* - * Copyright 2014 Daniel Vrátil + * Copyright 2014 - 2015 Daniel Vrátil * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License as @@ -15,13 +15,13 @@ * along with this library. If not, see . */ -#ifndef ASYNC_IMPL_H -#define ASYNC_IMPL_H +#ifndef KASYNC_IMPL_H +#define KASYNC_IMPL_H #include "async.h" #include -namespace Async { +namespace KAsync { namespace detail { @@ -48,34 +48,34 @@ struct prevOut { template inline typename std::enable_if::value, void>::type -copyFutureValue(const Async::Future &in, Async::Future &out) +copyFutureValue(const KAsync::Future &in, KAsync::Future &out) { out.setValue(in.value()); } template inline typename std::enable_if::value, void>::type -copyFutureValue(const Async::Future &in, Async::Future &out) +copyFutureValue(const KAsync::Future &in, KAsync::Future &out) { // noop } template inline typename std::enable_if::value, void>::type -aggregateFutureValue(const Async::Future &in, Async::Future &out) +aggregateFutureValue(const KAsync::Future &in, KAsync::Future &out) { out.setValue(out.value() + in.value()); } template inline typename std::enable_if::value, void>::type -aggregateFutureValue(const Async::Future &in, Async::Future &out) +aggregateFutureValue(const KAsync::Future &in, KAsync::Future &out) { // noop } } // namespace Detail -} // namespace Async +} // namespace KAsync -#endif // ASYNC_IMPL_H +#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 @@ #include #endif -namespace Async +namespace KAsync { Q_LOGGING_CATEGORY(Debug, "org.kde.async", QtWarningMsg); @@ -37,15 +37,15 @@ QString demangleName(const char *name) int status = 1; // uses -3 to 0 error codes std::unique_ptr demangled(abi::__cxa_demangle(name, 0, 0, &status), std::free); if (status == 0) { - return QString(demangled.get()); + return QString::fromLatin1(demangled.get()); } #endif - return QString(name); + return QString::fromLatin1(name); } } -using namespace Async; +using namespace KAsync; int Tracer::lastId = 0; @@ -53,12 +53,12 @@ Tracer::Tracer(Private::Execution *execution) : mId(lastId++) , mExecution(execution) { - msg(Async::Tracer::Start); + msg(KAsync::Tracer::Start); } Tracer::~Tracer() { - msg(Async::Tracer::End); + msg(KAsync::Tracer::End); // FIXME: Does this work on parallel executions? --lastId; --mId; @@ -68,8 +68,8 @@ void Tracer::msg(Tracer::MsgType msgType) { #ifndef QT_NO_DEBUG qCDebug(Trace).nospace() << (QString().fill(QLatin1Char(' '), mId * 2) % - (msgType == Async::Tracer::Start ? " START " : " END ") % - QString::number(mId) % " " % + (msgType == KAsync::Tracer::Start ? QStringLiteral(" START ") : QStringLiteral(" END ")) % + QString::number(mId) % QStringLiteral(" ") % mExecution->executor->mExecutorName); #endif } 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 @@ * along with this library. If not, see . */ -#ifndef ASYNC_DEBUG_H -#define ASYNC_DEBUG_H +#ifndef KASYNC_DEBUG_H +#define KASYNC_DEBUG_H + +#include "kasync_export.h" #include #include @@ -25,20 +27,20 @@ #include #endif -namespace Async +namespace KAsync { Q_DECLARE_LOGGING_CATEGORY(Debug) Q_DECLARE_LOGGING_CATEGORY(Trace) -QString demangleName(const char *name); +KASYNC_EXPORT QString demangleName(const char *name); namespace Private { class Execution; } -class Tracer +class KASYNC_EXPORT Tracer { public: Tracer(Private::Execution *execution); @@ -62,7 +64,7 @@ private: #ifndef QT_NO_DEBUG template QString storeExecutorNameExpanded() { - return Async::demangleName(typeid(T).name()); + return KAsync::demangleName(typeid(T).name()); } template @@ -77,4 +79,4 @@ private: #define STORE_EXECUTOR_NAME(...) #endif -#endif // ASYNC_DEBUG_H \ No newline at end of file +#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 @@ #include "future.h" #include "async.h" -using namespace Async; +using namespace KAsync; FutureBase::PrivateBase::PrivateBase(const Private::ExecutionPtr &execution) : finished(false) @@ -53,7 +53,7 @@ FutureBase::FutureBase(FutureBase::PrivateBase *dd) { } -FutureBase::FutureBase(const Async::FutureBase &other) +FutureBase::FutureBase(const KAsync::FutureBase &other) : d(other.d) { } 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 @@ #ifndef FUTURE_H #define FUTURE_H +#include "kasync_export.h" + class QEventLoop; #include @@ -27,7 +29,7 @@ class QEventLoop; #include #include -namespace Async { +namespace KAsync { class FutureWatcherBase; template @@ -40,9 +42,9 @@ class ExecutorBase; typedef QSharedPointer ExecutionPtr; } // namespace Private -class FutureBase +class KASYNC_EXPORT FutureBase { - friend class Async::Private::Execution; + friend class KAsync::Private::Execution; friend class FutureWatcherBase; public: @@ -61,7 +63,7 @@ protected: class PrivateBase : public QSharedData { public: - PrivateBase(const Async::Private::ExecutionPtr &execution); + PrivateBase(const KAsync::Private::ExecutionPtr &execution); virtual ~PrivateBase(); void releaseExecution(); @@ -72,14 +74,14 @@ protected: QVector> watchers; private: - QWeakPointer mExecution; + QWeakPointer mExecution; }; FutureBase(); FutureBase(FutureBase::PrivateBase *dd); FutureBase(const FutureBase &other); - void addWatcher(Async::FutureWatcherBase *watcher); + void addWatcher(KAsync::FutureWatcherBase *watcher); void releaseExecution(); protected: @@ -105,14 +107,14 @@ public: } FutureWatcher watcher; QEventLoop eventLoop; - QObject::connect(&watcher, &Async::FutureWatcher::futureReady, + QObject::connect(&watcher, &KAsync::FutureWatcher::futureReady, &eventLoop, &QEventLoop::quit); - watcher.setFuture(*static_cast*>(this)); + watcher.setFuture(*static_cast*>(this)); eventLoop.exec(); } protected: - FutureGeneric(const Async::Private::ExecutionPtr &execution) + FutureGeneric(const KAsync::Private::ExecutionPtr &execution) : FutureBase(new Private(execution)) {} @@ -124,7 +126,7 @@ protected: class Private : public FutureBase::PrivateBase { public: - Private(const Async::Private::ExecutionPtr &execution) + Private(const KAsync::Private::ExecutionPtr &execution) : FutureBase::PrivateBase(execution) {} @@ -137,14 +139,14 @@ protected: template class Future : public FutureGeneric { - friend class Async::Private::ExecutorBase; + friend class KAsync::Private::ExecutorBase; template - friend class Async::FutureWatcher; + friend class KAsync::FutureWatcher; public: Future() - : FutureGeneric(Async::Private::ExecutionPtr()) + : FutureGeneric(KAsync::Private::ExecutionPtr()) {} Future(const Future &other) @@ -162,7 +164,7 @@ public: } protected: - Future(const Async::Private::ExecutionPtr &execution) + Future(const KAsync::Private::ExecutionPtr &execution) : FutureGeneric(execution) {} @@ -171,11 +173,11 @@ protected: template<> class Future : public FutureGeneric { - friend class Async::Private::ExecutorBase; + friend class KAsync::Private::ExecutorBase; public: Future() - : FutureGeneric(Async::Private::ExecutionPtr()) + : FutureGeneric(KAsync::Private::ExecutionPtr()) {} Future(const Future &other) @@ -183,7 +185,7 @@ public: {} protected: - Future(const Async::Private::ExecutionPtr &execution) + Future(const KAsync::Private::ExecutionPtr &execution) : FutureGeneric(execution) {} }; @@ -192,7 +194,7 @@ protected: -class FutureWatcherBase : public QObject +class KASYNC_EXPORT FutureWatcherBase : public QObject { Q_OBJECT @@ -209,12 +211,12 @@ protected: void futureReadyCallback(); void futureProgressCallback(qreal progress); - void setFutureImpl(const Async::FutureBase &future); + void setFutureImpl(const KAsync::FutureBase &future); protected: class Private { public: - Async::FutureBase future; + KAsync::FutureBase future; }; Private * const d; @@ -226,7 +228,7 @@ private: template class FutureWatcher : public FutureWatcherBase { - friend class Async::FutureGeneric; + friend class KAsync::FutureGeneric; public: FutureWatcher(QObject *parent = nullptr) @@ -236,14 +238,14 @@ public: ~FutureWatcher() {} - void setFuture(const Async::Future &future) + void setFuture(const KAsync::Future &future) { - setFutureImpl(*static_cast(&future)); + setFutureImpl(*static_cast(&future)); } - Async::Future future() const + KAsync::Future future() const { - return *static_cast*>(&d->future); + return *static_cast*>(&d->future); } private: -- cgit v1.2.3