From 60d096ff6d24a77f1c156c8354232a44e8516872 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C4=99drzej=20Nowacki?= Date: Fri, 21 Sep 2012 13:45:07 +0200 Subject: [PATCH] QMetaType build fix with Q_NO_CURSOR and friends. QMetaType has a way to "switch off" some types from the build. QtMetaTypePrivate::TypeDefinition::IsAvailable is defined as false for all unaccessible types. Sadly that information was never used by gui and widget handlers. The patch implements it. Change-Id: Ie5835be4c88cfbbca8a4e9199e31ddfc20cae190 Reviewed-by: J-P Nurmi Reviewed-by: Jing Bai Reviewed-by: Thiago Macieira --- src/corelib/kernel/qmetatype.cpp | 12 ++--- src/corelib/kernel/qmetatype.h | 72 +++++++++++++++++++++++++++--- src/corelib/kernel/qmetatype_p.h | 12 ++--- 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 dc0fb73..6194f20 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, qMetaTypeConstructHelper, 0, TypeFlags(), 0); + return registerType(typeName, deleter, creator, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Destruct, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Construct, 0, TypeFlags(), 0); } /*! @@ -1233,7 +1235,7 @@ class TypeCreator { struct CreatorImpl { static void *Create(const int /* type */, const void *copy) { - // Using qMetaTypeCreateHelper adds function call cost, even if it is a template (gcc). + // Using QMetaTypeFunctionHelper::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(copy)) : new T(); } @@ -1298,7 +1300,7 @@ namespace { class TypeDestroyer { template::IsAccepted> struct DestroyerImpl { - static void Destroy(const int /* type */, void *where) { qMetaTypeDeleteHelper(where); } + static void Destroy(const int /* type */, void *where) { QtMetaTypePrivate::QMetaTypeFunctionHelper::Delete(where); } }; template struct DestroyerImpl { @@ -1364,7 +1366,7 @@ namespace { class TypeConstructor { template::IsAccepted> struct ConstructorImpl { - static void *Construct(const int /*type*/, void *where, const void *copy) { return qMetaTypeConstructHelper(where, copy); } + static void *Construct(const int /*type*/, void *where, const void *copy) { return QtMetaTypePrivate::QMetaTypeFunctionHelper::Construct(where, copy); } }; template struct ConstructorImpl { @@ -1452,7 +1454,7 @@ namespace { class TypeDestructor { template::IsAccepted> struct DestructorImpl { - static void Destruct(const int /* type */, void *where) { qMetaTypeDestructHelper(where); } + static void Destruct(const int /* type */, void *where) { QtMetaTypePrivate::QMetaTypeFunctionHelper::Destruct(where); } }; template struct DestructorImpl { diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 1975379..997f61e 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 *) {} +namespace QtMetaTypePrivate { +template +struct QMetaTypeFunctionHelper { + static void Delete(void *t) + { + delete static_cast(t); + } + + static void *Create(const void *t) + { + if (t) + return new T(*static_cast(t)); + return new T(); + } + + static void Destruct(void *t) + { + Q_UNUSED(t) // Silence MSVC that warns for POD types. + static_cast(t)->~T(); + } + + static void *Construct(void *where, const void *t) + { + if (t) + return new (where) T(*static_cast(t)); + return new (where) T; + } +#ifndef QT_NO_DATASTREAM + static void Save(QDataStream &stream, const void *t) + { + stream << *static_cast(t); + } + + static void Load(QDataStream &stream, void *t) + { + stream >> *static_cast(t); + } +#endif // QT_NO_DATASTREAM +}; + +template +struct QMetaTypeFunctionHelper { + 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 + : public QMetaTypeFunctionHelper +{}; +} + template 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::Flags); - return QMetaType::registerNormalizedType(normalizedTypeName, qMetaTypeDeleteHelper, - qMetaTypeCreateHelper, - qMetaTypeDestructHelper, - qMetaTypeConstructHelper, + return QMetaType::registerNormalizedType(normalizedTypeName, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Delete, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Create, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Destruct, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Construct, sizeof(T), flags, QtPrivate::MetaObjectForType::value()); @@ -608,7 +666,8 @@ void qRegisterMetaTypeStreamOperators(const char *typeName ) { qRegisterMetaType(typeName); - QMetaType::registerStreamOperators(typeName, qMetaTypeSaveHelper, qMetaTypeLoadHelper); + QMetaType::registerStreamOperators(typeName, QtMetaTypePrivate::QMetaTypeFunctionHelper::Save, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Load); } #endif // QT_NO_DATASTREAM @@ -666,7 +725,8 @@ template inline int qRegisterMetaTypeStreamOperators() { register int id = qMetaTypeId(); - QMetaType::registerStreamOperators(id, qMetaTypeSaveHelper, qMetaTypeLoadHelper); + QMetaType::registerStreamOperators(id, QtMetaTypePrivate::QMetaTypeFunctionHelper::Save, + QtMetaTypePrivate::QMetaTypeFunctionHelper::Load); return id; } #endif diff --git a/src/corelib/kernel/qmetatype_p.h b/src/corelib/kernel/qmetatype_p.h index 3b1d95b..351baa9 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), \ - /*loadOp*/(qMetaTypeLoadHelper), + /*saveOp*/(QtMetaTypePrivate::QMetaTypeFunctionHelper::IsAvailable>::Save), \ + /*loadOp*/(QtMetaTypePrivate::QMetaTypeFunctionHelper::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), \ - /*deleter*/(qMetaTypeDeleteHelper), \ + /*creator*/(QtMetaTypePrivate::QMetaTypeFunctionHelper::IsAvailable>::Create), \ + /*deleter*/(QtMetaTypePrivate::QMetaTypeFunctionHelper::IsAvailable>::Delete), \ DATASTREAM_DELEGATE(Type) \ - /*constructor*/(qMetaTypeConstructHelper), \ - /*destructor*/(qMetaTypeDestructHelper), \ + /*constructor*/(QtMetaTypePrivate::QMetaTypeFunctionHelper::IsAvailable>::Construct), \ + /*destructor*/(QtMetaTypePrivate::QMetaTypeFunctionHelper::IsAvailable>::Destruct), \ /*size*/(QTypeInfo::sizeOf), \ /*flags*/QtPrivate::QMetaTypeTypeFlags::Flags, \ /*metaObject*/METAOBJECT_DELEGATE(Type) \ diff --git a/tests/auto/other/compiler/tst_compiler.cpp b/tests/auto/other/compiler/tst_compiler.cpp index 1f0914f..8d15816 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; + return ::QtMetaTypePrivate::QMetaTypeFunctionHelper::Save; } }; -- 2.7.4