Add automatic metatype handling for common standard library containers.

Change-Id: I7fc6db6ea71026066d3e3fa4bfd5ecd5c96ad067
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
bb10
Stephen Kelly 2013-03-28 14:26:34 +01:00 committed by The Qt Project
parent e006624a42
commit f84cb43eda
2 changed files with 69 additions and 15 deletions

View File

@ -52,6 +52,10 @@
#endif
#include <new>
#include <vector>
#include <list>
#include <map>
#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 <class T1, class T2> 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)

View File

@ -1302,15 +1302,20 @@ Q_DECLARE_METATYPE(MyObjectPtr)
void tst_QMetaType::automaticTemplateRegistration()
{
{
QList<int> intList;
intList << 42;
QVERIFY(QVariant::fromValue(intList).value<QList<int> >().first() == 42);
QVector<QList<int> > vectorList;
vectorList << intList;
QVERIFY(QVariant::fromValue(vectorList).value<QVector<QList<int> > >().first().first() == 42);
#define TEST_SEQUENTIAL_CONTAINER(CONTAINER, VALUE_TYPE) \
{ \
CONTAINER<VALUE_TYPE> innerContainer; \
innerContainer.push_back(42); \
QVERIFY(*QVariant::fromValue(innerContainer).value<CONTAINER<VALUE_TYPE> >().begin() == 42); \
QVector<CONTAINER<VALUE_TYPE> > outerContainer; \
outerContainer << innerContainer; \
QVERIFY(*QVariant::fromValue(outerContainer).value<QVector<CONTAINER<VALUE_TYPE> > >().first().begin() == 42); \
}
TEST_SEQUENTIAL_CONTAINER(QList, int)
TEST_SEQUENTIAL_CONTAINER(std::vector, int)
TEST_SEQUENTIAL_CONTAINER(std::list, int)
{
QList<QByteArray> bytearrayList;
bytearrayList << QByteArray("foo");
@ -1323,14 +1328,9 @@ void tst_QMetaType::automaticTemplateRegistration()
QCOMPARE(::qMetaTypeId<QVariantList>(), (int)QMetaType::QVariantList);
QCOMPARE(::qMetaTypeId<QList<QVariant> >(), (int)QMetaType::QVariantList);
{
QList<QVariant> variantList;
variantList << 42;
QVERIFY(QVariant::fromValue(variantList).value<QList<QVariant> >().first() == 42);
QVector<QList<QVariant> > vectorList;
vectorList << variantList;
QVERIFY(QVariant::fromValue(vectorList).value<QVector<QList<QVariant> > >().first().first() == 42);
}
TEST_SEQUENTIAL_CONTAINER(QList, QVariant)
TEST_SEQUENTIAL_CONTAINER(std::vector, QVariant)
TEST_SEQUENTIAL_CONTAINER(std::list, QVariant)
{
QList<QSharedPointer<QObject> > sharedPointerList;
@ -1394,6 +1394,31 @@ void tst_QMetaType::automaticTemplateRegistration()
variantMap.insert(QStringLiteral("4"), 2);
QCOMPARE(QVariant::fromValue(variantMap).value<QVariantMap>().value(QStringLiteral("4")), QVariant(2));
}
{
typedef std::map<int, int> IntIntMap;
IntIntMap intIntMap;
intIntMap[4] = 2;
QCOMPARE(QVariant::fromValue(intIntMap).value<IntIntMap>()[4], 2);
}
{
typedef std::map<int, uint> StdIntUIntMap;
StdIntUIntMap intUIntMap;
intUIntMap[4] = 2;
QCOMPARE(QVariant::fromValue(intUIntMap).value<StdIntUIntMap>()[4], (uint)2);
}
{
typedef std::map<int, CustomObject*> StdMapIntCustomObject ;
StdMapIntCustomObject intComparableMap;
CustomObject *o = 0;
intComparableMap[4] = o;
QCOMPARE(QVariant::fromValue(intComparableMap).value<StdMapIntCustomObject >()[4], o);
}
{
typedef std::map<QString, QVariant> StdMapStringVariant;
StdMapStringVariant variantMap;
variantMap[QStringLiteral("4")] = 2;
QCOMPARE(QVariant::fromValue(variantMap).value<StdMapStringVariant>()[QStringLiteral("4")], QVariant(2));
}
{
typedef QPair<int, int> IntIntPair;
IntIntPair intIntPair = qMakePair(4, 2);
@ -1411,6 +1436,25 @@ void tst_QMetaType::automaticTemplateRegistration()
QCOMPARE(QVariant::fromValue(intComparablePair).value<IntComparablePair>().first, 4);
QCOMPARE(QVariant::fromValue(intComparablePair).value<IntComparablePair>().second, m);
}
{
typedef std::pair<int, int> IntIntPair;
IntIntPair intIntPair = std::make_pair(4, 2);
QCOMPARE(QVariant::fromValue(intIntPair).value<IntIntPair>().first, 4);
QCOMPARE(QVariant::fromValue(intIntPair).value<IntIntPair>().second, 2);
}
{
typedef std::pair<int, uint> StdIntUIntPair;
StdIntUIntPair intUIntPair = std::make_pair<int, uint>(4, 2);
QCOMPARE(QVariant::fromValue(intUIntPair).value<StdIntUIntPair>().first, 4);
QCOMPARE(QVariant::fromValue(intUIntPair).value<StdIntUIntPair>().second, (uint)2);
}
{
typedef std::pair<int, CustomQObject*> StdIntComparablePair;
CustomQObject* o = 0;
StdIntComparablePair intComparablePair = std::make_pair(4, o);
QCOMPARE(QVariant::fromValue(intComparablePair).value<StdIntComparablePair>().first, 4);
QCOMPARE(QVariant::fromValue(intComparablePair).value<StdIntComparablePair>().second, o);
}
{
typedef QHash<int, UnregisteredType> IntUnregisteredTypeHash;
QVERIFY(qRegisterMetaType<IntUnregisteredTypeHash>("IntUnregisteredTypeHash") > 0);