summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2020-06-29 14:26:36 +0200
committerLars Knoll <lars.knoll@qt.io>2020-07-08 14:13:59 +0200
commit986d89c2eefd37ee0da8e07d7794716000608610 (patch)
tree5e709f713cacdf0bfac7ddb95d14e1ed8553a9f3 /tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
parent0e2cfdedf261a9d29d7466bd26545549479d9f8a (diff)
Automatically register comparison operators in QMetaType
This removes the fully manual registration of comparison operators in QMetaType and replaces it with an automatic registration through Q_DECLARE_METATYPE(). [ChangeLog][QMetaType] The QMetaType::registerComparator() and QMetaType::registerEqualsComparator() have been removed. Q_DECLARE_METATYPE() now automatically registers any operator==() and/or operator<() for a type visible where it is used on that type, as part of declaring its meta-type. Change-Id: I3df451b652b735c093533838bf32f3cc785439f8 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp')
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp69
1 files changed, 23 insertions, 46 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
index 22d481f5e3..35d3f433ec 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
@@ -411,6 +411,8 @@ void tst_QMetaType::registerGadget(const char *name, const QList<GadgetPropertyT
[](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 };
QMetaType gadgetMetaType(typeInfo);
dynamicGadgetProperties->m_metatype = gadgetMetaType;
@@ -1277,7 +1279,7 @@ 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 };
QMetaType metatype(typeInfo);
dynamicGadgetProperties->m_metatype = metatype;
int podTypeId = metatype.id();
@@ -2252,6 +2254,9 @@ bool operator==(const CustomEqualsOnlyType &lhs, const CustomEqualsOnlyType &rhs
bool operator!=(const CustomEqualsOnlyType &lhs, const CustomEqualsOnlyType &rhs)
{ return !operator==(lhs, rhs); }
+static_assert(QTypeTraits::has_operator_equal_v<CustomEqualsOnlyType>);
+static_assert(!QTypeTraits::has_operator_less_than_v<CustomEqualsOnlyType>);
+
Q_DECLARE_METATYPE(CustomConvertibleType);
Q_DECLARE_METATYPE(CustomConvertibleType2);
Q_DECLARE_METATYPE(CustomDebugStreamableType);
@@ -2497,9 +2502,7 @@ void tst_QMetaType::convertCustomType()
void tst_QMetaType::compareCustomEqualOnlyType()
{
- int metaTypeId = qRegisterMetaType<CustomEqualsOnlyType>();
- QMetaType::registerEqualsComparator<CustomEqualsOnlyType>();
- int result;
+ QMetaType type = QMetaType::fromType<CustomEqualsOnlyType>();
CustomEqualsOnlyType val50(50);
CustomEqualsOnlyType val100(100);
@@ -2517,52 +2520,26 @@ void tst_QMetaType::compareCustomEqualOnlyType()
QCOMPARE(variant100, variant100);
// check QMetaType::compare works/doesn't crash for equals only comparators
- bool wasSuccess = QMetaType::compare(variant50.constData(), variant50.constData(),
- metaTypeId, &result);
- QCOMPARE(result, 0);
- QVERIFY(wasSuccess);
- wasSuccess = QMetaType::compare(variant100.constData(), variant100x.constData(),
- metaTypeId, &result);
- QCOMPARE(result, 0);
- QVERIFY(wasSuccess);
-
- wasSuccess = QMetaType::compare(variant50.constData(), variant100.constData(),
- metaTypeId, &result);
- QVERIFY(!wasSuccess);
-
- // check QMetaType::equals works for equals only comparator
- wasSuccess = QMetaType::equals(variant50.constData(), variant50.constData(),
- metaTypeId, &result);
- QCOMPARE(result, 0);
- QVERIFY(wasSuccess);
- wasSuccess = QMetaType::equals(variant100.constData(), variant100.constData(),
- metaTypeId, &result);
- QCOMPARE(result, 0);
- QVERIFY(wasSuccess);
- wasSuccess = QMetaType::equals(variant100x.constData(), variant100x.constData(),
- metaTypeId, &result);
- QCOMPARE(result, 0);
- QVERIFY(wasSuccess);
- wasSuccess = QMetaType::equals(variant100.constData(), variant100x.constData(),
- metaTypeId, &result);
- QCOMPARE(result, 0);
- QVERIFY(wasSuccess);
- wasSuccess = QMetaType::equals(variant50.constData(), variant100.constData(),
- metaTypeId, &result);
- QCOMPARE(result, -1);
- QVERIFY(wasSuccess);
- wasSuccess = QMetaType::equals(variant50.constData(), variant100x.constData(),
- metaTypeId, &result);
- QCOMPARE(result, -1);
- QVERIFY(wasSuccess);
+ auto cmp = type.compare(variant50.constData(), variant50.constData());
+ QVERIFY(!cmp);
+ bool equals = type.equals(variant50.constData(), variant50.constData());
+ QVERIFY(equals);
+
+ cmp = type.compare(variant100.constData(), variant100x.constData());
+ QVERIFY(!cmp);
+ equals = type.equals(variant100.constData(), variant100x.constData());
+ QVERIFY(equals);
+
+ cmp = type.compare(variant50.constData(), variant100.constData());
+ QVERIFY(!cmp);
+ equals = type.equals(variant50.constData(), variant100.constData());
+ QVERIFY(!equals);
//check QMetaType::equals for type w/o equals comparator being registered
CustomMovable movable1;
CustomMovable movable2;
- wasSuccess = QMetaType::equals(&movable1, &movable2,
- qRegisterMetaType<CustomMovable>(), &result);
- QVERIFY(!wasSuccess);
-
+ type = QMetaType::fromType<CustomMovable>();
+ equals = type.equals(&movable1, &movable2);
}
struct MessageHandlerCustom : public MessageHandler