diff --git a/autotests/kfileitemtest.h b/autotests/kfileitemtest.h --- a/autotests/kfileitemtest.h +++ b/autotests/kfileitemtest.h @@ -30,6 +30,7 @@ void testNull(); void testDoesNotExist(); void testDetach(); + void testMove(); void testBasic(); void testRootDirectory(); void testHiddenFile(); diff --git a/autotests/kfileitemtest.cpp b/autotests/kfileitemtest.cpp --- a/autotests/kfileitemtest.cpp +++ b/autotests/kfileitemtest.cpp @@ -115,6 +115,37 @@ QVERIFY(!(fileItem != fileItem2)); } +void KFileItemTest::testMove() +{ + // Test move constructor + { + KFileItem fileItem(QUrl::fromLocalFile(QStringLiteral("/one")), QString(), KFileItem::Unknown); + QCOMPARE(fileItem.name(), QStringLiteral("one")); + KFileItem fileItem2(std::move(fileItem)); + QCOMPARE(fileItem2.name(), QStringLiteral("one")); + } + + // Test move assignment + { + KFileItem fileItem(QUrl::fromLocalFile(QStringLiteral("/one")), QString(), KFileItem::Unknown); + QCOMPARE(fileItem.name(), QStringLiteral("one")); + KFileItem fileItem2(QUrl::fromLocalFile(QStringLiteral("/two")), QString(), KFileItem::Unknown); + fileItem2 = std::move(fileItem); + QCOMPARE(fileItem2.name(), QStringLiteral("one")); + } + + // Now to test some value changes to make sure moving works as intended. + KFileItem fileItem(QUrl::fromLocalFile(QStringLiteral("/one")), QString(), KFileItem::Unknown); + QCOMPARE(fileItem.name(), QStringLiteral("one")); + fileItem.setUrl(QUrl::fromLocalFile(QStringLiteral("/two"))); + QCOMPARE(fileItem.name(), QStringLiteral("two")); + + // Move fileitem to fileItem2, it should now contain everything fileItem had. + // Just testing a property to make sure it does. + KFileItem fileItem2(std::move(fileItem)); + QCOMPARE(fileItem2.name(), QStringLiteral("two")); +} + void KFileItemTest::testBasic() { QTemporaryFile file; diff --git a/src/core/kfileitem.h b/src/core/kfileitem.h --- a/src/core/kfileitem.h +++ b/src/core/kfileitem.h @@ -118,16 +118,27 @@ /** * Copy constructor */ - KFileItem(const KFileItem &other); + KFileItem(const KFileItem&); + /** - * Assignment operator + * Destructor */ - KFileItem &operator=(const KFileItem &other); + ~KFileItem(); /** - * Destructs the KFileItem. + * Move constructor */ - ~KFileItem(); + KFileItem(KFileItem&&); + + /** + * Copy assignment + */ + KFileItem& operator=(const KFileItem&); + + /** + * Move assignment + */ + KFileItem& operator=(KFileItem&&); /** * Throw away and re-read (for local files) all information about the file. diff --git a/src/core/kfileitem.cpp b/src/core/kfileitem.cpp --- a/src/core/kfileitem.cpp +++ b/src/core/kfileitem.cpp @@ -477,14 +477,18 @@ } } -KFileItem::KFileItem(const KFileItem &other) - : d(other.d) -{ -} - -KFileItem::~KFileItem() -{ -} +// Default implementations for: +// - Copy constructor +// - Move constructor +// - Copy assignment +// - Move assignment +// - Destructor +// The compiler will now generate the content of those. +KFileItem::KFileItem(const KFileItem&) = default; +KFileItem::~KFileItem() = default; +KFileItem::KFileItem(KFileItem&&) = default; +KFileItem& KFileItem::operator=(const KFileItem&) = default; +KFileItem& KFileItem::operator=(KFileItem&&) = default; void KFileItem::refresh() { @@ -1488,12 +1492,6 @@ return d->m_entry; } -KFileItem &KFileItem::operator=(const KFileItem &other) -{ - d = other.d; - return *this; -} - bool KFileItem::isNull() const { return d == nullptr;