QSqlRecord: use QSharedData for private class

Use QSharedData for the private class instead a home-brew version.

Change-Id: Id3625bb0eb8f81c9caa672e2453dab3d44b15ea9
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-03-27 19:26:38 +02:00
parent f291575d95
commit 204f1764ca
2 changed files with 12 additions and 25 deletions

View File

@ -11,23 +11,17 @@
QT_BEGIN_NAMESPACE
class QSqlRecordPrivate
class QSqlRecordPrivate : public QSharedData
{
public:
QSqlRecordPrivate() = default;
QSqlRecordPrivate(const QSqlRecordPrivate &other)
: fields(other.fields)
{
}
inline bool contains(qsizetype index) const
{
return index >= 0 && index < fields.size();
}
QList<QSqlField> fields;
QAtomicInt ref{1};
};
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSqlRecordPrivate)
/*!
\class QSqlRecord
@ -82,10 +76,7 @@ QSqlRecord::QSqlRecord()
*/
QSqlRecord::QSqlRecord(const QSqlRecord& other)
: d(other.d)
{
d->ref.ref();
}
= default;
/*!
\fn QSqlRecord::QSqlRecord(QSqlRecord &&other)
@ -126,20 +117,15 @@ QSqlRecord::QSqlRecord(const QSqlRecord& other)
*/
QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
{
QSqlRecord(other).swap(*this);
return *this;
}
= default;
/*!
Destroys the object and frees any allocated resources.
*/
QSqlRecord::~QSqlRecord()
{
if (d && !d->ref.deref())
delete d;
}
= default;
/*!
\fn bool QSqlRecord::operator!=(const QSqlRecord &other) const
@ -501,7 +487,7 @@ void QSqlRecord::setValue(const QString& name, const QVariant& val)
*/
void QSqlRecord::detach()
{
qAtomicDetach(d);
d.detach();
}
#ifndef QT_NO_DEBUG_STREAM

View File

@ -5,6 +5,7 @@
#define QSQLRECORD_H
#include <QtSql/qtsqlglobal.h>
#include <QtCore/qshareddata.h>
#include <QtCore/qstring.h>
QT_BEGIN_NAMESPACE
@ -13,19 +14,19 @@ QT_BEGIN_NAMESPACE
class QSqlField;
class QVariant;
class QSqlRecordPrivate;
QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(QSqlRecordPrivate, Q_SQL_EXPORT)
class Q_SQL_EXPORT QSqlRecord
{
public:
QSqlRecord();
QSqlRecord(const QSqlRecord& other);
QSqlRecord(QSqlRecord &&other) noexcept
: d{std::exchange(other.d, nullptr)} {}
QSqlRecord(QSqlRecord &&other) noexcept = default;
QSqlRecord& operator=(const QSqlRecord& other);
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QSqlRecord)
~QSqlRecord();
void swap(QSqlRecord &other) noexcept { qt_ptr_swap(d, other.d); }
void swap(QSqlRecord &other) noexcept { d.swap(other.d); }
bool operator==(const QSqlRecord &other) const;
inline bool operator!=(const QSqlRecord &other) const { return !operator==(other); }
@ -65,7 +66,7 @@ public:
private:
void detach();
QSqlRecordPrivate* d;
QExplicitlySharedDataPointer<QSqlRecordPrivate> d;
};
#ifndef QT_NO_DEBUG_STREAM