diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-07-30 22:42:31 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-08-24 00:17:03 +0200 |
commit | ed8acbeb7c6fd137486b5c8a6eec58a3b9db862c (patch) | |
tree | d92c3f06a07082e2cc40be366faac8bc58b82def /tests/auto/corelib | |
parent | 5282545589244c1a3e01442e3586d5666213f76d (diff) |
Automatically register data/debug stream operations in QMetaType
And remove the old manual registration code for those operators.
Add some special handling for long/ulong, as these types could be
streamed as a QVariant so far, but are not directly streamable
through QDataStream.
[ChangeLog][QtCore][QMetaType] The QMetaType::registerStreamOperators()
and QMetaType::registerDebugStreamOperator() methods have been
removed. The streaming operators for a type are now automatically
registered together with the type registration. This implies that the
operators should be visible wherever the type is visible and being used.
[ChangeLog][Behavior Incompatible Changes] Because the QDataStream and
QDebug serialization operators are automatically registered with
QMetaType, the declarations of those functions must be present at any
point where the type is used with QMetaType and QVariant.
Change-Id: I4a0732651b20319af4a8397ff90b848ca4580d99
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/corelib')
4 files changed, 45 insertions, 50 deletions
diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp index 6b59a6f1af..3185dbcaf4 100644 --- a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp +++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp @@ -106,6 +106,8 @@ private: QItemSelectionModel *selection; }; +QT_BEGIN_NAMESPACE + QDataStream &operator<<(QDataStream &, const QModelIndex &); QDataStream &operator>>(QDataStream &, QModelIndex &); QDataStream &operator<<(QDataStream &, const QModelIndexList &); @@ -178,6 +180,8 @@ QDataStream &operator>>(QDataStream &s, QModelIndexList &output) return s; } +QT_END_NAMESPACE + tst_QItemSelectionModel::tst_QItemSelectionModel() : model(0), selection(0) { diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 6f916372e5..6f844e2fb4 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -217,7 +217,6 @@ private slots: void isRegisteredStaticLess_data(); void isRegisteredStaticLess(); void isEnum(); - void registerStreamBuiltin(); void automaticTemplateRegistration(); void saveAndLoadBuiltin_data(); void saveAndLoadBuiltin(); @@ -351,12 +350,12 @@ static void *GadgetTypedConstructor(int type, void *where, const void *copy) return it->first->constructor(type, where, copy); } -static void GadgetSaveOperator(QDataStream & out, const void *data) +static void GadgetSaveOperator(const QtPrivate::QMetaTypeInterface *, QDataStream & out, const void *data) { reinterpret_cast<const BaseGenericType *>(data)->saveOperator(out); } -static void GadgetLoadOperator(QDataStream &in, void *data) +static void GadgetLoadOperator(const QtPrivate::QMetaTypeInterface *, QDataStream &in, void *data) { reinterpret_cast<BaseGenericType *>(data)->loadOperator(in); } @@ -424,12 +423,15 @@ void tst_QMetaType::registerGadget(const char *name, const QList<GadgetPropertyT [](const TypeInfo *self, void *ptr) { GadgetTypedDestructor(self->typeId, ptr); }, nullptr, nullptr, - nullptr }; + nullptr, + GadgetSaveOperator, + GadgetLoadOperator, + nullptr + }; QMetaType gadgetMetaType(typeInfo); dynamicGadgetProperties->m_metatype = gadgetMetaType; int gadgetTypeId = QMetaType(typeInfo).id(); QVERIFY(gadgetTypeId > 0); - QMetaType::registerStreamOperators(gadgetTypeId, &GadgetSaveOperator, &GadgetLoadOperator); s_managedTypes[gadgetTypeId] = qMakePair(dynamicGadgetProperties, std::shared_ptr<QMetaObject>{meta, [](QMetaObject *ptr){ ::free(ptr); }}); } @@ -1326,12 +1328,17 @@ void tst_QMetaType::typedConstruct() [](const TypeInfo *self, void *where, const void *copy) { GadgetTypedConstructor(self->typeId, where, copy); }, [](const TypeInfo *self, void *where, void *copy) { GadgetTypedConstructor(self->typeId, where, copy); }, [](const TypeInfo *self, void *ptr) { GadgetTypedDestructor(self->typeId, ptr); }, - nullptr, nullptr, nullptr }; + nullptr, + nullptr, + nullptr, + GadgetSaveOperator, + GadgetLoadOperator, + nullptr + }; QMetaType metatype(typeInfo); dynamicGadgetProperties->m_metatype = metatype; int podTypeId = metatype.id(); QVERIFY(podTypeId > 0); - QMetaType::registerStreamOperators(podTypeId, &GadgetSaveOperator, &GadgetLoadOperator); s_managedTypes[podTypeId] = qMakePair(dynamicGadgetProperties, std::shared_ptr<QMetaObject>{}); // Test POD @@ -1552,13 +1559,6 @@ void tst_QMetaType::isRegisteredStaticLess() QCOMPARE(QMetaType(typeId).isRegistered(), registered); } -void tst_QMetaType::registerStreamBuiltin() -{ - //should not crash; - qRegisterMetaTypeStreamOperators<QString>("QString"); - qRegisterMetaTypeStreamOperators<QVariant>("QVariant"); -} - typedef QHash<int, uint> IntUIntHash; Q_DECLARE_METATYPE(IntUIntHash) typedef QMap<int, uint> IntUIntMap; @@ -2044,7 +2044,6 @@ struct CustomStreamableType { int a; }; -Q_DECLARE_METATYPE(CustomStreamableType) QDataStream &operator<<(QDataStream &out, const CustomStreamableType &t) { @@ -2059,6 +2058,7 @@ QDataStream &operator>>(QDataStream &in, CustomStreamableType &t) t.a = a; return in; } +Q_DECLARE_METATYPE(CustomStreamableType) void tst_QMetaType::saveAndLoadCustom() { @@ -2068,12 +2068,7 @@ void tst_QMetaType::saveAndLoadCustom() int id = ::qMetaTypeId<CustomStreamableType>(); QByteArray ba; QDataStream stream(&ba, QIODevice::ReadWrite); - QVERIFY(!QMetaType::save(stream, id, &t)); - QCOMPARE(stream.status(), QDataStream::Ok); - QVERIFY(!QMetaType::load(stream, id, &t)); - QCOMPARE(stream.status(), QDataStream::Ok); - qRegisterMetaTypeStreamOperators<CustomStreamableType>("CustomStreamableType"); QVERIFY(QMetaType::save(stream, id, &t)); QCOMPARE(stream.status(), QDataStream::Ok); @@ -2278,6 +2273,11 @@ struct CustomDebugStreamableType QString toString() const { return "test"; } }; +struct CustomDebugStreamableType2 +{ + QString toString() const { return "test"; } +}; + QDebug operator<<(QDebug dbg, const CustomDebugStreamableType&) { return dbg << "string-content"; @@ -2607,16 +2607,14 @@ void tst_QMetaType::customDebugStream() { MessageHandlerCustom handler(::qMetaTypeId<CustomDebugStreamableType>()); QVariant v1 = QVariant::fromValue(CustomDebugStreamableType()); - handler.expectedMessage = "QVariant(CustomDebugStreamableType, )"; - qDebug() << v1; - - QMetaType::registerConverter<CustomDebugStreamableType, QString>(&CustomDebugStreamableType::toString); - handler.expectedMessage = "QVariant(CustomDebugStreamableType, \"test\")"; - qDebug() << v1; - - QMetaType::registerDebugStreamOperator<CustomDebugStreamableType>(); handler.expectedMessage = "QVariant(CustomDebugStreamableType, string-content)"; qDebug() << v1; + + MessageHandlerCustom handler2(::qMetaTypeId<CustomDebugStreamableType2>()); + QMetaType::registerConverter<CustomDebugStreamableType2, QString>(&CustomDebugStreamableType2::toString); + handler2.expectedMessage = "QVariant(CustomDebugStreamableType2, \"test\")"; + QVariant v2 = QVariant::fromValue(CustomDebugStreamableType2()); + qDebug() << v2; } void tst_QMetaType::unknownType() diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 231cccc38d..83ec36253a 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -1405,6 +1405,20 @@ struct CustomType int value() { return i1 + i2 + i3; } }; +QDataStream &operator<<(QDataStream &stream, const CustomType &ct) +{ + stream << ct.i1 << ct.i2 << ct.i3; + return stream; +} + +QDataStream &operator>>(QDataStream &stream, CustomType &ct) +{ + stream >> ct.i1; + stream >> ct.i2; + stream >> ct.i3; + return stream; +} + Q_DECLARE_METATYPE(CustomType*) Q_DECLARE_METATYPE(CustomType) @@ -1475,26 +1489,11 @@ void tst_QObject::customTypes() QCOMPARE(instanceCount, 3); } -QDataStream &operator<<(QDataStream &stream, const CustomType &ct) -{ - stream << ct.i1 << ct.i2 << ct.i3; - return stream; -} - -QDataStream &operator>>(QDataStream &stream, CustomType &ct) -{ - stream >> ct.i1; - stream >> ct.i2; - stream >> ct.i3; - return stream; -} - void tst_QObject::streamCustomTypes() { QByteArray ba; int idx = qRegisterMetaType<CustomType>("CustomType"); - qRegisterMetaTypeStreamOperators<CustomType>("CustomType"); { CustomType t1(1, 2, 3); diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index aeac08fc34..02a537a9d6 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -1222,7 +1222,6 @@ struct CustomStreamableClass return i == other.i; } }; -Q_DECLARE_METATYPE(CustomStreamableClass); QDataStream &operator<<(QDataStream &out, const CustomStreamableClass &myObj) { @@ -1233,11 +1232,10 @@ QDataStream &operator>>(QDataStream &in, CustomStreamableClass &myObj) { return in >> myObj.i; } +Q_DECLARE_METATYPE(CustomStreamableClass); void tst_QVariant::writeToReadFromDataStream_data() { - qRegisterMetaTypeStreamOperators<CustomStreamableClass>(); - QTest::addColumn<QVariant>("writeVariant"); QTest::addColumn<bool>("isNull"); { @@ -2184,8 +2182,6 @@ void tst_QVariant::saveLoadCustomTypes() auto tp = QMetaType::fromType<Blah>(); QVariant v = QVariant(tp, &i); - qRegisterMetaTypeStreamOperators<Blah>("Blah"); - QCOMPARE(v.userType(), tp.id()); QCOMPARE(v.type(), QVariant::UserType); { @@ -4639,8 +4635,6 @@ void tst_QVariant::fromStdVariant() void tst_QVariant::qt4UuidDataStream() { - qRegisterMetaTypeStreamOperators<QUuid>(); - QByteArray data; QDataStream stream(&data, QIODevice::WriteOnly); stream.setVersion(QDataStream::Qt_4_8); |