From 0678d7c43c1658d2d2ec984be5844054031649a2 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Sat, 11 Feb 2012 07:02:13 +0100 Subject: Add QMetaType::type(QByteArray) function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QMetaType::type(const char *) requires that the string argument is 0-terminated. This new overload makes it possible to query the type of a string with an explicit length. In particular, QByteArrays constructed by QByteArray::fromRawData(), for example from a substring of a normalized method signature (the "int" part of "mySlot(int"), can now be queried without making a copy of the string. Also, Qt5 meta-objects represent type names as QByteArray literals, which can be fed directly to this new QMetaType::type() overload (no need to call strlen). Change-Id: I60d35aa6bdc0f77e0997f98b0e30e12fd3d5e100 Reviewed-by: Jędrzej Nowacki Reviewed-by: Thiago Macieira --- src/corelib/kernel/qmetatype.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src/corelib/kernel/qmetatype.cpp') diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index 311648c33a..4540e96de6 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -801,7 +801,7 @@ static inline int qMetaTypeStaticType(const char *typeName, int length) { int i = 0; while (types[i].typeName && ((length != types[i].typeNameLength) - || strcmp(typeName, types[i].typeName))) { + || memcmp(typeName, types[i].typeName, length))) { ++i; } return types[i].type; @@ -821,7 +821,7 @@ static int qMetaTypeCustomType_unlocked(const char *typeName, int length) for (int v = 0; v < ct->count(); ++v) { const QCustomTypeInfo &customInfo = ct->at(v); if ((length == customInfo.typeName.size()) - && !strcmp(typeName, customInfo.typeName.constData())) { + && !memcmp(typeName, customInfo.typeName.constData(), length)) { if (customInfo.alias >= 0) return customInfo.alias; return v + QMetaType::User; @@ -1048,9 +1048,8 @@ bool QMetaType::isRegistered(int type) Implementation of QMetaType::type(). */ template -static inline int qMetaTypeTypeImpl(const char *typeName) +static inline int qMetaTypeTypeImpl(const char *typeName, int length) { - int length = qstrlen(typeName); if (!length) return QMetaType::UnknownType; int type = qMetaTypeStaticType(typeName, length); @@ -1080,7 +1079,7 @@ static inline int qMetaTypeTypeImpl(const char *typeName) */ int QMetaType::type(const char *typeName) { - return qMetaTypeTypeImpl(typeName); + return qMetaTypeTypeImpl(typeName, qstrlen(typeName)); } /*! @@ -1092,7 +1091,21 @@ int QMetaType::type(const char *typeName) */ int qMetaTypeTypeInternal(const char *typeName) { - return qMetaTypeTypeImpl(typeName); + return qMetaTypeTypeImpl(typeName, qstrlen(typeName)); +} + +/*! + \since 5.5 + \overload + + Returns a handle to the type called \a typeName, or 0 if there is + no such type. + + \sa isRegistered(), typeName() +*/ +int QMetaType::type(const QT_PREPEND_NAMESPACE(QByteArray) &typeName) +{ + return qMetaTypeTypeImpl(typeName.constData(), typeName.size()); } #ifndef QT_NO_DATASTREAM -- cgit v1.2.3