From 4d9658b7cd2b072dd8b24d9bb6844b7cbcf22ad0 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Fri, 9 Oct 2020 18:21:17 +0200 Subject: Use universal references for passing callables in QtConcurrent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Task-number: QTBUG-87596 Change-Id: I219f08d73b97317820ec6e329ab1e6c89c0545f1 Reviewed-by: Jarek Kobus Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim --- src/concurrent/qtconcurrentfilter.cpp | 70 ++-- src/concurrent/qtconcurrentfilter.h | 372 ++++++++++--------- src/concurrent/qtconcurrentfilterkernel.h | 135 +++---- src/concurrent/qtconcurrentmap.cpp | 78 ++-- src/concurrent/qtconcurrentmap.h | 395 ++++++++++++--------- src/concurrent/qtconcurrentmapkernel.h | 141 ++++---- src/concurrent/qtconcurrentreducekernel.h | 34 +- .../qtconcurrentfilter/tst_qtconcurrentfilter.cpp | 191 ++++++++++ .../qtconcurrentmap/tst_qtconcurrentmap.cpp | 194 ++++++++++ tests/auto/concurrent/testhelper_functions.h | 26 ++ 10 files changed, 1093 insertions(+), 543 deletions(-) diff --git a/src/concurrent/qtconcurrentfilter.cpp b/src/concurrent/qtconcurrentfilter.cpp index 298e1684fc..2bec2a6eb9 100644 --- a/src/concurrent/qtconcurrentfilter.cpp +++ b/src/concurrent/qtconcurrentfilter.cpp @@ -231,12 +231,12 @@ */ /*! - \fn [QtConcurrent-1] template ThreadEngineStarter QtConcurrent::filterInternal(Sequence &sequence, KeepFunctor keep, ReduceFunctor reduce) + \fn [QtConcurrent-1] template ThreadEngineStarter QtConcurrent::filterInternal(Sequence &sequence, KeepFunctor &&keep, ReduceFunctor &&reduce) \internal */ /*! - \fn template QFuture QtConcurrent::filter(QThreadPool *pool, Sequence &sequence, KeepFunctor filterFunction) + \fn template QFuture QtConcurrent::filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -250,7 +250,7 @@ */ /*! - \fn template QFuture QtConcurrent::filter(Sequence &sequence, KeepFunctor filterFunction) + \fn template QFuture QtConcurrent::filter(Sequence &sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence. If \a filterFunction returns \c true, the item is kept in \a sequence; @@ -263,7 +263,7 @@ */ /*! - \fn template QFuture QtConcurrent::filtered(QThreadPool *pool, Sequence &&sequence, KeepFunctor filterFunction) + \fn template QFuture QtConcurrent::filtered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence and returns a new Sequence of kept items. All calls to \a filterFunction are invoked from the threads @@ -275,7 +275,7 @@ */ /*! - \fn template QFuture QtConcurrent::filtered(Sequence &&sequence, KeepFunctor filterFunction) + \fn template QFuture QtConcurrent::filtered(Sequence &&sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence and returns a new Sequence of kept items. If \a filterFunction returns \c true, a copy of @@ -286,7 +286,7 @@ */ /*! - \fn template QFuture::value_type> QtConcurrent::filtered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor filterFunction) + \fn template QFuture::value_type> QtConcurrent::filtered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item from \a begin to \a end and returns a new Sequence of kept items. All calls to \a filterFunction are invoked from the threads @@ -298,7 +298,7 @@ */ /*! - \fn template QFuture::value_type> QtConcurrent::filtered(Iterator begin, Iterator end, KeepFunctor filterFunction) + \fn template QFuture::value_type> QtConcurrent::filtered(Iterator begin, Iterator end, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item from \a begin to \a end and returns a new Sequence of kept items. If \a filterFunction returns \c true, a @@ -309,7 +309,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -328,7 +328,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. If \a filterFunction returns \c true for an item, that item is then passed to @@ -346,7 +346,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -368,7 +368,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. If \a filterFunction returns \c true for an item, that item is then passed to @@ -389,7 +389,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -408,7 +408,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. If \a filterFunction returns \c true for an item, that item is then passed to @@ -426,7 +426,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -448,7 +448,7 @@ */ /*! - \fn template QFuture QtConcurrent::filteredReduced(Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::filteredReduced(Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. If \a filterFunction returns \c true for an item, that item is then passed to @@ -469,7 +469,7 @@ */ /*! - \fn template void QtConcurrent::blockingFilter(QThreadPool *pool, Sequence &sequence, KeepFunctor filterFunction) + \fn template void QtConcurrent::blockingFilter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -485,7 +485,7 @@ */ /*! - \fn template void QtConcurrent::blockingFilter(Sequence &sequence, KeepFunctor filterFunction) + \fn template void QtConcurrent::blockingFilter(Sequence &sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence. If \a filterFunction returns \c true, the item is kept in \a sequence; @@ -500,7 +500,7 @@ */ /*! - \fn template Sequence QtConcurrent::blockingFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor filterFunction) + \fn template Sequence QtConcurrent::blockingFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence and returns a new Sequence of kept items. All calls to \a filterFunction are invoked from the threads @@ -514,7 +514,7 @@ */ /*! - \fn template Sequence QtConcurrent::blockingFiltered(Sequence &&sequence, KeepFunctor filterFunction) + \fn template Sequence QtConcurrent::blockingFiltered(Sequence &&sequence, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item in \a sequence and returns a new Sequence of kept items. If \a filterFunction returns \c true, a copy of @@ -527,7 +527,7 @@ */ /*! - \fn template OutputSequence QtConcurrent::blockingFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor filterFunction) + \fn template OutputSequence QtConcurrent::blockingFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item from \a begin to \a end and returns a new Sequence of kept items. All calls to \a filterFunction are invoked from the threads @@ -542,7 +542,7 @@ */ /*! - \fn template OutputSequence QtConcurrent::blockingFiltered(Iterator begin, Iterator end, KeepFunctor filterFunction) + \fn template OutputSequence QtConcurrent::blockingFiltered(Iterator begin, Iterator end, KeepFunctor &&filterFunction) Calls \a filterFunction once for each item from \a begin to \a end and returns a new Sequence of kept items. If \a filterFunction returns \c true, a @@ -556,7 +556,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -577,7 +577,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. If \a filterFunction returns \c true for an item, that item is then passed to @@ -597,7 +597,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -621,7 +621,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(Sequence &&sequence, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(Sequence &&sequence, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item in \a sequence. If \a filterFunction returns \c true for an item, that item is then passed to @@ -644,7 +644,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -666,7 +666,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. If \a filterFunction returns \c true for an item, that item is then passed to @@ -687,7 +687,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. All calls to \a filterFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -712,7 +712,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor filterFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingFilteredReduced(Iterator begin, Iterator end, KeepFunctor &&filterFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a filterFunction once for each item from \a begin to \a end. If \a filterFunction returns \c true for an item, that item is then passed to @@ -736,31 +736,31 @@ */ /*! - \fn [QtConcurrent-2] ThreadEngineStarter::value_type> QtConcurrent::startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor functor) + \fn [QtConcurrent-2] ThreadEngineStarter::value_type> QtConcurrent::startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&functor) \internal */ /*! - \fn [QtConcurrent-3] ThreadEngineStarter QtConcurrent::startFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor functor) + \fn [QtConcurrent-3] ThreadEngineStarter QtConcurrent::startFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&functor) \internal */ /*! - \fn [QtConcurrent-4] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ReduceOptions options) + \fn [QtConcurrent-4] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ReduceOptions options) \internal */ /*! - \fn [QtConcurrent-5] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ReduceOptions options) + \fn [QtConcurrent-5] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ReduceOptions options) \internal */ /*! - \fn [QtConcurrent-6] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ResultType initialValue, ReduceOptions options) + \fn [QtConcurrent-6] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) \internal */ /*! - \fn [QtConcurrent-7] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ResultType initialValue, ReduceOptions options) + \fn [QtConcurrent-7] ThreadEngineStarter QtConcurrent::startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) \internal */ diff --git a/src/concurrent/qtconcurrentfilter.h b/src/concurrent/qtconcurrentfilter.h index 286ff0c177..891eb4b9ca 100644 --- a/src/concurrent/qtconcurrentfilter.h +++ b/src/concurrent/qtconcurrentfilter.h @@ -54,48 +54,53 @@ namespace QtConcurrent { //! [QtConcurrent-1] template ThreadEngineStarter filterInternal(QThreadPool *pool, Sequence &sequence, - KeepFunctor keep, ReduceFunctor reduce) + KeepFunctor &&keep, ReduceFunctor &&reduce) { - typedef FilterKernel KernelType; - return startThreadEngine(new KernelType(pool, sequence, keep, reduce)); + typedef FilterKernel, std::decay_t> + KernelType; + return startThreadEngine(new KernelType(pool, sequence, std::forward(keep), + std::forward(reduce))); } // filter() on sequences template -QFuture filter(QThreadPool *pool, Sequence &sequence, KeepFunctor keep) +QFuture filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&keep) { - return filterInternal(pool, sequence, keep, QtPrivate::PushBackWrapper()); + return filterInternal(pool, sequence, std::forward(keep), + QtPrivate::PushBackWrapper()); } template -QFuture filter(Sequence &sequence, KeepFunctor keep) +QFuture filter(Sequence &sequence, KeepFunctor &&keep) { return filterInternal(QThreadPool::globalInstance(), - sequence, keep, QtPrivate::PushBackWrapper()); + sequence, std::forward(keep), QtPrivate::PushBackWrapper()); } // filteredReduced() on sequences template QFuture filteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(pool, std::forward(sequence), keep, reduce, - options); + return startFilteredReduced(pool, std::forward(sequence), + std::forward(keep), + std::forward(reduce), options); } template QFuture filteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startFilteredReduced( - QThreadPool::globalInstance(), std::forward(sequence), keep, reduce, options); + QThreadPool::globalInstance(), std::forward(sequence), + std::forward(keep), std::forward(reduce), options); } #ifdef Q_CLANG_QDOC @@ -108,14 +113,15 @@ template filteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startFilteredReduced( - pool, std::forward(sequence), keep, reduce, + pool, std::forward(sequence), std::forward(keep), + std::forward(reduce), ResultType(std::forward(initialValue)), options); } @@ -128,73 +134,82 @@ template , int> = 0> #endif QFuture filteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startFilteredReduced( - QThreadPool::globalInstance(), std::forward(sequence), keep, reduce, + QThreadPool::globalInstance(), std::forward(sequence), + std::forward(keep), std::forward(reduce), ResultType(std::forward(initialValue)), options); } #ifndef Q_CLANG_QDOC template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> QFuture filteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(pool, std::forward(sequence), keep, reduce, - options); + return startFilteredReduced(pool, std::forward(sequence), + std::forward(keep), + std::forward(reduce), options); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> QFuture filteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startFilteredReduced( - QThreadPool::globalInstance(), std::forward(sequence), keep, reduce, options); + QThreadPool::globalInstance(), std::forward(sequence), + std::forward(keep), std::forward(reduce), options); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture filteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startFilteredReduced( - pool, std::forward(sequence), keep, reduce, + pool, std::forward(sequence), std::forward(keep), + std::forward(reduce), ResultType(std::forward(initialValue)), options); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture filteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startFilteredReduced( - QThreadPool::globalInstance(), std::forward(sequence), keep, reduce, + QThreadPool::globalInstance(), std::forward(sequence), + std::forward(keep), std::forward(reduce), ResultType(std::forward(initialValue)), options); } #endif @@ -204,24 +219,26 @@ template filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(pool, begin, end, keep, reduce, options); + return startFilteredReduced(pool, begin, end, std::forward(keep), + std::forward(reduce), options); } template QFuture filteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(QThreadPool::globalInstance(), begin, end, keep, reduce, - options); + return startFilteredReduced(QThreadPool::globalInstance(), begin, end, + std::forward(keep), + std::forward(reduce), options); } #ifdef Q_CLANG_QDOC @@ -235,14 +252,15 @@ template filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(pool, begin, end, keep, reduce, - ResultType(std::forward(initialValue)), options); + return startFilteredReduced( + pool, begin, end, std::forward(keep), std::forward(reduce), + ResultType(std::forward(initialValue)), options); } #ifdef Q_CLANG_QDOC @@ -255,89 +273,102 @@ template filteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(QThreadPool::globalInstance(), begin, end, keep, reduce, - ResultType(std::forward(initialValue)), options); + return startFilteredReduced( + QThreadPool::globalInstance(), begin, end, std::forward(keep), + std::forward(reduce), + ResultType(std::forward(initialValue)), options); } #ifndef Q_CLANG_QDOC template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> QFuture filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(pool, begin, end, keep, reduce, options); + return startFilteredReduced(pool, begin, end, std::forward(keep), + std::forward(reduce), options); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> QFuture filteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(QThreadPool::globalInstance(), - begin, end, keep, reduce, options); + return startFilteredReduced(QThreadPool::globalInstance(), begin, end, + std::forward(keep), + std::forward(reduce), options); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture filteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(pool, begin, end, keep, reduce, - ResultType(std::forward(initialValue)), options); + return startFilteredReduced( + pool, begin, end, std::forward(keep), std::forward(reduce), + ResultType(std::forward(initialValue)), options); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> QFuture filteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startFilteredReduced(QThreadPool::globalInstance(), begin, end, keep, reduce, - ResultType(std::forward(initialValue)), options); + return startFilteredReduced( + QThreadPool::globalInstance(), begin, end, std::forward(keep), + std::forward(reduce), + ResultType(std::forward(initialValue)), options); } #endif // filtered() on sequences template -QFuture::value_type> filtered(QThreadPool *pool, - Sequence &&sequence, KeepFunctor keep) +QFuture::value_type> filtered(QThreadPool *pool,Sequence &&sequence, + KeepFunctor &&keep) { - return startFiltered(pool, std::forward(sequence), keep); + return startFiltered(pool, std::forward(sequence), std::forward(keep)); } template -QFuture::value_type> filtered(Sequence &&sequence, KeepFunctor keep) +QFuture::value_type> filtered(Sequence &&sequence, + KeepFunctor &&keep) { - return startFiltered(QThreadPool::globalInstance(), std::forward(sequence), keep); + return startFiltered(QThreadPool::globalInstance(), std::forward(sequence), + std::forward(keep)); } // filtered() on iterators @@ -345,31 +376,32 @@ template QFuture::value_type> filtered(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor keep) + KeepFunctor &&keep) { - return startFiltered(pool, begin, end, keep); + return startFiltered(pool, begin, end, std::forward(keep)); } template QFuture::value_type> filtered(Iterator begin, Iterator end, - KeepFunctor keep) + KeepFunctor &&keep) { - return startFiltered(QThreadPool::globalInstance(), begin, end, keep); + return startFiltered(QThreadPool::globalInstance(), begin, end, + std::forward(keep)); } // blocking filter() on sequences template -void blockingFilter(QThreadPool *pool, Sequence &sequence, KeepFunctor keep) +void blockingFilter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&keep) { - QFuture future = filter(pool, sequence, keep); + QFuture future = filter(pool, sequence, std::forward(keep)); future.waitForFinished(); } template -void blockingFilter(Sequence &sequence, KeepFunctor keep) +void blockingFilter(Sequence &sequence, KeepFunctor &&keep) { - QFuture future = filter(sequence, keep); + QFuture future = filter(sequence, std::forward(keep)); future.waitForFinished(); } @@ -377,25 +409,27 @@ void blockingFilter(Sequence &sequence, KeepFunctor keep) template ResultType blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = filteredReduced(pool, std::forward(sequence), - keep, reduce, options); + QFuture future = filteredReduced( + pool, std::forward(sequence), std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } template ResultType blockingFilteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = - filteredReduced(std::forward(sequence), keep, reduce, options); + QFuture future = filteredReduced( + std::forward(sequence), std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } @@ -409,14 +443,15 @@ template future = filteredReduced( - pool, std::forward(sequence), keep, reduce, + pool, std::forward(sequence), std::forward(keep), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -430,77 +465,86 @@ template , int> = 0> #endif ResultType blockingFilteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = filteredReduced( - std::forward(sequence), keep, reduce, + std::forward(sequence), std::forward(keep), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } #ifndef Q_CLANG_QDOC template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = filteredReduced(pool, std::forward(sequence), - keep, reduce, options); + QFuture future = filteredReduced( + pool, std::forward(sequence), std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingFilteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = - filteredReduced(std::forward(sequence), keep, reduce, options); + QFuture future = filteredReduced( + std::forward(sequence), std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingFilteredReduced(QThreadPool *pool, Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = filteredReduced( - pool, std::forward(sequence), keep, reduce, + pool, std::forward(sequence), std::forward(keep), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingFilteredReduced(Sequence &&sequence, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = filteredReduced( - std::forward(sequence), keep, reduce, + std::forward(sequence), std::forward(keep), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -511,25 +555,28 @@ template future = - filteredReduced(pool, begin, end, keep, reduce, options); + filteredReduced(pool, begin, end, std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } template ResultType blockingFilteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = filteredReduced(begin, end, keep, reduce, options); + QFuture future = + filteredReduced(begin, end, std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } @@ -544,14 +591,14 @@ template future = filteredReduced( - pool, begin, end, keep, reduce, + pool, begin, end, std::forward(keep), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -566,114 +613,123 @@ template future = filteredReduced( - begin, end, keep, reduce, ResultType(std::forward(initialValue)), - options); + begin, end, std::forward(keep), std::forward(reduce), + ResultType(std::forward(initialValue)), options); return future.takeResult(); } #ifndef Q_CLANG_QDOC template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = - filteredReduced(pool, begin, end, keep, reduce, options); + filteredReduced(pool, begin, end, std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingFilteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = filteredReduced(begin, end, keep, reduce, options); + QFuture future = + filteredReduced(begin, end, std::forward(keep), + std::forward(reduce), options); return future.takeResult(); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingFilteredReduced(QThreadPool *pool, Iterator begin, - Iterator end, KeepFunctor keep, - ReduceFunctor reduce, + Iterator end, KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = filteredReduced( - pool, begin, end, keep, reduce, + pool, begin, end, std::forward(keep), std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> ResultType blockingFilteredReduced(Iterator begin, Iterator end, - KeepFunctor keep, - ReduceFunctor reduce, + KeepFunctor &&keep, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = filteredReduced( - begin, end, keep, reduce, ResultType(std::forward(initialValue)), - options); + begin, end, std::forward(keep), std::forward(reduce), + ResultType(std::forward(initialValue)), options); return future.takeResult(); } #endif // blocking filtered() on sequences template -std::decay_t blockingFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor keep) +std::decay_t blockingFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&keep) { return blockingFilteredReduced>( - pool, std::forward(sequence), keep, QtPrivate::PushBackWrapper(), - OrderedReduce); + pool, std::forward(sequence), std::forward(keep), + QtPrivate::PushBackWrapper(), OrderedReduce); } template -std::decay_t blockingFiltered(Sequence &&sequence, KeepFunctor keep) +std::decay_t blockingFiltered(Sequence &&sequence, KeepFunctor &&keep) { return blockingFilteredReduced>( - QThreadPool::globalInstance(), std::forward(sequence), keep, - QtPrivate::PushBackWrapper(), OrderedReduce); + QThreadPool::globalInstance(), std::forward(sequence), + std::forward(keep), QtPrivate::PushBackWrapper(), OrderedReduce); } // blocking filtered() on iterators template -OutputSequence blockingFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor keep) +OutputSequence blockingFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&keep) { - return blockingFilteredReduced(pool, begin, end, keep, - QtPrivate::PushBackWrapper(), OrderedReduce); + return blockingFilteredReduced(pool, begin, end, + std::forward(keep), + QtPrivate::PushBackWrapper(), OrderedReduce); } template -OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor keep) +OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor &&keep) { - return blockingFilteredReduced(QThreadPool::globalInstance(), begin, end, keep, - QtPrivate::PushBackWrapper(), OrderedReduce); + return blockingFilteredReduced(QThreadPool::globalInstance(), begin, end, + std::forward(keep), + QtPrivate::PushBackWrapper(), OrderedReduce); } } // namespace QtConcurrent diff --git a/src/concurrent/qtconcurrentfilterkernel.h b/src/concurrent/qtconcurrentfilterkernel.h index 7077327ae4..5033d7b05e 100644 --- a/src/concurrent/qtconcurrentfilterkernel.h +++ b/src/concurrent/qtconcurrentfilterkernel.h @@ -87,12 +87,13 @@ class FilterKernel : public IterateKernel + FilterKernel(QThreadPool *pool, Sequence &_sequence, Keep &&_keep, Reduce &&_reduce) : IterateKernelType(pool, const_cast(_sequence).begin(), const_cast(_sequence).end()), reducedResult(), sequence(_sequence), - keep(_keep), - reduce(_reduce), + keep(std::forward(_keep)), + reduce(std::forward(_reduce)), reducer(pool, OrderedReduce) { } @@ -166,23 +167,26 @@ class FilteredReducedKernel : public IterateKernel typedef IterateKernel IterateKernelType; public: + template FilteredReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, - KeepFunctor _keep, - ReduceFunctor _reduce, + Keep &&_keep, + Reduce &&_reduce, ReduceOptions reduceOption) - : IterateKernelType(pool, begin, end), reducedResult(), keep(_keep), reduce(_reduce), + : IterateKernelType(pool, begin, end), reducedResult(), keep(std::forward(_keep)), + reduce(std::forward(_reduce)), reducer(pool, reduceOption) { } - FilteredReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor _keep, - ReduceFunctor _reduce, ReducedResultType &&initialValue, + template + FilteredReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, Keep &&_keep, + Reduce &&_reduce, ReducedResultType &&initialValue, ReduceOptions reduceOption) : IterateKernelType(pool, begin, end), reducedResult(std::forward(initialValue)), - keep(_keep), - reduce(_reduce), + keep(std::forward(_keep)), + reduce(std::forward(_reduce)), reducer(pool, reduceOption) { } @@ -255,8 +259,9 @@ public: typedef T ReturnType; typedef T ResultType; - FilteredEachKernel(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor _keep) - : IterateKernelType(pool, begin, end), keep(_keep) + template + FilteredEachKernel(QThreadPool *pool, Iterator begin, Iterator end, Keep &&_keep) + : IterateKernelType(pool, begin, end), keep(std::forward(_keep)) { } void start() override @@ -300,43 +305,47 @@ public: template inline ThreadEngineStarter::value_type> -startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor functor) +startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&functor) { - return startThreadEngine(new FilteredEachKernel - (pool, begin, end, functor)); + return startThreadEngine(new FilteredEachKernel> + (pool, begin, end, std::forward(functor))); } //! [QtConcurrent-3] template -inline decltype(auto) startFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor functor) +inline decltype(auto) startFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor &&functor) { using DecayedSequence = std::decay_t; - typedef SequenceHolder1, - KeepFunctor> - SequenceHolderType; - return startThreadEngine( - new SequenceHolderType(pool, std::forward(sequence), functor)); + using DecayedFunctor = std::decay_t; + using SequenceHolderType = SequenceHolder1, + DecayedFunctor>; + return startThreadEngine(new SequenceHolderType(pool, std::forward(sequence), + std::forward(functor))); } //! [QtConcurrent-4] template inline ThreadEngineStarter startFilteredReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ReduceOptions options) { using DecayedSequence = std::decay_t; - typedef typename DecayedSequence::const_iterator Iterator; - typedef ReduceKernel::value_type > - Reducer; - typedef FilteredReducedKernel - FilteredReduceType; - typedef SequenceHolder2 - SequenceHolderType; + using DecayedMapFunctor = std::decay_t; + using DecayedReduceFunctor = std::decay_t; + using Iterator = typename DecayedSequence::const_iterator; + using Reducer = ReduceKernel::value_type>; + using FilteredReduceType = FilteredReducedKernel; + using SequenceHolderType = SequenceHolder2; return startThreadEngine(new SequenceHolderType(pool, std::forward(sequence), - mapFunctor, reduceFunctor, options)); + std::forward(mapFunctor), + std::forward(reduceFunctor), + options)); } @@ -345,16 +354,17 @@ template startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ReduceOptions options) { - typedef ReduceKernel::value_type> - Reducer; - typedef FilteredReducedKernel - FilteredReduceType; - return startThreadEngine(new FilteredReduceType(pool, begin, end, mapFunctor, - reduceFunctor, options)); + using Reducer = ReduceKernel, ResultType, + typename qValueType::value_type>; + using FilteredReduceType = FilteredReducedKernel, + std::decay_t, Reducer>; + return startThreadEngine( + new FilteredReduceType(pool, begin, end, std::forward(mapFunctor), + std::forward(reduceFunctor), options)); } // Repeat the two functions above, but now with an initial value! @@ -362,22 +372,25 @@ inline ThreadEngineStarter startFilteredReduced(QThreadPool *pool, template inline ThreadEngineStarter startFilteredReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) { using DecayedSequence = std::decay_t; - typedef typename DecayedSequence::const_iterator Iterator; - typedef ReduceKernel::value_type > - Reducer; - typedef FilteredReducedKernel - FilteredReduceType; - typedef SequenceHolder2 - SequenceHolderType; - return startThreadEngine( - new SequenceHolderType(pool, std::forward(sequence), mapFunctor, - reduceFunctor, std::forward(initialValue), options)); + using DecayedMapFunctor = std::decay_t; + using DecayedReduceFunctor = std::decay_t; + using Iterator = typename DecayedSequence::const_iterator; + using Reducer = ReduceKernel::value_type>; + using FilteredReduceType = FilteredReducedKernel; + using SequenceHolderType = SequenceHolder2; + return startThreadEngine(new SequenceHolderType( + pool, std::forward(sequence), std::forward(mapFunctor), + std::forward(reduceFunctor), std::forward(initialValue), + options)); } //! [QtConcurrent-7] @@ -385,17 +398,19 @@ template startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) { - typedef ReduceKernel::value_type> - Reducer; - typedef FilteredReducedKernel - FilteredReduceType; - return startThreadEngine(new FilteredReduceType(pool, begin, end, mapFunctor, reduceFunctor, - std::forward(initialValue), options)); + using Reducer = ReduceKernel, ResultType, + typename qValueType::value_type>; + using FilteredReduceType = FilteredReducedKernel, + std::decay_t, Reducer>; + return startThreadEngine( + new FilteredReduceType(pool, begin, end, std::forward(mapFunctor), + std::forward(reduceFunctor), + std::forward(initialValue), options)); } diff --git a/src/concurrent/qtconcurrentmap.cpp b/src/concurrent/qtconcurrentmap.cpp index ec94975f38..ed7c103372 100644 --- a/src/concurrent/qtconcurrentmap.cpp +++ b/src/concurrent/qtconcurrentmap.cpp @@ -94,37 +94,37 @@ */ /*! - \fn [qtconcurrentmapkernel-1] ThreadEngineStarter QtConcurrent::startMap(Iterator begin, Iterator end, Functor functor) + \fn [qtconcurrentmapkernel-1] ThreadEngineStarter QtConcurrent::startMap(Iterator begin, Iterator end, Functor &&functor) \internal */ /*! - \fn [qtconcurrentmapkernel-2] ThreadEngineStarter QtConcurrent::startMapped(Iterator begin, Iterator end, Functor functor) + \fn [qtconcurrentmapkernel-2] ThreadEngineStarter QtConcurrent::startMapped(Iterator begin, Iterator end, Functor &&functor) \internal */ /*! - \fn [qtconcurrentmapkernel-3] ThreadEngineStarter QtConcurrent::startMapped(Sequence &&sequence, Functor functor) + \fn [qtconcurrentmapkernel-3] ThreadEngineStarter QtConcurrent::startMapped(Sequence &&sequence, Functor &&functor) \internal */ /*! - \fn [qtconcurrentmapkernel-4] ThreadEngineStarter QtConcurrent::startMappedReduced(Sequence && sequence, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ReduceOptions options) + \fn [qtconcurrentmapkernel-4] ThreadEngineStarter QtConcurrent::startMappedReduced(Sequence &&sequence, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ReduceOptions options) \internal */ /*! - \fn [qtconcurrentmapkernel-5] ThreadEngineStarter QtConcurrent::startMappedReduced(Iterator begin, Iterator end, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ReduceOptions options) + \fn [qtconcurrentmapkernel-5] ThreadEngineStarter QtConcurrent::startMappedReduced(Iterator begin, Iterator end, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ReduceOptions options) \internal */ /*! - \fn [qtconcurrentmapkernel-6] ThreadEngineStarter QtConcurrent::startMappedReduced(Sequence && sequence, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ResultType &&initialValue, ReduceOptions options) + \fn [qtconcurrentmapkernel-6] ThreadEngineStarter QtConcurrent::startMappedReduced(Sequence &&sequence, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) \internal */ /*! - \fn [qtconcurrentmapkernel-7] ThreadEngineStarter QtConcurrent::startMappedReduced(Iterator begin, Iterator end, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ResultType &&initialValue, ReduceOptions options) + \fn [qtconcurrentmapkernel-7] ThreadEngineStarter QtConcurrent::startMappedReduced(Iterator begin, Iterator end, MapFunctor &&mapFunctor, ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) \internal */ @@ -320,7 +320,7 @@ */ /*! - \fn template QFuture QtConcurrent::map(QThreadPool *pool, Sequence &&sequence, MapFunctor function) + \fn template QFuture QtConcurrent::map(QThreadPool *pool, Sequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence. All calls to \a function are invoked from the threads taken from the QThreadPool \a pool. @@ -331,7 +331,7 @@ */ /*! - \fn template QFuture QtConcurrent::map(Sequence &&sequence, MapFunctor function) + \fn template QFuture QtConcurrent::map(Sequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence. The \a function takes a reference to the item, so that any modifications done to the item @@ -341,7 +341,7 @@ */ /*! - \fn template QFuture QtConcurrent::map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor function) + \fn template QFuture QtConcurrent::map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end. All calls to \a function are invoked from the threads taken from the QThreadPool \a pool. @@ -352,7 +352,7 @@ */ /*! - \fn template QFuture QtConcurrent::map(Iterator begin, Iterator end, MapFunctor function) + \fn template QFuture QtConcurrent::map(Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end. The \a function takes a reference to the item, so that any modifications @@ -362,7 +362,7 @@ */ /*! - \fn template QFuture> QtConcurrent::mapped(QThreadPool *pool, Sequence &&sequence, MapFunctor function) + \fn template QFuture> QtConcurrent::mapped(QThreadPool *pool, Sequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence and returns a future with each mapped item as a result. All calls to \a function are invoked from the @@ -373,7 +373,7 @@ */ /*! - \fn template QFuture> QtConcurrent::mapped(Sequence &&sequence, MapFunctor function) + \fn template QFuture> QtConcurrent::mapped(Sequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence and returns a future with each mapped item as a result. You can use QFuture::const_iterator or @@ -383,7 +383,7 @@ */ /*! - \fn template QFuture> QtConcurrent::mapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor function) + \fn template QFuture> QtConcurrent::mapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end and returns a future with each mapped item as a result. All calls to \a function are invoked from the @@ -394,7 +394,7 @@ */ /*! - \fn template QFuture> QtConcurrent::mapped(Iterator begin, Iterator end, MapFunctor function) + \fn template QFuture> QtConcurrent::mapped(Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end and returns a future with each mapped item as a result. You can use @@ -404,7 +404,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -418,7 +418,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -431,7 +431,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -448,7 +448,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -464,7 +464,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -480,7 +480,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -495,7 +495,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -514,7 +514,7 @@ */ /*! - \fn template QFuture QtConcurrent::mappedReduced(Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template QFuture QtConcurrent::mappedReduced(Iterator begin, Iterator end, MapFunctor &&mapFunction, &&ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -532,7 +532,7 @@ */ /*! - \fn template void QtConcurrent::blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor function) + \fn template void QtConcurrent::blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence. All calls to \a function are invoked from the threads taken from the QThreadPool \a pool. @@ -545,7 +545,7 @@ */ /*! - \fn template void QtConcurrent::blockingMap(Sequence &&sequence, MapFunctor function) + \fn template void QtConcurrent::blockingMap(Sequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence. The \a function takes a reference to the item, so that any modifications done to the item @@ -557,7 +557,7 @@ */ /*! - \fn template void QtConcurrent::blockingMap(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor function) + \fn template void QtConcurrent::blockingMap(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end. All calls to \a function are invoked from the threads taken from the QThreadPool \a pool. @@ -571,7 +571,7 @@ */ /*! - \fn template void QtConcurrent::blockingMap(Iterator begin, Iterator end, MapFunctor function) + \fn template void QtConcurrent::blockingMap(Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end. The \a function takes a reference to the item, so that any modifications @@ -584,7 +584,7 @@ */ /*! - \fn template OutputSequence QtConcurrent::blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor function) + \fn template OutputSequence QtConcurrent::blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence and returns an OutputSequence containing the results. All calls to \a function are invoked from the threads taken from the QThreadPool @@ -596,7 +596,7 @@ */ /*! - \fn template OutputSequence QtConcurrent::blockingMapped(InputSequence &&sequence, MapFunctor function) + \fn template OutputSequence QtConcurrent::blockingMapped(InputSequence &&sequence, MapFunctor &&function) Calls \a function once for each item in \a sequence and returns an OutputSequence containing the results. The type of the results will match the type returned by the MapFunctor. @@ -607,7 +607,7 @@ */ /*! - \fn template Sequence QtConcurrent::blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor function) + \fn template Sequence QtConcurrent::blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end and returns a container with the results. All calls to \a function are invoked from the threads @@ -625,7 +625,7 @@ */ /*! - \fn template Sequence QtConcurrent::blockingMapped(Iterator begin, Iterator end, MapFunctor function) + \fn template Sequence QtConcurrent::blockingMapped(Iterator begin, Iterator end, MapFunctor &&function) Calls \a function once for each item from \a begin to \a end and returns a container with the results. You can specify the type of container as the a template @@ -642,7 +642,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -658,7 +658,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -673,7 +673,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -692,7 +692,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(Sequence &&sequence, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(Sequence &&sequence, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item in \a sequence. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -710,7 +710,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -727,7 +727,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. The return value of each \a mapFunction is passed to \a reduceFunction. @@ -743,7 +743,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. All calls to \a mapFunction are invoked from the threads taken from the QThreadPool \a pool. @@ -763,7 +763,7 @@ */ /*! - \fn template ResultType QtConcurrent::blockingMappedReduced(Iterator begin, Iterator end, MapFunctor mapFunction, ReduceFunctor reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) + \fn template ResultType QtConcurrent::blockingMappedReduced(Iterator begin, Iterator end, MapFunctor &&mapFunction, ReduceFunctor &&reduceFunction, InitialValueType &&initialValue, QtConcurrent::ReduceOptions reduceOptions) Calls \a mapFunction once for each item from \a begin to \a end. The return value of each \a mapFunction is passed to \a reduceFunction. diff --git a/src/concurrent/qtconcurrentmap.h b/src/concurrent/qtconcurrentmap.h index e6a434f852..4043ecd8a2 100644 --- a/src/concurrent/qtconcurrentmap.h +++ b/src/concurrent/qtconcurrentmap.h @@ -57,52 +57,55 @@ namespace QtConcurrent { // map() on sequences template -QFuture map(QThreadPool *pool, Sequence &&sequence, MapFunctor map) +QFuture map(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map) { - return startMap(pool, sequence.begin(), sequence.end(), map); + return startMap(pool, sequence.begin(), sequence.end(), std::forward(map)); } template -QFuture map(Sequence &&sequence, MapFunctor map) +QFuture map(Sequence &&sequence, MapFunctor &&map) { - return startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), map); + return startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), + std::forward(map)); } // map() on iterators template -QFuture map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) +QFuture map(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { - return startMap(pool, begin, end, map); + return startMap(pool, begin, end, std::forward(map)); } template -QFuture map(Iterator begin, Iterator end, MapFunctor map) +QFuture map(Iterator begin, Iterator end, MapFunctor &&map) { - return startMap(QThreadPool::globalInstance(), begin, end, map); + return startMap(QThreadPool::globalInstance(), begin, end, std::forward(map)); } // mappedReduced() for sequences. template QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (pool, std::forward(sequence), map, reduce, options); + (pool, std::forward(sequence), std::forward(map), + std::forward(reduce), options); } template QFuture mappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), std::forward(sequence), map, reduce, options); + (QThreadPool::globalInstance(), std::forward(sequence), + std::forward(map), std::forward(reduce), options); } #ifdef Q_CLANG_QDOC @@ -115,14 +118,15 @@ template mappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType>( - pool, std::forward(sequence), map, reduce, + pool, std::forward(sequence), std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); } #ifdef Q_CLANG_QDOC @@ -134,41 +138,45 @@ template , int> = 0> #endif QFuture mappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), std::forward(sequence), map, reduce, + (QThreadPool::globalInstance(), std::forward(sequence), + std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (pool, std::forward(sequence), map, reduce, options); + (pool, std::forward(sequence), std::forward(map), + std::forward(reduce), options); } template ::ResultType> -QFuture mappedReduced( - Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> +QFuture mappedReduced(Sequence &&sequence, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), std::forward(sequence), map, reduce, options); + (QThreadPool::globalInstance(), std::forward(sequence), + std::forward(map), std::forward(reduce), options); } #ifdef Q_CLANG_QDOC @@ -176,20 +184,22 @@ template #else template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType>( - pool, std::forward(sequence), map, reduce, + pool, std::forward(sequence), std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); } @@ -198,19 +208,21 @@ template #else template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), std::forward(sequence), map, reduce, + (QThreadPool::globalInstance(), std::forward(sequence), + std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } @@ -219,25 +231,27 @@ template mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (pool, begin, end, map, reduce, options); + (pool, begin, end, std::forward(map), std::forward(reduce), + options); } template QFuture mappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), begin, end, map, reduce, options); + (QThreadPool::globalInstance(), begin, end, std::forward(map), + std::forward(reduce), options); } #ifdef Q_CLANG_QDOC @@ -251,14 +265,14 @@ template mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (pool, begin, end, map, reduce, + (pool, begin, end, std::forward(map), std::forward(reduce), ResultType(std::forward(initialValue)), options); } @@ -272,42 +286,46 @@ template mappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), begin, end, map, reduce, + (QThreadPool::globalInstance(), begin, end, std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> QFuture mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - return startMappedReduced, ResultType> - (pool, begin, end, map, reduce, options); + return startMappedReduced, ResultType>( + pool, begin, end, std::forward(map), std::forward(reduce), + options); } template ::ResultType> + typename ResultType = typename QtPrivate::ReduceResultType>::ResultType> QFuture mappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), begin, end, map, reduce, options); + (QThreadPool::globalInstance(), begin, end, std::forward(map), + std::forward(reduce), options); } #ifdef Q_CLANG_QDOC @@ -315,43 +333,46 @@ template #else template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> #endif QFuture mappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (pool, begin, end, map, reduce, ResultType(std::forward(initialValue)), - options); + (pool, begin, end, std::forward(map), std::forward(reduce), + ResultType(std::forward(initialValue)), options); } #ifdef Q_CLANG_QDOC template #else -template ::ResultType, - typename InitialValueType, - std::enable_if_t, int> = 0> +template>::ResultType, + typename InitialValueType, + std::enable_if_t, int> = 0> #endif QFuture mappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { return startMappedReduced, ResultType> - (QThreadPool::globalInstance(), begin, end, map, reduce, + (QThreadPool::globalInstance(), begin, end, std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); } @@ -360,19 +381,20 @@ template QFuture> mapped( QThreadPool *pool, Sequence &&sequence, - MapFunctor map) + MapFunctor &&map) { return startMapped>( - pool, std::forward(sequence), map); + pool, std::forward(sequence), std::forward(map)); } template QFuture> mapped( Sequence &&sequence, - MapFunctor map) + MapFunctor &&map) { return startMapped> - (QThreadPool::globalInstance(), std::forward(sequence), map); + (QThreadPool::globalInstance(), std::forward(sequence), + std::forward(map)); } // mapped() for iterator ranges. @@ -381,48 +403,52 @@ QFuture> mapped( QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map) + MapFunctor &&map) { - return startMapped>(pool, begin, end, map); + return startMapped>( + pool, begin, end, std::forward(map)); } template QFuture> mapped( Iterator begin, Iterator end, - MapFunctor map) + MapFunctor &&map) { return startMapped> - (QThreadPool::globalInstance(), begin, end, map); + (QThreadPool::globalInstance(), begin, end, std::forward(map)); } // blockingMap() for sequences template void blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor map) { - QFuture future = startMap(pool, sequence.begin(), sequence.end(), map); + QFuture future = + startMap(pool, sequence.begin(), sequence.end(), std::forward(map)); future.waitForFinished(); } template -void blockingMap(Sequence &&sequence, MapFunctor map) +void blockingMap(Sequence &&sequence, MapFunctor &&map) { - QFuture future = startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), map); + QFuture future = startMap(QThreadPool::globalInstance(), sequence.begin(), sequence.end(), + std::forward(map)); future.waitForFinished(); } // blockingMap() for iterator ranges template -void blockingMap(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) +void blockingMap(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { QFuture future = startMap(pool, begin, end, map); future.waitForFinished(); } template -void blockingMap(Iterator begin, Iterator end, MapFunctor map) +void blockingMap(Iterator begin, Iterator end, MapFunctor &&map) { - QFuture future = startMap(QThreadPool::globalInstance(), begin, end, map); + QFuture future = startMap(QThreadPool::globalInstance(), begin, end, + std::forward(map)); future.waitForFinished(); } @@ -430,25 +456,29 @@ void blockingMap(Iterator begin, Iterator end, MapFunctor map) template ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = - mappedReduced(pool, std::forward(sequence), map, reduce, options); + mappedReduced(pool, std::forward(sequence), + std::forward(map), + std::forward(reduce), options); return future.takeResult(); } template ResultType blockingMappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = - mappedReduced(std::forward(sequence), map, reduce, options); + mappedReduced(std::forward(sequence), + std::forward(map), + std::forward(reduce), options); return future.takeResult(); } @@ -462,14 +492,15 @@ template future = mappedReduced( - pool, std::forward(sequence), map, reduce, + pool, std::forward(sequence), std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -483,42 +514,49 @@ template , int> = 0> #endif ResultType blockingMappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( - std::forward(sequence), map, reduce, + std::forward(sequence), std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = - mappedReduced(pool, std::forward(sequence), map, reduce, options); + mappedReduced(pool, std::forward(sequence), + std::forward(map), + std::forward(reduce), options); return future.takeResult(); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingMappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = - mappedReduced(std::forward(sequence), map, reduce, options); + mappedReduced(std::forward(sequence), + std::forward(map), + std::forward(reduce), options); return future.takeResult(); } @@ -527,20 +565,22 @@ template #else template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( - pool, std::forward(sequence), map, reduce, + pool, std::forward(sequence), std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -549,20 +589,22 @@ ResultType blockingMappedReduced(QThreadPool *pool, template #else -template ::ResultType, - typename InitialValueType, - std::enable_if_t, int> = 0> +template>::ResultType, + typename InitialValueType, + std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(Sequence &&sequence, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( - std::forward(sequence), map, reduce, + std::forward(sequence), std::forward(map), + std::forward(reduce), ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -572,24 +614,28 @@ template future = mappedReduced(pool, begin, end, map, reduce, options); + QFuture future = + mappedReduced(pool, begin, end, std::forward(map), + std::forward(reduce), options); return future.takeResult(); } template ResultType blockingMappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = mappedReduced(begin, end, map, reduce, options); + QFuture future = + mappedReduced(begin, end, std::forward(map), + std::forward(reduce), options); return future.takeResult(); } @@ -604,14 +650,15 @@ template future = mappedReduced( - pool, begin, end, map, reduce, ResultType(std::forward(initialValue)), + pool, begin, end, std::forward(map), std::forward(reduce), + ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -626,42 +673,49 @@ template future = mappedReduced( - begin, end, map, reduce, ResultType(std::forward(initialValue)), + begin, end, std::forward(map), std::forward(reduce), + ResultType(std::forward(initialValue)), options); return future.takeResult(); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = mappedReduced(pool, begin, end, map, reduce, options); + QFuture future = + mappedReduced(pool, begin, end, std::forward(map), + std::forward(reduce), options); return future.takeResult(); } template ::ResultType> + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType> ResultType blockingMappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { - QFuture future = mappedReduced(begin, end, map, reduce, options); + QFuture future = + mappedReduced(begin, end, std::forward(map), + std::forward(reduce), options); return future.takeResult(); } @@ -670,22 +724,23 @@ template #else template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( - pool, begin, end, map, reduce, ResultType(std::forward(initialValue)), - options); + pool, begin, end, std::forward(map), std::forward(reduce), + ResultType(std::forward(initialValue)), options); return future.takeResult(); } @@ -694,88 +749,94 @@ template #else template ::ResultType, + typename ResultType = + typename QtPrivate::ReduceResultType>::ResultType, typename InitialValueType, std::enable_if_t, int> = 0> #endif ResultType blockingMappedReduced(Iterator begin, Iterator end, - MapFunctor map, - ReduceFunctor reduce, + MapFunctor &&map, + ReduceFunctor &&reduce, InitialValueType &&initialValue, ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce)) { QFuture future = mappedReduced( - begin, end, map, reduce, ResultType(std::forward(initialValue)), - options); + begin, end, std::forward(map), std::forward(reduce), + ResultType(std::forward(initialValue)), options); return future.takeResult(); } // mapped() for sequences with a different putput sequence type. template -OutputSequence blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor map) +OutputSequence blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map) { - return blockingMappedReduced(pool, std::forward(sequence), map, - QtPrivate::PushBackWrapper(), OrderedReduce); + return blockingMappedReduced(pool, std::forward(sequence), + std::forward(map), + QtPrivate::PushBackWrapper(), OrderedReduce); } template -OutputSequence blockingMapped(InputSequence &&sequence, MapFunctor map) +OutputSequence blockingMapped(InputSequence &&sequence, MapFunctor &&map) { - return blockingMappedReduced(QThreadPool::globalInstance(), - std::forward(sequence), map, - QtPrivate::PushBackWrapper(), OrderedReduce); + return blockingMappedReduced( + QThreadPool::globalInstance(), std::forward(sequence), + std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template -auto blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor map) +auto blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map) { using OutputSequence = typename QtPrivate::MapSequenceResultType, MapFunctor>::ResultType; - return blockingMappedReduced(pool, std::forward(sequence), map, + return blockingMappedReduced(pool, std::forward(sequence), + std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template -auto blockingMapped(InputSequence &&sequence, MapFunctor map) +auto blockingMapped(InputSequence &&sequence, MapFunctor &&map) { using OutputSequence = typename QtPrivate::MapSequenceResultType, MapFunctor>::ResultType; return blockingMappedReduced(QThreadPool::globalInstance(), - std::forward(sequence), map, + std::forward(sequence), + std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } // mapped() for iterator ranges template -Sequence blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) +Sequence blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { - return blockingMappedReduced(pool, begin, end, map, + return blockingMappedReduced(pool, begin, end, std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template -Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor map) +Sequence blockingMapped(Iterator begin, Iterator end, MapFunctor &&map) { - return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, map, - QtPrivate::PushBackWrapper(), OrderedReduce); + return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, + std::forward(map), + QtPrivate::PushBackWrapper(), OrderedReduce); } template -auto blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor map) +auto blockingMapped(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor &&map) { using OutputSequence = QtPrivate::MapResultType; - return blockingMappedReduced(pool, begin, end, map, + return blockingMappedReduced(pool, begin, end, std::forward(map), QtPrivate::PushBackWrapper(), OrderedReduce); } template -auto blockingMapped(Iterator begin, Iterator end, MapFunctor map) +auto blockingMapped(Iterator begin, Iterator end, MapFunctor &&map) { using OutputSequence = QtPrivate::MapResultType; - return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, map, - QtPrivate::PushBackWrapper(), OrderedReduce); + return blockingMappedReduced(QThreadPool::globalInstance(), begin, end, + std::forward(map), + QtPrivate::PushBackWrapper(), OrderedReduce); } } // namespace QtConcurrent diff --git a/src/concurrent/qtconcurrentmapkernel.h b/src/concurrent/qtconcurrentmapkernel.h index 3acf299752..2753bc7d0a 100644 --- a/src/concurrent/qtconcurrentmapkernel.h +++ b/src/concurrent/qtconcurrentmapkernel.h @@ -60,8 +60,9 @@ class MapKernel : public IterateKernel MapFunctor map; public: typedef void ReturnType; - MapKernel(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor _map) - : IterateKernel(pool, begin, end), map(_map) + template + MapKernel(QThreadPool *pool, Iterator begin, Iterator end, F &&_map) + : IterateKernel(pool, begin, end), map(std::forward(_map)) { } bool runIteration(Iterator it, int, void *) override @@ -100,19 +101,23 @@ class MappedReducedKernel : public IterateKernel public: typedef ReducedResultType ReturnType; - MappedReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor _map, - ReduceFunctor _reduce, ReduceOptions reduceOptions) + + template + MappedReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, F1 &&_map, + F2 &&_reduce, ReduceOptions reduceOptions) : IterateKernel(pool, begin, end), reducedResult(), - map(_map), reduce(_reduce), reducer(pool, reduceOptions) + map(std::forward(_map)), reduce(std::forward(_reduce)), + reducer(pool, reduceOptions) { } - MappedReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor _map, - ReduceFunctor _reduce, ReducedResultType &&initialValue, + template + MappedReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, F1 &&_map, + F2 &&_reduce, ReducedResultType &&initialValue, ReduceOptions reduceOptions) : IterateKernel(pool, begin, end), reducedResult(std::forward(initialValue)), - map(_map), - reduce(_reduce), + map(std::forward(_map)), + reduce(std::forward(_reduce)), reducer(pool, reduceOptions) { } @@ -175,8 +180,10 @@ class MappedEachKernel : public IterateKernel; public: - MappedEachKernel(QThreadPool *pool, Iterator begin, Iterator end, MapFunctor _map) - : IterateKernel(pool, begin, end), map(_map) { } + template + MappedEachKernel(QThreadPool *pool, Iterator begin, Iterator end, F &&_map) + : IterateKernel(pool, begin, end), map(std::forward(_map)) + { } bool runIteration(Iterator it, int, T *result) override { @@ -201,17 +208,19 @@ public: //! [qtconcurrentmapkernel-1] template inline ThreadEngineStarter startMap(QThreadPool *pool, Iterator begin, - Iterator end, Functor functor) + Iterator end, Functor &&functor) { - return startThreadEngine(new MapKernel(pool, begin, end, functor)); + return startThreadEngine(new MapKernel>( + pool, begin, end, std::forward(functor))); } //! [qtconcurrentmapkernel-2] template inline ThreadEngineStarter startMapped(QThreadPool *pool, Iterator begin, - Iterator end, Functor functor) + Iterator end, Functor &&functor) { - return startThreadEngine(new MappedEachKernel(pool, begin, end, functor)); + return startThreadEngine(new MappedEachKernel>( + pool, begin, end, std::forward(functor))); } /* @@ -221,14 +230,10 @@ inline ThreadEngineStarter startMapped(QThreadPool *pool, Iterator begin, template struct SequenceHolder1 : private QtPrivate::SequenceHolder, public Base { - SequenceHolder1(QThreadPool *pool, Sequence &&_sequence, Functor functor) - : QtPrivate::SequenceHolder(std::move(_sequence)), - Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor) - { } - - SequenceHolder1(QThreadPool *pool, const Sequence &_sequence, Functor functor) - : QtPrivate::SequenceHolder(_sequence), - Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor) + template + SequenceHolder1(QThreadPool *pool, S &&_sequence, F &&functor) + : QtPrivate::SequenceHolder(std::forward(_sequence)), + Base(pool, this->sequence.cbegin(), this->sequence.cend(), std::forward(functor)) { } void finish() override @@ -243,16 +248,17 @@ struct SequenceHolder1 : private QtPrivate::SequenceHolder, public Bas //! [qtconcurrentmapkernel-3] template inline ThreadEngineStarter startMapped(QThreadPool *pool, Sequence &&sequence, - Functor functor) + Functor &&functor) { using DecayedSequence = std::decay_t; - typedef SequenceHolder1, - Functor> - SequenceHolderType; + using DecayedFunctor = std::decay_t; + using SequenceHolderType = SequenceHolder1< + DecayedSequence, + MappedEachKernel, + DecayedFunctor>; - return startThreadEngine( - new SequenceHolderType(pool, std::forward(sequence), functor)); + return startThreadEngine(new SequenceHolderType(pool, std::forward(sequence), + std::forward(functor))); } //! [qtconcurrentmapkernel-4] @@ -260,19 +266,23 @@ template inline ThreadEngineStarter startMappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ReduceOptions options) { using DecayedSequence = std::decay_t; - typedef typename DecayedSequence::const_iterator Iterator; - typedef ReduceKernel Reducer; - typedef MappedReducedKernel - MappedReduceType; - typedef SequenceHolder2 - SequenceHolderType; + using DecayedMapFunctor = std::decay_t; + using DecayedReduceFunctor = std::decay_t; + using Iterator = typename DecayedSequence::const_iterator; + using Reducer = ReduceKernel; + using MappedReduceType = MappedReducedKernel; + using SequenceHolderType = SequenceHolder2; return startThreadEngine(new SequenceHolderType(pool, std::forward(sequence), - mapFunctor, reduceFunctor, options)); + std::forward(mapFunctor), + std::forward(reduceFunctor), + options)); } //! [qtconcurrentmapkernel-5] @@ -281,14 +291,17 @@ template startMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ReduceOptions options) { - typedef ReduceKernel Reducer; - typedef MappedReducedKernel - MappedReduceType; - return startThreadEngine(new MappedReduceType(pool, begin, end, mapFunctor, reduceFunctor, + using Reducer = + ReduceKernel, std::decay_t, IntermediateType>; + using MappedReduceType = MappedReducedKernel, + std::decay_t, Reducer>; + return startThreadEngine(new MappedReduceType(pool, begin, end, + std::forward(mapFunctor), + std::forward(reduceFunctor), options)); } @@ -297,21 +310,25 @@ template inline ThreadEngineStarter startMappedReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) { using DecayedSequence = std::decay_t; - typedef typename DecayedSequence::const_iterator Iterator; - typedef ReduceKernel Reducer; - typedef MappedReducedKernel - MappedReduceType; - typedef SequenceHolder2 - SequenceHolderType; + using DecayedMapFunctor = std::decay_t; + using DecayedReduceFunctor = std::decay_t; + using Iterator = typename DecayedSequence::const_iterator; + using Reducer = ReduceKernel; + using MappedReduceType = MappedReducedKernel; + using SequenceHolderType = SequenceHolder2; return startThreadEngine( - new SequenceHolderType(pool, std::forward(sequence), mapFunctor, - reduceFunctor, std::forward(initialValue), options)); + new SequenceHolderType(pool, std::forward(sequence), + std::forward(mapFunctor), + std::forward(reduceFunctor), + std::forward(initialValue), options)); } //! [qtconcurrentmapkernel-7] @@ -320,15 +337,17 @@ template startMappedReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) { - typedef ReduceKernel Reducer; - typedef MappedReducedKernel - MappedReduceType; - return startThreadEngine(new MappedReduceType(pool, begin, end, mapFunctor, reduceFunctor, + using Reducer = ReduceKernel, ResultType, IntermediateType>; + using MappedReduceType = MappedReducedKernel, + std::decay_t, Reducer>; + return startThreadEngine(new MappedReduceType(pool, begin, end, + std::forward(mapFunctor), + std::forward(reduceFunctor), std::forward(initialValue), options)); } diff --git a/src/concurrent/qtconcurrentreducekernel.h b/src/concurrent/qtconcurrentreducekernel.h index cdddfdde0d..5ad8bded6c 100644 --- a/src/concurrent/qtconcurrentreducekernel.h +++ b/src/concurrent/qtconcurrentreducekernel.h @@ -225,33 +225,21 @@ public: template struct SequenceHolder2 : private QtPrivate::SequenceHolder, public Base { - SequenceHolder2(QThreadPool *pool, const Sequence &_sequence, Functor1 functor1, - Functor2 functor2, ReduceOptions reduceOptions) - : QtPrivate::SequenceHolder(_sequence), - Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor1, functor2, - reduceOptions) - { } - - SequenceHolder2(QThreadPool *pool, Sequence &&_sequence, Functor1 functor1, Functor2 functor2, + template + SequenceHolder2(QThreadPool *pool, S &&_sequence, F1 &&functor1, F2 &&functor2, ReduceOptions reduceOptions) - : QtPrivate::SequenceHolder(std::move(_sequence)), - Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor1, functor2, - reduceOptions) - { } - - template - SequenceHolder2(QThreadPool *pool, const Sequence &_sequence, Functor1 functor1, - Functor2 functor2, InitialValueType &&initialValue, ReduceOptions reduceOptions) - : QtPrivate::SequenceHolder(_sequence), - Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor1, functor2, - std::forward(initialValue), reduceOptions) + : QtPrivate::SequenceHolder(std::forward(_sequence)), + Base(pool, this->sequence.cbegin(), this->sequence.cend(), + std::forward(functor1), std::forward(functor2), reduceOptions) { } - template - SequenceHolder2(QThreadPool *pool, Sequence &&_sequence, Functor1 functor1, Functor2 functor2, + template + SequenceHolder2(QThreadPool *pool, S &&_sequence, F1 &&functor1, F2 &&functor2, InitialValueType &&initialValue, ReduceOptions reduceOptions) - : QtPrivate::SequenceHolder(std::move(_sequence)), - Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor1, functor2, + : QtPrivate::SequenceHolder(std::forward(_sequence)), + Base(pool, this->sequence.cbegin(), this->sequence.cend(), + std::forward(functor1), std::forward(functor2), std::forward(initialValue), reduceOptions) { } diff --git a/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp b/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp index 8cce82defd..242f95fd92 100644 --- a/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp +++ b/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp @@ -39,13 +39,17 @@ class tst_QtConcurrentFilter : public QObject private slots: void filter(); void filterThreadPool(); + void filterWithMoveOnlyCallable(); void filtered(); void filteredThreadPool(); + void filteredWithMoveOnlyCallable(); void filteredReduced(); void filteredReducedThreadPool(); + void filteredReducedWithMoveOnlyCallables(); void filteredReducedDifferentType(); void filteredReducedInitialValue(); void filteredReducedInitialValueThreadPool(); + void filteredReducedInitialValueWithMoveOnlyCallables(); void filteredReducedDifferentTypeInitialValue(); void resultAt(); void incrementalResults(); @@ -177,6 +181,34 @@ void tst_QtConcurrentFilter::filterThreadPool() CHECK_FAIL("lambda"); } +void tst_QtConcurrentFilter::filterWithMoveOnlyCallable() +{ + const QList intListEven { 2, 4 }; + { + QList intList { 1, 2, 3, 4 }; + QtConcurrent::filter(intList, KeepEvenIntegersMoveOnly()).waitForFinished(); + QCOMPARE(intList, intListEven); + } + + { + QList intList { 1, 2, 3, 4 }; + QtConcurrent::blockingFilter(intList, KeepEvenIntegersMoveOnly()); + QCOMPARE(intList, intListEven); + } + + QThreadPool pool; + { + QList intList { 1, 2, 3, 4 }; + QtConcurrent::filter(&pool, intList, KeepEvenIntegersMoveOnly()).waitForFinished(); + QCOMPARE(intList, intListEven); + } + { + QList intList { 1, 2, 3, 4 }; + QtConcurrent::blockingFilter(&pool, intList, KeepEvenIntegersMoveOnly()); + QCOMPARE(intList, intListEven); + } +} + template @@ -323,6 +355,52 @@ void tst_QtConcurrentFilter::filteredThreadPool() } } +void tst_QtConcurrentFilter::filteredWithMoveOnlyCallable() +{ + const QList intList { 1, 2, 3, 4 }; + const QList intListEven { 2, 4 }; + { + const auto result = QtConcurrent::filtered(intList, KeepEvenIntegersMoveOnly()).results(); + QCOMPARE(result, intListEven); + } + { + const auto result = QtConcurrent::filtered( + intList.begin(), intList.end(), KeepEvenIntegersMoveOnly()).results(); + QCOMPARE(result, intListEven); + } + { + const auto result = QtConcurrent::blockingFiltered(intList, KeepEvenIntegersMoveOnly()); + QCOMPARE(result, intListEven); + } + { + const auto result = QtConcurrent::blockingFiltered>( + intList.begin(), intList.end(), KeepEvenIntegersMoveOnly()); + QCOMPARE(result, intListEven); + } + + QThreadPool pool; + { + const auto result = + QtConcurrent::filtered(&pool, intList, KeepEvenIntegersMoveOnly()).results(); + QCOMPARE(result, intListEven); + } + { + const auto result = QtConcurrent::filtered(&pool, intList.begin(), intList.end(), + KeepEvenIntegersMoveOnly()).results(); + QCOMPARE(result, intListEven); + } + { + const auto result = + QtConcurrent::blockingFiltered(&pool, intList, KeepEvenIntegersMoveOnly()); + QCOMPARE(result, intListEven); + } + { + const auto result = QtConcurrent::blockingFiltered>( + &pool, intList.begin(), intList.end(), KeepEvenIntegersMoveOnly()); + QCOMPARE(result, intListEven); + } +} + template intList { 1, 2, 3, 4 }; + const QList intListEven { 2, 4 }; + const auto sum = 6; + { + const auto result = + QtConcurrent::filteredReduced(intList, KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = + QtConcurrent::filteredReduced(intList.begin(), intList.end(), + KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + intList, KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + intList.begin(), intList.end(), KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } + + QThreadPool pool; + { + const auto result = + QtConcurrent::filteredReduced(&pool, intList, KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::filteredReduced( + &pool, intList.begin(), intList.end(), + KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + &pool, intList, KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + &pool, intList.begin(), intList.end(), KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } +} + void tst_QtConcurrentFilter::filteredReducedDifferentType() { const QList numberList {1, 2, 3, 4}; @@ -911,6 +1044,64 @@ void tst_QtConcurrentFilter::filteredReducedInitialValueThreadPool() } } +void tst_QtConcurrentFilter::filteredReducedInitialValueWithMoveOnlyCallables() +{ + const QList intList { 1, 2, 3, 4 }; + const QList intListEven { 2, 4 }; + const auto initial = 10; + const auto sum = 16; + { + const auto result = + QtConcurrent::filteredReduced(intList, KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly(), initial).result(); + QCOMPARE(result, sum); + } + { + const auto result = + QtConcurrent::filteredReduced(intList.begin(), intList.end(), + KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly(), initial).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + intList, KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly(), initial); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + intList.begin(), intList.end(), KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly(), + initial); + QCOMPARE(result, sum); + } + + QThreadPool pool; + { + const auto result = + QtConcurrent::filteredReduced(&pool, intList, KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly(), initial).result(); + QCOMPARE(result, sum); + } + { + const auto result = + QtConcurrent::filteredReduced( + &pool, intList.begin(), intList.end(), + KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly(), initial).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + &pool, intList, KeepEvenIntegersMoveOnly(), IntSumReduceMoveOnly(), initial); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingFilteredReduced( + &pool, intList.begin(), intList.end(), KeepEvenIntegersMoveOnly(), + IntSumReduceMoveOnly(), initial); + QCOMPARE(result, sum); + } +} + void tst_QtConcurrentFilter::filteredReducedDifferentTypeInitialValue() { const QList numberList {1, 2, 3, 4}; diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index e3b61bb109..d1ea999e37 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -45,11 +45,14 @@ private slots: void mapOnRvalue(); void mapped(); void mappedThreadPool(); + void mappedWithMoveOnlyCallable(); void mappedReduced(); void mappedReducedThreadPool(); + void mappedReducedWithMoveOnlyCallable(); void mappedReducedDifferentType(); void mappedReducedInitialValue(); void mappedReducedInitialValueThreadPool(); + void mappedReducedInitialValueWithMoveOnlyCallable(); void mappedReducedDifferentTypeInitialValue(); void assignResult(); void functionOverloads(); @@ -98,6 +101,19 @@ public: } }; +class MultiplyBy2InPlaceMoveOnly +{ +public: + MultiplyBy2InPlaceMoveOnly() = default; + MultiplyBy2InPlaceMoveOnly(MultiplyBy2InPlaceMoveOnly &&) = default; + MultiplyBy2InPlaceMoveOnly &operator=(MultiplyBy2InPlaceMoveOnly &&other) = default; + + MultiplyBy2InPlaceMoveOnly(const MultiplyBy2InPlaceMoveOnly &) = delete; + MultiplyBy2InPlaceMoveOnly &operator=(const MultiplyBy2InPlaceMoveOnly &) = delete; + + void operator()(int &x) { x *= 2; } +}; + Q_DECLARE_METATYPE(QList); void tst_QtConcurrentMap::map() @@ -138,6 +154,12 @@ void tst_QtConcurrentMap::map() QCOMPARE(list, QList() << 128 << 256 << 384); QtConcurrent::map(list.begin(), list.end(), [](int &x){x *= 2;}).waitForFinished(); QCOMPARE(list, QList() << 256 << 512 << 768); + + // move-only functor + QtConcurrent::map(list, MultiplyBy2InPlaceMoveOnly()).waitForFinished(); + QCOMPARE(list, QList() << 512 << 1024 << 1536); + QtConcurrent::map(list.begin(), list.end(), MultiplyBy2InPlaceMoveOnly()).waitForFinished(); + QCOMPARE(list, QList() << 1024 << 2048 << 3072); } // functors don't take arguments by reference, making these no-ops @@ -259,6 +281,12 @@ void tst_QtConcurrentMap::blockingMap() QCOMPARE(list, QList() << 128 << 256 << 384); QtConcurrent::blockingMap(list.begin(), list.end(), [](int &x) { x *= 2; }); QCOMPARE(list, QList() << 256 << 512 << 768); + + // move-only functor + QtConcurrent::blockingMap(list, MultiplyBy2InPlaceMoveOnly()); + QCOMPARE(list, QList() << 512 << 1024 << 1536); + QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2InPlaceMoveOnly()); + QCOMPARE(list, QList() << 1024 << 2048 << 3072); } // functors take arguments by reference, modifying the move-only sequence in place @@ -409,6 +437,23 @@ public: } }; +class MultiplyBy2MoveOnly +{ +public: + MultiplyBy2MoveOnly() = default; + MultiplyBy2MoveOnly(MultiplyBy2MoveOnly &&) = default; + MultiplyBy2MoveOnly &operator=(MultiplyBy2MoveOnly &&other) = default; + + MultiplyBy2MoveOnly(const MultiplyBy2MoveOnly &) = delete; + MultiplyBy2MoveOnly &operator=(const MultiplyBy2MoveOnly &) = delete; + + int operator()(int x) const + { + int y = x * 2; + return y; + } +}; + double intToDouble(int x) { return double(x); @@ -642,6 +687,50 @@ void tst_QtConcurrentMap::mappedThreadPool() } } +void tst_QtConcurrentMap::mappedWithMoveOnlyCallable() +{ + const QList intList { 1, 2, 3 }; + const QList intListMultipiedBy2 { 2, 4, 6 }; + { + const auto result = QtConcurrent::mapped(intList, MultiplyBy2()).results(); + QCOMPARE(result, intListMultipiedBy2); + } + { + const auto result = + QtConcurrent::mapped(intList.begin(), intList.end(), MultiplyBy2()).results(); + QCOMPARE(result, intListMultipiedBy2); + } + { + const auto result = QtConcurrent::blockingMapped(intList, MultiplyBy2()); + QCOMPARE(result, intListMultipiedBy2); + } + { + const auto result = QtConcurrent::blockingMapped>(intList.begin(), intList.end(), + MultiplyBy2()); + QCOMPARE(result, intListMultipiedBy2); + } + + QThreadPool pool; + { + const auto result = QtConcurrent::mapped(&pool, intList, MultiplyBy2()).results(); + QCOMPARE(result, intListMultipiedBy2); + } + { + const auto result = QtConcurrent::mapped( + &pool, intList.begin(), intList.end(), MultiplyBy2()).results(); + QCOMPARE(result, intListMultipiedBy2); + } + { + const auto result = QtConcurrent::blockingMapped(&pool, intList, MultiplyBy2()); + QCOMPARE(result, intListMultipiedBy2); + } + { + const auto result = QtConcurrent::blockingMapped>(&pool, intList.begin(), + intList.end(), MultiplyBy2()); + QCOMPARE(result, intListMultipiedBy2); + } +} + int intSquare(int x) { return x * x; @@ -901,6 +990,56 @@ void tst_QtConcurrentMap::mappedReducedThreadPool() } } +void tst_QtConcurrentMap::mappedReducedWithMoveOnlyCallable() +{ + const QList intList { 1, 2, 3 }; + const auto sum = 12; + { + const auto result = QtConcurrent::mappedReduced( + intList, MultiplyBy2(), IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = + QtConcurrent::mappedReduced(intList.begin(), intList.end(), + MultiplyBy2(), IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced(intList, MultiplyBy2(), + IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced( + intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } + + QThreadPool pool; + { + const auto result = QtConcurrent::mappedReduced(&pool, intList, MultiplyBy2(), + IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = + QtConcurrent::mappedReduced(&pool, intList.begin(), intList.end(), + MultiplyBy2(), IntSumReduceMoveOnly()).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced(&pool, intList, MultiplyBy2(), + IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced( + &pool, intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly()); + QCOMPARE(result, sum); + } +} + void tst_QtConcurrentMap::mappedReducedDifferentType() { const QList intList {1, 2, 3}; @@ -1196,6 +1335,61 @@ void tst_QtConcurrentMap::mappedReducedInitialValueThreadPool() } } +void tst_QtConcurrentMap::mappedReducedInitialValueWithMoveOnlyCallable() +{ + const QList intList { 1, 2, 3 }; + const auto initialValue = 10; + const auto sum = 22; + { + const auto result = + QtConcurrent::mappedReduced(intList, MultiplyBy2(), + IntSumReduceMoveOnly(), initialValue).result(); + QCOMPARE(result, sum); + } + { + const auto result = + QtConcurrent::mappedReduced(intList.begin(), intList.end(), MultiplyBy2(), + IntSumReduceMoveOnly(), initialValue).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced( + intList, MultiplyBy2(), IntSumReduceMoveOnly(), initialValue); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced( + intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly(), + initialValue); + QCOMPARE(result, sum); + } + + QThreadPool pool; + { + const auto result = + QtConcurrent::mappedReduced(&pool, intList, MultiplyBy2(), + IntSumReduceMoveOnly(), initialValue).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::mappedReduced(&pool, intList.begin(), intList.end(), + MultiplyBy2(), IntSumReduceMoveOnly(), + initialValue).result(); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced( + &pool, intList, MultiplyBy2(), IntSumReduceMoveOnly(), initialValue); + QCOMPARE(result, sum); + } + { + const auto result = QtConcurrent::blockingMappedReduced( + &pool, intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly(), + initialValue); + QCOMPARE(result, sum); + } +} + void tst_QtConcurrentMap::mappedReducedDifferentTypeInitialValue() { // This is a copy of tst_QtConcurrentMap::mappedReducedDifferentType diff --git a/tests/auto/concurrent/testhelper_functions.h b/tests/auto/concurrent/testhelper_functions.h index aeba794977..17836cba51 100644 --- a/tests/auto/concurrent/testhelper_functions.h +++ b/tests/auto/concurrent/testhelper_functions.h @@ -44,6 +44,19 @@ public: } }; +class KeepEvenIntegersMoveOnly +{ +public: + KeepEvenIntegersMoveOnly() = default; + KeepEvenIntegersMoveOnly(KeepEvenIntegersMoveOnly &&) = default; + KeepEvenIntegersMoveOnly &operator=(KeepEvenIntegersMoveOnly &&other) = default; + + KeepEvenIntegersMoveOnly(const KeepEvenIntegersMoveOnly &) = delete; + KeepEvenIntegersMoveOnly &operator=(const KeepEvenIntegersMoveOnly &) = delete; + + bool operator()(int x) { return (x & 1) == 0; } +}; + class Number { int n; @@ -121,6 +134,19 @@ public: } }; +class IntSumReduceMoveOnly +{ +public: + IntSumReduceMoveOnly() = default; + IntSumReduceMoveOnly(IntSumReduceMoveOnly &&) = default; + IntSumReduceMoveOnly &operator=(IntSumReduceMoveOnly &&other) = default; + + IntSumReduceMoveOnly(const IntSumReduceMoveOnly &) = delete; + IntSumReduceMoveOnly &operator=(const IntSumReduceMoveOnly &) = delete; + + void operator()(int &sum, int x) { sum += x; } +}; + void numberSumReduce(int &sum, const Number &x) { sum += x.toInt(); -- cgit v1.2.3