summaryrefslogtreecommitdiffstats
path: root/src/concurrent/qtconcurrentmapkernel.h
diff options
context:
space:
mode:
authorSona Kurazyan <sona.kurazyan@qt.io>2020-09-24 11:07:32 +0200
committerSona Kurazyan <sona.kurazyan@qt.io>2020-09-30 15:28:01 +0200
commit2e27b98cffcd96d4e1d8a0b5836a36dc9929f676 (patch)
tree5276d28f7a834fa59e653a9cb887f55b72891fe3 /src/concurrent/qtconcurrentmapkernel.h
parent5c7307775d32ae0a54acad8ae7a1c9d3cff7d45e (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/qtconcurrentmapkernel.h')
-rw-r--r--src/concurrent/qtconcurrentmapkernel.h42
1 files changed, 26 insertions, 16 deletions
diff --git a/src/concurrent/qtconcurrentmapkernel.h b/src/concurrent/qtconcurrentmapkernel.h
index 0e6cfa4b9a..3acf299752 100644
--- a/src/concurrent/qtconcurrentmapkernel.h
+++ b/src/concurrent/qtconcurrentmapkernel.h
@@ -221,6 +221,11 @@ inline ThreadEngineStarter<T> startMapped(QThreadPool *pool, Iterator begin,
template <typename Sequence, typename Base, typename Functor>
struct SequenceHolder1 : private QtPrivate::SequenceHolder<Sequence>, public Base
{
+ SequenceHolder1(QThreadPool *pool, Sequence &&_sequence, Functor functor)
+ : QtPrivate::SequenceHolder<Sequence>(std::move(_sequence)),
+ Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor)
+ { }
+
SequenceHolder1(QThreadPool *pool, const Sequence &_sequence, Functor functor)
: QtPrivate::SequenceHolder<Sequence>(_sequence),
Base(pool, this->sequence.cbegin(), this->sequence.cend(), functor)
@@ -237,33 +242,37 @@ struct SequenceHolder1 : private QtPrivate::SequenceHolder<Sequence>, public Bas
//! [qtconcurrentmapkernel-3]
template <typename T, typename Sequence, typename Functor>
-inline ThreadEngineStarter<T> startMapped(QThreadPool *pool, const Sequence &sequence,
+inline ThreadEngineStarter<T> startMapped(QThreadPool *pool, Sequence &&sequence,
Functor functor)
{
- typedef SequenceHolder1<Sequence,
- MappedEachKernel<typename Sequence::const_iterator , Functor>, Functor>
- SequenceHolderType;
+ using DecayedSequence = std::decay_t<Sequence>;
+ typedef SequenceHolder1<DecayedSequence,
+ MappedEachKernel<typename DecayedSequence::const_iterator, Functor>,
+ Functor>
+ SequenceHolderType;
- return startThreadEngine(new SequenceHolderType(pool, sequence, functor));
+ return startThreadEngine(
+ new SequenceHolderType(pool, std::forward<Sequence>(sequence), functor));
}
//! [qtconcurrentmapkernel-4]
template <typename IntermediateType, typename ResultType, typename Sequence, typename MapFunctor,
typename ReduceFunctor>
inline ThreadEngineStarter<ResultType> startMappedReduced(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, IntermediateType> Reducer;
typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer>
MappedReduceType;
- typedef SequenceHolder2<Sequence, MappedReduceType, MapFunctor, ReduceFunctor>
+ typedef SequenceHolder2<DecayedSequence, MappedReduceType, MapFunctor, ReduceFunctor>
SequenceHolderType;
- return startThreadEngine(new SequenceHolderType(pool, sequence, mapFunctor, reduceFunctor,
- options));
+ return startThreadEngine(new SequenceHolderType(pool, std::forward<Sequence>(sequence),
+ mapFunctor, reduceFunctor, options));
}
//! [qtconcurrentmapkernel-5]
@@ -287,21 +296,22 @@ inline ThreadEngineStarter<ResultType> startMappedReduced(QThreadPool *pool,
template <typename IntermediateType, typename ResultType, typename Sequence, typename MapFunctor,
typename ReduceFunctor>
inline ThreadEngineStarter<ResultType> startMappedReduced(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, IntermediateType> Reducer;
typedef MappedReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer>
MappedReduceType;
- typedef SequenceHolder2<Sequence, MappedReduceType, MapFunctor, ReduceFunctor>
+ typedef SequenceHolder2<DecayedSequence, MappedReduceType, 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));
}
//! [qtconcurrentmapkernel-7]