summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qmetatype.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/kernel/qmetatype.cpp')
-rw-r--r--src/corelib/kernel/qmetatype.cpp118
1 files changed, 83 insertions, 35 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp
index 53b22958c3..2756dd5241 100644
--- a/src/corelib/kernel/qmetatype.cpp
+++ b/src/corelib/kernel/qmetatype.cpp
@@ -125,16 +125,16 @@ struct DefinedTypesFilter {
This example shows a typical use case of Q_DECLARE_METATYPE():
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 0
+ \snippet code/src_corelib_kernel_qmetatype.cpp 0
If \c MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro
has to be outside the namespace:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 1
+ \snippet code/src_corelib_kernel_qmetatype.cpp 1
Since \c{MyStruct} is now known to QMetaType, it can be used in QVariant:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 2
+ \snippet code/src_corelib_kernel_qmetatype.cpp 2
\sa qRegisterMetaType()
*/
@@ -220,21 +220,13 @@ struct DefinedTypesFilter {
\value User Base value for user types
\value UnknownType This is an invalid type id. It is returned from QMetaType for types that are not registered
- \omitvalue FirstGuiType
- \omitvalue FirstWidgetsType
- \omitvalue LastCoreType
- \omitvalue LastGuiType
- \omitvalue LastWidgetsType
- \omitvalue QReal
- \omitvalue HighestInternalId
-
Additional types can be registered using Q_DECLARE_METATYPE().
\sa type(), typeName()
*/
/*!
- \enum QMetaType::TypeFlags
+ \enum QMetaType::TypeFlag
The enum describes attributes of a type supported by QMetaType.
@@ -265,7 +257,7 @@ struct DefinedTypesFilter {
The following code allocates and destructs an instance of
\c{MyClass}:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 3
+ \snippet code/src_corelib_kernel_qmetatype.cpp 3
If we want the stream operators \c operator<<() and \c
operator>>() to work on QVariant objects that store custom types,
@@ -449,16 +441,37 @@ int QMetaType::registerType(const char *typeName, Deleter deleter,
int size, TypeFlags flags, const QMetaObject *metaObject)
{
Q_UNUSED(metaObject);
- QVector<QCustomTypeInfo> *ct = customTypes();
- if (!ct || !typeName || !deleter || !creator || !destructor || !constructor)
- return -1;
-
#ifdef QT_NO_QOBJECT
NS(QByteArray) normalizedTypeName = typeName;
#else
NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
#endif
+ return registerNormalizedType(normalizedTypeName, deleter, creator, destructor, constructor, size, flags, metaObject);
+}
+
+
+/*! \internal
+ \since 5.0
+
+ Registers a user type for marshalling, with \a normalizedTypeName, a \a
+ deleter, a \a creator, a \a destructor, a \a constructor, and
+ a \a size. Returns the type's handle, or -1 if the type could
+ not be registered. Note that normalizedTypeName is not checked for
+ conformance with Qt's normalized format, so it must already
+ conform.
+ */
+int QMetaType::registerNormalizedType(const NS(QByteArray) &normalizedTypeName, Deleter deleter,
+ Creator creator,
+ Destructor destructor,
+ Constructor constructor,
+ int size, TypeFlags flags, const QMetaObject *metaObject)
+{
+ Q_UNUSED(metaObject);
+ QVector<QCustomTypeInfo> *ct = customTypes();
+ if (!ct || normalizedTypeName.isEmpty() || !deleter || !creator || !destructor || !constructor)
+ return -1;
+
int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
normalizedTypeName.size());
@@ -521,16 +534,28 @@ int QMetaType::registerType(const char *typeName, Deleter deleter,
*/
int QMetaType::registerTypedef(const char* typeName, int aliasId)
{
- QVector<QCustomTypeInfo> *ct = customTypes();
- if (!ct || !typeName)
- return -1;
-
#ifdef QT_NO_QOBJECT
NS(QByteArray) normalizedTypeName = typeName;
#else
NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
#endif
+ return registerNormalizedTypedef(normalizedTypeName, aliasId);
+}
+
+/*! \internal
+ \since 5.0
+
+ Registers a user type for marshalling, as an alias of another type (typedef).
+ Note that normalizedTypeName is not checked for conformance with Qt's normalized format,
+ so it must already conform.
+*/
+int QMetaType::registerNormalizedTypedef(const NS(QByteArray) &normalizedTypeName, int aliasId)
+{
+ QVector<QCustomTypeInfo> *ct = customTypes();
+ if (!ct || normalizedTypeName.isEmpty())
+ return -1;
+
int idx = qMetaTypeStaticType(normalizedTypeName.constData(),
normalizedTypeName.size());
@@ -581,26 +606,26 @@ bool QMetaType::isRegistered(int type)
}
/*!
- Returns a handle to the type called \a typeName, or QMetaType::UnknownType if there is
- no such type.
+ \internal
- \sa isRegistered(), typeName(), Type
+ Implementation of QMetaType::type().
*/
-int QMetaType::type(const char *typeName)
+template <int tryNormalizedType>
+static inline int qMetaTypeTypeImpl(const char *typeName)
{
int length = qstrlen(typeName);
if (!length)
- return UnknownType;
+ return QMetaType::UnknownType;
int type = qMetaTypeStaticType(typeName, length);
- if (type == UnknownType) {
+ if (type == QMetaType::UnknownType) {
QReadLocker locker(customTypesLock());
type = qMetaTypeCustomType_unlocked(typeName, length);
#ifndef QT_NO_QOBJECT
- if (type == UnknownType) {
+ if ((type == QMetaType::UnknownType) && tryNormalizedType) {
const NS(QByteArray) normalizedTypeName = QMetaObject::normalizedType(typeName);
type = qMetaTypeStaticType(normalizedTypeName.constData(),
normalizedTypeName.size());
- if (type == UnknownType) {
+ if (type == QMetaType::UnknownType) {
type = qMetaTypeCustomType_unlocked(normalizedTypeName.constData(),
normalizedTypeName.size());
}
@@ -610,6 +635,29 @@ int QMetaType::type(const char *typeName)
return type;
}
+/*!
+ Returns a handle to the type called \a typeName, or QMetaType::UnknownType if there is
+ no such type.
+
+ \sa isRegistered(), typeName(), Type
+*/
+int QMetaType::type(const char *typeName)
+{
+ return qMetaTypeTypeImpl</*tryNormalizedType=*/true>(typeName);
+}
+
+/*!
+ \a internal
+
+ Similar to QMetaType::type(); the only difference is that this function
+ doesn't attempt to normalize the type name (i.e., the lookup will fail
+ for type names in non-normalized form).
+*/
+int qMetaTypeTypeInternal(const char *typeName)
+{
+ return qMetaTypeTypeImpl</*tryNormalizedType=*/false>(typeName);
+}
+
#ifndef QT_NO_DATASTREAM
/*!
Writes the object pointed to by \a data with the ID \a type to
@@ -1486,12 +1534,12 @@ QMetaType::TypeFlags QMetaType::typeFlags(int type)
This example registers the class \c{MyClass}:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 4
+ \snippet code/src_corelib_kernel_qmetatype.cpp 4
This function is useful to register typedefs so they can be used
by QMetaProperty, or in QueuedConnections
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 9
+ \snippet code/src_corelib_kernel_qmetatype.cpp 9
\sa qRegisterMetaTypeStreamOperators(), QMetaType::isRegistered(),
Q_DECLARE_METATYPE()
@@ -1509,11 +1557,11 @@ QMetaType::TypeFlags QMetaType::typeFlags(int type)
QMetaType::save(). These functions are used when streaming a
QVariant.
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 5
+ \snippet code/src_corelib_kernel_qmetatype.cpp 5
The stream operators should have the following signatures:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 6
+ \snippet code/src_corelib_kernel_qmetatype.cpp 6
\sa qRegisterMetaType(), QMetaType::isRegistered(), Q_DECLARE_METATYPE()
*/
@@ -1548,7 +1596,7 @@ QMetaType::TypeFlags QMetaType::typeFlags(int type)
Example:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 7
+ \snippet code/src_corelib_kernel_qmetatype.cpp 7
To use the type \c T in QVariant, using Q_DECLARE_METATYPE() is
sufficient. To use the type \c T in queued signal and slot connections,
@@ -1574,7 +1622,7 @@ QMetaType::TypeFlags QMetaType::typeFlags(int type)
Typical usage:
- \snippet doc/src/snippets/code/src_corelib_kernel_qmetatype.cpp 8
+ \snippet code/src_corelib_kernel_qmetatype.cpp 8
QMetaType::type() returns the same ID as qMetaTypeId(), but does
a lookup at runtime based on the name of the type.