diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-10-09 18:21:17 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-10-30 17:19:26 +0100 |
commit | 4d9658b7cd2b072dd8b24d9bb6844b7cbcf22ad0 (patch) | |
tree | ab43ab1986e351468b3f099ee1b02752e4479f81 /src/concurrent/qtconcurrentfilterkernel.h | |
parent | ff0ba7e2d7b91fd5809cb314935a1ca1a436f6c9 (diff) |
Use universal references for passing callables in QtConcurrent
Task-number: QTBUG-87596
Change-Id: I219f08d73b97317820ec6e329ab1e6c89c0545f1
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@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 | 135 |
1 files changed, 75 insertions, 60 deletions
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<typename Sequence::const_iterator, voi Reducer reducer; public: - FilterKernel(QThreadPool *pool, Sequence &_sequence, KeepFunctor _keep, ReduceFunctor _reduce) + template <typename Keep = KeepFunctor, typename Reduce = ReduceFunctor> + FilterKernel(QThreadPool *pool, Sequence &_sequence, Keep &&_keep, Reduce &&_reduce) : IterateKernelType(pool, const_cast<const Sequence &>(_sequence).begin(), const_cast<const Sequence &>(_sequence).end()), reducedResult(), sequence(_sequence), - keep(_keep), - reduce(_reduce), + keep(std::forward<Keep>(_keep)), + reduce(std::forward<Reduce>(_reduce)), reducer(pool, OrderedReduce) { } @@ -166,23 +167,26 @@ class FilteredReducedKernel : public IterateKernel<Iterator, ReducedResultType> typedef IterateKernel<Iterator, ReducedResultType> IterateKernelType; public: + template <typename Keep = KeepFunctor, typename Reduce = ReduceFunctor> 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>(_keep)), + reduce(std::forward<Reduce>(_reduce)), reducer(pool, reduceOption) { } - FilteredReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor _keep, - ReduceFunctor _reduce, ReducedResultType &&initialValue, + template <typename Keep = KeepFunctor, typename Reduce = ReduceFunctor> + FilteredReducedKernel(QThreadPool *pool, Iterator begin, Iterator end, Keep &&_keep, + Reduce &&_reduce, ReducedResultType &&initialValue, ReduceOptions reduceOption) : IterateKernelType(pool, begin, end), reducedResult(std::forward<ReducedResultType>(initialValue)), - keep(_keep), - reduce(_reduce), + keep(std::forward<Keep>(_keep)), + reduce(std::forward<Reduce>(_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 <typename Keep = KeepFunctor> + FilteredEachKernel(QThreadPool *pool, Iterator begin, Iterator end, Keep &&_keep) + : IterateKernelType(pool, begin, end), keep(std::forward<Keep>(_keep)) { } void start() override @@ -300,43 +305,47 @@ public: template <typename Iterator, typename KeepFunctor> inline ThreadEngineStarter<typename qValueType<Iterator>::value_type> -startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor functor) +startFiltered(QThreadPool *pool, Iterator begin, Iterator end, KeepFunctor &&functor) { - return startThreadEngine(new FilteredEachKernel<Iterator, KeepFunctor> - (pool, begin, end, functor)); + return startThreadEngine(new FilteredEachKernel<Iterator, std::decay_t<KeepFunctor>> + (pool, begin, end, std::forward<KeepFunctor>(functor))); } //! [QtConcurrent-3] template <typename Sequence, typename KeepFunctor> -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<Sequence>; - typedef SequenceHolder1<DecayedSequence, - FilteredEachKernel<typename DecayedSequence::const_iterator, KeepFunctor>, - KeepFunctor> - SequenceHolderType; - return startThreadEngine( - new SequenceHolderType(pool, std::forward<Sequence>(sequence), functor)); + using DecayedFunctor = std::decay_t<KeepFunctor>; + using SequenceHolderType = SequenceHolder1<DecayedSequence, + FilteredEachKernel<typename DecayedSequence::const_iterator, DecayedFunctor>, + DecayedFunctor>; + return startThreadEngine(new SequenceHolderType(pool, std::forward<Sequence>(sequence), + std::forward<KeepFunctor>(functor))); } //! [QtConcurrent-4] template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ReduceOptions options) { 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<DecayedSequence, FilteredReduceType, MapFunctor, ReduceFunctor> - SequenceHolderType; + using DecayedMapFunctor = std::decay_t<MapFunctor>; + using DecayedReduceFunctor = std::decay_t<ReduceFunctor>; + using Iterator = typename DecayedSequence::const_iterator; + using Reducer = ReduceKernel<DecayedReduceFunctor, ResultType, + typename qValueType<Iterator>::value_type>; + using FilteredReduceType = FilteredReducedKernel<ResultType, Iterator, DecayedMapFunctor, + DecayedReduceFunctor, Reducer>; + using SequenceHolderType = SequenceHolder2<DecayedSequence, FilteredReduceType, + DecayedMapFunctor, DecayedReduceFunctor>; return startThreadEngine(new SequenceHolderType(pool, std::forward<Sequence>(sequence), - mapFunctor, reduceFunctor, options)); + std::forward<MapFunctor>(mapFunctor), + std::forward<ReduceFunctor>(reduceFunctor), + options)); } @@ -345,16 +354,17 @@ template <typename ResultType, typename Iterator, typename MapFunctor, typename inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ReduceOptions options) { - typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type> - Reducer; - typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> - FilteredReduceType; - return startThreadEngine(new FilteredReduceType(pool, begin, end, mapFunctor, - reduceFunctor, options)); + using Reducer = ReduceKernel<std::decay_t<ReduceFunctor>, ResultType, + typename qValueType<Iterator>::value_type>; + using FilteredReduceType = FilteredReducedKernel<ResultType, Iterator, std::decay_t<MapFunctor>, + std::decay_t<ReduceFunctor>, Reducer>; + return startThreadEngine( + new FilteredReduceType(pool, begin, end, std::forward<MapFunctor>(mapFunctor), + std::forward<ReduceFunctor>(reduceFunctor), options)); } // Repeat the two functions above, but now with an initial value! @@ -362,22 +372,25 @@ inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, template <typename ResultType, typename Sequence, typename MapFunctor, typename ReduceFunctor> inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, Sequence &&sequence, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) { 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<DecayedSequence, FilteredReduceType, MapFunctor, ReduceFunctor> - SequenceHolderType; - return startThreadEngine( - new SequenceHolderType(pool, std::forward<Sequence>(sequence), mapFunctor, - reduceFunctor, std::forward<ResultType>(initialValue), options)); + using DecayedMapFunctor = std::decay_t<MapFunctor>; + using DecayedReduceFunctor = std::decay_t<ReduceFunctor>; + using Iterator = typename DecayedSequence::const_iterator; + using Reducer = ReduceKernel<DecayedReduceFunctor, ResultType, + typename qValueType<Iterator>::value_type>; + using FilteredReduceType = FilteredReducedKernel<ResultType, Iterator, DecayedMapFunctor, + DecayedReduceFunctor, Reducer>; + using SequenceHolderType = SequenceHolder2<DecayedSequence, FilteredReduceType, + DecayedMapFunctor, DecayedReduceFunctor>; + return startThreadEngine(new SequenceHolderType( + pool, std::forward<Sequence>(sequence), std::forward<MapFunctor>(mapFunctor), + std::forward<ReduceFunctor>(reduceFunctor), std::forward<ResultType>(initialValue), + options)); } //! [QtConcurrent-7] @@ -385,17 +398,19 @@ template <typename ResultType, typename Iterator, typename MapFunctor, typename inline ThreadEngineStarter<ResultType> startFilteredReduced(QThreadPool *pool, Iterator begin, Iterator end, - MapFunctor mapFunctor, - ReduceFunctor reduceFunctor, + MapFunctor &&mapFunctor, + ReduceFunctor &&reduceFunctor, ResultType &&initialValue, ReduceOptions options) { - typedef ReduceKernel<ReduceFunctor, ResultType, typename qValueType<Iterator>::value_type> - Reducer; - typedef FilteredReducedKernel<ResultType, Iterator, MapFunctor, ReduceFunctor, Reducer> - FilteredReduceType; - return startThreadEngine(new FilteredReduceType(pool, begin, end, mapFunctor, reduceFunctor, - std::forward<ResultType>(initialValue), options)); + using Reducer = ReduceKernel<std::decay_t<ReduceFunctor>, ResultType, + typename qValueType<Iterator>::value_type>; + using FilteredReduceType = FilteredReducedKernel<ResultType, Iterator, std::decay_t<MapFunctor>, + std::decay_t<ReduceFunctor>, Reducer>; + return startThreadEngine( + new FilteredReduceType(pool, begin, end, std::forward<MapFunctor>(mapFunctor), + std::forward<ReduceFunctor>(reduceFunctor), + std::forward<ResultType>(initialValue), options)); } |