diff options
author | Eike Ziller <eike.ziller@theqtcompany.com> | 2016-01-18 12:48:09 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@theqtcompany.com> | 2016-01-19 06:07:43 +0000 |
commit | 6513fc88b307cf8e8f4e47fa2339cb9ea363e1e8 (patch) | |
tree | 12fb3c13369b2891d89e0ba7f7a2816e4a950bd3 /src/libs/utils/runextensions.h | |
parent | 4b077921e208fd4809a0115f2636722ec055e868 (diff) |
runAsync: Allow calling member functions
Change-Id: Ide09b5585730fb3dcb4a18718a02a4d8351ef91e
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Diffstat (limited to 'src/libs/utils/runextensions.h')
-rw-r--r-- | src/libs/utils/runextensions.h | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/src/libs/utils/runextensions.h b/src/libs/utils/runextensions.h index cb1ef5f7f0..4b68fdd0f0 100644 --- a/src/libs/utils/runextensions.h +++ b/src/libs/utils/runextensions.h @@ -519,8 +519,25 @@ void blockingMapReduce(QFutureInterface<ReduceResult> futureInterface, const Con futureInterface.reportFinished(); } +// TODO: Use universal references and std::forward to function when we require MSVC2015 +// (MSVC2013 has a bug that std::thread does not move the copied arguments to the function call, +// which leads to compile errors with rvalue arguments) +template <typename ResultType, typename Function, typename Obj, typename... Args> +typename std::enable_if<std::is_member_pointer<typename std::decay<Function>::type>::value>::type +runAsyncImpl(QFutureInterface<ResultType> futureInterface, const Function &function, const Obj &obj, const Args&... args) +{ + std::mem_fn(function)(obj, futureInterface, args...); + if (futureInterface.isPaused()) + futureInterface.waitForResume(); + futureInterface.reportFinished(); +} + +// TODO: Use universal references and std::forward to function when we require MSVC2015 +// (MSVC2013 has a bug that std::thread does not move the copied arguments to the function call, +// which leads to compile errors with rvalue arguments) template <typename ResultType, typename Function, typename... Args> -void runAsyncImpl(QFutureInterface<ResultType> futureInterface, const Function &function, const Args&... args) +typename std::enable_if<!std::is_member_pointer<typename std::decay<Function>::type>::value>::type +runAsyncImpl(QFutureInterface<ResultType> futureInterface, const Function &function, const Args&... args) { function(futureInterface, args...); if (futureInterface.isPaused()) @@ -556,6 +573,19 @@ QFuture<ReduceResult> mapReduce(const Container &container, const InitFunction & return future; } +/*! + The interface of \c {runAsync} is similar to the std::thread constructor and \c {std::invoke}. + + The \a function argument can be a member function, an object with \c {operator()}, a + \c {std::function}, lambda, function pointer or function reference. + They need to take a \c {QFutureInterface<ResultType>&} as their first argument, followed by + other custom arguments which need to be passed to this function. + If \a function is a (non-static) member function, the first argument in \a args is expected + to be the object that the function is called on. + + \sa std::thread + \sa std::invoke + */ template <typename ResultType, typename Function, typename... Args> QFuture<ResultType> runAsync(Function &&function, Args&&... args) { |