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

View File

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