Make QCryptographicHash move constructible

This adds a move constructor, a move assignment operator and a swap
function to QCryptographicHash. This can (to name one example) be useful
when you want to store multiple hashes in a vector.

[ChangeLog][QtCore][QCryptographicHash] Added move constructor, move
assignment operator and swap() function.

Change-Id: Id54594fa69104ec25ad78581f962a021e85531c2
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Linus Jahn 2022-09-10 19:40:51 +02:00 committed by Marc Mutz
parent 553185e8c3
commit 358248b495
3 changed files with 74 additions and 0 deletions

View File

@ -315,6 +315,18 @@ QCryptographicHash::QCryptographicHash(Algorithm method)
{
}
/*!
\fn QCryptographicHash::QCryptographicHash(QCryptographicHash &&other)
Move-constructs a new QCryptographicHash from \a other.
\note The moved-from object \a other is placed in a
partially-formed state, in which the only valid operations are
destruction and assignment of a new value.
\since 6.5
*/
/*!
Destroys the object.
*/
@ -323,6 +335,27 @@ QCryptographicHash::~QCryptographicHash()
delete d;
}
/*!
\fn QCryptographicHash &QCryptographicHash::operator=(QCryptographicHash &&other)
Move-assigns \a other to this QCryptographicHash instance.
\note The moved-from object \a other is placed in a
partially-formed state, in which the only valid operations are
destruction and assignment of a new value.
\since 6.5
*/
/*!
\fn void QCryptographicHash::swap(QCryptographicHash &other)
Swaps cryptographic hash \a other with this cryptographic hash. This
operation is very fast and never fails.
\since 6.5
*/
/*!
Resets the object.
*/

View File

@ -64,8 +64,12 @@ public:
Q_ENUM(Algorithm)
explicit QCryptographicHash(Algorithm method);
QCryptographicHash(QCryptographicHash &&other) noexcept : d(std::exchange(other.d, nullptr)) {}
~QCryptographicHash();
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCryptographicHash)
void swap(QCryptographicHash &other) noexcept { qt_ptr_swap(d, other.d); }
void reset() noexcept;
#if QT_DEPRECATED_SINCE(6, 4)

View File

@ -31,6 +31,8 @@ private slots:
void files();
void hashLength_data();
void hashLength();
void move();
void swap();
// keep last
void moreThan4GiBOfData_data();
void moreThan4GiBOfData();
@ -401,6 +403,41 @@ void tst_QCryptographicHash::hashLength()
QCOMPARE(QCryptographicHash::hashLength(algorithm), output.length());
}
void tst_QCryptographicHash::move()
{
QCryptographicHash hash1(QCryptographicHash::Sha1);
hash1.addData("a");
// move constructor
auto hash2(std::move(hash1));
hash2.addData("b");
// move assign operator
QCryptographicHash hash3(QCryptographicHash::Sha256);
hash3.addData("no effect on the end result");
hash3 = std::move(hash2);
hash3.addData("c");
QCOMPARE(hash3.resultView(), QByteArray::fromHex("A9993E364706816ABA3E25717850C26C9CD0D89D"));
}
void tst_QCryptographicHash::swap()
{
QCryptographicHash hash1(QCryptographicHash::Sha1);
QCryptographicHash hash2(QCryptographicHash::Sha256);
hash1.addData("da");
hash2.addData("te");
hash1.swap(hash2);
hash2.addData("ta");
hash1.addData("st");
QCOMPARE(hash2.result(), QCryptographicHash::hash("data", QCryptographicHash::Sha1));
QCOMPARE(hash1.result(), QCryptographicHash::hash("test", QCryptographicHash::Sha256));
}
void tst_QCryptographicHash::moreThan4GiBOfData_data()
{
#if QT_POINTER_SIZE > 4