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 /tests/auto | |
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 'tests/auto')
-rw-r--r-- | tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp | 53 |
1 files changed, 51 insertions, 2 deletions
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<int>::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; |