QJson*: value semantics cleanup

Re-add the move operations, add a swap(), mark as shared
(we're already using them in containers).

QJsonValue is slightly tricky, because it has an anonymous union,
which can't be easily moved in C++. Use the implementation of
the copy constructor as inspiration for the move.

[ChangeLog][QtCore][JSON] QJsonArray, QJsonDocument, QJsonObject
and QJsonValue now have move operations and a swap() member function.

Change-Id: Idfb94a93370ace96100efbd6559ef05b4c5adc39
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Giuseppe D'Angelo 2017-02-22 11:49:24 +00:00
parent 6462f299ed
commit 8a375341cf
8 changed files with 173 additions and 0 deletions

View File

@ -212,6 +212,27 @@ QJsonArray &QJsonArray::operator =(const QJsonArray &other)
return *this;
}
/*!
\fn QJsonArray::QJsonArray(QJsonArray &&other)
\since 5.10
Move-constructs a QJsonArray from \a other.
*/
/*!
\fn QJsonArray &QJsonArray::operator =(QJsonArray &&other)
\since 5.10
Move-assigns \a other to this array.
*/
/*!
\fn void QJsonArray::swap(QJsonArray &other)
\since 5.10
Swaps the array \a other with this. This operation is very fast and never fails.
*/
/*! \fn QJsonArray &QJsonArray::operator+=(const QJsonValue &value)
Appends \a value to the array, and returns a reference to the array itself.

View File

@ -72,6 +72,20 @@ public:
QJsonArray(const QJsonArray &other);
QJsonArray &operator =(const QJsonArray &other);
QJsonArray(QJsonArray &&other) Q_DECL_NOTHROW
: d(other.d),
a(other.a)
{
other.d = nullptr;
other.a = nullptr;
}
QJsonArray &operator =(QJsonArray &&other) Q_DECL_NOTHROW
{
swap(other);
return *this;
}
static QJsonArray fromStringList(const QStringList &list);
static QJsonArray fromVariantList(const QVariantList &list);
QVariantList toVariantList() const;
@ -101,6 +115,12 @@ public:
bool operator==(const QJsonArray &other) const;
bool operator!=(const QJsonArray &other) const;
void swap(QJsonArray &other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
qSwap(a, other.a);
}
class const_iterator;
class iterator {
@ -243,6 +263,8 @@ private:
QJsonPrivate::Array *a;
};
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonArray)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
#endif

View File

@ -153,6 +153,28 @@ QJsonDocument &QJsonDocument::operator =(const QJsonDocument &other)
return *this;
}
/*!
\fn QJsonDocument::QJsonDocument(QJsonDocument &&other)
\since 5.10
Move-constructs a QJsonDocument from \a other.
*/
/*!
\fn QJsonDocument &QJsonDocument::operator =(QJsonDocument &&other)
\since 5.10
Move-assigns \a other to this document.
*/
/*!
\fn void QJsonDocument::swap(QJsonDocument &other)
\since 5.10
Swaps the document \a other with this. This operation is very fast and never fails.
*/
/*! \enum QJsonDocument::DataValidation
This value is used to tell QJsonDocument whether to validate the binary data

View File

@ -93,6 +93,23 @@ public:
QJsonDocument(const QJsonDocument &other);
QJsonDocument &operator =(const QJsonDocument &other);
QJsonDocument(QJsonDocument &&other) Q_DECL_NOTHROW
: d(other.d)
{
other.d = nullptr;
}
QJsonDocument &operator =(QJsonDocument &&other) Q_DECL_NOTHROW
{
swap(other);
return *this;
}
void swap(QJsonDocument &other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
}
enum DataValidation {
Validate,
BypassValidation
@ -147,6 +164,8 @@ private:
QJsonPrivate::Data *d;
};
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonDocument)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &);
#endif

View File

@ -193,6 +193,28 @@ QJsonObject &QJsonObject::operator =(const QJsonObject &other)
return *this;
}
/*!
\fn QJsonObject::QJsonObject(QJsonObject &&other)
\since 5.10
Move-constructs a QJsonObject from \a other.
*/
/*!
\fn QJsonObject &QJsonObject::operator =(QJsonObject &&other)
\since 5.10
Move-assigns \a other to this object.
*/
/*!
\fn void QJsonObject::swap(QJsonObject &other)
\since 5.10
Swaps the object \a other with this. This operation is very fast and never fails.
*/
/*!
Converts the variant map \a map to a QJsonObject.

View File

@ -74,6 +74,25 @@ public:
QJsonObject(const QJsonObject &other);
QJsonObject &operator =(const QJsonObject &other);
QJsonObject(QJsonObject &&other) Q_DECL_NOTHROW
: d(other.d), o(other.o)
{
other.d = nullptr;
other.o = nullptr;
}
QJsonObject &operator =(QJsonObject &&other) Q_DECL_NOTHROW
{
swap(other);
return *this;
}
void swap(QJsonObject &other) Q_DECL_NOTHROW
{
qSwap(d, other.d);
qSwap(o, other.o);
}
static QJsonObject fromVariantMap(const QVariantMap &map);
QVariantMap toVariantMap() const;
static QJsonObject fromVariantHash(const QVariantHash &map);
@ -241,6 +260,8 @@ private:
QJsonPrivate::Object *o;
};
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonObject)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
#endif

View File

@ -283,6 +283,27 @@ QJsonValue &QJsonValue::operator =(const QJsonValue &other)
return *this;
}
/*!
\fn QJsonValue::QJsonValue(QJsonValue &&other)
\since 5.10
Move-constructs a QJsonValue from \a other.
*/
/*!
\fn QJsonValue &QJsonValue::operator =(QJsonValue &&other)
\since 5.10
Move-assigns \a other to this value.
*/
/*!
\fn void QJsonValue::swap(QJsonValue &other)
\since 5.10
Swaps the value \a other with this. This operation is very fast and never fails.
*/
/*!
\fn bool QJsonValue::isNull() const

View File

@ -92,6 +92,29 @@ public:
QJsonValue(const QJsonValue &other);
QJsonValue &operator =(const QJsonValue &other);
QJsonValue(QJsonValue &&other) Q_DECL_NOTHROW
: ui(other.ui),
d(other.d),
t(other.t)
{
other.ui = 0;
other.d = nullptr;
other.t = Null;
}
QJsonValue &operator =(QJsonValue &&other) Q_DECL_NOTHROW
{
swap(other);
return *this;
}
void swap(QJsonValue &other) Q_DECL_NOTHROW
{
qSwap(ui, other.ui);
qSwap(d, other.d);
qSwap(t, other.t);
}
static QJsonValue fromVariant(const QVariant &variant);
QVariant toVariant() const;
@ -217,6 +240,8 @@ public:
};
#endif
Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonValue)
#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
#endif