QMetaType build fix with Q_NO_CURSOR and friends.
authorJędrzej Nowacki <jedrzej.nowacki@digia.com>
Fri, 21 Sep 2012 11:45:07 +0000 (13:45 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 27 Sep 2012 00:37:19 +0000 (02:37 +0200)
QMetaType has a way to "switch off" some types from the build.
QtMetaTypePrivate::TypeDefinition<T>::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 <jpnurmi@digia.com>
Reviewed-by: Jing Bai <jing.bai@digia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
src/corelib/kernel/qmetatype.cpp
src/corelib/kernel/qmetatype.h
src/corelib/kernel/qmetatype_p.h
tests/auto/other/compiler/tst_compiler.cpp

index dc0fb73..6194f20 100644 (file)
@@ -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> {
index 1975379..997f61e 100644 (file)
@@ -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
index 3b1d95b..351baa9 100644 (file)
@@ -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) \
index 1f0914f..8d15816 100644 (file)
@@ -268,7 +268,7 @@ namespace QtTestInternal
         struct Getter {
             static QMetaType::SaveOperator saveOp()
             {
-                return ::qMetaTypeSaveHelper<T>;
+                return ::QtMetaTypePrivate::QMetaTypeFunctionHelper<T>::Save;
             }
         };