diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-09-24 11:07:32 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-09-30 15:28:01 +0200 |
commit | 2e27b98cffcd96d4e1d8a0b5836a36dc9929f676 (patch) | |
tree | 5276d28f7a834fa59e653a9cb887f55b72891fe3 /src/concurrent/qtconcurrentfilterkernel.h | |
parent | 5c7307775d32ae0a54acad8ae7a1c9d3cff7d45e (diff) |
Use universal references in QtConcurrent
Changed QtConcurrent algorithms to take the passed sequences as
universal references, where it makes sense. In addition to avoiding
to create extra copies when passing rvalues, this change allows
passing temporary container adaptors to QtConcurrent::map (e.g. see
the example in the ticket and the new test-cases).
Task-number: QTBUG-83170
Change-Id: Ia7c0833f4ec1d860294fa5214cd53934b65ff084
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/concurrent/qtconcurrentfilterkernel.h')
-rw-r--r-- | src/concurrent/qtconcurrentfilterkernel.h | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/concurrent/qtconcurrentfilterkernel.h b/src/concurrent/qtconcurrentfilterkernel.h index 5f9094f6b6..7077327ae4 100644 --- a/src/concurrent/qtconcurrentfilterkernel.h +++ b/src/concurrent/qtconcurrentfilterkernel.h @@ -308,33 +308,35 @@ startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor funct //! [QtConcurrent-3] template <typename Sequence, typename KeepFunctor> -inline ThreadEngineStarter<typename Sequence::value_type> -startFiltered(QThreadPool *pool, const Sequence &sequence, KeepFunctor functor) +inline decltype(auto) startFiltered(QThreadPool *pool, Sequence &&sequence, KeepFunctor functor) { - typedef SequenceHolder1<Sequence, - FilteredEachKernel<typename Sequence::const_iterator, KeepFunctor>, + using DecayedSequence = std::decay_t<Sequence>; + typedef SequenceHolder1<DecayedSequence, + FilteredEachKernel<typename DecayedSequence::const_iterator, KeepFunctor>, KeepFunctor> - SequenceHolderType; - return startThreadEngine(new SequenceHolderType(pool, sequence, functor)); + SequenceHolderType; + return startThreadEngine( + new SequenceHolderType(pool, std::forward<Sequence>(sequence), functor)); } //! [QtConcurrent-4] template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, - const Sequence &sequence, + Sequence &&sequence, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ReduceOptions options) { - typedef typename Sequence::const_iterator Iterator; + using DecayedSequence = std::decay_t<Sequence>; + typedef typename DecayedSequence::const_iterator Iterator; typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type > Reducer; typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> FilteredReduceType; - typedef SequenceHolder2<Sequence, FilteredReduceType, MapFunctor, ReduceFunctor> + typedef SequenceHolder2<DecayedSequence, FilteredReduceType, MapFunctor, ReduceFunctor> SequenceHolderType; - return startThreadEngine(new SequenceHolderType(pool, sequence, mapFunctor, - reduceFunctor, options)); + return startThreadEngine(new SequenceHolderType(pool, std::forward<Sequence>(sequence), + mapFunctor, reduceFunctor, options)); } @@ -359,21 +361,23 @@ inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, //! [QtConcurrent-6] template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, - const Sequence &sequence, + Sequence &&sequence, MapFunctor mapFunctor, ReduceFunctor reduceFunctor, ResultType &&initialValue, ReduceOptions options) { - typedef typename Sequence::const_iterator Iterator; + using DecayedSequence = std::decay_t<Sequence>; + typedef typename DecayedSequence::const_iterator Iterator; typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type > Reducer; typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> FilteredReduceType; - typedef SequenceHolder2<Sequence, FilteredReduceType, MapFunctor, ReduceFunctor> + typedef SequenceHolder2<DecayedSequence, FilteredReduceType, MapFunctor, ReduceFunctor> SequenceHolderType; - return startThreadEngine(new SequenceHolderType(pool, sequence, mapFunctor, reduceFunctor, - std::forward<ResultType>(initialValue), options)); + return startThreadEngine( + new SequenceHolderType(pool, std::forward<Sequence>(sequence), mapFunctor, + reduceFunctor, std::forward<ResultType>(initialValue), options)); } //! [QtConcurrent-7] |