QMetaType & QVariant: "load" and "save" std::nullptr_t

We don't load and save pointers usually because the pointer value cannot
be guaranteed to remain across program invocations. However, nullptr is
an exception: a null pointer is always a null pointer.

We don't actually have to read or write anything: there's only one value
possible for a std::nullptr_t and it is nullptr.

[ChangeLog][Important Behavior Changes] A QVariant containing a
std::nullptr_t is now streamable to/from QDataStream.

Task-number: QTBUG-59391
Change-Id: Iae839f6a131a4f0784bffffd14aa374f6475d283
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2017-03-09 13:44:00 +01:00
parent 3548cdedb6
commit 99fc96fd37
2 changed files with 12 additions and 5 deletions

View File

@ -1255,7 +1255,6 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data)
case QMetaType::UnknownType:
case QMetaType::Void:
case QMetaType::VoidStar:
case QMetaType::Nullptr:
case QMetaType::QObjectStar:
case QMetaType::QModelIndex:
case QMetaType::QPersistentModelIndex:
@ -1264,6 +1263,8 @@ bool QMetaType::save(QDataStream &stream, int type, const void *data)
case QMetaType::QJsonArray:
case QMetaType::QJsonDocument:
return false;
case QMetaType::Nullptr:
return true;
case QMetaType::Long:
stream << qlonglong(*static_cast<const long *>(data));
break;
@ -1477,7 +1478,6 @@ bool QMetaType::load(QDataStream &stream, int type, void *data)
case QMetaType::UnknownType:
case QMetaType::Void:
case QMetaType::VoidStar:
case QMetaType::Nullptr:
case QMetaType::QObjectStar:
case QMetaType::QModelIndex:
case QMetaType::QPersistentModelIndex:
@ -1486,6 +1486,8 @@ bool QMetaType::load(QDataStream &stream, int type, void *data)
case QMetaType::QJsonArray:
case QMetaType::QJsonDocument:
return false;
case QMetaType::Nullptr:
return true;
case QMetaType::Long: {
qlonglong l;
stream >> l;

View File

@ -1559,7 +1559,6 @@ DECLARE_NONSTREAMABLE(QJsonArray)
DECLARE_NONSTREAMABLE(QJsonDocument)
DECLARE_NONSTREAMABLE(QObject*)
DECLARE_NONSTREAMABLE(QWidget*)
DECLARE_NONSTREAMABLE(std::nullptr_t)
#define DECLARE_GUI_CLASS_NONSTREAMABLE(MetaTypeName, MetaTypeId, RealType) \
DECLARE_NONSTREAMABLE(RealType)
@ -1598,6 +1597,9 @@ void tst_QMetaType::saveAndLoadBuiltin()
if (isStreamable) {
QVERIFY(QMetaType::load(stream, type, value)); // Hmmm, shouldn't it return false?
// std::nullptr_t is nullary: it doesn't actually read anything
if (type != QMetaType::Nullptr)
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
}
@ -1608,6 +1610,9 @@ void tst_QMetaType::saveAndLoadBuiltin()
if (isStreamable) {
QVERIFY(QMetaType::load(stream, type, value)); // Hmmm, shouldn't it return false?
// std::nullptr_t is nullary: it doesn't actually read anything
if (type != QMetaType::Nullptr)
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
}