diff options
-rw-r--r-- | src/corelib/kernel/qmetatype.cpp | 12 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype.h | 72 | ||||
-rw-r--r-- | src/corelib/kernel/qmetatype_p.h | 12 | ||||
-rw-r--r-- | tests/auto/other/compiler/tst_compiler.cpp | 2 |
4 files changed, 80 insertions, 18 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index dc0fb73603..6194f20912 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -548,7 +548,9 @@ static int qMetaTypeCustomType_unlocked(const char *typeName, int length) int QMetaType::registerType(const char *typeName, Deleter deleter, Creator creator) { - return registerType(typeName, deleter, creator, qMetaTypeDestructHelper<void>, qMetaTypeConstructHelper<void>, 0, TypeFlags(), 0); + return registerType(typeName, deleter, creator, + QtMetaTypePrivate::QMetaTypeFunctionHelper<void>::Destruct, + QtMetaTypePrivate::QMetaTypeFunctionHelper<void>::Construct, 0, TypeFlags(), 0); } /*! @@ -1233,7 +1235,7 @@ class TypeCreator { struct CreatorImpl { static void *Create(const int /* type */, const void *copy) { - // Using qMetaTypeCreateHelper<T> adds function call cost, even if it is a template (gcc). + // Using QMetaTypeFunctionHelper<T>::Create adds function call cost, even if it is a template (gcc). // This "copy" check is moved out from the switcher by compiler (at least by gcc) return copy ? new T(*static_cast<const T*>(copy)) : new T(); } @@ -1298,7 +1300,7 @@ namespace { class TypeDestroyer { template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> struct DestroyerImpl { - static void Destroy(const int /* type */, void *where) { qMetaTypeDeleteHelper<T>(where); } + static void Destroy(const int /* type */, void *where) { QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Delete(where); } }; template<typename T> struct DestroyerImpl<T, /* IsAcceptedType = */ false> { @@ -1364,7 +1366,7 @@ namespace { class TypeConstructor { template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> struct ConstructorImpl { - static void *Construct(const int /*type*/, void *where, const void *copy) { return qMetaTypeConstructHelper<T>(where, copy); } + static void *Construct(const int /*type*/, void *where, const void *copy) { return QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Construct(where, copy); } }; template<typename T> struct ConstructorImpl<T, /* IsAcceptedType = */ false> { @@ -1452,7 +1454,7 @@ namespace { class TypeDestructor { template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted> struct DestructorImpl { - static void Destruct(const int /* type */, void *where) { qMetaTypeDestructHelper<T>(where); } + static void Destruct(const int /* type */, void *where) { QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Destruct(where); } }; template<typename T> struct DestructorImpl<T, /* IsAcceptedType = */ false> { diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 197537972e..997f61e281 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -374,6 +374,63 @@ void qMetaTypeDeleteHelper(void *t) } template <> inline void qMetaTypeDeleteHelper<void>(void *) {} +namespace QtMetaTypePrivate { +template <typename T, bool Accepted = true> +struct QMetaTypeFunctionHelper { + static void Delete(void *t) + { + delete static_cast<T*>(t); + } + + static void *Create(const void *t) + { + if (t) + return new T(*static_cast<const T*>(t)); + return new T(); + } + + static void Destruct(void *t) + { + Q_UNUSED(t) // Silence MSVC that warns for POD types. + static_cast<T*>(t)->~T(); + } + + static void *Construct(void *where, const void *t) + { + if (t) + return new (where) T(*static_cast<const T*>(t)); + return new (where) T; + } +#ifndef QT_NO_DATASTREAM + static void Save(QDataStream &stream, const void *t) + { + stream << *static_cast<const T*>(t); + } + + static void Load(QDataStream &stream, void *t) + { + stream >> *static_cast<T*>(t); + } +#endif // QT_NO_DATASTREAM +}; + +template <typename T> +struct QMetaTypeFunctionHelper<T, /* Accepted */ false> { + static void Delete(void *) {} + static void *Create(const void *) { return 0; } + static void Destruct(void *) {} + static void *Construct(void *, const void *) { return 0; } +#ifndef QT_NO_DATASTREAM + static void Save(QDataStream &, const void *) {} + static void Load(QDataStream &, void *) {} +#endif // QT_NO_DATASTREAM +}; +template <> +struct QMetaTypeFunctionHelper<void, /* Accepted */ true> + : public QMetaTypeFunctionHelper<void, /* Accepted */ false> +{}; +} + template <typename T> void *qMetaTypeCreateHelper(const void *t) { @@ -575,10 +632,11 @@ int qRegisterNormalizedMetaType(const QT_PREPEND_NAMESPACE(QByteArray) &normaliz return QMetaType::registerNormalizedTypedef(normalizedTypeName, typedefOf); QMetaType::TypeFlags flags(QtPrivate::QMetaTypeTypeFlags<T>::Flags); - return QMetaType::registerNormalizedType(normalizedTypeName, qMetaTypeDeleteHelper<T>, - qMetaTypeCreateHelper<T>, - qMetaTypeDestructHelper<T>, - qMetaTypeConstructHelper<T>, + return QMetaType::registerNormalizedType(normalizedTypeName, + QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Delete, + QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Create, + QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Destruct, + QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Construct, sizeof(T), flags, QtPrivate::MetaObjectForType<T>::value()); @@ -608,7 +666,8 @@ void qRegisterMetaTypeStreamOperators(const char *typeName ) { qRegisterMetaType<T>(typeName); - QMetaType::registerStreamOperators(typeName, qMetaTypeSaveHelper<T>, qMetaTypeLoadHelper<T>); + QMetaType::registerStreamOperators(typeName, QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Save, + QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Load); } #endif // QT_NO_DATASTREAM @@ -666,7 +725,8 @@ template <typename T> inline int qRegisterMetaTypeStreamOperators() { register int id = qMetaTypeId<T>(); - QMetaType::registerStreamOperators(id, qMetaTypeSaveHelper<T>, qMetaTypeLoadHelper<T>); + QMetaType::registerStreamOperators(id, QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Save, + QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Load); return id; } #endif diff --git a/src/corelib/kernel/qmetatype_p.h b/src/corelib/kernel/qmetatype_p.h index 3b1d95b1d8..351baa9aaa 100644 --- a/src/corelib/kernel/qmetatype_p.h +++ b/src/corelib/kernel/qmetatype_p.h @@ -135,8 +135,8 @@ public: #ifndef QT_NO_DATASTREAM # define QT_METATYPE_INTERFACE_INIT_DATASTREAM_IMPL(Type) \ - /*saveOp*/(qMetaTypeSaveHelper<Type>), \ - /*loadOp*/(qMetaTypeLoadHelper<Type>), + /*saveOp*/(QtMetaTypePrivate::QMetaTypeFunctionHelper<Type, QtMetaTypePrivate::TypeDefinition<Type>::IsAvailable>::Save), \ + /*loadOp*/(QtMetaTypePrivate::QMetaTypeFunctionHelper<Type, QtMetaTypePrivate::TypeDefinition<Type>::IsAvailable>::Load), # define QT_METATYPE_INTERFACE_INIT_EMPTY_DATASTREAM_IMPL(Type) \ /*saveOp*/ 0, \ /*loadOp*/ 0, @@ -156,11 +156,11 @@ public: #define QT_METATYPE_INTERFACE_INIT_IMPL(Type, DATASTREAM_DELEGATE) \ { \ - /*creator*/(qMetaTypeCreateHelper<Type>), \ - /*deleter*/(qMetaTypeDeleteHelper<Type>), \ + /*creator*/(QtMetaTypePrivate::QMetaTypeFunctionHelper<Type, QtMetaTypePrivate::TypeDefinition<Type>::IsAvailable>::Create), \ + /*deleter*/(QtMetaTypePrivate::QMetaTypeFunctionHelper<Type, QtMetaTypePrivate::TypeDefinition<Type>::IsAvailable>::Delete), \ DATASTREAM_DELEGATE(Type) \ - /*constructor*/(qMetaTypeConstructHelper<Type>), \ - /*destructor*/(qMetaTypeDestructHelper<Type>), \ + /*constructor*/(QtMetaTypePrivate::QMetaTypeFunctionHelper<Type, QtMetaTypePrivate::TypeDefinition<Type>::IsAvailable>::Construct), \ + /*destructor*/(QtMetaTypePrivate::QMetaTypeFunctionHelper<Type, QtMetaTypePrivate::TypeDefinition<Type>::IsAvailable>::Destruct), \ /*size*/(QTypeInfo<Type>::sizeOf), \ /*flags*/QtPrivate::QMetaTypeTypeFlags<Type>::Flags, \ /*metaObject*/METAOBJECT_DELEGATE(Type) \ diff --git a/tests/auto/other/compiler/tst_compiler.cpp b/tests/auto/other/compiler/tst_compiler.cpp index 1f0914f936..8d15816c36 100644 --- a/tests/auto/other/compiler/tst_compiler.cpp +++ b/tests/auto/other/compiler/tst_compiler.cpp @@ -268,7 +268,7 @@ namespace QtTestInternal struct Getter { static QMetaType::SaveOperator saveOp() { - return ::qMetaTypeSaveHelper<T>; + return ::QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Save; } }; |