diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-07-19 12:31:07 -0700 |
---|---|---|
committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-07-26 20:12:30 -0700 |
commit | 74fac865cf7c55d3afba1043072ba6cc932d161d (patch) | |
tree | 4bb096785a07fb5986c2b1311de66fbd1a32c349 /tests/auto | |
parent | b1d9331c156b7ff5c724600eee21ac50f4565868 (diff) |
QMetaType: add registerType() and qRegisterMetaType(QMetaType)
This also rewrites QMetaType::id() on top of the helper, with the benefit
of calling a member static function, so QMetaType doesn't need to be
spilled onto the stack. In some upcoming changes I need to ensure that
QMetaTypes are registered so they can be found by name and I'd like to
have a dedicated function name for that, instead of calling .id().
Since I needed to add docs for the new function, I've updated for the
old one too.
[ChangeLog][QMetaType] Added QMetaType::registerType() and an overload
of qRegisterMetaType() taking QMetaType (the two functions do the same
thing). These two functions ensure a given QMetaType is registered with
the Qt global registry, so they can be found by name later. Using
qRegisterMetaType<T>() also accomplishes the same thing, but is slightly
better for completely generic code because it will avoid emitting the
registration for built-in types.
Change-Id: I3859764fed084846bcb0fffd170351d606034c22
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 9a3ab830cb..c25c5c6b16 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -1424,20 +1424,27 @@ void tst_QMetaType::typedefs() QCOMPARE(QMetaType::type("WhityDouble"), ::qMetaTypeId<WhityDouble>()); } +struct RegisterTypeType {}; + void tst_QMetaType::registerType() { // Built-in QCOMPARE(qRegisterMetaType<QString>("QString"), int(QMetaType::QString)); QCOMPARE(qRegisterMetaType<QString>("QString"), int(QMetaType::QString)); + qRegisterMetaType(QMetaType::fromType<QString>()); // Custom int fooId = qRegisterMetaType<TestSpace::Foo>("TestSpace::Foo"); QVERIFY(fooId >= int(QMetaType::User)); QCOMPARE(qRegisterMetaType<TestSpace::Foo>("TestSpace::Foo"), fooId); + qRegisterMetaType(QMetaType::fromType<TestSpace::Foo>()); int movableId = qRegisterMetaType<CustomMovable>("CustomMovable"); QVERIFY(movableId >= int(QMetaType::User)); QCOMPARE(qRegisterMetaType<CustomMovable>("CustomMovable"), movableId); + qRegisterMetaType(QMetaType::fromType<CustomMovable>()); + + // Aliases are deprecated // Alias to built-in typedef QString MyString; @@ -1460,6 +1467,23 @@ void tst_QMetaType::registerType() QCOMPARE(qRegisterMetaType<MyFoo>("MyFoo"), fooId); QCOMPARE(QMetaType::type("MyFoo"), fooId); + + // this portion of the test can only be run once + static bool typeWasRegistered = false; + if (!typeWasRegistered) { + QMetaType mt = QMetaType::fromType<RegisterTypeType>(); + QVERIFY(mt.isValid()); + QCOMPARE_NE(mt.name(), nullptr); + + QVERIFY(!mt.isRegistered()); + QVERIFY(!QMetaType::fromName(mt.name()).isValid()); + + QCOMPARE_GT(qRegisterMetaType(mt), 0); + typeWasRegistered = true; + + QVERIFY(mt.isRegistered()); + QVERIFY(QMetaType::fromName(mt.name()).isValid()); + } } class IsRegisteredDummyType { }; |