QMessageAuthenticationCode: add move SMFs and swap()

QCryptographicHash is move-only these days, so
QMessageAuthenticationCode should not be left behind.

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

Fixes: QTBUG-111677
Change-Id: I420f24c04828e8ad7043a9e8c9e7e2d47dd183e0
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-03-02 15:00:31 +01:00
parent 41fea4f5fa
commit c5d4dde678
3 changed files with 84 additions and 0 deletions

View File

@ -1262,6 +1262,39 @@ QMessageAuthenticationCode::~QMessageAuthenticationCode()
delete d;
}
/*!
\fn QMessageAuthenticationCode::QMessageAuthenticationCode(QMessageAuthenticationCode &&other)
Move-constructs a new QMessageAuthenticationCode 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 object.
\since 6.6
*/
/*!
\fn QMessageAuthenticationCode &QMessageAuthenticationCode::operator=(QMessageAuthenticationCode &&other)
Move-assigns \a other to this QMessageAuthenticationCode 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 object.
\since 6.6
*/
/*!
\fn void QMessageAuthenticationCode::swap(QMessageAuthenticationCode &other)
Swaps message authentication code \a other with this message authentication
code. This operation is very fast and never fails.
\since 6.6
*/
/*!
Resets message data. Calling this method doesn't affect the key.
*/

View File

@ -18,8 +18,14 @@ class Q_CORE_EXPORT QMessageAuthenticationCode
public:
explicit QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
const QByteArray &key = QByteArray());
QMessageAuthenticationCode(QMessageAuthenticationCode &&other) noexcept
: d{std::exchange(other.d, nullptr)} {}
~QMessageAuthenticationCode();
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QMessageAuthenticationCode)
void swap(QMessageAuthenticationCode &other) noexcept
{ qt_ptr_swap(d, other.d); }
void reset();
void setKey(const QByteArray &key);

View File

@ -20,6 +20,8 @@ private slots:
void result_incremental();
void addData_overloads_data();
void addData_overloads();
void move();
void swap();
};
Q_DECLARE_METATYPE(QCryptographicHash::Algorithm)
@ -219,5 +221,48 @@ void tst_QMessageAuthenticationCode::addData_overloads()
}
}
void tst_QMessageAuthenticationCode::move()
{
const QByteArray key = "123";
QMessageAuthenticationCode src(QCryptographicHash::Sha1, key);
src.addData("a");
// move constructor
auto intermediary = std::move(src);
intermediary.addData("b");
// move assign operator
QMessageAuthenticationCode dst(QCryptographicHash::Sha256, key);
dst.addData("no effect on the end result");
dst = std::move(intermediary);
dst.addData("c");
QCOMPARE(dst.resultView(),
QMessageAuthenticationCode::hash("abc", key, QCryptographicHash::Sha1));
}
void tst_QMessageAuthenticationCode::swap()
{
const QByteArray key1 = "123";
const QByteArray key2 = "abcdefg";
QMessageAuthenticationCode mac1(QCryptographicHash::Sha1, key1);
QMessageAuthenticationCode mac2(QCryptographicHash::Sha256, key2);
mac1.addData("da");
mac2.addData("te");
mac1.swap(mac2);
mac2.addData("ta");
mac1.addData("st");
QCOMPARE(mac2.resultView(),
QMessageAuthenticationCode::hash("data", key1, QCryptographicHash::Sha1));
QCOMPARE(mac1.resultView(),
QMessageAuthenticationCode::hash("test", key2, QCryptographicHash::Sha256));
}
QTEST_MAIN(tst_QMessageAuthenticationCode)
#include "tst_qmessageauthenticationcode.moc"