From f84cb43eda9d583d4cb07a7ed044a21955e89c93 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 28 Mar 2013 14:26:34 +0100 Subject: [PATCH] Add automatic metatype handling for common standard library containers. Change-Id: I7fc6db6ea71026066d3e3fa4bfd5ecd5c96ad067 Reviewed-by: Thiago Macieira Reviewed-by: Olivier Goffart --- src/corelib/kernel/qmetatype.h | 10 +++ .../kernel/qmetatype/tst_qmetatype.cpp | 74 +++++++++++++++---- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 16ea1042a2..bd6e074e0a 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -52,6 +52,10 @@ #endif #include +#include +#include +#include + #ifdef Bool #error qmetatype.h must be included before any header file that defines Bool #endif @@ -865,6 +869,9 @@ QT_FOR_EACH_AUTOMATIC_TEMPLATE_1ARG(Q_DECLARE_METATYPE_TEMPLATE_1ARG_ITER) #undef Q_DECLARE_METATYPE_TEMPLATE_1ARG_ITER +Q_DECLARE_METATYPE_TEMPLATE_1ARG(std::vector) +Q_DECLARE_METATYPE_TEMPLATE_1ARG(std::list) + #define Q_DECLARE_METATYPE_TEMPLATE_2ARG_ITER(TEMPLATENAME, CPPTYPE) \ template CPPTYPE TEMPLATENAME; \ Q_DECLARE_METATYPE_TEMPLATE_2ARG(TEMPLATENAME) @@ -873,6 +880,9 @@ QT_FOR_EACH_AUTOMATIC_TEMPLATE_2ARG(Q_DECLARE_METATYPE_TEMPLATE_2ARG_ITER) #undef Q_DECLARE_METATYPE_TEMPLATE_2ARG_ITER +Q_DECLARE_METATYPE_TEMPLATE_2ARG(std::pair) +Q_DECLARE_METATYPE_TEMPLATE_2ARG(std::map) + #define Q_DECLARE_METATYPE_TEMPLATE_SMART_POINTER_ITER(TEMPLATENAME) \ Q_DECLARE_SMART_POINTER_METATYPE(TEMPLATENAME) diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 77ea39da53..f412ee2970 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -1302,15 +1302,20 @@ Q_DECLARE_METATYPE(MyObjectPtr) void tst_QMetaType::automaticTemplateRegistration() { - { - QList intList; - intList << 42; - QVERIFY(QVariant::fromValue(intList).value >().first() == 42); - QVector > vectorList; - vectorList << intList; - QVERIFY(QVariant::fromValue(vectorList).value > >().first().first() == 42); +#define TEST_SEQUENTIAL_CONTAINER(CONTAINER, VALUE_TYPE) \ + { \ + CONTAINER innerContainer; \ + innerContainer.push_back(42); \ + QVERIFY(*QVariant::fromValue(innerContainer).value >().begin() == 42); \ + QVector > outerContainer; \ + outerContainer << innerContainer; \ + QVERIFY(*QVariant::fromValue(outerContainer).value > >().first().begin() == 42); \ } + TEST_SEQUENTIAL_CONTAINER(QList, int) + TEST_SEQUENTIAL_CONTAINER(std::vector, int) + TEST_SEQUENTIAL_CONTAINER(std::list, int) + { QList bytearrayList; bytearrayList << QByteArray("foo"); @@ -1323,14 +1328,9 @@ void tst_QMetaType::automaticTemplateRegistration() QCOMPARE(::qMetaTypeId(), (int)QMetaType::QVariantList); QCOMPARE(::qMetaTypeId >(), (int)QMetaType::QVariantList); - { - QList variantList; - variantList << 42; - QVERIFY(QVariant::fromValue(variantList).value >().first() == 42); - QVector > vectorList; - vectorList << variantList; - QVERIFY(QVariant::fromValue(vectorList).value > >().first().first() == 42); - } + TEST_SEQUENTIAL_CONTAINER(QList, QVariant) + TEST_SEQUENTIAL_CONTAINER(std::vector, QVariant) + TEST_SEQUENTIAL_CONTAINER(std::list, QVariant) { QList > sharedPointerList; @@ -1394,6 +1394,31 @@ void tst_QMetaType::automaticTemplateRegistration() variantMap.insert(QStringLiteral("4"), 2); QCOMPARE(QVariant::fromValue(variantMap).value().value(QStringLiteral("4")), QVariant(2)); } + { + typedef std::map IntIntMap; + IntIntMap intIntMap; + intIntMap[4] = 2; + QCOMPARE(QVariant::fromValue(intIntMap).value()[4], 2); + } + { + typedef std::map StdIntUIntMap; + StdIntUIntMap intUIntMap; + intUIntMap[4] = 2; + QCOMPARE(QVariant::fromValue(intUIntMap).value()[4], (uint)2); + } + { + typedef std::map StdMapIntCustomObject ; + StdMapIntCustomObject intComparableMap; + CustomObject *o = 0; + intComparableMap[4] = o; + QCOMPARE(QVariant::fromValue(intComparableMap).value()[4], o); + } + { + typedef std::map StdMapStringVariant; + StdMapStringVariant variantMap; + variantMap[QStringLiteral("4")] = 2; + QCOMPARE(QVariant::fromValue(variantMap).value()[QStringLiteral("4")], QVariant(2)); + } { typedef QPair IntIntPair; IntIntPair intIntPair = qMakePair(4, 2); @@ -1411,6 +1436,25 @@ void tst_QMetaType::automaticTemplateRegistration() QCOMPARE(QVariant::fromValue(intComparablePair).value().first, 4); QCOMPARE(QVariant::fromValue(intComparablePair).value().second, m); } + { + typedef std::pair IntIntPair; + IntIntPair intIntPair = std::make_pair(4, 2); + QCOMPARE(QVariant::fromValue(intIntPair).value().first, 4); + QCOMPARE(QVariant::fromValue(intIntPair).value().second, 2); + } + { + typedef std::pair StdIntUIntPair; + StdIntUIntPair intUIntPair = std::make_pair(4, 2); + QCOMPARE(QVariant::fromValue(intUIntPair).value().first, 4); + QCOMPARE(QVariant::fromValue(intUIntPair).value().second, (uint)2); + } + { + typedef std::pair StdIntComparablePair; + CustomQObject* o = 0; + StdIntComparablePair intComparablePair = std::make_pair(4, o); + QCOMPARE(QVariant::fromValue(intComparablePair).value().first, 4); + QCOMPARE(QVariant::fromValue(intComparablePair).value().second, o); + } { typedef QHash IntUnregisteredTypeHash; QVERIFY(qRegisterMetaType("IntUnregisteredTypeHash") > 0);