diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-03-19 10:47:29 +0100 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-06-02 22:42:15 +0200 |
commit | fa987d44417528856d5e80ed7b48ba99e19fa307 (patch) | |
tree | 50cd74c1a9dd3c2197f7de2ac0d431a5b16b0a42 /tests/auto/corelib | |
parent | 5306fdabc1ceb09875f791526553b3665017f7ce (diff) |
MetaObject: Store the QMetaType of the methods
This does the analog of 46f407126ef3e94d59254012cdc34d6a4ad2faf2 for the
methods we care about (signals, slots, Q_INVOKABLEs). In addition to the
actual QMetaType, we store an array with offsets so that we later can do
a mapping from methodIndex to metatype.
The newly added QMetaMethod::{return,parameter}MetaType methods can then
be used to retrieve the metatypes.
This does however require that all involved types are complete. This is
unfortunately not a feasible requirement. Thus, we only populate the
metatype array on a best effort basis. For any incomplete type, we store
QMetaType::Unknown. Then, when accessing the metatype, we fall back to
the old string based code base if it's Unknown.
Squashes "moc: support incomplete types" and "Fix compile failures
after QMetaMethod change"
Fixes: QTBUG-82932
Change-Id: I6b7a587cc364b7cad0c158d6de54e8a204289ad4
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'tests/auto/corelib')
3 files changed, 64 insertions, 5 deletions
diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp index 4584b6ce31..8450537019 100644 --- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp +++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp @@ -48,6 +48,9 @@ private slots: void fromSignal(); void gadget(); + + void returnMetaType(); + void parameterMetaType(); }; struct CustomType { }; @@ -378,10 +381,11 @@ void tst_QMetaMethod::method_data() << QMetaMethod::Public << QMetaMethod::Constructor; + // since Qt 6.0, parameter types get automatically registered QTest::newRow("voidSignalCustomUnregisteredType") << QByteArray("voidSignalCustomUnregisteredType(CustomUnregisteredType)") << int(QMetaType::Void) << QByteArray("void") - << (QList<int>() << 0) + << (QList<int>() << QMetaType::fromType<CustomUnregisteredType>().id()) << (QList<QByteArray>() << QByteArray("CustomUnregisteredType")) << (QList<QByteArray>() << QByteArray("voidSignalCustomUnregisteredTypeArg")) << QMetaMethod::Public @@ -390,7 +394,7 @@ void tst_QMetaMethod::method_data() QTest::newRow("voidInvokableCustomUnregisteredType") << QByteArray("voidInvokableCustomUnregisteredType(CustomUnregisteredType)") << int(QMetaType::Void) << QByteArray("void") - << (QList<int>() << 0) + << (QList<int>() << QMetaType::fromType<CustomUnregisteredType>().id()) << (QList<QByteArray>() << QByteArray("CustomUnregisteredType")) << (QList<QByteArray>() << QByteArray("voidInvokableCustomUnregisteredTypeArg")) << QMetaMethod::Public @@ -399,7 +403,7 @@ void tst_QMetaMethod::method_data() QTest::newRow("voidSlotCustomUnregisteredType") << QByteArray("voidSlotCustomUnregisteredType(CustomUnregisteredType)") << int(QMetaType::Void) << QByteArray("void") - << (QList<int>() << 0) + << (QList<int>() << QMetaType::fromType<CustomUnregisteredType>().id()) << (QList<QByteArray>() << QByteArray("CustomUnregisteredType")) << (QList<QByteArray>() << QByteArray("voidSlotCustomUnregisteredTypeArg")) << QMetaMethod::Public @@ -408,7 +412,7 @@ void tst_QMetaMethod::method_data() QTest::newRow("MethodTestObject(CustomUnregisteredType)") << QByteArray("MethodTestObject(CustomUnregisteredType)") << int(QMetaType::UnknownType) << QByteArray("") - << (QList<int>() << 0) + << (QList<int>() << QMetaType::fromType<CustomUnregisteredType>().id()) << (QList<QByteArray>() << QByteArray("CustomUnregisteredType")) << (QList<QByteArray>() << QByteArray("constructorCustomUnregisteredTypeArg")) << QMetaMethod::Public @@ -770,6 +774,59 @@ void tst_QMetaMethod::gadget() } } +class MyTestClass : public QObject +{ + Q_OBJECT + +public: + MyTestClass() {}; +public Q_SLOTS: + MyGadget doStuff(int, float, MyGadget) {return {};} +Q_SIGNALS: + QObject *mySignal(); +}; + +void tst_QMetaMethod::returnMetaType() +{ + { + QMetaMethod mm = QMetaMethod::fromSignal(&MyTestClass::mySignal); + QCOMPARE(mm.returnMetaType(), QMetaType::fromType<QObject*>()); + } + auto mo = MyTestClass::staticMetaObject; + { + const auto normalized = QMetaObject::normalizedSignature("doStuff(int, float, MyGadget)"); + const int idx = mo.indexOfSlot(normalized); + QMetaMethod mm = mo.method(idx); + QVERIFY(mm.isValid()); + QCOMPARE(mm.returnMetaType(), QMetaType::fromType<MyGadget>()); + } + { + // access of parent class meta methods works, too + const auto normalized = QMetaObject::normalizedSignature("deleteLater()"); + const int idx = mo.indexOfSlot(normalized); + QMetaMethod mm = mo.method(idx); + QVERIFY(mm.isValid()); + QCOMPARE(mm.returnMetaType(), QMetaType::fromType<void>()); + } +} + +void tst_QMetaMethod::parameterMetaType() +{ + auto mo = MyTestClass::staticMetaObject; + const auto normalized = QMetaObject::normalizedSignature("doStuff(int, float, MyGadget)"); + const int idx = mo.indexOfSlot(normalized); + QMetaMethod mm = mo.method(idx); + { + QVERIFY(!mm.parameterMetaType(-1).isValid()); + QVERIFY(!mm.parameterMetaType(3).isValid()); + } + { + QCOMPARE(mm.parameterMetaType(0), QMetaType::fromType<int>()); + QCOMPARE(mm.parameterMetaType(1), QMetaType::fromType<float>()); + QCOMPARE(mm.parameterMetaType(2), QMetaType::fromType<MyGadget>()); + } +} + QTEST_MAIN(tst_QMetaMethod) #include "tst_qmetamethod.moc" diff --git a/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp b/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp index 14719f36f8..b8212ee6cf 100644 --- a/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp +++ b/tests/auto/corelib/kernel/qmetaobjectbuilder/tst_qmetaobjectbuilder.cpp @@ -1391,7 +1391,7 @@ private: }; QMetaObject TestObject::staticMetaObject = { - { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr } + { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr} }; TestObject::TestObject(QObject *parent) diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index b977761995..68d218d87b 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -5278,6 +5278,8 @@ void tst_QObject::connectForwardDeclare() QVERIFY(connect(&ob, &ForwardDeclareArguments::mySignal, &ob, &ForwardDeclareArguments::mySlot, Qt::QueuedConnection)); } +class ForwardDeclared {}; // complete definition for moc + class NoDefaultConstructor { Q_GADGET |