From 2e27b98cffcd96d4e1d8a0b5836a36dc9929f676 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 24 Sep 2020 11:07:32 +0200 Subject: Use universal references in QtConcurrent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Jarek Kobus Reviewed-by: MÃ¥rten Nordheim --- .../qtconcurrentmap/tst_qtconcurrentmap.cpp | 53 +++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 33ec251452..6b844fa73d 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -41,7 +41,8 @@ class tst_QtConcurrentMap : public QObject Q_OBJECT private slots: void map(); - void blocking_map(); + void blockingMap(); + void mapOnRvalue(); void mapped(); void mappedThreadPool(); void mappedReduced(); @@ -211,7 +212,7 @@ void tst_QtConcurrentMap::map() #endif } -void tst_QtConcurrentMap::blocking_map() +void tst_QtConcurrentMap::blockingMap() { // functors take arguments by reference, modifying the sequence in place { @@ -323,6 +324,54 @@ void tst_QtConcurrentMap::blocking_map() #endif } +void tst_QtConcurrentMap::mapOnRvalue() +{ + struct ListRange + { + using iterator = QList::iterator; + ListRange(iterator b, iterator e) : m_begin(b), m_end(e) { } + + iterator begin() const { return m_begin; } + iterator end() const { return m_end; } + + private: + iterator m_begin; + iterator m_end; + }; + + const QList expected { 1, 4, 6, 4 }; + { + QList list { 1, 2, 3, 4 }; + auto future = + QtConcurrent::map(ListRange(list.begin() + 1, list.end() - 1), multiplyBy2InPlace); + future.waitForFinished(); + QCOMPARE(list, expected); + } + + { + QList list { 1, 2, 3, 4 }; + QThreadPool pool; + auto future = QtConcurrent::map(&pool, ListRange(list.begin() + 1, list.end() - 1), + multiplyBy2InPlace); + future.waitForFinished(); + QCOMPARE(list, expected); + } + + { + QList list { 1, 2, 3, 4 }; + QtConcurrent::blockingMap(ListRange(list.begin() + 1, list.end() - 1), multiplyBy2InPlace); + QCOMPARE(list, expected); + } + + { + QList list { 1, 2, 3, 4 }; + QThreadPool pool; + QtConcurrent::blockingMap(&pool, ListRange(list.begin() + 1, list.end() - 1), + multiplyBy2InPlace); + QCOMPARE(list, expected); + } +} + int multiplyBy2(int x) { int y = x * 2; -- cgit v1.2.3