diff options
Diffstat (limited to 'src/corelib/serialization')
28 files changed, 1061 insertions, 223 deletions
diff --git a/src/corelib/serialization/qcborarray.cpp b/src/corelib/serialization/qcborarray.cpp index c56536cdb1..28ae40f3df 100644 --- a/src/corelib/serialization/qcborarray.cpp +++ b/src/corelib/serialization/qcborarray.cpp @@ -39,6 +39,7 @@ #include "qcborarray.h" #include "qcborvalue_p.h" +#include "qdatastream.h" QT_BEGIN_NAMESPACE @@ -1218,4 +1219,23 @@ QDebug operator<<(QDebug dbg, const QCborArray &a) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QCborArray &value) +{ + stream << value.toCborValue().toCbor(); + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QCborArray &value) +{ + QByteArray buffer; + stream >> buffer; + QCborParserError parseError{}; + value = QCborValue::fromCbor(buffer, &parseError).toArray(); + if (parseError.error) + stream.setStatus(QDataStream::ReadCorruptData); + return stream; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/serialization/qcborarray.h b/src/corelib/serialization/qcborarray.h index ed0f4912ba..e06544f245 100644 --- a/src/corelib/serialization/qcborarray.h +++ b/src/corelib/serialization/qcborarray.h @@ -47,6 +47,7 @@ QT_BEGIN_NAMESPACE class QJsonArray; +class QDataStream; class QCborContainerPrivate; class Q_CORE_EXPORT QCborArray @@ -271,6 +272,7 @@ private: void detach(qsizetype reserve = 0); friend QCborValue; + friend QCborValueRef; explicit QCborArray(QCborContainerPrivate &dd) noexcept; QExplicitlySharedDataPointer<QCborContainerPrivate> d; }; @@ -298,6 +300,11 @@ Q_CORE_EXPORT uint qHash(const QCborArray &array, uint seed = 0); Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborArray &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborArray &); +#endif + QT_END_NAMESPACE #endif // QCBORARRAY_H diff --git a/src/corelib/serialization/qcborcommon.h b/src/corelib/serialization/qcborcommon.h index f8278f1649..3dfe50cd09 100644 --- a/src/corelib/serialization/qcborcommon.h +++ b/src/corelib/serialization/qcborcommon.h @@ -133,6 +133,11 @@ Q_CORE_EXPORT QDebug operator<<(QDebug, QCborKnownTags tg); Q_CORE_EXPORT QDebug operator<<(QDebug, QCborTag tg); #endif +#if !defined(QT_NO_DEBUG_STREAM) +QDataStream &operator<<(QDataStream &ds, QCborSimpleType st); +QDataStream &operator>>(QDataStream &ds, QCborSimpleType &st); +#endif + inline uint qHash(QCborSimpleType tag, uint seed = 0) { return qHash(quint8(tag), seed); diff --git a/src/corelib/serialization/qcbormap.cpp b/src/corelib/serialization/qcbormap.cpp index 33f9249993..4b28ca4a2e 100644 --- a/src/corelib/serialization/qcbormap.cpp +++ b/src/corelib/serialization/qcbormap.cpp @@ -1763,4 +1763,23 @@ QDebug operator<<(QDebug dbg, const QCborMap &m) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QCborMap &value) +{ + stream << value.toCborValue().toCbor(); + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QCborMap &value) +{ + QByteArray buffer; + stream >> buffer; + QCborParserError parseError{}; + value = QCborValue::fromCbor(buffer, &parseError).toMap(); + if (parseError.error) + stream.setStatus(QDataStream::ReadCorruptData); + return stream; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/serialization/qcbormap.h b/src/corelib/serialization/qcbormap.h index 3eb2107691..4aea901eef 100644 --- a/src/corelib/serialization/qcbormap.h +++ b/src/corelib/serialization/qcbormap.h @@ -52,6 +52,7 @@ typedef QMap<QString, QVariant> QVariantMap; template <class Key, class T> class QHash; typedef QHash<QString, QVariant> QVariantHash; class QJsonObject; +class QDataStream; class QCborContainerPrivate; class Q_CORE_EXPORT QCborMap @@ -117,6 +118,8 @@ public: QCborValueRef item; // points to the value friend class Iterator; friend class QCborMap; + friend class QCborValue; + friend class QCborValueRef; ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} public: typedef std::random_access_iterator_tag iterator_category; @@ -170,7 +173,7 @@ public: : QCborMap() { detach(args.size()); - for (auto pair : args) + for (const auto &pair : args) insert(pair.first, pair.second); } ~QCborMap(); @@ -322,9 +325,10 @@ public: QJsonObject toJsonObject() const; private: + friend class QCborValue; + friend class QCborValueRef; void detach(qsizetype reserve = 0); - friend QCborValue; explicit QCborMap(QCborContainerPrivate &dd) noexcept; QExplicitlySharedDataPointer<QCborContainerPrivate> d; }; @@ -352,6 +356,12 @@ Q_CORE_EXPORT uint qHash(const QCborMap &map, uint seed = 0); Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborMap &m); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborMap &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborMap &); +#endif + + QT_END_NAMESPACE #endif // QCBORMAP_H diff --git a/src/corelib/serialization/qcborstream.cpp b/src/corelib/serialization/qcborstream.cpp index 87ae316041..078c14a32d 100644 --- a/src/corelib/serialization/qcborstream.cpp +++ b/src/corelib/serialization/qcborstream.cpp @@ -44,6 +44,7 @@ #include <qbuffer.h> #include <qdebug.h> #include <qstack.h> +#include <qdatastream.h> QT_BEGIN_NAMESPACE @@ -180,6 +181,21 @@ Q_CORE_EXPORT const char *qt_cbor_simpletype_id(QCborSimpleType st) return nullptr; } +#if !defined(QT_NO_DATASTREAM) +QDataStream &operator<<(QDataStream &ds, QCborSimpleType st) +{ + return ds << quint8(st); +} + +QDataStream &operator>>(QDataStream &ds, QCborSimpleType &st) +{ + quint8 v; + ds >> v; + st = QCborSimpleType(v); + return ds; +} +#endif + #if !defined(QT_NO_DEBUG_STREAM) QDebug operator<<(QDebug dbg, QCborSimpleType st) { diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp index 80ef515fd2..ba616c0a7d 100644 --- a/src/corelib/serialization/qcborvalue.cpp +++ b/src/corelib/serialization/qcborvalue.cpp @@ -39,7 +39,7 @@ #include "qcborvalue.h" #include "qcborvalue_p.h" - +#include "qdatastream.h" #include "qcborarray.h" #include "qcbormap.h" #include "qcborstream.h" @@ -108,7 +108,7 @@ QT_BEGIN_NAMESPACE QCborValue can contain a value of "null", which is not of any specific type. It resembles the C++ \c {std::nullptr_t} type, whose only possible value is - \c nullptr. QCborValue has a constructor taking such a type and creates a + \nullptr. QCborValue has a constructor taking such a type and creates a null QCborValue. Null values are used to indicate that an optional value is not present. In @@ -128,10 +128,11 @@ QT_BEGIN_NAMESPACE Such values are completely valid and may appear in CBOR streams, unlike JSON content and QJsonValue's undefined bit. But like QJsonValue's - Undefined, it is returned by QCborArray::value() when out of range or - QCborMap::operator[] when the key is not found in the container. It is not - possible to tell such a case apart from the value of Undefined, so if that - is required, check the QCborArray size and use the QCborMap iterator API. + Undefined, it is returned by a CBOR container's value() or read-only + operator[] for invalid look-ups (index out of range for QCborArray, or key + not found for QCborMap). It is not possible to tell such a case apart from + the value of Undefined, so if that is required, check the QCborArray size + and use the QCborMap iterator API. \section1 Simple types @@ -416,7 +417,7 @@ QT_BEGIN_NAMESPACE using toSimpleType() as well as isSimpleType(st). CBOR simple types are types that do not have any associated value, like - C++'s \c{std::nullptr_t} type, whose only possible value is \c nullptr. + C++'s \c{std::nullptr_t} type, whose only possible value is \nullptr. If \a st is \c{QCborSimpleType::Null}, the resulting QCborValue will be of the \l{Type}{Null} type and similarly for \c{QCborSimpleType::Undefined}. @@ -457,7 +458,7 @@ QT_BEGIN_NAMESPACE \fn QCborValue::QCborValue(QCborValue &&other) \overload - Moves the contents of the \a other CBorValue object into this one and frees + Moves the contents of the \a other QCborValue object into this one and frees the resources of this one. */ @@ -465,7 +466,7 @@ QT_BEGIN_NAMESPACE \fn QCborValue &&QCborValue::operator=(QCborValue &&other) \overload - Moves the contents of the \a other CBorValue object into this one and frees + Moves the contents of the \a other QCborValue object into this one and frees the resources of this one. Returns a reference to this object. */ @@ -765,8 +766,8 @@ static void writeDoubleToCbor(QCborStreamWriter &writer, double d, QCborValue::E if (qt_is_nan(d)) { if (opt & QCborValue::UseFloat16) { if ((opt & QCborValue::UseFloat16) == QCborValue::UseFloat16) - return writer.append(qfloat16(qt_qnan())); - return writer.append(float(qt_qnan())); + return writer.append(std::numeric_limits<qfloat16>::quiet_NaN()); + return writer.append(std::numeric_limits<float>::quiet_NaN()); } return writer.append(qt_qnan()); } @@ -848,7 +849,7 @@ QCborContainerPrivate *QCborContainerPrivate::clone(QCborContainerPrivate *d, qs QCborContainerPrivate *QCborContainerPrivate::detach(QCborContainerPrivate *d, qsizetype reserved) { - if (!d || d->ref.load() != 1) + if (!d || d->ref.loadRelaxed() != 1) return clone(d, reserved); return d; } @@ -883,12 +884,12 @@ void QCborContainerPrivate::replaceAt_complex(Element &e, const QCborValue &valu // detect self-assignment if (Q_UNLIKELY(this == value.container)) { - Q_ASSERT(ref.load() >= 2); + Q_ASSERT(ref.loadRelaxed() >= 2); if (disp == MoveContainer) ref.deref(); // not deref() because it can't drop to 0 QCborContainerPrivate *d = QCborContainerPrivate::clone(this); d->elements.detach(); - d->ref.store(1); + d->ref.storeRelaxed(1); e.container = d; } else { e.container = value.container; @@ -1367,7 +1368,7 @@ static Element decodeBasicValueFromCbor(QCborStreamReader &reader) static inline QCborContainerPrivate *createContainerFromCbor(QCborStreamReader &reader) { auto d = new QCborContainerPrivate; - d->ref.store(1); + d->ref.storeRelaxed(1); d->decodeFromCbor(reader); return d; } @@ -1642,7 +1643,7 @@ QCborValue::QCborValue(const QByteArray &ba) : n(0), container(new QCborContainerPrivate), t(ByteArray) { container->appendByteData(ba.constData(), ba.size(), t); - container->ref.store(1); + container->ref.storeRelaxed(1); } /*! @@ -1655,7 +1656,7 @@ QCborValue::QCborValue(const QString &s) : n(0), container(new QCborContainerPrivate), t(String) { container->append(s); - container->ref.store(1); + container->ref.storeRelaxed(1); } /*! @@ -1670,7 +1671,7 @@ QCborValue::QCborValue(QLatin1String s) : n(0), container(new QCborContainerPrivate), t(String) { container->append(s); - container->ref.store(1); + container->ref.storeRelaxed(1); } /*! @@ -1718,7 +1719,7 @@ QCborValue::QCborValue(const QCborMap &m) QCborValue::QCborValue(QCborTag t, const QCborValue &tv) : n(-1), container(new QCborContainerPrivate), t(Tag) { - container->ref.store(1); + container->ref.storeRelaxed(1); container->append(t); container->append(tv); } @@ -1986,12 +1987,24 @@ QUuid QCborValue::toUuid(const QUuid &defaultValue) const return QUuid::fromRfc4122(byteData->asByteArrayView()); } -QCborArray QCborValue::toArray() const -{ - return toArray(QCborArray()); -} +/*! + \fn QCborArray QCborValue::toArray() const + \fn QCborArray QCborValue::toArray(const QCborArray &defaultValue) const + + Returns the array value stored in this QCborValue, if it is of the array + type. Otherwise, it returns \a defaultValue. + + Note that this function performs no conversion from other types to + QCborArray. + + \sa isArray(), isByteArray(), isMap(), isContainer(), toMap() + */ /*! + \fn QCborArray QCborValueRef::toArray() const + \fn QCborArray QCborValueRef::toArray(const QCborArray &defaultValue) const + \internal + Returns the array value stored in this QCborValue, if it is of the array type. Otherwise, it returns \a defaultValue. @@ -2000,6 +2013,11 @@ QCborArray QCborValue::toArray() const \sa isArray(), isByteArray(), isMap(), isContainer(), toMap() */ +QCborArray QCborValue::toArray() const +{ + return toArray(QCborArray()); +} + QCborArray QCborValue::toArray(const QCborArray &defaultValue) const { if (!isArray()) @@ -2011,12 +2029,24 @@ QCborArray QCborValue::toArray(const QCborArray &defaultValue) const return dd ? QCborArray(*dd) : defaultValue; } -QCborMap QCborValue::toMap() const -{ - return toMap(QCborMap()); -} +/*! + \fn QCborMap QCborValue::toMap() const + \fn QCborMap QCborValue::toMap(const QCborMap &defaultValue) const + + Returns the map value stored in this QCborValue, if it is of the map type. + Otherwise, it returns \a defaultValue. + + Note that this function performs no conversion from other types to + QCborMap. + + \sa isMap(), isArray(), isContainer(), toArray() + */ /*! + \fn QCborMap QCborValueRef::toMap() const + \fn QCborMap QCborValueRef::toMap(const QCborMap &defaultValue) const + \internal + Returns the map value stored in this QCborValue, if it is of the map type. Otherwise, it returns \a defaultValue. @@ -2025,6 +2055,11 @@ QCborMap QCborValue::toMap() const \sa isMap(), isArray(), isContainer(), toArray() */ +QCborMap QCborValue::toMap() const +{ + return toMap(QCborMap()); +} + QCborMap QCborValue::toMap(const QCborMap &defaultValue) const { if (!isMap()) @@ -2077,15 +2112,16 @@ const QCborValue QCborValue::operator[](QLatin1String key) const } /*! + \overload + If this QCborValue is a QCborMap, searches elements for the value whose key - matches \a key. If this is an array, returns the element whose index is \a - key. If there's no matching value in the array or map, or if this + matches \a key. If this is a QCborArray, returns the element whose index is + \a key. If there's no matching value in the array or map, or if this QCborValue object is not an array or map, returns the undefined value. \sa operator[], QCborMap::operator[], QCborMap::value(), QCborMap::find(), QCborArray::operator[], QCborArray::at() */ - const QCborValue QCborValue::operator[](qint64 key) const { if (isMap()) @@ -2096,6 +2132,191 @@ const QCborValue QCborValue::operator[](qint64 key) const } /*! + \internal + */ +static Q_DECL_COLD_FUNCTION QCborMap arrayAsMap(const QCborArray &array) +{ + if (array.size()) + qWarning("Using CBOR array as map forced conversion"); + QCborMap map; + for (qsizetype i = array.size(); i-- > 0; ) { + QCborValue entry = array.at(i); + // Ignore padding entries that may have been added to grow the array + // when inserting past its end: + if (!entry.isInvalid()) + map[i] = entry; + } + return map; +} + +/*! + \internal + */ +static QCborContainerPrivate *maybeDetach(QCborContainerPrivate *container, qsizetype size) +{ + auto replace = QCborContainerPrivate::detach(container, size); + Q_ASSERT(replace); + if (replace != container) { + if (container) + container->deref(); + replace->ref.ref(); + } + return replace; +} + +/*! + \internal + */ +static QCborContainerPrivate *maybeGrow(QCborContainerPrivate *container, qsizetype index) +{ + auto replace = QCborContainerPrivate::grow(container, index); + Q_ASSERT(replace); + if (replace != container) { + if (container) + container->deref(); + replace->ref.ref(); + } + if (replace->elements.size() == index) + replace->append(Undefined()); + else + Q_ASSERT(replace->elements.size() > index); + return replace; +} + +/*! + Returns a QCborValueRef that can be used to read or modify the entry in + this, as a map, with the given \a key. When this QCborValue is a QCborMap, + this function is equivalent to the matching operator[] on that map. + + Before returning the reference: if this QCborValue was an array, it is first + converted to a map (so that \c{map[i]} is \c{array[i]} for each index, \c i, + with valid \c{array[i]}); otherwise, if it was not a map it will be + over-written with an empty map. + + \sa operator[](qint64), QCborMap::operator[], QCborMap::value(), + QCborMap::find() + */ +QCborValueRef QCborValue::operator[](const QString &key) +{ + if (!isMap()) + *this = QCborValue(isArray() ? arrayAsMap(toArray()) : QCborMap()); + + const qsizetype size = container ? container->elements.size() : 0; + qsizetype index = size + 1; + bool found = false; + if (container) { + QCborMap proxy(*container); + auto it = proxy.constFind(key); + if (it < proxy.constEnd()) { + found = true; + index = it.item.i; + } + } + + container = maybeDetach(container, size + (found ? 0 : 2)); + Q_ASSERT(container); + if (!found) { + container->append(key); + container->append(QCborValue()); + } + Q_ASSERT(index & 1 && !(container->elements.size() & 1)); + Q_ASSERT(index < container->elements.size()); + return { container, index }; +} + +/*! + \overload + + Returns a QCborValueRef that can be used to read or modify the entry in + this, as a map, with the given \a key. When this QCborValue is a QCborMap, + this function is equivalent to the matching operator[] on that map. + + Before returning the reference: if this QCborValue was an array, it is first + converted to a map (so that \c{map[i]} is \c{array[i]} for each index, \c i, + with valid \c{array[i]}); otherwise, if it was not a map it will be + over-written with an empty map. + + \sa operator[](qint64), QCborMap::operator[], QCborMap::value(), + QCborMap::find() + */ +QCborValueRef QCborValue::operator[](QLatin1String key) +{ + if (!isMap()) + *this = QCborValue(isArray() ? arrayAsMap(toArray()) : QCborMap()); + + const qsizetype size = container ? container->elements.size() : 0; + qsizetype index = size + 1; + bool found = false; + if (container) { + QCborMap proxy(*container); + auto it = proxy.constFind(key); + if (it < proxy.constEnd()) { + found = true; + index = it.item.i; + } + } + + container = maybeDetach(container, size + (found ? 0 : 2)); + Q_ASSERT(container); + if (!found) { + container->append(key); + container->append(QCborValue()); + } + Q_ASSERT(index & 1 && !(container->elements.size() & 1)); + Q_ASSERT(index < container->elements.size()); + return { container, index }; +} + +/*! + \overload + + Returns a QCborValueRef that can be used to read or modify the entry in + this, as a map or array, with the given \a key. When this QCborValue is a + QCborMap or, for 0 <= key < 0x10000, a QCborArray, this function is + equivalent to the matching operator[] on that map or array. + + Before returning the reference: if this QCborValue was an array but the key + is out of range, the array is first converted to a map (so that \c{map[i]} + is \c{array[i]} for each index, \c i, with valid \c{array[i]}); otherwise, + if it was not a map it will be over-written with an empty map. + + \sa operator[], QCborMap::operator[], QCborMap::value(), + QCborMap::find(), QCborArray::operator[], QCborArray::at() + */ +QCborValueRef QCborValue::operator[](qint64 key) +{ + if (isArray() && key >= 0 && key < 0x10000) { + container = maybeGrow(container, key); + return { container, qsizetype(key) }; + } + if (!isMap()) + *this = QCborValue(isArray() ? arrayAsMap(toArray()) : QCborMap()); + + const qsizetype size = container ? container->elements.size() : 0; + Q_ASSERT(!(size & 1)); + qsizetype index = size + 1; + bool found = false; + if (container) { + QCborMap proxy(*container); + auto it = proxy.constFind(key); + if (it < proxy.constEnd()) { + found = true; + index = it.item.i; + } + } + + container = maybeDetach(container, size + (found ? 0 : 2)); + Q_ASSERT(container); + if (!found) { + container->append(key); + container->append(QCborValue()); + } + Q_ASSERT(index & 1 && !(container->elements.size() & 1)); + Q_ASSERT(index < container->elements.size()); + return { container, index }; +} + +/*! Decodes one item from the CBOR stream found in \a reader and returns the equivalent representation. This function is recursive: if the item is a map or array, it will decode all items found in that map or array, until the @@ -2359,6 +2580,255 @@ QCborValue::Type QCborValueRef::concreteType(QCborValueRef self) noexcept return self.d->elements.at(self.i).type; } +/*! + If this QCborValueRef refers to a QCborMap, searches elements for the value + whose key matches \a key. If there's no key matching \a key in the map or if + this QCborValueRef object is not a map, returns the undefined value. + + This function is equivalent to: + + \code + value.toMap().value(key); + \endcode + + \sa operator[](qint64), QCborMap::operator[], QCborMap::value(), + QCborMap::find() + */ +const QCborValue QCborValueRef::operator[](const QString &key) const +{ + const QCborValue item = d->valueAt(i); + return item[key]; +} + +/*! + \overload + + If this QCborValueRef refers to a QCborMap, searches elements for the value + whose key matches \a key. If there's no key matching \a key in the map or if + this QCborValueRef object is not a map, returns the undefined value. + + This function is equivalent to: + + \code + value.toMap().value(key); + \endcode + + \sa operator[](qint64), QCborMap::operator[], QCborMap::value(), + QCborMap::find() + */ +const QCborValue QCborValueRef::operator[](QLatin1String key) const +{ + const QCborValue item = d->valueAt(i); + return item[key]; +} + +/*! + \overload + + If this QCborValueRef refers to a QCborMap, searches elements for the value + whose key matches \a key. If this is a QCborArray, returns the element whose + index is \a key. If there's no matching value in the array or map, or if + this QCborValueRef object is not an array or map, returns the undefined + value. + + \sa operator[], QCborMap::operator[], QCborMap::value(), + QCborMap::find(), QCborArray::operator[], QCborArray::at() + */ +const QCborValue QCborValueRef::operator[](qint64 key) const +{ + const QCborValue item = d->valueAt(i); + return item[key]; +} + +/*! + Returns a QCborValueRef that can be used to read or modify the entry in + this, as a map, with the given \a key. When this QCborValueRef refers to a + QCborMap, this function is equivalent to the matching operator[] on that + map. + + Before returning the reference: if the QCborValue referenced was an array, + it is first converted to a map (so that \c{map[i]} is \c{array[i]} for each + index, \c i, with valid \c{array[i]}); otherwise, if it was not a map it + will be over-written with an empty map. + + \sa operator[](qint64), QCborMap::operator[], QCborMap::value(), + QCborMap::find() + */ +QCborValueRef QCborValueRef::operator[](const QString &key) +{ + auto &e = d->elements[i]; + qsizetype size = 0; + if (e.flags & QtCbor::Element::IsContainer) { + if (e.container) { + if (e.type == QCborValue::Array) { + QCborValue repack = QCborValue(arrayAsMap(QCborArray(*e.container))); + qSwap(e.container, repack.container); + } else if (e.type != QCborValue::Map) { + e.container->deref(); + e.container = nullptr; + } + } + e.type = QCborValue::Map; + if (e.container) + size = e.container->elements.size(); + } else { + // Stomp any prior e.value, replace with a map (that we'll grow) + e.container = nullptr; + e.type = QCborValue::Map; + e.flags = QtCbor::Element::IsContainer; + } + + qsizetype index = size + 1; + bool found = false; + if (e.container) { + QCborMap proxy(*e.container); + auto it = proxy.constFind(key); + if (it < proxy.constEnd()) { + found = true; + index = it.item.i; + } + } + + e.container = maybeDetach(e.container, size + (found ? 0 : 2)); + Q_ASSERT(e.container); + if (!found) { + e.container->append(key); + e.container->append(QCborValue()); + } + Q_ASSERT(index & 1 && !(e.container->elements.size() & 1)); + Q_ASSERT(index < e.container->elements.size()); + return { e.container, index }; +} + +/*! + \overload + + Returns a QCborValueRef that can be used to read or modify the entry in + this, as a map, with the given \a key. When this QCborValue is a QCborMap, + this function is equivalent to the matching operator[] on that map. + + Before returning the reference: if the QCborValue referenced was an array, + it is first converted to a map (so that \c{map[i]} is \c{array[i]} for each + index, \c i, with valid \c{array[i]}); otherwise, if it was not a map it + will be over-written with an empty map. + + \sa operator[](qint64), QCborMap::operator[], QCborMap::value(), + QCborMap::find() + */ +QCborValueRef QCborValueRef::operator[](QLatin1String key) +{ + auto &e = d->elements[i]; + qsizetype size = 0; + if (e.flags & QtCbor::Element::IsContainer) { + if (e.container) { + if (e.type == QCborValue::Array) { + QCborValue repack = QCborValue(arrayAsMap(QCborArray(*e.container))); + qSwap(e.container, repack.container); + } else if (e.type != QCborValue::Map) { + e.container->deref(); + e.container = nullptr; + } + } + e.type = QCborValue::Map; + if (e.container) + size = e.container->elements.size(); + } else { + // Stomp any prior e.value, replace with a map (that we'll grow) + e.container = nullptr; + e.type = QCborValue::Map; + e.flags = QtCbor::Element::IsContainer; + } + + qsizetype index = size + 1; + bool found = false; + if (e.container) { + QCborMap proxy(*e.container); + auto it = proxy.constFind(key); + if (it < proxy.constEnd()) { + found = true; + index = it.item.i; + } + } + + e.container = maybeDetach(e.container, size + (found ? 0 : 2)); + Q_ASSERT(e.container); + if (!found) { + e.container->append(key); + e.container->append(QCborValue()); + } + Q_ASSERT(index & 1 && !(e.container->elements.size() & 1)); + Q_ASSERT(index < e.container->elements.size()); + return { e.container, index }; +} + +/*! + \overload + + Returns a QCborValueRef that can be used to read or modify the entry in + this, as a map or array, with the given \a key. When this QCborValue is a + QCborMap or, for 0 <= key < 0x10000, a QCborArray, this function is + equivalent to the matching operator[] on that map or array. + + Before returning the reference: if the QCborValue referenced was an array + but the key is out of range, the array is first converted to a map (so that + \c{map[i]} is \c{array[i]} for each index, \c i, with valid \c{array[i]}); + otherwise, if it was not a map it will be over-written with an empty map. + + \sa operator[], QCborMap::operator[], QCborMap::value(), + QCborMap::find(), QCborArray::operator[], QCborArray::at() + */ +QCborValueRef QCborValueRef::operator[](qint64 key) +{ + auto &e = d->elements[i]; + if (e.type == QCborValue::Array && key >= 0 && key < 0x10000) { + e.container = maybeGrow(e.container, key); + return { e.container, qsizetype(key) }; + } + qsizetype size = 0; + if (e.flags & QtCbor::Element::IsContainer) { + if (e.container) { + if (e.type == QCborValue::Array) { + QCborValue repack = QCborValue(arrayAsMap(QCborArray(*e.container))); + qSwap(e.container, repack.container); + } else if (e.type != QCborValue::Map) { + e.container->deref(); + e.container = nullptr; + } + } + e.type = QCborValue::Map; + if (e.container) + size = e.container->elements.size(); + } else { + // Stomp any prior e.value, replace with a map (that we'll grow) + e.container = nullptr; + e.type = QCborValue::Map; + e.flags = QtCbor::Element::IsContainer; + } + Q_ASSERT(!(size & 1)); + + qsizetype index = size + 1; + bool found = false; + if (e.container) { + QCborMap proxy(*e.container); + auto it = proxy.constFind(key); + if (it < proxy.constEnd()) { + found = true; + index = it.item.i; + } + } + + e.container = maybeDetach(e.container, size + (found ? 0 : 2)); + Q_ASSERT(e.container); + if (!found) { + e.container->append(key); + e.container->append(QCborValue()); + } + Q_ASSERT(index & 1 && !(e.container->elements.size() & 1)); + Q_ASSERT(index < e.container->elements.size()); + return { e.container, index }; +} + + inline QCborArray::QCborArray(QCborContainerPrivate &dd) noexcept : d(&dd) { @@ -2471,7 +2941,7 @@ static QDebug debugContents(QDebug &dbg, const QCborValue &v) } if (v.isSimpleType()) return dbg << v.toSimpleType(); - return dbg << "<unknown type " << hex << int(v.type()) << dec << '>'; + return dbg << "<unknown type " << Qt::hex << int(v.type()) << Qt::dec << '>'; } QDebug operator<<(QDebug dbg, const QCborValue &v) { @@ -2481,6 +2951,26 @@ QDebug operator<<(QDebug dbg, const QCborValue &v) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QCborValue &value) +{ + stream << QCborValue(value).toCbor(); + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QCborValue &value) +{ + QByteArray buffer; + stream >> buffer; + QCborParserError parseError{}; + value = QCborValue::fromCbor(buffer, &parseError); + if (parseError.error) + stream.setStatus(QDataStream::ReadCorruptData); + return stream; +} +#endif + + QT_END_NAMESPACE #include "qcborarray.cpp" diff --git a/src/corelib/serialization/qcborvalue.h b/src/corelib/serialization/qcborvalue.h index d6ba4e88d8..f542e44c47 100644 --- a/src/corelib/serialization/qcborvalue.h +++ b/src/corelib/serialization/qcborvalue.h @@ -69,6 +69,7 @@ class QCborArray; class QCborMap; class QCborStreamReader; class QCborStreamWriter; +class QDataStream; struct QCborParserError { @@ -78,6 +79,7 @@ struct QCborParserError QString errorString() const { return error.toString(); } }; +class QCborValueRef; class QCborContainerPrivate; class Q_CORE_EXPORT QCborValue { @@ -242,20 +244,18 @@ public: #endif QUuid toUuid(const QUuid &defaultValue = {}) const; -#ifdef Q_QDOC - QCborArray toArray(const QCborArray &a = {}) const; - QCborMap toMap(const QCborMap &m = {}) const; -#else // only forward-declared, need split functions QCborArray toArray() const; QCborArray toArray(const QCborArray &defaultValue) const; QCborMap toMap() const; QCborMap toMap(const QCborMap &defaultValue) const; -#endif const QCborValue operator[](const QString &key) const; const QCborValue operator[](QLatin1String key) const; const QCborValue operator[](qint64 key) const; + QCborValueRef operator[](qint64 key); + QCborValueRef operator[](QLatin1String key); + QCborValueRef operator[](const QString & key); int compare(const QCborValue &other) const; #if 0 && QT_HAS_INCLUDE(<compare>) @@ -393,16 +393,18 @@ public: QUuid toUuid(const QUuid &defaultValue = {}) const { return concrete().toUuid(defaultValue); } -#ifdef Q_QDOC - QCborArray toArray(const QCborArray &a = {}) const; - QCborMap toMap(const QCborMap &m = {}) const; -#else // only forward-declared, need split functions. Implemented in qcbor{array,map}.h QCborArray toArray() const; QCborArray toArray(const QCborArray &a) const; QCborMap toMap() const; QCborMap toMap(const QCborMap &m) const; -#endif + + const QCborValue operator[](const QString &key) const; + const QCborValue operator[](QLatin1String key) const; + const QCborValue operator[](qint64 key) const; + QCborValueRef operator[](qint64 key); + QCborValueRef operator[](QLatin1String key); + QCborValueRef operator[](const QString & key); int compare(const QCborValue &other) const { return concrete().compare(other); } @@ -434,6 +436,7 @@ public: { return concrete().toDiagnosticNotation(opt); } private: + friend class QCborValue; friend class QCborArray; friend class QCborMap; friend class QCborContainerPrivate; @@ -465,6 +468,11 @@ Q_CORE_EXPORT uint qHash(const QCborValue &value, uint seed = 0); Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborValue &v); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborValue &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborValue &); +#endif + QT_END_NAMESPACE #if defined(QT_X11_DEFINES_FOUND) diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp index 951f6c9736..b3330d6cea 100644 --- a/src/corelib/serialization/qdatastream.cpp +++ b/src/corelib/serialization/qdatastream.cpp @@ -98,6 +98,10 @@ QT_BEGIN_NAMESPACE ensures that you get integers of the size you want and insulates you from compiler and platform differences. + Enumerations can be serialized through QDataStream without the + need of manually defining streaming operators. Enum classes are + serialized using the declared size. + To take one example, a \c{char *} string is written as a 32-bit integer equal to the length of the string including the '\\0' byte, followed by all the characters of the string including the @@ -273,7 +277,7 @@ QT_BEGIN_NAMESPACE QDataStream::QDataStream() { - dev = 0; + dev = nullptr; owndev = false; byteorder = BigEndian; ver = Qt_DefaultCompiledVersion; @@ -368,7 +372,7 @@ QDataStream::~QDataStream() /*! \fn QIODevice *QDataStream::device() const - Returns the I/O device currently set, or 0 if no + Returns the I/O device currently set, or \nullptr if no device is currently set. \sa setDevice() @@ -377,7 +381,7 @@ QDataStream::~QDataStream() /*! void QDataStream::setDevice(QIODevice *d) - Sets the I/O device to \a d, which can be 0 + Sets the I/O device to \a d, which can be \nullptr to unset to current I/O device. \sa device() @@ -392,17 +396,18 @@ void QDataStream::setDevice(QIODevice *d) dev = d; } +#if QT_DEPRECATED_SINCE(5, 13) /*! \obsolete Unsets the I/O device. - Use setDevice(0) instead. + Use setDevice(nullptr) instead. */ void QDataStream::unsetDevice() { - setDevice(0); + setDevice(nullptr); } - +#endif /*! \fn bool QDataStream::atEnd() const @@ -428,7 +433,7 @@ bool QDataStream::atEnd() const */ QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const { - return d == 0 ? QDataStream::DoublePrecision : d->floatingPointPrecision; + return d ? d->floatingPointPrecision : QDataStream::DoublePrecision; } /*! @@ -453,7 +458,7 @@ QDataStream::FloatingPointPrecision QDataStream::floatingPointPrecision() const */ void QDataStream::setFloatingPointPrecision(QDataStream::FloatingPointPrecision precision) { - if (d == 0) + if (!d) d.reset(new QDataStreamPrivate()); d->floatingPointPrecision = precision; } @@ -559,6 +564,8 @@ void QDataStream::setByteOrder(ByteOrder bo) \value Qt_5_10 Same as Qt_5_6 \value Qt_5_11 Same as Qt_5_6 \value Qt_5_12 Version 18 (Qt 5.12) + \value Qt_5_13 Version 19 (Qt 5.13) + \value Qt_5_14 Same as Qt_5_13 \omitvalue Qt_DefaultCompiledVersion \sa setVersion(), version() @@ -632,7 +639,7 @@ void QDataStream::startTransaction() { CHECK_STREAM_PRECOND(Q_VOID) - if (d == 0) + if (!d) d.reset(new QDataStreamPrivate()); if (++d->transactionDepth == 1) { @@ -1026,8 +1033,7 @@ QDataStream &QDataStream::operator>>(char *&s) \c{delete []} operator. The \a l parameter is set to the length of the buffer. If the - string read is empty, \a l is set to 0 and \a s is set to - a null pointer. + string read is empty, \a l is set to 0 and \a s is set to \nullptr. The serialization format is a quint32 length specifier first, then \a l bytes of data. @@ -1037,7 +1043,7 @@ QDataStream &QDataStream::operator>>(char *&s) QDataStream &QDataStream::readBytes(char *&s, uint &l) { - s = 0; + s = nullptr; l = 0; CHECK_STREAM_PRECOND(*this) @@ -1048,8 +1054,8 @@ QDataStream &QDataStream::readBytes(char *&s, uint &l) const quint32 Step = 1024 * 1024; quint32 allocated = 0; - char *prevBuf = 0; - char *curBuf = 0; + char *prevBuf = nullptr; + char *curBuf = nullptr; do { int blockSize = qMin(Step, len - allocated); diff --git a/src/corelib/serialization/qdatastream.h b/src/corelib/serialization/qdatastream.h index eae0146553..cfcd89333b 100644 --- a/src/corelib/serialization/qdatastream.h +++ b/src/corelib/serialization/qdatastream.h @@ -55,7 +55,6 @@ class QByteArray; class QIODevice; template <typename T> class QList; -template <typename T> class QLinkedList; template <typename T> class QVector; template <typename T> class QSet; template <class Key, class T> class QHash; @@ -99,10 +98,20 @@ public: Qt_5_10 = Qt_5_9, Qt_5_11 = Qt_5_10, Qt_5_12 = 18, -#if QT_VERSION >= 0x050d00 + Qt_5_13 = 19, + Qt_5_14 = Qt_5_13, +#if QT_VERSION >= 0x050f00 + Qt_5_15 = Qt_5_14, + Qt_DefaultCompiledVersion = Qt_5_15 +#elif QT_VERSION >= 0x060000 + Qt_6_0 = Qt_5_15, + Qt_DefaultCompiledVersion = Qt_6_0 +#else + Qt_DefaultCompiledVersion = Qt_5_14 +#endif +#if QT_VERSION >= 0x060100 #error Add the datastream version for this Qt version and update Qt_DefaultCompiledVersion #endif - Qt_DefaultCompiledVersion = Qt_5_12 }; enum ByteOrder { @@ -130,7 +139,10 @@ public: QIODevice *device() const; void setDevice(QIODevice *); +#if QT_DEPRECATED_SINCE(5, 13) + QT_DEPRECATED_X("Use QDataStream::setDevice(nullptr) instead") void unsetDevice(); +#endif bool atEnd() const; @@ -373,25 +385,23 @@ inline QDataStream &operator>>(QDataStream &s, QFlags<Enum> &e) { return s >> e.i; } template <typename T> -inline QDataStream &operator>>(QDataStream &s, QList<T> &l) -{ - return QtPrivate::readArrayBasedContainer(s, l); -} +typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type& +operator<<(QDataStream &s, const T &t) +{ return s << static_cast<typename std::underlying_type<T>::type>(t); } template <typename T> -inline QDataStream &operator<<(QDataStream &s, const QList<T> &l) -{ - return QtPrivate::writeSequentialContainer(s, l); -} +typename std::enable_if<std::is_enum<T>::value, QDataStream &>::type& +operator>>(QDataStream &s, T &t) +{ return s >> reinterpret_cast<typename std::underlying_type<T>::type &>(t); } template <typename T> -inline QDataStream &operator>>(QDataStream &s, QLinkedList<T> &l) +inline QDataStream &operator>>(QDataStream &s, QList<T> &l) { - return QtPrivate::readListBasedContainer(s, l); + return QtPrivate::readArrayBasedContainer(s, l); } template <typename T> -inline QDataStream &operator<<(QDataStream &s, const QLinkedList<T> &l) +inline QDataStream &operator<<(QDataStream &s, const QList<T> &l) { return QtPrivate::writeSequentialContainer(s, l); } diff --git a/src/corelib/serialization/qjson_p.h b/src/corelib/serialization/qjson_p.h index feba1faac6..9de53d5b74 100644 --- a/src/corelib/serialization/qjson_p.h +++ b/src/corelib/serialization/qjson_p.h @@ -304,7 +304,7 @@ public: return *this; } - QLatin1String toQLatin1String() const Q_DECL_NOTHROW { + QLatin1String toQLatin1String() const noexcept { return QLatin1String(d->latin1, d->length); } @@ -339,23 +339,23 @@ public: }; #define DEF_OP(op) \ - inline bool operator op(Latin1String lhs, Latin1String rhs) Q_DECL_NOTHROW \ + inline bool operator op(Latin1String lhs, Latin1String rhs) noexcept \ { \ return lhs.toQLatin1String() op rhs.toQLatin1String(); \ } \ - inline bool operator op(QLatin1String lhs, Latin1String rhs) Q_DECL_NOTHROW \ + inline bool operator op(QLatin1String lhs, Latin1String rhs) noexcept \ { \ return lhs op rhs.toQLatin1String(); \ } \ - inline bool operator op(Latin1String lhs, QLatin1String rhs) Q_DECL_NOTHROW \ + inline bool operator op(Latin1String lhs, QLatin1String rhs) noexcept \ { \ return lhs.toQLatin1String() op rhs; \ } \ - inline bool operator op(const QString &lhs, Latin1String rhs) Q_DECL_NOTHROW \ + inline bool operator op(const QString &lhs, Latin1String rhs) noexcept \ { \ return lhs op rhs.toQLatin1String(); \ } \ - inline bool operator op(Latin1String lhs, const QString &rhs) Q_DECL_NOTHROW \ + inline bool operator op(Latin1String lhs, const QString &rhs) noexcept \ { \ return lhs.toQLatin1String() op rhs; \ } \ @@ -686,7 +686,7 @@ public: { } inline Data(int reserved, QJsonValue::Type valueType) - : rawData(0), compactionCounter(0), ownsData(true) + : rawData(nullptr), compactionCounter(0), ownsData(true) { Q_ASSERT(valueType == QJsonValue::Array || valueType == QJsonValue::Object); @@ -719,7 +719,7 @@ public: Data *clone(Base *b, int reserve = 0) { int size = sizeof(Header) + b->size; - if (b == header->root() && ref.load() == 1 && alloc >= size + reserve) + if (b == header->root() && ref.loadRelaxed() == 1 && alloc >= size + reserve) return this; if (reserve) { @@ -728,7 +728,7 @@ public: size = qMax(size + reserve, qMin(size *2, (int)Value::MaxSize)); if (size > Value::MaxSize) { qWarning("QJson: Document too large to store in data structure"); - return 0; + return nullptr; } } char *raw = (char *)malloc(size); @@ -746,7 +746,7 @@ public: bool valid() const; private: - Q_DISABLE_COPY(Data) + Q_DISABLE_COPY_MOVE(Data) }; } diff --git a/src/corelib/serialization/qjsonarray.cpp b/src/corelib/serialization/qjsonarray.cpp index 1187bb03a3..6b327619ad 100644 --- a/src/corelib/serialization/qjsonarray.cpp +++ b/src/corelib/serialization/qjsonarray.cpp @@ -132,7 +132,7 @@ QT_BEGIN_NAMESPACE Creates an empty array. */ QJsonArray::QJsonArray() - : d(0), a(0) + : d(nullptr), a(nullptr) { } @@ -168,8 +168,8 @@ QJsonArray::QJsonArray(QJsonPrivate::Data *data, QJsonPrivate::Array *array) */ void QJsonArray::initialize() { - d = 0; - a = 0; + d = nullptr; + a = nullptr; } /*! @@ -675,6 +675,14 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \sa begin(), constEnd() */ +/*! \fn QJsonArray::const_iterator QJsonArray::cbegin() const + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item + in the array. + + \sa begin(), cend() +*/ + /*! \fn QJsonArray::iterator QJsonArray::end() Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item @@ -696,6 +704,14 @@ bool QJsonArray::operator!=(const QJsonArray &other) const \sa constBegin(), end() */ +/*! \fn QJsonArray::const_iterator QJsonArray::cend() const + + Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary + item after the last item in the array. + + \sa cbegin(), end() +*/ + /*! \fn void QJsonArray::push_back(const QJsonValue &value) This function is provided for STL compatibility. It is equivalent @@ -1210,7 +1226,7 @@ bool QJsonArray::detach2(uint reserve) d->ref.ref(); return true; } - if (reserve == 0 && d->ref.load() == 1) + if (reserve == 0 && d->ref.loadRelaxed() == 1) return true; QJsonPrivate::Data *x = d->clone(a, reserve); @@ -1259,5 +1275,22 @@ QDebug operator<<(QDebug dbg, const QJsonArray &a) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QJsonArray &array) +{ + QJsonDocument doc{array}; + stream << doc.toJson(QJsonDocument::Compact); + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QJsonArray &array) +{ + QJsonDocument doc; + stream >> doc; + array = doc.array(); + return stream; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/serialization/qjsonarray.h b/src/corelib/serialization/qjsonarray.h index 5dff4a0aa9..983a6753b5 100644 --- a/src/corelib/serialization/qjsonarray.h +++ b/src/corelib/serialization/qjsonarray.h @@ -42,9 +42,7 @@ #include <QtCore/qjsonvalue.h> #include <QtCore/qiterator.h> -#if defined(Q_COMPILER_INITIALIZER_LISTS) #include <initializer_list> -#endif QT_BEGIN_NAMESPACE @@ -58,21 +56,19 @@ class Q_CORE_EXPORT QJsonArray public: QJsonArray(); -#if defined(Q_COMPILER_INITIALIZER_LISTS) || defined(Q_QDOC) QJsonArray(std::initializer_list<QJsonValue> args) { initialize(); for (std::initializer_list<QJsonValue>::const_iterator i = args.begin(); i != args.end(); ++i) append(*i); } -#endif ~QJsonArray(); QJsonArray(const QJsonArray &other); QJsonArray &operator =(const QJsonArray &other); - QJsonArray(QJsonArray &&other) Q_DECL_NOTHROW + QJsonArray(QJsonArray &&other) noexcept : d(other.d), a(other.a) { @@ -80,7 +76,7 @@ public: other.a = nullptr; } - QJsonArray &operator =(QJsonArray &&other) Q_DECL_NOTHROW + QJsonArray &operator =(QJsonArray &&other) noexcept { swap(other); return *this; @@ -115,7 +111,7 @@ public: bool operator==(const QJsonArray &other) const; bool operator!=(const QJsonArray &other) const; - void swap(QJsonArray &other) Q_DECL_NOTHROW + void swap(QJsonArray &other) noexcept { qSwap(d, other.d); qSwap(a, other.a); @@ -214,9 +210,11 @@ public: inline iterator begin() { detach2(); return iterator(this, 0); } inline const_iterator begin() const { return const_iterator(this, 0); } inline const_iterator constBegin() const { return const_iterator(this, 0); } + inline const_iterator cbegin() const { return const_iterator(this, 0); } inline iterator end() { detach2(); return iterator(this, size()); } inline const_iterator end() const { return const_iterator(this, size()); } inline const_iterator constEnd() const { return const_iterator(this, size()); } + inline const_iterator cend() const { return const_iterator(this, size()); } iterator insert(iterator before, const QJsonValue &value) { insert(before.i, value); return before; } iterator erase(iterator it) { removeAt(it.i); return it; } @@ -271,6 +269,11 @@ Q_CORE_EXPORT uint qHash(const QJsonArray &array, uint seed = 0); Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &); +#endif + QT_END_NAMESPACE #endif // QJSONARRAY_H diff --git a/src/corelib/serialization/qjsoncbor.cpp b/src/corelib/serialization/qjsoncbor.cpp index 4603750d11..e0c390f610 100644 --- a/src/corelib/serialization/qjsoncbor.cpp +++ b/src/corelib/serialization/qjsoncbor.cpp @@ -826,7 +826,7 @@ QCborArray QCborArray::fromJsonArray(const QJsonArray &array) { QCborArray a; a.detach(array.size()); - for (const QJsonValue v : array) { + for (const QJsonValue &v : array) { if (v.isString()) a.d->append(v.toString()); else diff --git a/src/corelib/serialization/qjsondocument.cpp b/src/corelib/serialization/qjsondocument.cpp index 0cd86d3ded..f8027efb58 100644 --- a/src/corelib/serialization/qjsondocument.cpp +++ b/src/corelib/serialization/qjsondocument.cpp @@ -47,6 +47,7 @@ #include "qjsonwriter_p.h" #include "qjsonparser_p.h" #include "qjson_p.h" +#include "qdatastream.h" QT_BEGIN_NAMESPACE @@ -83,7 +84,7 @@ QT_BEGIN_NAMESPACE * Constructs an empty and invalid document. */ QJsonDocument::QJsonDocument() - : d(0) + : d(nullptr) { } @@ -91,7 +92,7 @@ QJsonDocument::QJsonDocument() * Creates a QJsonDocument from \a object. */ QJsonDocument::QJsonDocument(const QJsonObject &object) - : d(0) + : d(nullptr) { setObject(object); } @@ -100,7 +101,7 @@ QJsonDocument::QJsonDocument(const QJsonObject &object) * Constructs a QJsonDocument from \a array. */ QJsonDocument::QJsonDocument(const QJsonArray &array) - : d(0) + : d(nullptr) { setArray(array); } @@ -235,7 +236,7 @@ const char *QJsonDocument::rawData(int *size) const { if (!d) { *size = 0; - return 0; + return nullptr; } *size = d->alloc; return d->rawData; @@ -634,7 +635,7 @@ bool QJsonDocument::operator==(const QJsonDocument &other) const */ bool QJsonDocument::isNull() const { - return (d == 0); + return (d == nullptr); } #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) @@ -657,4 +658,23 @@ QDebug operator<<(QDebug dbg, const QJsonDocument &o) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QJsonDocument &doc) +{ + stream << doc.toJson(QJsonDocument::Compact); + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QJsonDocument &doc) +{ + QByteArray buffer; + stream >> buffer; + QJsonParseError parseError{}; + doc = QJsonDocument::fromJson(buffer, &parseError); + if (parseError.error && !buffer.isEmpty()) + stream.setStatus(QDataStream::ReadCorruptData); + return stream; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/serialization/qjsondocument.h b/src/corelib/serialization/qjsondocument.h index b784890c54..a8006a6cc5 100644 --- a/src/corelib/serialization/qjsondocument.h +++ b/src/corelib/serialization/qjsondocument.h @@ -93,19 +93,19 @@ public: QJsonDocument(const QJsonDocument &other); QJsonDocument &operator =(const QJsonDocument &other); - QJsonDocument(QJsonDocument &&other) Q_DECL_NOTHROW + QJsonDocument(QJsonDocument &&other) noexcept : d(other.d) { other.d = nullptr; } - QJsonDocument &operator =(QJsonDocument &&other) Q_DECL_NOTHROW + QJsonDocument &operator =(QJsonDocument &&other) noexcept { swap(other); return *this; } - void swap(QJsonDocument &other) Q_DECL_NOTHROW + void swap(QJsonDocument &other) noexcept { qSwap(d, other.d); } @@ -172,6 +172,11 @@ Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonDocument) Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonDocument &); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonDocument &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonDocument &); +#endif + QT_END_NAMESPACE #endif // QJSONDOCUMENT_H diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index 950bec535b..c1a6328b2f 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -110,7 +110,7 @@ QT_BEGIN_NAMESPACE \sa isEmpty() */ QJsonObject::QJsonObject() - : d(0), o(0) + : d(nullptr), o(nullptr) { } @@ -149,8 +149,8 @@ QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object) void QJsonObject::initialize() { - d = 0; - o = 0; + d = nullptr; + o = nullptr; } /*! @@ -646,7 +646,7 @@ bool QJsonObject::operator!=(const QJsonObject &other) const */ QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) { - Q_ASSERT(d && d->ref.load() == 1); + Q_ASSERT(d && d->ref.loadRelaxed() == 1); if (it.o != this || it.i < 0 || it.i >= (int)o->length) return iterator(this, o->length); @@ -1231,7 +1231,7 @@ bool QJsonObject::detach2(uint reserve) d->ref.ref(); return true; } - if (reserve == 0 && d->ref.load() == 1) + if (reserve == 0 && d->ref.loadRelaxed() == 1) return true; QJsonPrivate::Data *x = d->clone(o, reserve); @@ -1320,4 +1320,21 @@ QDebug operator<<(QDebug dbg, const QJsonObject &o) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QJsonObject &object) +{ + QJsonDocument doc{object}; + stream << doc.toJson(QJsonDocument::Compact); + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QJsonObject &object) +{ + QJsonDocument doc; + stream >> doc; + object = doc.object(); + return stream; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/serialization/qjsonobject.h b/src/corelib/serialization/qjsonobject.h index be42d3747a..d8e2ab9ca7 100644 --- a/src/corelib/serialization/qjsonobject.h +++ b/src/corelib/serialization/qjsonobject.h @@ -42,10 +42,8 @@ #include <QtCore/qjsonvalue.h> #include <QtCore/qiterator.h> -#ifdef Q_COMPILER_INITIALIZER_LISTS #include <QtCore/qpair.h> #include <initializer_list> -#endif QT_BEGIN_NAMESPACE @@ -60,34 +58,32 @@ class Q_CORE_EXPORT QJsonObject public: QJsonObject(); -#if defined(Q_COMPILER_INITIALIZER_LISTS) || defined(Q_QDOC) QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) { initialize(); for (std::initializer_list<QPair<QString, QJsonValue> >::const_iterator i = args.begin(); i != args.end(); ++i) insert(i->first, i->second); } -#endif ~QJsonObject(); QJsonObject(const QJsonObject &other); QJsonObject &operator =(const QJsonObject &other); - QJsonObject(QJsonObject &&other) Q_DECL_NOTHROW + QJsonObject(QJsonObject &&other) noexcept : d(other.d), o(other.o) { other.d = nullptr; other.o = nullptr; } - QJsonObject &operator =(QJsonObject &&other) Q_DECL_NOTHROW + QJsonObject &operator =(QJsonObject &&other) noexcept { swap(other); return *this; } - void swap(QJsonObject &other) Q_DECL_NOTHROW + void swap(QJsonObject &other) noexcept { qSwap(d, other.d); qSwap(o, other.o); @@ -268,6 +264,11 @@ Q_CORE_EXPORT uint qHash(const QJsonObject &object, uint seed = 0); Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &); +#endif + QT_END_NAMESPACE #endif // QJSONOBJECT_H diff --git a/src/corelib/serialization/qjsonparser.cpp b/src/corelib/serialization/qjsonparser.cpp index bfba95520e..cd36bd5a5b 100644 --- a/src/corelib/serialization/qjsonparser.cpp +++ b/src/corelib/serialization/qjsonparser.cpp @@ -198,7 +198,9 @@ QString QJsonParseError::errorString() const using namespace QJsonPrivate; Parser::Parser(const char *json, int length) - : head(json), json(json), data(0), dataLength(0), current(0), nestingLevel(0), lastError(QJsonParseError::NoError) + : head(json), json(json), data(nullptr) + , dataLength(0), current(0), nestingLevel(0) + , lastError(QJsonParseError::NoError) { end = json + length; } @@ -316,7 +318,7 @@ QJsonDocument Parser::parse(QJsonParseError *error) eatBOM(); char token = nextToken(); - DEBUG << hex << (uint)token; + DEBUG << Qt::hex << (uint)token; if (token == BeginArray) { if (!parseArray()) goto error; diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp index 2c04da4885..0bd28581f3 100644 --- a/src/corelib/serialization/qjsonvalue.cpp +++ b/src/corelib/serialization/qjsonvalue.cpp @@ -45,6 +45,7 @@ #include <qvariant.h> #include <qstringlist.h> #include <qdebug.h> +#include "qdatastream.h" #ifndef QT_BOOTSTRAPPED # include <qcborarray.h> @@ -111,7 +112,7 @@ QT_BEGIN_NAMESPACE The default is to create a Null value. */ QJsonValue::QJsonValue(Type type) - : ui(0), d(0), t(type) + : ui(0), d(nullptr), t(type) { } @@ -119,7 +120,7 @@ QJsonValue::QJsonValue(Type type) \internal */ QJsonValue::QJsonValue(QJsonPrivate::Data *data, QJsonPrivate::Base *base, const QJsonPrivate::Value &v) - : d(0) + : d(nullptr) { t = (Type)(uint)v.type; switch (t) { @@ -153,7 +154,7 @@ QJsonValue::QJsonValue(QJsonPrivate::Data *data, QJsonPrivate::Base *base, const Creates a value of type Bool, with value \a b. */ QJsonValue::QJsonValue(bool b) - : d(0), t(Bool) + : d(nullptr), t(Bool) { this->b = b; } @@ -162,7 +163,7 @@ QJsonValue::QJsonValue(bool b) Creates a value of type Double, with value \a n. */ QJsonValue::QJsonValue(double n) - : d(0), t(Double) + : d(nullptr), t(Double) { this->dbl = n; } @@ -172,7 +173,7 @@ QJsonValue::QJsonValue(double n) Creates a value of type Double, with value \a n. */ QJsonValue::QJsonValue(int n) - : d(0), t(Double) + : d(nullptr), t(Double) { this->dbl = n; } @@ -184,7 +185,7 @@ QJsonValue::QJsonValue(int n) If you pass in values outside this range expect a loss of precision to occur. */ QJsonValue::QJsonValue(qint64 n) - : d(0), t(Double) + : d(nullptr), t(Double) { this->dbl = double(n); } @@ -193,7 +194,7 @@ QJsonValue::QJsonValue(qint64 n) Creates a value of type String, with value \a s. */ QJsonValue::QJsonValue(const QString &s) - : d(0), t(String) + : d(nullptr), t(String) { stringDataFromQStringHelper(s); } @@ -220,7 +221,7 @@ void QJsonValue::stringDataFromQStringHelper(const QString &string) Creates a value of type String, with value \a s. */ QJsonValue::QJsonValue(QLatin1String s) - : d(0), t(String) + : d(nullptr), t(String) { // ### FIXME: Avoid creating the temp QString below QString str(s); @@ -933,4 +934,78 @@ QDebug operator<<(QDebug dbg, const QJsonValue &o) } #endif +#ifndef QT_NO_DATASTREAM +QDataStream &operator<<(QDataStream &stream, const QJsonValue &v) +{ + quint8 type = v.t; + stream << type; + switch (type) { + case QJsonValue::Undefined: + case QJsonValue::Null: + break; + case QJsonValue::Bool: + stream << v.toBool(); + break; + case QJsonValue::Double: + stream << v.toDouble(); + break; + case QJsonValue::String: + stream << v.toString(); + break; + case QJsonValue::Array: + stream << v.toArray(); + break; + case QJsonValue::Object: + stream << v.toObject(); + break; + } + return stream; +} + +QDataStream &operator>>(QDataStream &stream, QJsonValue &v) +{ + quint8 type; + stream >> type; + switch (type) { + case QJsonValue::Undefined: + case QJsonValue::Null: + v = QJsonValue{QJsonValue::Type(type)}; + break; + case QJsonValue::Bool: { + bool b; + stream >> b; + v = QJsonValue(b); + break; + } case QJsonValue::Double: { + double d; + stream >> d; + v = QJsonValue{d}; + break; + } case QJsonValue::String: { + QString s; + stream >> s; + v = QJsonValue{s}; + break; + } + case QJsonValue::Array: { + QJsonArray a; + stream >> a; + v = QJsonValue{a}; + break; + } + case QJsonValue::Object: { + QJsonObject o; + stream >> o; + v = QJsonValue{o}; + break; + } + default: { + stream.setStatus(QDataStream::ReadCorruptData); + v = QJsonValue{QJsonValue::Undefined}; + } + } + return stream; +} +#endif + QT_END_NAMESPACE diff --git a/src/corelib/serialization/qjsonvalue.h b/src/corelib/serialization/qjsonvalue.h index d8e121524d..37d84f9e60 100644 --- a/src/corelib/serialization/qjsonvalue.h +++ b/src/corelib/serialization/qjsonvalue.h @@ -92,7 +92,7 @@ public: QJsonValue(const QJsonValue &other); QJsonValue &operator =(const QJsonValue &other); - QJsonValue(QJsonValue &&other) Q_DECL_NOTHROW + QJsonValue(QJsonValue &&other) noexcept : ui(other.ui), d(other.d), t(other.t) @@ -102,13 +102,13 @@ public: other.t = Null; } - QJsonValue &operator =(QJsonValue &&other) Q_DECL_NOTHROW + QJsonValue &operator =(QJsonValue &&other) noexcept { swap(other); return *this; } - void swap(QJsonValue &other) Q_DECL_NOTHROW + void swap(QJsonValue &other) noexcept { qSwap(ui, other.ui); qSwap(d, other.d); @@ -152,6 +152,7 @@ private: friend class QJsonObject; friend class QCborValue; friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); + friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); QJsonValue(QJsonPrivate::Data *d, QJsonPrivate::Base *b, const QJsonPrivate::Value& v); void stringDataFromQStringHelper(const QString &string); @@ -218,7 +219,6 @@ private: uint index : 31; }; -#ifndef Q_QDOC // ### Qt 6: Get rid of these fake pointer classes class QJsonValuePtr { @@ -243,7 +243,6 @@ public: QJsonValueRef& operator*() { return valueRef; } QJsonValueRef* operator->() { return &valueRef; } }; -#endif Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonValue) @@ -253,6 +252,11 @@ Q_CORE_EXPORT uint qHash(const QJsonValue &value, uint seed = 0); Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); #endif +#ifndef QT_NO_DATASTREAM +Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); +Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &); +#endif + QT_END_NAMESPACE #endif // QJSONVALUE_H diff --git a/src/corelib/serialization/qjsonwriter.cpp b/src/corelib/serialization/qjsonwriter.cpp index 12ce20ef09..5b246837d2 100644 --- a/src/corelib/serialization/qjsonwriter.cpp +++ b/src/corelib/serialization/qjsonwriter.cpp @@ -58,7 +58,6 @@ static inline uchar hexdig(uint u) static QByteArray escapedString(const QString &s) { - const uchar replacement = '?'; QByteArray ba(s.length(), Qt::Uninitialized); uchar *cursor = reinterpret_cast<uchar *>(const_cast<char *>(ba.constData())); @@ -111,9 +110,14 @@ static QByteArray escapedString(const QString &s) } else { *cursor++ = (uchar)u; } - } else { - if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0) - *cursor++ = replacement; + } else if (QUtf8Functions::toUtf8<QUtf8BaseTraits>(u, cursor, src, end) < 0) { + // failed to get valid utf8 use JSON escape sequence + *cursor++ = '\\'; + *cursor++ = 'u'; + *cursor++ = hexdig(u>>12 & 0x0f); + *cursor++ = hexdig(u>>8 & 0x0f); + *cursor++ = hexdig(u>>4 & 0x0f); + *cursor++ = hexdig(u & 0x0f); } } diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp index fb7b677b2d..9d4bc223ab 100644 --- a/src/corelib/serialization/qtextstream.cpp +++ b/src/corelib/serialization/qtextstream.cpp @@ -135,30 +135,30 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384; \table \header \li Manipulator \li Description - \row \li \c bin \li Same as setIntegerBase(2). - \row \li \c oct \li Same as setIntegerBase(8). - \row \li \c dec \li Same as setIntegerBase(10). - \row \li \c hex \li Same as setIntegerBase(16). - \row \li \c showbase \li Same as setNumberFlags(numberFlags() | ShowBase). - \row \li \c forcesign \li Same as setNumberFlags(numberFlags() | ForceSign). - \row \li \c forcepoint \li Same as setNumberFlags(numberFlags() | ForcePoint). - \row \li \c noshowbase \li Same as setNumberFlags(numberFlags() & ~ShowBase). - \row \li \c noforcesign \li Same as setNumberFlags(numberFlags() & ~ForceSign). - \row \li \c noforcepoint \li Same as setNumberFlags(numberFlags() & ~ForcePoint). - \row \li \c uppercasebase \li Same as setNumberFlags(numberFlags() | UppercaseBase). - \row \li \c uppercasedigits \li Same as setNumberFlags(numberFlags() | UppercaseDigits). - \row \li \c lowercasebase \li Same as setNumberFlags(numberFlags() & ~UppercaseBase). - \row \li \c lowercasedigits \li Same as setNumberFlags(numberFlags() & ~UppercaseDigits). - \row \li \c fixed \li Same as setRealNumberNotation(FixedNotation). - \row \li \c scientific \li Same as setRealNumberNotation(ScientificNotation). - \row \li \c left \li Same as setFieldAlignment(AlignLeft). - \row \li \c right \li Same as setFieldAlignment(AlignRight). - \row \li \c center \li Same as setFieldAlignment(AlignCenter). - \row \li \c endl \li Same as operator<<('\\n') and flush(). - \row \li \c flush \li Same as flush(). - \row \li \c reset \li Same as reset(). - \row \li \c ws \li Same as skipWhiteSpace(). - \row \li \c bom \li Same as setGenerateByteOrderMark(true). + \row \li \c Qt::bin \li Same as setIntegerBase(2). + \row \li \c Qt::oct \li Same as setIntegerBase(8). + \row \li \c Qt::dec \li Same as setIntegerBase(10). + \row \li \c Qt::hex \li Same as setIntegerBase(16). + \row \li \c Qt::showbase \li Same as setNumberFlags(numberFlags() | ShowBase). + \row \li \c Qt::forcesign \li Same as setNumberFlags(numberFlags() | ForceSign). + \row \li \c Qt::forcepoint \li Same as setNumberFlags(numberFlags() | ForcePoint). + \row \li \c Qt::noshowbase \li Same as setNumberFlags(numberFlags() & ~ShowBase). + \row \li \c Qt::noforcesign \li Same as setNumberFlags(numberFlags() & ~ForceSign). + \row \li \c Qt::noforcepoint \li Same as setNumberFlags(numberFlags() & ~ForcePoint). + \row \li \c Qt::uppercasebase \li Same as setNumberFlags(numberFlags() | UppercaseBase). + \row \li \c Qt::uppercasedigits \li Same as setNumberFlags(numberFlags() | UppercaseDigits). + \row \li \c Qt::lowercasebase \li Same as setNumberFlags(numberFlags() & ~UppercaseBase). + \row \li \c Qt::lowercasedigits \li Same as setNumberFlags(numberFlags() & ~UppercaseDigits). + \row \li \c Qt::fixed \li Same as setRealNumberNotation(FixedNotation). + \row \li \c Qt::scientific \li Same as setRealNumberNotation(ScientificNotation). + \row \li \c Qt::left \li Same as setFieldAlignment(AlignLeft). + \row \li \c Qt::right \li Same as setFieldAlignment(AlignRight). + \row \li \c Qt::center \li Same as setFieldAlignment(AlignCenter). + \row \li \c Qt::endl \li Same as operator<<('\\n') and flush(). + \row \li \c Qt::flush \li Same as flush(). + \row \li \c Qt::reset \li Same as reset(). + \row \li \c Qt::ws \li Same as skipWhiteSpace(). + \row \li \c Qt::bom \li Same as setGenerateByteOrderMark(true). \endtable In addition, Qt provides three global manipulators that take a @@ -327,7 +327,7 @@ QT_BEGIN_NAMESPACE QTextStreamPrivate::QTextStreamPrivate(QTextStream *q_ptr) : #if QT_CONFIG(textcodec) - readConverterSavedState(0), + readConverterSavedState(nullptr), #endif readConverterSavedStateOffset(0), locale(QLocale::c()) @@ -381,7 +381,7 @@ void QTextStreamPrivate::Params::reset() padChar = QLatin1Char(' '); fieldAlignment = QTextStream::AlignRight; realNumberNotation = QTextStream::SmartNotation; - numberFlags = 0; + numberFlags = { }; } /*! @@ -391,9 +391,9 @@ void QTextStreamPrivate::reset() { params.reset(); - device = 0; + device = nullptr; deleteDevice = false; - string = 0; + string = nullptr; stringOffset = 0; stringOpenMode = QIODevice::NotOpen; @@ -406,7 +406,7 @@ void QTextStreamPrivate::reset() resetCodecConverterStateHelper(&readConverterState); resetCodecConverterStateHelper(&writeConverterState); delete readConverterSavedState; - readConverterSavedState = 0; + readConverterSavedState = nullptr; writeConverterState.flags |= QTextCodec::IgnoreHeader; autoDetectUnicode = true; #endif @@ -1207,7 +1207,7 @@ bool QTextStream::seek(qint64 pos) resetCodecConverterStateHelper(&d->readConverterState); resetCodecConverterStateHelper(&d->writeConverterState); delete d->readConverterSavedState; - d->readConverterSavedState = 0; + d->readConverterSavedState = nullptr; d->writeConverterState.flags |= QTextCodec::IgnoreHeader; #endif return true; @@ -1295,7 +1295,7 @@ void QTextStream::skipWhiteSpace() { Q_D(QTextStream); CHECK_VALID_STREAM(Q_VOID); - d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); + d->scan(nullptr, nullptr, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); } @@ -1332,7 +1332,7 @@ void QTextStream::setDevice(QIODevice *device) /*! Returns the current device associated with the QTextStream, - or 0 if no device has been assigned. + or \nullptr if no device has been assigned. \sa setDevice(), string() */ @@ -1369,8 +1369,8 @@ void QTextStream::setString(QString *string, QIODevice::OpenMode openMode) } /*! - Returns the current string assigned to the QTextStream, or 0 if no - string has been assigned. + Returns the current string assigned to the QTextStream, or + \nullptr if no string has been assigned. \sa setString(), device() */ @@ -1751,7 +1751,7 @@ QString QTextStream::read(qint64 maxlen) */ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong *ret) { - scan(0, 0, 0, NotSpace); + scan(nullptr, nullptr, 0, NotSpace); consumeLastToken(); // detect int encoding @@ -1980,7 +1980,7 @@ bool QTextStreamPrivate::getReal(double *f) ParserState state = Init; InputToken input = None; - scan(0, 0, 0, NotSpace); + scan(nullptr, nullptr, 0, NotSpace); consumeLastToken(); const int BufferSize = 128; @@ -2084,7 +2084,7 @@ QTextStream &QTextStream::operator>>(QChar &c) { Q_D(QTextStream); CHECK_VALID_STREAM(*this); - d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); + d->scan(nullptr, nullptr, 0, QTextStreamPrivate::NotSpace); if (!d->getChar(&c)) setStatus(ReadPastEnd); return *this; @@ -2245,7 +2245,7 @@ QTextStream &QTextStream::operator>>(QString &str) CHECK_VALID_STREAM(*this); str.clear(); - d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); + d->scan(nullptr, nullptr, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); const QChar *ptr; @@ -2273,7 +2273,7 @@ QTextStream &QTextStream::operator>>(QByteArray &array) CHECK_VALID_STREAM(*this); array.clear(); - d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); + d->scan(nullptr, nullptr, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); const QChar *ptr; @@ -2308,7 +2308,7 @@ QTextStream &QTextStream::operator>>(char *c) Q_D(QTextStream); *c = 0; CHECK_VALID_STREAM(*this); - d->scan(0, 0, 0, QTextStreamPrivate::NotSpace); + d->scan(nullptr, nullptr, 0, QTextStreamPrivate::NotSpace); d->consumeLastToken(); const QChar *ptr; @@ -2356,7 +2356,7 @@ void QTextStreamPrivate::putNumber(qulonglong number, bool negative) } else if (negative) { // Workaround for backward compatibility for writing negative // numbers in octal and hex: - // QTextStream(result) << showbase << hex << -1 << oct << -1 + // QTextStream(result) << Qt::showbase << Qt::hex << -1 << oct << -1 // should output: -0x1 -0b1 result = dd->unsLongLongToString(number, -1, base, -1, flags); result.prepend(locale.negativeSign()); @@ -2689,6 +2689,11 @@ QTextStream &QTextStream::operator<<(const void *ptr) d->params.numberFlags = oldFlags; return *this; } +#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +namespace Qt { +#else +namespace QTextStreamFunctions { +#endif /*! \relates QTextStream @@ -2973,7 +2978,7 @@ QTextStream ¢er(QTextStream &stream) */ QTextStream &endl(QTextStream &stream) { - return stream << QLatin1Char('\n') << flush; + return stream << QLatin1Char('\n') << Qt::flush; } /*! @@ -3015,6 +3020,8 @@ QTextStream &ws(QTextStream &stream) return stream; } +} // namespace QTextStreamFunctions + /*! \fn QTextStreamManipulator qSetFieldWidth(int width) \relates QTextStream @@ -3037,6 +3044,12 @@ QTextStream &ws(QTextStream &stream) */ #if QT_CONFIG(textcodec) + +#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +namespace Qt { +#else +namespace QTextStreamFunctions { +#endif /*! \relates QTextStream @@ -3051,6 +3064,8 @@ QTextStream &bom(QTextStream &stream) return stream; } +} // namespace QTextStreamFunctions + /*! Sets the codec for this stream to \a codec. The codec is used for decoding any data that is read from the assigned device, and for @@ -3200,6 +3215,43 @@ QLocale QTextStream::locale() const return d->locale; } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC) +// Binary compatible definitions for Qt<5.14 +Q_CORE_EXPORT QTextStream &bin(QTextStream &s) { return Qt::bin(s); } +Q_CORE_EXPORT QTextStream &oct(QTextStream &s) { return Qt::oct(s); } +Q_CORE_EXPORT QTextStream &dec(QTextStream &s) { return Qt::dec(s); } +Q_CORE_EXPORT QTextStream &hex(QTextStream &s) { return Qt::hex(s); } + +Q_CORE_EXPORT QTextStream &showbase(QTextStream &s) { return Qt::showbase(s); } +Q_CORE_EXPORT QTextStream &forcesign(QTextStream &s) { return Qt::forcesign(s); } +Q_CORE_EXPORT QTextStream &forcepoint(QTextStream &s) { return Qt::forcepoint(s); } +Q_CORE_EXPORT QTextStream &noshowbase(QTextStream &s) { return Qt::noshowbase(s); } +Q_CORE_EXPORT QTextStream &noforcesign(QTextStream &s) { return Qt::noforcesign(s); } +Q_CORE_EXPORT QTextStream &noforcepoint(QTextStream &s) { return Qt::noforcepoint(s); } + +Q_CORE_EXPORT QTextStream &uppercasebase(QTextStream &s) { return Qt::uppercasebase(s); } +Q_CORE_EXPORT QTextStream &uppercasedigits(QTextStream &s) { return Qt::uppercasedigits(s); } +Q_CORE_EXPORT QTextStream &lowercasebase(QTextStream &s) { return Qt::lowercasebase(s); } +Q_CORE_EXPORT QTextStream &lowercasedigits(QTextStream &s) { return Qt::lowercasedigits(s); } + +Q_CORE_EXPORT QTextStream &fixed(QTextStream &s) { return Qt::fixed(s); } +Q_CORE_EXPORT QTextStream &scientific(QTextStream &s) { return Qt::scientific(s); } + +Q_CORE_EXPORT QTextStream &left(QTextStream &s) { return Qt::left(s); } +Q_CORE_EXPORT QTextStream &right(QTextStream &s) { return Qt::right(s); } +Q_CORE_EXPORT QTextStream ¢er(QTextStream &s) { return Qt::center(s); } + +Q_CORE_EXPORT QTextStream &endl(QTextStream &s) { return Qt::endl(s); } +Q_CORE_EXPORT QTextStream &flush(QTextStream &s) { return Qt::flush(s); } +Q_CORE_EXPORT QTextStream &reset(QTextStream &s) { return Qt::reset(s); } + +Q_CORE_EXPORT QTextStream &ws(QTextStream &s) { return Qt::ws(s); } + +#if QT_CONFIG(textcodec) +Q_CORE_EXPORT QTextStream &bom(QTextStream &s) { return Qt::bom(s); } +#endif +#endif + QT_END_NAMESPACE #ifndef QT_NO_QOBJECT diff --git a/src/corelib/serialization/qtextstream.h b/src/corelib/serialization/qtextstream.h index 1d86a18b9c..4efa84f1f1 100644 --- a/src/corelib/serialization/qtextstream.h +++ b/src/corelib/serialization/qtextstream.h @@ -213,8 +213,8 @@ typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument class Q_CORE_EXPORT QTextStreamManipulator { public: - Q_DECL_CONSTEXPR QTextStreamManipulator(QTSMFI m, int a) Q_DECL_NOTHROW : mf(m), mc(nullptr), arg(a), ch() {} - Q_DECL_CONSTEXPR QTextStreamManipulator(QTSMFC m, QChar c) Q_DECL_NOTHROW : mf(nullptr), mc(m), arg(-1), ch(c) {} + Q_DECL_CONSTEXPR QTextStreamManipulator(QTSMFI m, int a) noexcept : mf(m), mc(nullptr), arg(a), ch() {} + Q_DECL_CONSTEXPR QTextStreamManipulator(QTSMFC m, QChar c) noexcept : mf(nullptr), mc(m), arg(-1), ch(c) {} void exec(QTextStream &s) { if (mf) { (s.*mf)(arg); } else { (s.*mc)(ch); } } private: @@ -233,6 +233,13 @@ inline QTextStream &operator<<(QTextStream &s, QTextStreamFunction f) inline QTextStream &operator<<(QTextStream &s, QTextStreamManipulator m) { m.exec(s); return s; } +#if defined(Q_QDOC) || QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) +namespace Qt { +#else +// This namespace only exists for 'using namespace' declarations. +namespace QTextStreamFunctions { +#endif + Q_CORE_EXPORT QTextStream &bin(QTextStream &s); Q_CORE_EXPORT QTextStream &oct(QTextStream &s); Q_CORE_EXPORT QTextStream &dec(QTextStream &s); @@ -265,6 +272,18 @@ Q_CORE_EXPORT QTextStream &bom(QTextStream &s); Q_CORE_EXPORT QTextStream &ws(QTextStream &s); +} // namespace QTextStreamFunctions + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC) +namespace Qt { +using namespace QTextStreamFunctions; +} + +// We use 'using namespace' as that doesn't cause +// conflicting definitions compiler errors. +using namespace QTextStreamFunctions; +#endif // QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && !defined(Q_QDOC) + inline QTextStreamManipulator qSetFieldWidth(int width) { QTSMFI func = &QTextStream::setFieldWidth; diff --git a/src/corelib/serialization/qxmlstream.cpp b/src/corelib/serialization/qxmlstream.cpp index 827996ee2d..be3a476cb2 100644 --- a/src/corelib/serialization/qxmlstream.cpp +++ b/src/corelib/serialization/qxmlstream.cpp @@ -58,9 +58,9 @@ // case for most bootstrapped applications. #define Q_DECLARE_TR_FUNCTIONS(context) \ public: \ - static inline QString tr(const char *sourceText, const char *comment = 0) \ + static inline QString tr(const char *sourceText, const char *comment = nullptr) \ { Q_UNUSED(comment); return QString::fromLatin1(sourceText); } \ - static inline QString trUtf8(const char *sourceText, const char *comment = 0) \ + static inline QString trUtf8(const char *sourceText, const char *comment = nullptr) \ { Q_UNUSED(comment); return QString::fromLatin1(sourceText); } \ static inline QString tr(const char *sourceText, const char*, int) \ { return QString::fromLatin1(sourceText); } \ @@ -223,7 +223,7 @@ QString QXmlStreamReaderPrivate::resolveUndeclaredEntity(const QString &name) The stream reader does \e not take ownership of the resolver. It's the callers responsibility to ensure that the resolver is valid during the entire life-time of the stream reader object, or until - another resolver or 0 is set. + another resolver or \nullptr is set. \sa entityResolver() */ @@ -236,7 +236,7 @@ void QXmlStreamReader::setEntityResolver(QXmlStreamEntityResolver *resolver) /*! \since 4.4 - Returns the entity resolver, or 0 if there is no entity resolver. + Returns the entity resolver, or \nullptr if there is no entity resolver. \sa setEntityResolver() */ @@ -480,7 +480,7 @@ void QXmlStreamReader::setDevice(QIODevice *device) /*! Returns the current device associated with the QXmlStreamReader, - or 0 if no device has been assigned. + or \nullptr if no device has been assigned. \sa setDevice() */ @@ -548,7 +548,7 @@ void QXmlStreamReader::clear() if (d->device) { if (d->deleteDevice) delete d->device; - d->device = 0; + d->device = nullptr; } } @@ -792,16 +792,16 @@ QXmlStreamPrivateTagStack::QXmlStreamPrivateTagStack() QXmlStreamReaderPrivate::QXmlStreamReaderPrivate(QXmlStreamReader *q) :q_ptr(q) { - device = 0; + device = nullptr; deleteDevice = false; #if QT_CONFIG(textcodec) - decoder = 0; + decoder = nullptr; #endif stack_size = 64; - sym_stack = 0; - state_stack = 0; + sym_stack = nullptr; + state_stack = nullptr; reallocateStack(); - entityResolver = 0; + entityResolver = nullptr; init(); #define ADD_PREDEFINED(n, v) \ do { \ @@ -843,11 +843,11 @@ void QXmlStreamReaderPrivate::init() #if QT_CONFIG(textcodec) codec = QTextCodec::codecForMib(106); // utf8 delete decoder; - decoder = 0; + decoder = nullptr; #endif attributeStack.clear(); attributeStack.reserve(16); - entityParser = 0; + entityParser = nullptr; hasCheckedStartDocument = false; normalizeLiterals = false; hasSeenTag = false; @@ -2285,12 +2285,14 @@ QXmlStreamAttribute::QXmlStreamAttribute() m_isDefault = false; } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) /*! Destructs an attribute. */ QXmlStreamAttribute::~QXmlStreamAttribute() { } +#endif /*! Constructs an attribute in the namespace described with \a namespaceUri with \a name and value \a value. @@ -2366,6 +2368,7 @@ QXmlStreamAttribute::QXmlStreamAttribute(const QString &qualifiedName, const QSt */ +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) /*! Creates a copy of \a other. */ @@ -2386,7 +2389,7 @@ QXmlStreamAttribute& QXmlStreamAttribute::operator=(const QXmlStreamAttribute &o m_isDefault = other.m_isDefault; return *this; } - +#endif /*! \class QXmlStreamAttributes @@ -2442,6 +2445,8 @@ QXmlStreamAttribute& QXmlStreamAttribute::operator=(const QXmlStreamAttribute &o QXmlStreamNotationDeclaration::QXmlStreamNotationDeclaration() { } + +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) /*! Creates a copy of \a other. */ @@ -2467,6 +2472,7 @@ Destructs this notation declaration. QXmlStreamNotationDeclaration::~QXmlStreamNotationDeclaration() { } +#endif /*! \fn QStringRef QXmlStreamNotationDeclaration::name() const @@ -2539,6 +2545,7 @@ QXmlStreamNamespaceDeclaration::QXmlStreamNamespaceDeclaration(const QString &pr m_namespaceUri = namespaceUri; } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) /*! Creates a copy of \a other. */ @@ -2562,6 +2569,7 @@ Destructs this namespace declaration. QXmlStreamNamespaceDeclaration::~QXmlStreamNamespaceDeclaration() { } +#endif /*! \fn QStringRef QXmlStreamNamespaceDeclaration::prefix() const @@ -2609,6 +2617,7 @@ QXmlStreamEntityDeclaration::QXmlStreamEntityDeclaration() { } +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) /*! Creates a copy of \a other. */ @@ -2636,6 +2645,7 @@ QXmlStreamEntityDeclaration& QXmlStreamEntityDeclaration::operator=(const QXmlSt QXmlStreamEntityDeclaration::~QXmlStreamEntityDeclaration() { } +#endif /*! \fn QXmlStreamStringRef::swap(QXmlStreamStringRef &other) \since 5.6 @@ -3014,8 +3024,8 @@ QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) :autoFormattingIndent(4, ' ') { q_ptr = q; - device = 0; - stringDevice = 0; + device = nullptr; + stringDevice = nullptr; deleteDevice = false; #if QT_CONFIG(textcodec) codec = QTextCodec::codecForMib(106); // utf8 @@ -3305,7 +3315,7 @@ void QXmlStreamWriter::setDevice(QIODevice *device) Q_D(QXmlStreamWriter); if (device == d->device) return; - d->stringDevice = 0; + d->stringDevice = nullptr; if (d->deleteDevice) { delete d->device; d->deleteDevice = false; @@ -3315,7 +3325,7 @@ void QXmlStreamWriter::setDevice(QIODevice *device) /*! Returns the current device associated with the QXmlStreamWriter, - or 0 if no device has been assigned. + or \nullptr if no device has been assigned. \sa setDevice() */ diff --git a/src/corelib/serialization/qxmlstream.g b/src/corelib/serialization/qxmlstream.g index 10bfcd491c..12ecc9bdb2 100644 --- a/src/corelib/serialization/qxmlstream.g +++ b/src/corelib/serialization/qxmlstream.g @@ -41,6 +41,8 @@ %merged_output qxmlstream_p.h +%expect 4 + %token NOTOKEN %token SPACE " " %token LANGLE "<" @@ -144,7 +146,12 @@ %start document + + /. + +#include <QtCore/private/qglobal_p.h> + template <typename T> class QXmlStreamSimpleStack { T *data; int tos, cap; @@ -155,7 +162,8 @@ public: inline void reserve(int extraCapacity) { if (tos + extraCapacity + 1 > cap) { cap = qMax(tos + extraCapacity + 1, cap << 1 ); - data = reinterpret_cast<T *>(realloc(data, cap * sizeof(T))); + void *ptr = realloc(static_cast<void *>(data), cap * sizeof(T)); + data = reinterpret_cast<T *>(ptr); Q_CHECK_PTR(data); } } @@ -498,7 +506,7 @@ public: int fastScanLiteralContent(); int fastScanSpace(); int fastScanContentCharList(); - int fastScanName(int *prefix = 0); + int fastScanName(int *prefix = nullptr); inline int fastScanNMTOKEN(); @@ -753,7 +761,7 @@ bool QXmlStreamReaderPrivate::parse() state_stack[tos] = 0; return true; } else if (act > 0) { - if (++tos == stack_size-1) + if (++tos >= stack_size-1) reallocateStack(); Value &val = sym_stack[tos]; @@ -890,7 +898,7 @@ doctype_decl ::= langle_bang DOCTYPE qname markup space_opt RANGLE; /. case $rule_number: dtdName = symString(3); - // fall through + Q_FALLTHROUGH(); ./ doctype_decl ::= doctype_decl_start external_id space_opt markup space_opt RANGLE; /. diff --git a/src/corelib/serialization/qxmlstream.h b/src/corelib/serialization/qxmlstream.h index d30c6bc01f..7d0aa64570 100644 --- a/src/corelib/serialization/qxmlstream.h +++ b/src/corelib/serialization/qxmlstream.h @@ -59,19 +59,15 @@ public: inline QXmlStreamStringRef(const QStringRef &aString) :m_string(aString.string()?*aString.string():QString()), m_position(aString.position()), m_size(aString.size()){} QXmlStreamStringRef(const QString &aString) : m_string(aString), m_position(0), m_size(m_string.size()) {} -#ifdef Q_COMPILER_RVALUE_REFS - QXmlStreamStringRef(QString &&aString) Q_DECL_NOTHROW : m_string(std::move(aString)), m_position(0), m_size(m_string.size()) {} -#endif + QXmlStreamStringRef(QString &&aString) noexcept : m_string(std::move(aString)), m_position(0), m_size(m_string.size()) {} #if QT_VERSION < QT_VERSION_CHECK(6,0,0) QXmlStreamStringRef(const QXmlStreamStringRef &other) // = default : m_string(other.m_string), m_position(other.m_position), m_size(other.m_size) {} -#ifdef Q_COMPILER_RVALUE_REFS - QXmlStreamStringRef(QXmlStreamStringRef &&other) Q_DECL_NOTHROW // = default + QXmlStreamStringRef(QXmlStreamStringRef &&other) noexcept // = default : m_string(std::move(other.m_string)), m_position(other.m_position), m_size(other.m_size) {} - QXmlStreamStringRef &operator=(QXmlStreamStringRef &&other) Q_DECL_NOTHROW // = default + QXmlStreamStringRef &operator=(QXmlStreamStringRef &&other) noexcept // = default { swap(other); return *this; } -#endif QXmlStreamStringRef &operator=(const QXmlStreamStringRef &other) // = default { m_string = other.m_string; m_position = other.m_position; m_size = other.m_size; return *this; } inline ~QXmlStreamStringRef() {} // ### this prevents (or deprecates) all the move/copy special member functions, @@ -79,7 +75,7 @@ public: // ### Qt 5, since that would change the way its passed to functions. In Qt 6, remove all. #endif // Qt < 6.0 - void swap(QXmlStreamStringRef &other) Q_DECL_NOTHROW + void swap(QXmlStreamStringRef &other) noexcept { qSwap(m_string, other.m_string); qSwap(m_position, other.m_position); @@ -108,11 +104,10 @@ class Q_CORE_EXPORT QXmlStreamAttribute { public: QXmlStreamAttribute(); QXmlStreamAttribute(const QString &qualifiedName, const QString &value); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QXmlStreamAttribute(const QString &namespaceUri, const QString &name, const QString &value); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QXmlStreamAttribute(const QXmlStreamAttribute &); -#ifdef Q_COMPILER_RVALUE_REFS - QXmlStreamAttribute(QXmlStreamAttribute &&other) Q_DECL_NOTHROW // = default; + QXmlStreamAttribute(QXmlStreamAttribute &&other) noexcept // = default; : m_name(std::move(other.m_name)), m_namespaceUri(std::move(other.m_namespaceUri)), m_qualifiedName(std::move(other.m_qualifiedName)), @@ -122,7 +117,7 @@ public: { other.reserved = nullptr; } - QXmlStreamAttribute &operator=(QXmlStreamAttribute &&other) Q_DECL_NOTHROW // = default; + QXmlStreamAttribute &operator=(QXmlStreamAttribute &&other) noexcept // = default; { m_name = std::move(other.m_name); m_namespaceUri = std::move(other.m_namespaceUri); @@ -132,7 +127,6 @@ public: m_isDefault = other.m_isDefault; return *this; } -#endif QXmlStreamAttribute& operator=(const QXmlStreamAttribute &); ~QXmlStreamAttribute(); #endif // < Qt 6 @@ -197,23 +191,23 @@ class Q_CORE_EXPORT QXmlStreamNamespaceDeclaration { friend class QXmlStreamReaderPrivate; public: QXmlStreamNamespaceDeclaration(); + QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri); #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) QXmlStreamNamespaceDeclaration(const QXmlStreamNamespaceDeclaration &); - QXmlStreamNamespaceDeclaration(QXmlStreamNamespaceDeclaration &&other) Q_DECL_NOTHROW // = default + QXmlStreamNamespaceDeclaration(QXmlStreamNamespaceDeclaration &&other) noexcept // = default : m_prefix(std::move(other.m_prefix)), m_namespaceUri(std::move(other.m_namespaceUri)), reserved(other.reserved) { other.reserved = nullptr; } - QXmlStreamNamespaceDeclaration &operator=(QXmlStreamNamespaceDeclaration &&other) Q_DECL_NOTHROW // = default + QXmlStreamNamespaceDeclaration &operator=(QXmlStreamNamespaceDeclaration &&other) noexcept // = default { m_prefix = std::move(other.m_prefix); m_namespaceUri = std::move(other.m_namespaceUri); qSwap(reserved, other.reserved); return *this; } - QXmlStreamNamespaceDeclaration(const QString &prefix, const QString &namespaceUri); ~QXmlStreamNamespaceDeclaration(); QXmlStreamNamespaceDeclaration& operator=(const QXmlStreamNamespaceDeclaration &); #endif // < Qt 6 @@ -242,7 +236,7 @@ public: #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) ~QXmlStreamNotationDeclaration(); QXmlStreamNotationDeclaration(const QXmlStreamNotationDeclaration &); - QXmlStreamNotationDeclaration(QXmlStreamNotationDeclaration &&other) Q_DECL_NOTHROW // = default + QXmlStreamNotationDeclaration(QXmlStreamNotationDeclaration &&other) noexcept // = default : m_name(std::move(other.m_name)), m_systemId(std::move(other.m_systemId)), m_publicId(std::move(other.m_publicId)), @@ -251,7 +245,7 @@ public: other.reserved = nullptr; } QXmlStreamNotationDeclaration& operator=(const QXmlStreamNotationDeclaration &); - QXmlStreamNotationDeclaration &operator=(QXmlStreamNotationDeclaration &&other) Q_DECL_NOTHROW // = default + QXmlStreamNotationDeclaration &operator=(QXmlStreamNotationDeclaration &&other) noexcept // = default { m_name = std::move(other.m_name); m_systemId = std::move(other.m_systemId); @@ -287,7 +281,7 @@ public: #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) ~QXmlStreamEntityDeclaration(); QXmlStreamEntityDeclaration(const QXmlStreamEntityDeclaration &); - QXmlStreamEntityDeclaration(QXmlStreamEntityDeclaration &&other) Q_DECL_NOTHROW // = default + QXmlStreamEntityDeclaration(QXmlStreamEntityDeclaration &&other) noexcept // = default : m_name(std::move(other.m_name)), m_notationName(std::move(other.m_notationName)), m_systemId(std::move(other.m_systemId)), @@ -298,7 +292,7 @@ public: other.reserved = nullptr; } QXmlStreamEntityDeclaration& operator=(const QXmlStreamEntityDeclaration &); - QXmlStreamEntityDeclaration &operator=(QXmlStreamEntityDeclaration &&other) Q_DECL_NOTHROW // = default + QXmlStreamEntityDeclaration &operator=(QXmlStreamEntityDeclaration &&other) noexcept // = default { m_name = std::move(other.m_name); m_notationName = std::move(other.m_notationName); diff --git a/src/corelib/serialization/qxmlstream_p.h b/src/corelib/serialization/qxmlstream_p.h index 61f501f81b..cde66a48a3 100644 --- a/src/corelib/serialization/qxmlstream_p.h +++ b/src/corelib/serialization/qxmlstream_p.h @@ -162,12 +162,12 @@ public: const char *const QXmlStreamReader_Table::spell [] = { - "end of file", 0, " ", "<", ">", "&", "#", "\'", "\"", "[", + "end of file", nullptr, " ", "<", ">", "&", "#", "\'", "\"", "[", "]", "(", ")", "|", "=", "%", "/", ":", ";", ",", "-", "+", "*", ".", "?", "!", "[a-zA-Z]", "[0-9]", "[CDATA[", "DOCTYPE", "ELEMENT", "ATTLIST", "ENTITY", "NOTATION", "SYSTEM", "PUBLIC", "NDATA", "REQUIRED", "IMPLIED", "FIXED", - "EMPTY", "ANY", "PCDATA", 0, 0, 0, 0, "CDATA", "ID", "IDREF", - "IDREFS", "ENTITIES", "NMTOKEN", "NMTOKENS", "<?xml", "version", 0}; + "EMPTY", "ANY", "PCDATA", nullptr, nullptr, nullptr, nullptr, "CDATA", "ID", "IDREF", + "IDREFS", "ENTITIES", "NMTOKEN", "NMTOKENS", "<?xml", "version", nullptr}; const short QXmlStreamReader_Table::lhs [] = { 57, 57, 59, 59, 59, 59, 59, 59, 59, 59, @@ -645,7 +645,7 @@ template <typename T> class QXmlStreamSimpleStack { T *data; int tos, cap; public: - inline QXmlStreamSimpleStack():data(0), tos(-1), cap(0){} + inline QXmlStreamSimpleStack():data(nullptr), tos(-1), cap(0){} inline ~QXmlStreamSimpleStack(){ if (data) free(data); } inline void reserve(int extraCapacity) { @@ -995,7 +995,7 @@ public: int fastScanLiteralContent(); int fastScanSpace(); int fastScanContentCharList(); - int fastScanName(int *prefix = 0); + int fastScanName(int *prefix = nullptr); inline int fastScanNMTOKEN(); |