From 3df316e9615027d9384cef81ac6cbfa2f9f81f01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C4=99drzej=20Nowacki?= Date: Tue, 22 May 2012 13:48:22 +0200 Subject: [PATCH] Avoid a type name normalization during auto-registration. Containers are auto-registered and use normalized names. Change-Id: Id65c3940401f69436929220e1f6a971135e147ed Reviewed-by: Stephen Kelly --- src/corelib/kernel/qmetatype.h | 44 +++++++++++++++++++--- .../corelib/kernel/qmetatype/tst_qmetatype.cpp | 14 ++++++- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 7470254..005d2b9 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -523,6 +523,9 @@ int qRegisterNormalizedMetaType(const QT_PREPEND_NAMESPACE(QByteArray) &normaliz #endif ) { +#ifndef QT_NO_QOBJECT + Q_ASSERT_X(normalizedTypeName == QMetaObject::normalizedType(normalizedTypeName.constData()), "qRegisterNormalizedMetaType", "qRegisterNormalizedMetaType was called with a not normalized type name, please call qRegisterMetaType instead."); +#endif const int typedefOf = dummy ? -1 : QtPrivate::QMetaTypeIdHelper::qt_metatype_id(); if (typedefOf != -1) return QMetaType::registerNormalizedTypedef(normalizedTypeName, typedefOf); @@ -604,8 +607,9 @@ struct QMetaTypeIdQObject const int len = int(strlen(T::staticMetaObject.className())); QVarLengthArray classNameStar; classNameStar.append(T::staticMetaObject.className(), len); - classNameStar.append("*\0", 2); - metatype_id.storeRelease(qRegisterMetaType(classNameStar.constData(), + classNameStar.append('*'); + metatype_id.storeRelease(qRegisterNormalizedMetaType( \ + QByteArray(classNameStar.constData(), classNameStar.size()), reinterpret_cast(quintptr(-1)))); } return metatype_id.loadAcquire(); @@ -691,9 +695,21 @@ struct QMetaTypeId< SINGLE_ARG_TEMPLATE > \ static int qt_metatype_id() \ { \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ - if (!metatype_id.load()) \ - metatype_id.storeRelease(qRegisterMetaType< SINGLE_ARG_TEMPLATE >( QByteArray(QByteArray(#SINGLE_ARG_TEMPLATE "<") + QMetaType::typeName(qMetaTypeId()) + ">").constData(), \ + if (!metatype_id.load()) { \ + QVarLengthArray name; \ + name.append(#SINGLE_ARG_TEMPLATE, sizeof(#SINGLE_ARG_TEMPLATE) - 1); \ + name.append('<'); \ + const char *tName = QMetaType::typeName(qMetaTypeId()); \ + Q_ASSERT(tName); \ + name.append(tName, strlen(tName)); \ + if (name.last() == '>') \ + name.append(' '); \ + name.append('>'); \ + metatype_id.storeRelease( \ + qRegisterNormalizedMetaType< SINGLE_ARG_TEMPLATE >( \ + QByteArray(name.constData(), name.size()), \ reinterpret_cast< SINGLE_ARG_TEMPLATE *>(quintptr(-1)))); \ + } \ return metatype_id.loadAcquire(); \ } \ }; @@ -708,9 +724,25 @@ struct QMetaTypeId< DOUBLE_ARG_TEMPLATE > \ static int qt_metatype_id() \ { \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ - if (!metatype_id.load()) \ - metatype_id.storeRelease(qRegisterMetaType< DOUBLE_ARG_TEMPLATE >( QByteArray(QByteArray(#DOUBLE_ARG_TEMPLATE "<") + QMetaType::typeName(qMetaTypeId()) + ", " + QMetaType::typeName(qMetaTypeId()) + ">").constData(), \ + if (!metatype_id.load()) {\ + QVarLengthArray name; \ + name.append(#DOUBLE_ARG_TEMPLATE, sizeof(#DOUBLE_ARG_TEMPLATE) - 1); \ + name.append('<'); \ + const char *tName = QMetaType::typeName(qMetaTypeId()); \ + Q_ASSERT(tName); \ + name.append(tName, strlen(tName)); \ + name.append(','); \ + const char *uName = QMetaType::typeName(qMetaTypeId()); \ + Q_ASSERT(uName); \ + name.append(uName, strlen(uName)); \ + if (name.last() == '>') \ + name.append(' '); \ + name.append('>'); \ + metatype_id.storeRelease(\ + qRegisterNormalizedMetaType< DOUBLE_ARG_TEMPLATE >(\ + QByteArray(name.constData(), name.size()), \ reinterpret_cast< DOUBLE_ARG_TEMPLATE *>(quintptr(-1)))); \ + }\ return metatype_id.loadAcquire(); \ } \ }; diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index d82f756..da8c182 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -348,6 +348,13 @@ void tst_QMetaType::typeName_data() QTest::newRow("-1") << QMetaType::Type(-1) << QString(); QTest::newRow("-124125534") << QMetaType::Type(-124125534) << QString(); QTest::newRow("124125534") << QMetaType::Type(124125534) << QString(); + + // automatic registration + QTest::newRow("QList") << static_cast(::qMetaTypeId >()) << QString::fromLatin1("QList"); + QTest::newRow("QHash") << static_cast(::qMetaTypeId >()) << QString::fromLatin1("QHash"); + QTest::newRow("QMap") << static_cast(::qMetaTypeId >()) << QString::fromLatin1("QMap"); + QTest::newRow("QVector>") << static_cast(::qMetaTypeId > >()) << QString::fromLatin1("QVector >"); + QTest::newRow("QVector>") << static_cast(::qMetaTypeId > >()) << QString::fromLatin1("QVector >"); } void tst_QMetaType::typeName() @@ -355,7 +362,10 @@ void tst_QMetaType::typeName() QFETCH(QMetaType::Type, aType); QFETCH(QString, aTypeName); - QCOMPARE(QString::fromLatin1(QMetaType::typeName(aType)), aTypeName); + QString name = QString::fromLatin1(QMetaType::typeName(aType)); + + QCOMPARE(name, aTypeName); + QCOMPARE(name.toLatin1(), QMetaObject::normalizedType(name.toLatin1().constData())); } #define FOR_EACH_PRIMITIVE_METATYPE(F) \ @@ -1392,6 +1402,8 @@ void tst_QMetaType::automaticTemplateRegistration() tn += it->trimmed(); \ } \ } \ + if (tn.endsWith('>')) \ + tn += ' '; \ tn += ">"; \ const int type = QMetaType::type(tn); \ const int expectedType = ::qMetaTypeId >(); \ -- 2.7.4