QSqlRecord: add missing C++11 move SMFs

[ChangeLog][QtSql][QSqlRecord] Added move constructor, -assignment
operator, and swap().

Task-number: QTBUG-109938
Change-Id: I54f2666defbf56f5ba2faea6358722ebd7cac157
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Marc Mutz 2023-01-09 08:13:29 +01:00
parent 0cd153b268
commit 595360506d
3 changed files with 58 additions and 2 deletions

View File

@ -88,6 +88,37 @@ QSqlRecord::QSqlRecord(const QSqlRecord& other)
d->ref.ref(); d->ref.ref();
} }
/*!
\fn QSqlRecord::QSqlRecord(QSqlRecord &&other)
\since 6.6
Move-constructs a new QSqlRecord 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.
*/
/*!
\fn QSqlRecord &QSqlRecord::operator=(QSqlRecord &&other)
\since 6.6
Move-assigns \a other to this QSqlRecord 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.
*/
/*!
\fn void QSqlRecord::swap(QSqlRecord &other)
\since 6.6
Swaps SQL record \a other with this SQL record. This operation is very fast
and never fails.
*/
/*! /*!
Sets the record equal to \a other. Sets the record equal to \a other.
@ -97,7 +128,7 @@ QSqlRecord::QSqlRecord(const QSqlRecord& other)
QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other) QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
{ {
qAtomicAssign(d, other.d); QSqlRecord(other).swap(*this);
return *this; return *this;
} }
@ -107,7 +138,7 @@ QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
QSqlRecord::~QSqlRecord() QSqlRecord::~QSqlRecord()
{ {
if (!d->ref.deref()) if (d && !d->ref.deref())
delete d; delete d;
} }

View File

@ -19,9 +19,14 @@ class Q_SQL_EXPORT QSqlRecord
public: public:
QSqlRecord(); QSqlRecord();
QSqlRecord(const QSqlRecord& other); QSqlRecord(const QSqlRecord& other);
QSqlRecord(QSqlRecord &&other) noexcept
: d{std::exchange(other.d, nullptr)} {}
QSqlRecord& operator=(const QSqlRecord& other); QSqlRecord& operator=(const QSqlRecord& other);
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSqlRecord)
~QSqlRecord(); ~QSqlRecord();
void swap(QSqlRecord &other) noexcept { qt_ptr_swap(d, other.d); }
bool operator==(const QSqlRecord &other) const; bool operator==(const QSqlRecord &other) const;
inline bool operator!=(const QSqlRecord &other) const { return !operator==(other); } inline bool operator!=(const QSqlRecord &other) const { return !operator==(other); }

View File

@ -40,6 +40,7 @@ private slots:
void clearValues(); void clearValues();
void clear(); void clear();
void append(); void append();
void moveSemantics();
private: private:
std::unique_ptr<QSqlRecord> rec; std::unique_ptr<QSqlRecord> rec;
@ -449,5 +450,24 @@ void tst_QSqlRecord::value()
QCOMPARE(rec2.value("string").toString(), QLatin1String("Harry")); QCOMPARE(rec2.value("string").toString(), QLatin1String("Harry"));
} }
void tst_QSqlRecord::moveSemantics()
{
QSqlRecord rec, empty;
rec.append(QSqlField("string", QMetaType(QMetaType::QString)));
rec.setValue("string", "Harry");
auto moved = std::move(rec);
// `rec` is not partially-formed
// moving transfers state:
QCOMPARE(moved.value("string").toString(), QLatin1String("Harry"));
// moved-from objects can be assigned-to:
rec = empty;
QVERIFY(rec.value("string").isNull());
// moved-from object can be destroyed:
moved = std::move(rec);
}
QTEST_MAIN(tst_QSqlRecord) QTEST_MAIN(tst_QSqlRecord)
#include "tst_qsqlrecord.moc" #include "tst_qsqlrecord.moc"