diff --git a/CMakeLists.txt b/CMakeLists.txt index ce42978..b2bc655 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,65 +1,67 @@ cmake_minimum_required(VERSION 3.0) project(KAsync VERSION 0.3.0) # ECM setup find_package(ECM 5.10.0 CONFIG REQUIRED) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) set(CMAKE_CXX_STANDARD 14) include(KDEInstallDirs) include(KDECMakeSettings) include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) include(GenerateExportHeader) include(ECMGenerateHeaders) include(ECMGeneratePriFile) include(CMakePackageConfigHelpers) include(ECMSetupVersion) include(FeatureSummary) +kde_enable_exceptions() + set(QT_REQUIRED_VERSION "5.2.0") ecm_setup_version(${KAsync_VERSION} VARIABLE_PREFIX KASYNC VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/kasync_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KAsyncConfigVersion.cmake" SOVERSION 0 ) ########### Find packages ########### find_package(Qt5 ${QT_REQUIRED_VERSION} REQUIRED COMPONENTS Core Test) ########### Targets ########### add_subdirectory(src) add_subdirectory(autotests) ########### CMake Config Files ########### set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KAsync") configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KAsyncConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KAsyncConfig.cmake" INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KAsyncConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KAsyncConfigVersion.cmake" DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) install(EXPORT KAsyncTargets DESTINATION "${CMAKECONFIG_INSTALL_DIR}" FILE KAsyncTargets.cmake ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kasync_version.h DESTINATION ${KDE_INSTALL_INCLUDEDIR} COMPONENT Devel ) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index c04c025..0c1cdb2 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,6 +1,11 @@ include(ECMAddTests) ecm_add_test(asynctest.cpp TEST_NAME asynctest LINK_LIBRARIES KAsync Qt5::Test ) +ecm_add_test(continuationstest.cpp + TEST_NAME continuationstest + LINK_LIBRARIES KAsync Qt5::Test +) + diff --git a/autotests/continuationstest.cpp b/autotests/continuationstest.cpp new file mode 100644 index 0000000..ac055f3 --- /dev/null +++ b/autotests/continuationstest.cpp @@ -0,0 +1,116 @@ +/* + * Copyright 2019 Daniel Vrátil + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include +#include + +#define KASYNC_TEST + +#include "../src/continuations_p.h" + +namespace KAsync +{ +// Simplified definition of KAsync::Job so we can return it from +// the test continuations +template +class Job {}; +} + +Q_DECLARE_METATYPE(std::function) + +#define STATIC_COMPARE(actual, expected) \ + static_assert((actual) == (expected), "Check failed: " #actual " == " #expected) + +class ContinuationHolderTest : public QObject +{ + Q_OBJECT + + using TestedHolder = KAsync::Private::ContinuationHolder; + template + void testContinuation(T &&func, int index) + { + + #define CHECK(Cont) \ + QCOMPARE(KAsync::Private::continuationIs>(holder), (std::is_same>::value)) + + TestedHolder holder(Continuation(std::move(func))); + QCOMPARE(holder.mIndex, index); + CHECK(SyncContinuation); + CHECK(SyncErrorContinuation); + CHECK(AsyncContinuation); + CHECK(AsyncErrorContinuation); + CHECK(JobContinuation); + CHECK(JobErrorContinuation); + + //KAsync::Private::continuation_get(holder)(); + } + +private Q_SLOTS: + void testTupleMax() + { + using TestHolder = KAsync::Private::ContinuationHolder; + using Tuple = std::tuple; + + STATIC_COMPARE(TestHolder::tuple_max::size, sizeof(uint32_t)); + STATIC_COMPARE(TestHolder::tuple_max::alignment, alignof(uint32_t)); + } + + void testTupleIndex() + { + using TestHolder = KAsync::Private::ContinuationHolder; + using Tuple = std::tuple; + + STATIC_COMPARE((TestHolder::tuple_index::value), 0); + STATIC_COMPARE((TestHolder::tuple_index::value), 1); + STATIC_COMPARE((TestHolder::tuple_index::value), 2); + } + + void testContinuationHolder_data() + { + QTest::addColumn>("func"); + +#define ADD_ROW(name, lambda, index) \ + QTest::newRow(#name) << std::function([this]() { \ + bool called = true; \ + testContinuation>(lambda, index); \ + return called; \ + }); + + ADD_ROW(AsyncContinuation, [&called](KAsync::Future &) mutable { called = true; }, 0); + ADD_ROW(AsyncErrorContinuation, [&called](const KAsync::Error &, KAsync::Future &) mutable { called = true; }, 1); +ADD_ROW(SyncContinuation, [&called]() mutable { called = true; }, 2) + ADD_ROW(SyncErrorContinuation, [&called](const KAsync::Error &) mutable { called = true; }, 3); + ADD_ROW(JobContinuation, [&called]() mutable { called = true; return KAsync::Job(); }, 4); + ADD_ROW(JobErrorContinuation, [&called](const KAsync::Error &) mutable { called = true; return KAsync::Job(); }, 5); + } + + void testContinuationHolder() + { + QFETCH(std::function, func); + + QVERIFY(func()); + } +}; + +QTEST_GUILESS_MAIN(ContinuationHolderTest) + +#include "continuationstest.moc" + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0157167..2492982 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,61 +1,62 @@ set(kasync_SRCS async.cpp future.cpp debug.cpp ) set(kasync_priv_HEADERS + continuations_p.h async_impl.h job_impl.h debug.h ) ecm_generate_headers(kasync_HEADERS HEADER_NAMES Async Future REQUIRED_HEADERS kasync_HEADERS ) add_library(KAsync ${kasync_SRCS}) generate_export_header(KAsync BASE_NAME kasync) target_include_directories(KAsync INTERFACE "$") target_include_directories(KAsync PUBLIC "$") target_link_libraries(KAsync PUBLIC Qt5::Core ) set_target_properties(KAsync PROPERTIES VERSION ${KASYNC_VERSION_STRING} SOVERSION ${KASYNC_SOVERSION} EXPORT_NAME KAsync ) ecm_generate_pri_file(BASE_NAME KAsync LIB_NAME KAsync FILENAME_VAR PRI_FILENAME ) install(TARGETS KAsync EXPORT KAsyncTargets ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kasync_export.h ${kasync_HEADERS} ${kasync_priv_HEADERS} DESTINATION ${KDE_INSTALL_INCLUDEDIR}/KAsync COMPONENT Devel ) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR} ) diff --git a/src/async.h b/src/async.h index a7bb76d..252ffc4 100644 --- a/src/async.h +++ b/src/async.h @@ -1,755 +1,708 @@ /* * Copyright 2014 - 2015 Daniel Vrátil * Copyright 2016 Daniel Vrátil * Copyright 2016 Christian Mollekopf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library. If not, see . */ #ifndef KASYNC_H #define KASYNC_H #include "kasync_export.h" #include #include #include #include #include #include #include "future.h" #include "debug.h" #include "async_impl.h" +#include "continuations_p.h" #include #include #include #include #include /** * @mainpage KAsync * * @brief API to help write async code. * * This API is based around jobs that take lambdas to execute asynchronous tasks. * Each async operation can take a continuation that can then be used to execute * further async operations. That way it is possible to build async chains of * operations that can be stored and executed later on. Jobs can be composed, * similarly to functions. * * Relations between the components: * * Job: API wrapper around Executors chain. Can be destroyed while still running, * because the actual execution happens in the background * * Executor: Describes task to execute. Executors form a linked list matching the * order in which they will be executed. The Executor chain is destroyed when * the parent Job is destroyed. However if the Job is still running it is * guaranteed that the Executor chain will not be destroyed until the execution * is finished. * * Execution: The running execution of the task stored in Executor. Each call to * Job::exec() instantiates new Execution chain, which makes it possible for * the Job to be executed multiple times (even in parallel). * * Future: Representation of the result that is being calculated * * * 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 KAsync { template class Executor; class JobBase; template class Job; -template -using AsyncContinuation = typename detail::identity&)>>::type; - -template -using AsyncErrorContinuation = typename detail::identity&)>>::type; - -template -using SyncContinuation = typename detail::identity>::type; - -template -using SyncErrorContinuation = typename detail::identity>::type; - -template -using JobContinuation = typename detail::identity(In ...)>>::type; - -template -using JobErrorContinuation = typename detail::identity(const KAsync::Error &, In ...)>>::type; - //@cond PRIVATE namespace Private { class ExecutorBase; typedef QSharedPointer ExecutorBasePtr; class ExecutionContext; struct KASYNC_EXPORT Execution { explicit Execution(const ExecutorBasePtr &executor); virtual ~Execution(); void setFinished(); template KAsync::Future* result() const { return static_cast*>(resultBase); } void releaseFuture(); ExecutorBasePtr executor; ExecutionPtr prevExecution; std::unique_ptr tracer; FutureBase *resultBase = nullptr; }; - -template -struct ContinuationHolder { - ContinuationHolder(AsyncContinuation &&func) - : asyncContinuation(std::move(func)) - {}; - ContinuationHolder(AsyncErrorContinuation &&func) - : asyncErrorContinuation(std::move(func)) - {}; - ContinuationHolder(SyncContinuation &&func) - : syncContinuation(std::move(func)) - {} - ContinuationHolder(SyncErrorContinuation &&func) - : syncErrorContinuation(std::move(func)) - {} - ContinuationHolder(JobContinuation &&func) - : jobContinuation(std::move(func)) - {}; - ContinuationHolder(JobErrorContinuation &&func) - : jobErrorContinuation(std::move(func)) - {}; - - AsyncContinuation asyncContinuation; - AsyncErrorContinuation asyncErrorContinuation; - SyncContinuation syncContinuation; - SyncErrorContinuation syncErrorContinuation; - JobContinuation jobContinuation; - JobErrorContinuation jobErrorContinuation; -}; - typedef QSharedPointer ExecutionPtr; class KASYNC_EXPORT ExecutorBase { template friend class Executor; template friend class KAsync::Job; friend struct Execution; friend class KAsync::Tracer; public: virtual ~ExecutorBase() = default; virtual ExecutionPtr exec(const ExecutorBasePtr &self, QSharedPointer context) = 0; protected: ExecutorBase(const ExecutorBasePtr &parent) : mPrev(parent) {} template KAsync::Future* createFuture(const ExecutionPtr &execution) const; ExecutorBasePtr mPrev; void prepend(const ExecutorBasePtr &e) { if (mPrev) { mPrev->prepend(e); } else { mPrev = e; } } void addToContext(const QVariant &entry) { mContext << entry; } void guard(const QObject *o) { mGuards.append(QPointer{o}); } QString mExecutorName; QVector mContext; QVector> mGuards; }; enum ExecutionFlag { Always, ErrorCase, GoodCase }; template class Executor : public ExecutorBase { protected: Executor(const Private::ExecutorBasePtr &parent, ExecutionFlag executionFlag) : ExecutorBase(parent) , executionFlag(executionFlag) {} virtual ~Executor() {} virtual void run(const ExecutionPtr &execution) = 0; ExecutionPtr exec(const ExecutorBasePtr &self, QSharedPointer context) override; const ExecutionFlag executionFlag; private: void runExecution(const KAsync::Future *prevFuture, const ExecutionPtr &execution, bool guardIsBroken); }; } // namespace Private //@endcond template Job startImpl(Private::ContinuationHolder &&); template Job syncStartImpl(SyncContinuation &&); /** * @relates Job * * Start an asynchronous job sequence. * * start() is your starting point to build a chain of jobs to be executed * asynchronously. * * @param func A continuation to be executed. */ ///Sync continuation without job: [] () -> T { ... } template auto start(F &&func) -> std::enable_if_t() ...))>::value, Job() ...)), In...>> { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); return syncStartImpl(std::forward(func)); } ///continuation with job: [] () -> KAsync::Job<...> { ... } template auto start(F &&func) -> std::enable_if_t() ...))>::value, Job() ...))::OutType, In...>> { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); return startImpl(Private::ContinuationHolder(JobContinuation(std::forward(func)))); } ///Handle continuation: [] (KAsync::Future, ...) { ... } template auto start(AsyncContinuation &&func) -> Job { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); return startImpl(Private::ContinuationHolder(std::forward>(func))); } enum ControlFlowFlag { Break, Continue }; /** * @relates Job * * Async while loop. * * Loop continues while body returns ControlFlowFlag::Continue. */ KASYNC_EXPORT Job doWhile(const Job &body); /** * @relates Job * * Async while loop. * * Shorthand that takes a continuation. * * @see doWhile */ KASYNC_EXPORT Job doWhile(const JobContinuation &body); /** * @relates Job * * Async delay. */ KASYNC_EXPORT Job wait(int delay); /** * @relates Job * * A null job. * * An async noop. * */ template Job null(); /** * @relates Job * * Async value. */ template Job value(Out); /** * @relates Job * * Async foreach loop. * * This will execute a job for every value in the list. * Errors while not stop processing of other jobs but set an error on the wrapper job. */ template Job forEach(KAsync::Job job); /** * @relates Job * * Async foreach loop. * * Shorthand that takes a continuation. * * @see serialForEach */ template Job forEach(JobContinuation &&); /** * @relates Job * * Serial Async foreach loop. * * This will execute a job for every value in the list sequentially. * Errors while not stop processing of other jobs but set an error on the wrapper job. */ template Job serialForEach(KAsync::Job job); /** * @relates Job * * Serial Async foreach loop. * * Shorthand that takes a continuation. * * @see serialForEach */ template Job serialForEach(JobContinuation &&); /** * @relates Job * * An error job. * * An async error. * */ template Job error(int errorCode = 1, const QString &errorMessage = QString()); /** * @relates Job * * An error job. * * An async error. * */ template Job error(const char *); /** * @relates Job * * An error job. * * An async error. * */ template Job error(const Error &); //@cond PRIVATE class KASYNC_EXPORT JobBase { template friend class Job; public: explicit JobBase(const Private::ExecutorBasePtr &executor) : mExecutor(executor) {} virtual ~JobBase() = default; protected: Private::ExecutorBasePtr mExecutor; }; //@endcond /** * @brief An Asynchronous job * * A single instance of Job represents a single method that will be executed * asynchronously. The Job is started by exec(), which returns Future * immediatelly. The Future will be set to finished state once the asynchronous * task has finished. You can use 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 exec() will then return a pending * Future, and will execute the entire chain of jobs. * * @code * auto job = Job::start>( * [](KAsync::Future> &future) { * MyREST::PendingUsers *pu = MyREST::requestListOfUsers(); * QObject::connect(pu, &PendingOperation::finished, * [&](PendingOperation *pu) { * future->setValue(dynamic_cast(pu)->userIds()); * future->setFinished(); * }); * }) * .each, int>( * [](const int &userId, KAsync::Future> &future) { * MyREST::PendingUser *pu = MyREST::requestUserDetails(userId); * QObject::connect(pu, &PendingOperation::finished, * [&](PendingOperation *pu) { * future->setValue(Qlist() << dynamic_cast(pu)->user()); * future->setFinished(); * }); * }); * * KAsync::Future> usersFuture = job.exec(); * usersFuture.waitForFinished(); * QList users = usersFuture.value(); * @endcode * * In the example above, calling @p job.exec() will first invoke the first job, * which will retrieve a list of IDs and then will invoke the second function * for each single entry in the list returned by the first function. */ template class Job : public JobBase { //@cond PRIVATE template friend class Job; template friend Job startImpl(Private::ContinuationHolder &&); template friend Job syncStartImpl(SyncContinuation &&); template friend Job forEach(KAsync::Job job); template friend Job serialForEach(KAsync::Job job); // Used to disable implicit conversion of Job which triggers // comiler warning. struct IncompleteType; //@endcond public: typedef Out OutType; ///A continuation template Job then(const Job &job) const; ///Shorthands for a job that returns another job from it's continuation // //OutOther and InOther are only there fore backwards compatibility, but are otherwise ignored. //It should never be neccessary to specify any template arguments, as they are automatically deduced from the provided argument. // //We currently have to write a then overload for: //* One argument in the continuation //* No argument in the continuation //* One argument + error in the continuation //* No argument + error in the continuation //This is due to how we extract the return type with "decltype(func(std::declval()))". //Ideally we could conflate this into at least fewer overloads, but I didn't manage so far and this at least works as expected. ///Continuation returning job: [] (Arg) -> KAsync::Job<...> { ... } template auto then(F &&func) const -> std::enable_if_t()))>::value, Job()))::OutType, In...>> { using ResultJob = decltype(func(std::declval())); //Job return thenImpl( {JobContinuation(std::forward(func))}, Private::ExecutionFlag::GoodCase); } ///Void continuation with job: [] () -> KAsync::Job<...> { ... } template auto then(F &&func) const -> std::enable_if_t::value, Job> { using ResultJob = decltype(func()); //Job return thenImpl( {JobContinuation(std::forward(func))}, Private::ExecutionFlag::GoodCase); } ///Error continuation returning job: [] (KAsync::Error, Arg) -> KAsync::Job<...> { ... } template auto then(F &&func) const -> std::enable_if_t()))>::value, Job()))::OutType, In...>> { using ResultJob = decltype(func(KAsync::Error{}, std::declval())); //Job return thenImpl( {JobErrorContinuation(std::forward(func))}, Private::ExecutionFlag::Always); } ///Error void continuation returning job: [] (KAsync::Error) -> KAsync::Job<...> { ... } template auto then(F &&func) const -> std::enable_if_t::value, Job> { using ResultJob = decltype(func(KAsync::Error{})); return thenImpl( {JobErrorContinuation(std::forward(func))}, Private::ExecutionFlag::Always); } ///Sync continuation: [] (Arg) -> void { ... } template auto then(F &&func) const -> std::enable_if_t()))>::value, Job())), In...>> { using ResultType = decltype(func(std::declval())); //QString return syncThenImpl( {SyncContinuation(std::forward(func))}, Private::ExecutionFlag::GoodCase); } ///Sync void continuation: [] () -> void { ... } template auto then(F &&func) const -> std::enable_if_t::value, Job> { using ResultType = decltype(func()); //QString return syncThenImpl( {SyncContinuation(std::forward(func))}, Private::ExecutionFlag::GoodCase); } ///Sync error continuation: [] (KAsync::Error, Arg) -> void { ... } template auto then(F &&func) const -> std::enable_if_t()))>::value, Job())),In...>> { using ResultType = decltype(func(KAsync::Error{}, std::declval())); //QString return syncThenImpl( {SyncErrorContinuation(std::forward(func))}, Private::ExecutionFlag::Always); } ///Sync void error continuation: [] (KAsync::Error) -> void { ... } template auto then(F &&func) const -> std::enable_if_t::value, Job> { using ResultType = decltype(func(KAsync::Error{})); return syncThenImpl( {SyncErrorContinuation(std::forward(func))}, Private::ExecutionFlag::Always); } ///Shorthand for a job that receives the error and a handle template Job then(AsyncContinuation &&func) const { return thenImpl({std::forward>(func)}, Private::ExecutionFlag::GoodCase); } ///Shorthand for a job that receives the error and a handle template Job then(AsyncErrorContinuation &&func) const { return thenImpl({std::forward>(func)}, Private::ExecutionFlag::Always); } ///Shorthand for a job that receives the error only Job onError(SyncErrorContinuation &&errorFunc) const; /** * Shorthand for a forEach loop that automatically uses the return type of * this job to deduce the type exepected. */ template::value, int> = 0> Job each(JobContinuation &&func) const { eachInvariants(); return then(forEach(std::forward>(func))); } /** * Shorthand for a serialForEach loop that automatically uses the return type * of this job to deduce the type exepected. */ template::value, int> = 0> Job serialEach(JobContinuation &&func) const { eachInvariants(); return then(serialForEach(std::forward>(func))); } /** * Enable implicit conversion to Job. * * This is necessary in assignments that only use the return value (which is the normal case). * This avoids constructs like: * auto job = KAsync::start( ... ) * .then( ... ) * .then([](){}); //Necessary for the assignment without the implicit conversion */ template operator std::conditional_t::value, IncompleteType, Job>(); /** * Adds an unnamed value to the context. * The context is guaranteed to persist until the jobs execution has finished. * * Useful for setting smart pointer to manage lifetime of objects required * during the execution of the job. */ template Job &addToContext(const T &value) { assert(mExecutor); mExecutor->addToContext(QVariant::fromValue(value)); return *this; } /** * Adds a guard. * It is guaranteed that no callback is executed after the guard vanishes. * * Use this i.e. ensure you don't call-back into an already destroyed object. */ Job &guard(const QObject *o) { assert(mExecutor); mExecutor->guard(o); return *this; } /** * @brief Starts execution of the job chain. * * This will start the execution of the task chain, starting from the * first one. It is possible to call this function multiple times, each * invocation will start a new processing and provide a new Future to * watch its status. * * @param in Argument to be passed to the very first task * @return Future<Out> object which will contain result of the last * task once if finishes executing. See Future documentation for more details. * * @see exec(), Future */ template KAsync::Future exec(FirstIn in); /** * @brief Starts execution of the job chain. * * This will start the execution of the task chain, starting from the * first one. It is possible to call this function multiple times, each * invocation will start a new processing and provide a new Future to * watch its status. * * @return Future<Out> object which will contain result of the last * task once if finishes executing. See Future documentation for more details. * * @see exec(FirstIn in), Future */ KAsync::Future exec(); explicit Job(JobContinuation &&func); explicit Job(AsyncContinuation &&func); private: //@cond PRIVATE explicit Job(Private::ExecutorBasePtr executor); template Job thenImpl(Private::ContinuationHolder helper, Private::ExecutionFlag execFlag = Private::ExecutionFlag::GoodCase) const; template Job syncThenImpl(SyncContinuation &&func, Private::ExecutionFlag execFlag = Private::ExecutionFlag::GoodCase) const; template Job syncThenImpl(SyncErrorContinuation &&func, Private::ExecutionFlag execFlag = Private::ExecutionFlag::Always) const; template void thenInvariants() const; //Base case for an empty parameter pack template auto thenInvariants() const -> std::enable_if_t<(sizeof...(InOther) == 0)>; template void eachInvariants() const; //@endcond }; } // namespace KAsync // out-of-line definitions of Job methods #include "job_impl.h" #endif // KASYNC_H diff --git a/src/async_impl.h b/src/async_impl.h index 9abfd85..545ee66 100644 --- a/src/async_impl.h +++ b/src/async_impl.h @@ -1,97 +1,94 @@ /* * Copyright 2014 - 2015 Daniel Vrátil * Copyright 2016 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 * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library. If not, see . */ #ifndef KASYNC_IMPL_H #define KASYNC_IMPL_H -#include "async.h" #include +#include //@cond PRIVATE namespace KAsync { -namespace detail { - template -struct identity -{ - typedef T type; -}; +class Future; + +namespace detail { template struct isIterable { enum { value = 0 }; }; template struct isIterable> { enum { value = 1 }; }; template struct prevOut { using type = std::tuple_element_t<0, std::tuple>; }; template struct funcHelper { using type = void(T::*)(In ..., KAsync::Future &); }; template struct syncFuncHelper { using type = Out(T::*)(In ...); }; template inline std::enable_if_t::value, void> copyFutureValue(const KAsync::Future &in, KAsync::Future &out) { out.setValue(in.value()); } template inline std::enable_if_t::value, void> copyFutureValue(const KAsync::Future &/* in */, KAsync::Future &/* out */) { // noop } template inline std::enable_if_t::value, void> aggregateFutureValue(const KAsync::Future &in, KAsync::Future &out) { out.setValue(out.value() + in.value()); } template inline std::enable_if_t::value, void> aggregateFutureValue(const KAsync::Future & /*in */, KAsync::Future & /*out */) { // noop } } // namespace Detail } // namespace KAsync //@endcond #endif // KASYNC_IMPL_H diff --git a/src/continuations_p.h b/src/continuations_p.h new file mode 100644 index 0000000..89eaa9c --- /dev/null +++ b/src/continuations_p.h @@ -0,0 +1,242 @@ +/* + * Copyright 2014 - 2015 Daniel Vrátil + * Copyright 2016 - 2019 Daniel Vrátil + * Copyright 2016 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library. If not, see . + */ + +#ifndef KASYNC_CONTINUATIONS_P_H_ +#define KASYNC_CONTINUATIONS_P_H_ + +#include +#include +#include + +namespace KAsync +{ + +template +class Job; + +template +class Future; + +struct Error; + +//@cond PRIVATE +namespace detail { +template +struct identity { + using type = T; +}; +template +using identity_t = typename identity::type; +} +//@endcond + +template +using AsyncContinuation = detail::identity_t&)>>; + +template +using AsyncErrorContinuation = detail::identity_t&)>>; + +template +using SyncContinuation = detail::identity_t>; + +template +using SyncErrorContinuation = detail::identity_t>; + +template +using JobContinuation = detail::identity_t(In ...)>>; + +template +using JobErrorContinuation = detail::identity_t(const KAsync::Error &, In ...)>>; + +//@cond PRIVATE +namespace Private +{ +/** + * FIXME: This should be a simple alias to std::variant once we can depend on C++17. + */ +template +struct ContinuationHolder +{ +#ifndef KASYNC_TEST +private: +#endif + using Tuple = std::tuple< + AsyncContinuation, + AsyncErrorContinuation, + SyncContinuation, + SyncErrorContinuation, + JobContinuation, + JobErrorContinuation + >; + + template + struct tuple_max; + + template + struct tuple_max> { + static constexpr std::size_t size = sizeof(T); + static constexpr std::size_t alignment = alignof(T); + }; + template + struct tuple_max> { + static constexpr std::size_t size = std::max(sizeof(T), tuple_max>::size); + static constexpr std::size_t alignment = std::max(alignof(T), tuple_max>::alignment); + }; + + template + struct tuple_index; + + template + struct tuple_index> { + static constexpr std::size_t value = 0; + }; + template + struct tuple_index> { + static constexpr std::size_t value = tuple_index>::value + 1; + }; + + template + inline static void move_helper(void *storage, void *data) { + new (storage) T(std::move(*reinterpret_cast(data))); + } + + template + inline static void destroy_helper(void *storage) { + reinterpret_cast(storage)->~T(); + } + + template::value - 1> + struct storage_helper { + using T = std::tuple_element_t; + inline static void move(std::size_t index, void *storage, void *data) { + if (I == index) { + move_helper(storage, data); + } else { + storage_helper::move(index, storage, data); + } + } + inline static void destroy(std::size_t index, void *storage) { + if (I == index) { + destroy_helper(storage); + } else { + storage_helper::destroy(index, storage); + } + } + }; + + template + struct storage_helper { + using T = std::tuple_element_t<0, Tuple>; + inline static void move(std::size_t, void *storage, void *data) { + move_helper(storage, data); + } + inline static void destroy(std::size_t, void *storage) { + destroy_helper(storage); + } + }; + + enum { + Invalid = std::numeric_limits::max() - 1 + }; + + std::size_t mIndex = Invalid; + std::aligned_storage_t::size, tuple_max::alignment> mStorage = {}; + +public: + #define KASYNC_P_DEFINE_CONSTRUCTOR(type) \ + ContinuationHolder(type &&cont) \ + : mIndex(tuple_index, Tuple>::value) \ + { \ + move_helper>(&mStorage, &cont); \ + } + KASYNC_P_DEFINE_CONSTRUCTOR(AsyncContinuation) + KASYNC_P_DEFINE_CONSTRUCTOR(AsyncErrorContinuation) + KASYNC_P_DEFINE_CONSTRUCTOR(SyncContinuation) + KASYNC_P_DEFINE_CONSTRUCTOR(SyncErrorContinuation) + KASYNC_P_DEFINE_CONSTRUCTOR(JobContinuation) + KASYNC_P_DEFINE_CONSTRUCTOR(JobErrorContinuation) + #undef KASYNC_P_DEFINE_CONSTRUCTOR + + ContinuationHolder(ContinuationHolder &&other) noexcept { + std::swap(mIndex, other.mIndex); + storage_helper::move(mIndex, &mStorage, &other.mStorage); + } + + ContinuationHolder &operator=(ContinuationHolder &&other) noexcept { + std::swap(mIndex, other.mIndex); + storage_helper::move(mIndex, &mStorage, &other.mStorage); + return *this; + } + + ContinuationHolder(const ContinuationHolder &) = delete; + ContinuationHolder &operator=(const ContinuationHolder &) = delete; + + ~ContinuationHolder() { + if (mIndex != Invalid) { + storage_helper::destroy(mIndex, &mStorage); + mIndex = Invalid; + } + } + + template + inline bool is() const { + return mIndex == tuple_index::value; + } + + template + inline const T &get() const { + if (!is()) { + throw std::bad_cast(); + } + return *reinterpret_cast(&mStorage); + } + + template + inline T &&get() { + if (!is()) { + throw std::bad_cast(); + } + return std::move(*reinterpret_cast(&mStorage)); + } +}; + +template +inline bool continuationIs(const Holder &holder) { + return holder.template is(); +} + +template +inline const T &continuationGet(const Holder &holder) { + return holder.template get(); +} + +template +inline T &&continuationGet(Holder &holder) { + return holder.template get(); +} + +} // namespace Private +//@endcond + + +} + + +#endif diff --git a/src/job_impl.h b/src/job_impl.h index afaca2e..ae66983 100644 --- a/src/job_impl.h +++ b/src/job_impl.h @@ -1,579 +1,580 @@ /* * Copyright 2014 - 2015 Daniel Vrátil * Copyright 2015 - 2016 Daniel Vrátil * Copyright 2016 Christian Mollekopf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library. If not, see . */ #ifndef KASYNC_JOB_IMPL_H #define KASYNC_JOB_IMPL_H #include "async.h" //@cond PRIVATE namespace KAsync { namespace Private { template class ThenExecutor: public Executor::type, Out, In ...> { public: ThenExecutor(ContinuationHolder &&workerHelper, const ExecutorBasePtr &parent = {}, ExecutionFlag executionFlag = ExecutionFlag::GoodCase) : Executor::type, Out, In ...>(parent, executionFlag) , mContinuationHolder(std::move(workerHelper)) { STORE_EXECUTOR_NAME("ThenExecutor", Out, In ...); } void run(const ExecutionPtr &execution) Q_DECL_OVERRIDE { KAsync::Future::type> *prevFuture = nullptr; if (execution->prevExecution) { prevFuture = execution->prevExecution->result::type>(); assert(prevFuture->isFinished()); } //Execute one of the available workers KAsync::Future *future = execution->result(); - const auto &helper = ThenExecutor::mContinuationHolder; - if (helper.asyncContinuation) { - helper.asyncContinuation(prevFuture ? prevFuture->value() : In() ..., *future); - } else if (helper.asyncErrorContinuation) { - helper.asyncErrorContinuation(prevFuture->hasError() ? prevFuture->errors().first() : Error(), - prevFuture ? prevFuture->value() : In() ..., *future); - } else if (helper.syncContinuation) { + const auto &continuation = ThenExecutor::mContinuationHolder; + if (continuationIs>(continuation)) { + continuationGet>(continuation)(prevFuture ? prevFuture->value() : In() ..., *future); + } else if (continuationIs>(continuation)) { + continuationGet>(continuation)( + prevFuture->hasError() ? prevFuture->errors().first() : Error(), + prevFuture ? prevFuture->value() : In() ..., *future); + } else if (continuationIs>(continuation)) { callAndApply(prevFuture ? prevFuture->value() : In() ..., - helper.syncContinuation, *future, std::is_void()); + continuationGet>(continuation), *future, std::is_void()); future->setFinished(); - } else if (helper.syncErrorContinuation) { + } else if (continuationIs>(continuation)) { assert(prevFuture); callAndApply(prevFuture->hasError() ? prevFuture->errors().first() : Error(), prevFuture ? prevFuture->value() : In() ..., - helper.syncErrorContinuation, *future, std::is_void()); + continuationGet>(continuation), *future, std::is_void()); future->setFinished(); - } else if (helper.jobContinuation) { + } else if (continuationIs>(continuation)) { executeJobAndApply(prevFuture ? prevFuture->value() : In() ..., - helper.jobContinuation, *future, std::is_void()); - } else if (helper.jobErrorContinuation) { + continuationGet>(continuation), *future, std::is_void()); + } else if (continuationIs>(continuation)) { executeJobAndApply(prevFuture->hasError() ? prevFuture->errors().first() : Error(), prevFuture ? prevFuture->value() : In() ..., - helper.jobErrorContinuation, *future, std::is_void()); + continuationGet>(continuation), *future, std::is_void()); } } private: void executeJobAndApply(In && ... input, const JobContinuation &func, Future &future, std::false_type) { func(std::forward(input) ...) .template then([&future](const KAsync::Error &error, const Out &v, KAsync::Future &f) { if (error) { future.setError(error); } else { future.setResult(v); } f.setFinished(); }).exec(); } void executeJobAndApply(In && ... input, const JobContinuation &func, Future &future, std::true_type) { func(std::forward(input) ...) .template then([&future](const KAsync::Error &error, KAsync::Future &f) { if (error) { future.setError(error); } else { future.setFinished(); } f.setFinished(); }).exec(); } void executeJobAndApply(const Error &error, In && ... input, const JobErrorContinuation &func, Future &future, std::false_type) { func(error, std::forward(input) ...) .template then([&future](const KAsync::Error &error, const Out &v, KAsync::Future &f) { if (error) { future.setError(error); } else { future.setResult(v); } f.setFinished(); }).exec(); } void executeJobAndApply(const Error &error, In && ... input, const JobErrorContinuation &func, Future &future, std::true_type) { func(error, std::forward(input) ...) .template then([&future](const KAsync::Error &error, KAsync::Future &f) { if (error) { future.setError(error); } else { future.setFinished(); } f.setFinished(); }).exec(); } void callAndApply(In && ... input, const SyncContinuation &func, Future &future, std::false_type) { future.setValue(func(std::forward(input) ...)); } void callAndApply(In && ... input, const SyncContinuation &func, Future &, std::true_type) { func(std::forward(input) ...); } void callAndApply(const Error &error, In && ... input, const SyncErrorContinuation &func, Future &future, std::false_type) { future.setValue(func(error, std::forward(input) ...)); } void callAndApply(const Error &error, In && ... input, const SyncErrorContinuation &func, Future &, std::true_type) { func(error, std::forward(input) ...); } ContinuationHolder mContinuationHolder; }; template KAsync::Future* ExecutorBase::createFuture(const ExecutionPtr &execution) const { return new KAsync::Future(execution); } template void Executor::runExecution(const KAsync::Future *prevFuture, const ExecutionPtr &execution, bool guardIsBroken) { if (guardIsBroken) { execution->resultBase->setFinished(); return; } if (prevFuture) { if (prevFuture->hasError() && executionFlag == ExecutionFlag::GoodCase) { //Propagate the error to the outer Future Q_ASSERT(prevFuture->errors().size() == 1); execution->resultBase->setError(prevFuture->errors().first()); return; } if (!prevFuture->hasError() && executionFlag == ExecutionFlag::ErrorCase) { //Propagate the value to the outer Future KAsync::detail::copyFutureValue(*prevFuture, *execution->result()); execution->resultBase->setFinished(); return; } } run(execution); } class ExecutionContext { public: typedef QSharedPointer Ptr; QVector> guards; bool guardIsBroken() const { for (const auto &g : guards) { if (!g) { return true; } } return false; } }; template ExecutionPtr Executor::exec(const ExecutorBasePtr &self, ExecutionContext::Ptr context) { /* * One executor per job, created with the construction of the Job object. * One execution per job per exec(), created only once exec() is called. * * The executors make up the linked list that makes up the complete execution chain. * * The execution then tracks the execution of each executor. */ // Passing 'self' to execution ensures that the Executor chain remains // valid until the entire execution is finished ExecutionPtr execution = ExecutionPtr::create(self); #ifndef QT_NO_DEBUG execution->tracer = std::make_unique(execution.data()); // owned by execution #endif context->guards += mGuards; // chainup execution->prevExecution = mPrev ? mPrev->exec(mPrev, context) : ExecutionPtr(); execution->resultBase = ExecutorBase::createFuture(execution); //We watch our own future to finish the execution once we're done auto fw = new KAsync::FutureWatcher(); QObject::connect(fw, &KAsync::FutureWatcher::futureReady, [fw, execution]() { execution->setFinished(); delete fw; }); fw->setFuture(*execution->result()); KAsync::Future *prevFuture = execution->prevExecution ? execution->prevExecution->result() : nullptr; if (!prevFuture || prevFuture->isFinished()) { //The previous job is already done runExecution(prevFuture, execution, context->guardIsBroken()); } else { //The previous job is still running and we have to wait for it's completion auto prevFutureWatcher = new KAsync::FutureWatcher(); QObject::connect(prevFutureWatcher, &KAsync::FutureWatcher::futureReady, [prevFutureWatcher, execution, this, context]() { auto prevFuture = prevFutureWatcher->future(); assert(prevFuture.isFinished()); delete prevFutureWatcher; runExecution(&prevFuture, execution, context->guardIsBroken()); }); prevFutureWatcher->setFuture(*static_cast*>(prevFuture)); } return execution; } } // namespace Private template template Job::operator std::conditional_t::value, IncompleteType, Job> () { return thenImpl({JobContinuation([](InOther ...){ return KAsync::null(); })}, {}); } template template Job Job::thenImpl(Private::ContinuationHolder workHelper, Private::ExecutionFlag execFlag) const { thenInvariants(); return Job(QSharedPointer>::create( std::forward>(workHelper), mExecutor, execFlag)); } template template Job Job::then(const Job &job) const { thenInvariants(); auto executor = job.mExecutor; executor->prepend(mExecutor); return Job(executor); } template template Job Job::syncThenImpl(SyncContinuation &&func, Private::ExecutionFlag execFlag) const { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); thenInvariants(); return Job(QSharedPointer>::create( Private::ContinuationHolder(std::forward>(func)), mExecutor, execFlag)); } template template Job Job::syncThenImpl(SyncErrorContinuation &&func, Private::ExecutionFlag execFlag) const { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); thenInvariants(); return Job(QSharedPointer>::create( Private::ContinuationHolder(std::forward>(func)), mExecutor, execFlag)); } template Job Job::onError(SyncErrorContinuation &&errorFunc) const { return Job(QSharedPointer>::create( // Extra indirection to allow propagating the result of a previous future when no // error occurs Private::ContinuationHolder([errorFunc = std::move(errorFunc)](const Error &error, const Out &val) { errorFunc(error); return val; }), mExecutor, Private::ExecutionFlag::ErrorCase)); } template<> // Specialize for void jobs inline Job Job::onError(SyncErrorContinuation &&errorFunc) const { return Job(QSharedPointer>::create( Private::ContinuationHolder(std::forward>(errorFunc)), mExecutor, Private::ExecutionFlag::ErrorCase)); } template template KAsync::Future Job::exec(FirstIn in) { // Inject a fake sync executor that will return the initial value Private::ExecutorBasePtr first = mExecutor; while (first->mPrev) { first = first->mPrev; } first->mPrev = QSharedPointer>::create( Private::ContinuationHolder([val = std::move(in)](Future &future) { future.setResult(val); })); auto result = exec(); // Remove the injected executor first->mPrev.reset(); return result; } template KAsync::Future Job::exec() { Private::ExecutionPtr execution = mExecutor->exec(mExecutor, Private::ExecutionContext::Ptr::create()); KAsync::Future result = *execution->result(); return result; } template Job::Job(Private::ExecutorBasePtr executor) : JobBase(executor) {} template Job::Job(JobContinuation &&func) : JobBase(new Private::ThenExecutor(std::forward>(func), {})) { qWarning() << "Creating job job"; static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); } template template void Job::eachInvariants() const { static_assert(detail::isIterable::value, "The 'Each' task can only be connected to a job that returns a list or an array."); static_assert(std::is_void::value || detail::isIterable::value, "The result type of 'Each' task must be void, a list or an array."); } template template void Job::thenInvariants() const { static_assert(!std::is_void::value && (std::is_convertible::value || std::is_base_of::value), "The return type of previous task must be compatible with input type of this task"); } template template auto Job::thenInvariants() const -> std::enable_if_t<(sizeof...(InOther) == 0)> { } template Job startImpl(Private::ContinuationHolder &&helper) { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); return Job(QSharedPointer>::create( std::forward>(helper), nullptr, Private::ExecutionFlag::GoodCase)); } template Job syncStartImpl(SyncContinuation &&func) { static_assert(sizeof...(In) <= 1, "Only one or zero input parameters are allowed."); return Job(QSharedPointer>::create( Private::ContinuationHolder(std::forward>(func)), nullptr, Private::ExecutionFlag::GoodCase)); } static inline KAsync::Job waitForCompletion(QList> &futures) { auto context = new QObject; return start([futures, context](KAsync::Future &future) { const auto total = futures.size(); auto count = QSharedPointer::create(); int i = 0; for (KAsync::Future subFuture : futures) { i++; if (subFuture.isFinished()) { *count += 1; continue; } // FIXME bind lifetime all watcher to future (repectively the main job auto watcher = QSharedPointer>::create(); QObject::connect(watcher.data(), &KAsync::FutureWatcher::futureReady, [count, total, &future, context]() { *count += 1; if (*count == total) { delete context; future.setFinished(); } }); watcher->setFuture(subFuture); context->setProperty(QString::fromLatin1("future%1").arg(i).toLatin1().data(), QVariant::fromValue(watcher)); } if (*count == total) { delete context; future.setFinished(); } }); // .finally([context]() { delete context; }); } template Job forEach(KAsync::Job job) { auto cont = [job] (const List &values) mutable { auto error = QSharedPointer::create(); QList> list; for (const auto &v : values) { auto future = job .template then([error] (const KAsync::Error &e) { if (e && !*error) { //TODO ideally we would aggregate the errors instead of just using the first one *error = e; } }) .exec(v); list << future; } return waitForCompletion(list) .then([error](KAsync::Future &future) { if (*error) { future.setError(*error); } else { future.setFinished(); } }); }; return Job(QSharedPointer>::create( Private::ContinuationHolder(JobContinuation(std::move(cont))), nullptr, Private::ExecutionFlag::GoodCase)); } template Job serialForEach(KAsync::Job job) { auto cont = [job] (const List &values) mutable { auto error = QSharedPointer::create(); auto serialJob = KAsync::null(); for (const auto &value : values) { serialJob = serialJob.then([value, job, error](KAsync::Future &future) { job.template then([&future, error] (const KAsync::Error &e) { if (e && !*error) { //TODO ideally we would aggregate the errors instead of just using the first one *error = e; } future.setFinished(); }) .exec(value); }); } return serialJob .then([error](KAsync::Future &future) { if (*error) { future.setError(*error); } else { future.setFinished(); } }); }; return Job(QSharedPointer>::create( Private::ContinuationHolder(JobContinuation(std::move(cont))), nullptr, Private::ExecutionFlag::GoodCase)); } template Job forEach(JobContinuation &&func) { return forEach(KAsync::start(std::forward>(func))); } template Job serialForEach(JobContinuation &&func) { return serialForEach(KAsync::start(std::forward>(func))); } template Job null() { return KAsync::start( [](KAsync::Future &future) { future.setFinished(); }); } template Job value(Out v) { return KAsync::start( [val = std::move(v)](KAsync::Future &future) { future.setResult(val); }); } template Job error(int errorCode, const QString &errorMessage) { return error({errorCode, errorMessage}); } template Job error(const char *message) { return error(Error(message)); } template Job error(const Error &error) { return KAsync::start( [error](KAsync::Future &future) { future.setError(error); }); } } // namespace KAsync //@endconf #endif // KASYNC_JOB_IMPL_H