diff --git a/autotests/udsentrytest.h b/autotests/udsentrytest.h --- a/autotests/udsentrytest.h +++ b/autotests/udsentrytest.h @@ -28,6 +28,7 @@ private Q_SLOTS: void testSaveLoad(); + void testMove(); }; #endif diff --git a/autotests/udsentrytest.cpp b/autotests/udsentrytest.cpp --- a/autotests/udsentrytest.cpp +++ b/autotests/udsentrytest.cpp @@ -22,7 +22,9 @@ #include #include #include +#include +#include #include struct UDSTestField { @@ -221,4 +223,51 @@ } } +/** + * Test to verify that move semantics work. This is only useful when ran through callgrind. + */ +void UDSEntryTest::testMove() +{ + // Create a temporaty file. Just to make a UDSEntry further down. + QTemporaryFile file; + QVERIFY(file.open()); + const QByteArray filePath = file.fileName().toLocal8Bit(); + const QString fileName = QUrl(file.fileName()).fileName(); // QTemporaryFile::fileName returns the full path. + QVERIFY(!fileName.isEmpty()); + + // We have a file now. Get the stat data from it to make the UDSEntry. + QT_STATBUF statBuf; + QVERIFY(QT_LSTAT(filePath.constData(), &statBuf) == 0); + KIO::UDSEntry entry(statBuf, fileName); + + // Verify that the name in the UDSEntry is the same as we've got from the fileName var. + QCOMPARE(fileName, entry.stringValue(KIO::UDSEntry::UDS_NAME)); + + // That was the boilerplate code. Now for move semantics. + // First: move assignment. + { + // First a copy as we need to keep the entry for the next test. + KIO::UDSEntry entryCopy = entry; + + // Now move-assignement (two lines to prevent compiler optimization) + KIO::UDSEntry movedEntry; + movedEntry = std::move(entryCopy); + + // And veryfy that this works. + QCOMPARE(fileName, movedEntry.stringValue(KIO::UDSEntry::UDS_NAME)); + } + + // Move constructor + { + // First a copy again + KIO::UDSEntry entryCopy = entry; + + // Now move-assignement + KIO::UDSEntry movedEntry(std::move(entryCopy)); + + // And veryfy that this works. + QCOMPARE(fileName, movedEntry.stringValue(KIO::UDSEntry::UDS_NAME)); + } +} + QTEST_MAIN(UDSEntryTest) diff --git a/src/core/udsentry.h b/src/core/udsentry.h --- a/src/core/udsentry.h +++ b/src/core/udsentry.h @@ -71,17 +71,39 @@ public: UDSEntry(); - UDSEntry(const UDSEntry &other); /** * Create a UDSEntry by QT_STATBUF * @param buff QT_STATBUFF object * @param name filename * @since 5.0 */ UDSEntry(const QT_STATBUF &buff, const QString &name = QString()); + + /** + * Copy constructor + */ + UDSEntry(const UDSEntry&); + + /** + * Destructor + */ ~UDSEntry(); - UDSEntry &operator=(const UDSEntry &other); + + /** + * Move constructor + */ + UDSEntry(UDSEntry&&); + + /** + * Copy assignment + */ + UDSEntry& operator=(const UDSEntry&); + + /** + * Move assignment + */ + UDSEntry& operator=(UDSEntry&&); /** * @return value of a textual field diff --git a/src/core/udsentry.cpp b/src/core/udsentry.cpp --- a/src/core/udsentry.cpp +++ b/src/core/udsentry.cpp @@ -72,11 +72,6 @@ { } -UDSEntry::UDSEntry(const UDSEntry &other) - : d(other.d) -{ -} - // BUG: this API doesn't allow to handle symlinks correctly (we need buff from QT_LSTAT for most things, but buff from QT_STAT for st_mode and st_size) UDSEntry::UDSEntry(const QT_STATBUF &buff, const QString &name) : d(new UDSEntryPrivate()) @@ -96,15 +91,11 @@ #endif } -UDSEntry::~UDSEntry() -{ -} - -UDSEntry &UDSEntry::operator=(const UDSEntry &other) -{ - d = other.d; - return *this; -} +UDSEntry::UDSEntry(const UDSEntry&) = default; +UDSEntry::~UDSEntry() = default; +UDSEntry::UDSEntry(UDSEntry&&) = default; +UDSEntry& UDSEntry::operator=(const UDSEntry&) = default; +UDSEntry& UDSEntry::operator=(UDSEntry&&) = default; QString UDSEntry::stringValue(uint field) const {