Add a way to check whether a metatype has datastream operators

Add QMetaType::hasRegisteredDataStreamOperators() to complement
the method to check whether a data stream operator exists.

Fixes: QTBUG-82916
Change-Id: Ib2f841131b7c401d5a3ae76d49104e41697c4eac
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Lars Knoll 2020-12-01 15:02:03 +01:00
parent 95cea24fa2
commit 60ea10e70b
3 changed files with 18 additions and 1 deletions

View File

@ -1733,7 +1733,6 @@ bool QMetaType::debugStream(QDebug& dbg, const void *rhs)
meta type.
*/
bool QMetaType::hasRegisteredDebugStreamOperator() const
{
return d_ptr && d_ptr->debugStream != nullptr;
}
@ -2686,6 +2685,20 @@ bool QMetaType::load(QDataStream &stream, void *data) const
return true;
}
/*!
\since 6.1
Returns \c true, if the meta type system has registered data stream operators for this
meta type.
*/
bool QMetaType::hasRegisteredDataStreamOperators() const
{
int type = id();
if (type == QMetaType::Long || type == QMetaType::ULong)
return true;
return d_ptr && d_ptr->dataStreamIn != nullptr && d_ptr->dataStreamOut != nullptr;
}
/*!
\fn bool QMetaType::load(QDataStream &stream, int type, void *data)
\overload

View File

@ -428,6 +428,7 @@ public:
#ifndef QT_NO_DATASTREAM
bool save(QDataStream &stream, const void *data) const;
bool load(QDataStream &stream, void *data) const;
bool hasRegisteredDataStreamOperators() const;
#if QT_DEPRECATED_SINCE(6, 0)
QT_DEPRECATED_VERSION_6_0

View File

@ -2033,11 +2033,14 @@ void tst_QMetaType::saveAndLoadBuiltin()
QCOMPARE(stream.status(), QDataStream::Ok);
if (isStreamable) {
QVERIFY(QMetaType(type).hasRegisteredDataStreamOperators());
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);
} else {
QVERIFY(!QMetaType(type).hasRegisteredDataStreamOperators());
}
stream.device()->seek(0);