QSqlField: add move ctor & move operator

Add the move ctor and move operator for QSqlField

Task-number: QTBUG-109938
Change-Id: Ib66eff76c3a920de9cfb3288f4219555005e7ae5
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Christian Ehrlicher 2023-04-12 21:40:02 +02:00 committed by Marc Mutz
parent 204f1764ca
commit 1f27dc6871
3 changed files with 37 additions and 37 deletions

View File

@ -2,36 +2,20 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qsqlfield.h"
#include "qatomic.h"
#include "qdebug.h"
QT_BEGIN_NAMESPACE
class QSqlFieldPrivate
class QSqlFieldPrivate : public QSharedData
{
public:
QSqlFieldPrivate(const QString &name,
QMetaType type, const QString &tableName) :
ref(1), nm(name), table(tableName), def(QVariant()), type(type),
nm(name), table(tableName), def(QVariant()), type(type),
req(QSqlField::Unknown), len(-1), prec(-1), tp(-1),
ro(false), gen(true), autoval(false)
{}
QSqlFieldPrivate(const QSqlFieldPrivate &other)
: ref(1),
nm(other.nm),
table(other.table),
def(other.def),
type(other.type),
req(other.req),
len(other.len),
prec(other.prec),
tp(other.tp),
ro(other.ro),
gen(other.gen),
autoval(other.autoval)
{}
bool operator==(const QSqlFieldPrivate& other) const
{
return (nm == other.nm
@ -46,7 +30,6 @@ public:
&& autoval == other.autoval);
}
QAtomicInt ref;
QString nm;
QString table;
QVariant def;
@ -59,6 +42,7 @@ public:
bool gen: 1;
bool autoval: 1;
};
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSqlFieldPrivate)
/*!
@ -151,23 +135,14 @@ QSqlField::QSqlField(const QString &fieldName, QMetaType type, const QString &ta
*/
QSqlField::QSqlField(const QSqlField &other)
: val(other.val),
d(other.d)
{
d->ref.ref();
}
= default;
/*!
Sets the field equal to \a other.
*/
QSqlField& QSqlField::operator=(const QSqlField& other)
{
qAtomicAssign(d, other.d);
val = other.val;
return *this;
}
= default;
/*! \fn bool QSqlField::operator!=(const QSqlField &other) const
Returns \c true if the field is unequal to \a other; otherwise returns
@ -189,10 +164,7 @@ bool QSqlField::operator==(const QSqlField& other) const
*/
QSqlField::~QSqlField()
{
if (!d->ref.deref())
delete d;
}
= default;
/*!
Sets the required status of this field to \a required.
@ -426,7 +398,7 @@ bool QSqlField::isNull() const
*/
void QSqlField::detach()
{
qAtomicDetach(d);
d.detach();
}
/*!

View File

@ -5,6 +5,7 @@
#define QSQLFIELD_H
#include <QtSql/qtsqlglobal.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qvariant.h>
#include <QtCore/qstring.h>
@ -12,6 +13,7 @@ QT_BEGIN_NAMESPACE
class QSqlFieldPrivate;
QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QSqlFieldPrivate, Q_SQL_EXPORT)
class Q_SQL_EXPORT QSqlField
{
@ -22,9 +24,14 @@ public:
QSqlField(const QSqlField& other);
QSqlField& operator=(const QSqlField& other);
QSqlField(QSqlField &&other) noexcept = default;
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSqlField)
~QSqlField();
void swap(QSqlField &other) noexcept { val.swap(other.val); d.swap(other.d); }
bool operator==(const QSqlField& other) const;
inline bool operator!=(const QSqlField &other) const { return !operator==(other); }
~QSqlField();
void setValue(const QVariant& value);
inline QVariant value() const
@ -76,8 +83,9 @@ public:
private:
void detach();
// ### Qt7: move to private class
QVariant val;
QSqlFieldPrivate* d;
QExplicitlySharedDataPointer<QSqlFieldPrivate> d;
};
#ifndef QT_NO_DEBUG_STREAM

View File

@ -39,6 +39,7 @@ private slots:
void clear();
void setTableName_data();
void setTableName();
void moveSemantics();
};
// Testing get/set functions
@ -344,5 +345,24 @@ void tst_QSqlField::setTableName()
QCOMPARE(field.tableName(), tableName);
}
void tst_QSqlField::moveSemantics()
{
QSqlField field("test", QMetaType(QMetaType::QString), "testTable");
QSqlField empty;
field.setValue("string");
auto moved = std::move(field);
// `field` is now partially-formed
// moving transfers state:
QCOMPARE(moved.value().toString(), QLatin1String("string"));
// moved-from objects can be assigned-to:
field = empty;
QVERIFY(field.value().isNull());
// moved-from object can be destroyed:
moved = std::move(field);
}
QTEST_MAIN(tst_QSqlField)
#include "tst_qsqlfield.moc"