Avoid a type name normalization during auto-registration.

Containers are auto-registered and use normalized names.

Change-Id: Id65c3940401f69436929220e1f6a971135e147ed
Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>
This commit is contained in:
Jędrzej Nowacki 2012-05-22 13:48:22 +02:00 committed by Qt by Nokia
parent 778e2b3bf5
commit 3df316e961
2 changed files with 51 additions and 7 deletions

View File

@ -523,6 +523,9 @@ int qRegisterNormalizedMetaType(const QT_PREPEND_NAMESPACE(QByteArray) &normaliz
#endif #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<T>::qt_metatype_id(); const int typedefOf = dummy ? -1 : QtPrivate::QMetaTypeIdHelper<T>::qt_metatype_id();
if (typedefOf != -1) if (typedefOf != -1)
return QMetaType::registerNormalizedTypedef(normalizedTypeName, typedefOf); return QMetaType::registerNormalizedTypedef(normalizedTypeName, typedefOf);
@ -604,8 +607,9 @@ struct QMetaTypeIdQObject<T*, /* isPointerToTypeDerivedFromQObject */ true>
const int len = int(strlen(T::staticMetaObject.className())); const int len = int(strlen(T::staticMetaObject.className()));
QVarLengthArray<char, 16> classNameStar; QVarLengthArray<char, 16> classNameStar;
classNameStar.append(T::staticMetaObject.className(), len); classNameStar.append(T::staticMetaObject.className(), len);
classNameStar.append("*\0", 2); classNameStar.append('*');
metatype_id.storeRelease(qRegisterMetaType<T*>(classNameStar.constData(), metatype_id.storeRelease(qRegisterNormalizedMetaType<T*>( \
QByteArray(classNameStar.constData(), classNameStar.size()),
reinterpret_cast<T**>(quintptr(-1)))); reinterpret_cast<T**>(quintptr(-1))));
} }
return metatype_id.loadAcquire(); return metatype_id.loadAcquire();
@ -691,9 +695,21 @@ struct QMetaTypeId< SINGLE_ARG_TEMPLATE<T> > \
static int qt_metatype_id() \ static int qt_metatype_id() \
{ \ { \
static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
if (!metatype_id.load()) \ if (!metatype_id.load()) { \
metatype_id.storeRelease(qRegisterMetaType< SINGLE_ARG_TEMPLATE<T> >( QByteArray(QByteArray(#SINGLE_ARG_TEMPLATE "<") + QMetaType::typeName(qMetaTypeId<T>()) + ">").constData(), \ QVarLengthArray<char, 24> name; \
name.append(#SINGLE_ARG_TEMPLATE, sizeof(#SINGLE_ARG_TEMPLATE) - 1); \
name.append('<'); \
const char *tName = QMetaType::typeName(qMetaTypeId<T>()); \
Q_ASSERT(tName); \
name.append(tName, strlen(tName)); \
if (name.last() == '>') \
name.append(' '); \
name.append('>'); \
metatype_id.storeRelease( \
qRegisterNormalizedMetaType< SINGLE_ARG_TEMPLATE<T> >( \
QByteArray(name.constData(), name.size()), \
reinterpret_cast< SINGLE_ARG_TEMPLATE<T> *>(quintptr(-1)))); \ reinterpret_cast< SINGLE_ARG_TEMPLATE<T> *>(quintptr(-1)))); \
} \
return metatype_id.loadAcquire(); \ return metatype_id.loadAcquire(); \
} \ } \
}; };
@ -708,9 +724,25 @@ struct QMetaTypeId< DOUBLE_ARG_TEMPLATE<T, U> > \
static int qt_metatype_id() \ static int qt_metatype_id() \
{ \ { \
static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \ static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0); \
if (!metatype_id.load()) \ if (!metatype_id.load()) {\
metatype_id.storeRelease(qRegisterMetaType< DOUBLE_ARG_TEMPLATE<T, U> >( QByteArray(QByteArray(#DOUBLE_ARG_TEMPLATE "<") + QMetaType::typeName(qMetaTypeId<T>()) + ", " + QMetaType::typeName(qMetaTypeId<U>()) + ">").constData(), \ QVarLengthArray<char, 24> name; \
name.append(#DOUBLE_ARG_TEMPLATE, sizeof(#DOUBLE_ARG_TEMPLATE) - 1); \
name.append('<'); \
const char *tName = QMetaType::typeName(qMetaTypeId<T>()); \
Q_ASSERT(tName); \
name.append(tName, strlen(tName)); \
name.append(','); \
const char *uName = QMetaType::typeName(qMetaTypeId<U>()); \
Q_ASSERT(uName); \
name.append(uName, strlen(uName)); \
if (name.last() == '>') \
name.append(' '); \
name.append('>'); \
metatype_id.storeRelease(\
qRegisterNormalizedMetaType< DOUBLE_ARG_TEMPLATE<T, U> >(\
QByteArray(name.constData(), name.size()), \
reinterpret_cast< DOUBLE_ARG_TEMPLATE<T, U> *>(quintptr(-1)))); \ reinterpret_cast< DOUBLE_ARG_TEMPLATE<T, U> *>(quintptr(-1)))); \
}\
return metatype_id.loadAcquire(); \ return metatype_id.loadAcquire(); \
} \ } \
}; };

View File

@ -348,6 +348,13 @@ void tst_QMetaType::typeName_data()
QTest::newRow("-1") << QMetaType::Type(-1) << QString(); QTest::newRow("-1") << QMetaType::Type(-1) << QString();
QTest::newRow("-124125534") << QMetaType::Type(-124125534) << QString(); QTest::newRow("-124125534") << QMetaType::Type(-124125534) << QString();
QTest::newRow("124125534") << QMetaType::Type(124125534) << QString(); QTest::newRow("124125534") << QMetaType::Type(124125534) << QString();
// automatic registration
QTest::newRow("QList<int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QList<int> >()) << QString::fromLatin1("QList<int>");
QTest::newRow("QHash<int,int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QHash<int, int> >()) << QString::fromLatin1("QHash<int,int>");
QTest::newRow("QMap<int,int>") << static_cast<QMetaType::Type>(::qMetaTypeId<QMap<int, int> >()) << QString::fromLatin1("QMap<int,int>");
QTest::newRow("QVector<QList<int>>") << static_cast<QMetaType::Type>(::qMetaTypeId<QVector<QList<int> > >()) << QString::fromLatin1("QVector<QList<int> >");
QTest::newRow("QVector<QMap<int,int>>") << static_cast<QMetaType::Type>(::qMetaTypeId<QVector<QMap<int, int> > >()) << QString::fromLatin1("QVector<QMap<int,int> >");
} }
void tst_QMetaType::typeName() void tst_QMetaType::typeName()
@ -355,7 +362,10 @@ void tst_QMetaType::typeName()
QFETCH(QMetaType::Type, aType); QFETCH(QMetaType::Type, aType);
QFETCH(QString, aTypeName); 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) \ #define FOR_EACH_PRIMITIVE_METATYPE(F) \
@ -1392,6 +1402,8 @@ void tst_QMetaType::automaticTemplateRegistration()
tn += it->trimmed(); \ tn += it->trimmed(); \
} \ } \
} \ } \
if (tn.endsWith('>')) \
tn += ' '; \
tn += ">"; \ tn += ">"; \
const int type = QMetaType::type(tn); \ const int type = QMetaType::type(tn); \
const int expectedType = ::qMetaTypeId<CONTAINER< __VA_ARGS__ > >(); \ const int expectedType = ::qMetaTypeId<CONTAINER< __VA_ARGS__ > >(); \