Fix automatic declaration of QSharedPointer<T> metatypes.
authorStephen Kelly <stephen.kelly@kdab.com>
Thu, 24 May 2012 12:23:54 +0000 (14:23 +0200)
committerQt by Nokia <qt-info@nokia.com>
Tue, 12 Jun 2012 09:16:45 +0000 (11:16 +0200)
QSharedPointer doesn't work like the other automatic template metatype
declarations because in some cases T* is declared as a metatype, but we
are interested in QSharedPointer<T> (eg QObject*). In other cases, T is
declared as a metatype and we are interested
in QSharedPointer<T> (eg char).

In particular the macro used before this patch was attempting to get the
metatype id of the element_type using for example qMetaTypeId<QObject>()
instead of qMetaTypeId<QObject*>(), which did not work.

Similarly, the variadic macro driven test is no good, because it was
testing QSharedPointer<QObject*> instead of QSharedPointer<QObject>,
so that is removed.

In the end, the only thing we can sensibly automatically declare as
metatypes are QSharedPointers to QObject derived types. That is also
the type that makes the most sense in a QML context anyway.

Change-Id: I13dd40147e2e6bedf38661f898102abaaaa96208
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
src/corelib/kernel/qmetatype.h
tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp

index 005d2b9..a7b2e78 100644 (file)
@@ -752,13 +752,42 @@ Q_DECLARE_METATYPE_TEMPLATE_1ARG(QVector)
 Q_DECLARE_METATYPE_TEMPLATE_1ARG(QQueue)
 Q_DECLARE_METATYPE_TEMPLATE_1ARG(QStack)
 Q_DECLARE_METATYPE_TEMPLATE_1ARG(QSet)
-Q_DECLARE_METATYPE_TEMPLATE_1ARG(QSharedPointer)
 Q_DECLARE_METATYPE_TEMPLATE_1ARG(QLinkedList)
 
 Q_DECLARE_METATYPE_TEMPLATE_2ARG(QHash)
 Q_DECLARE_METATYPE_TEMPLATE_2ARG(QMap)
 Q_DECLARE_METATYPE_TEMPLATE_2ARG(QPair)
 
+template <typename T, bool = QtPrivate::IsPointerToTypeDerivedFromQObject<T*>::Value>
+struct QMetaTypeIdSharedPointerQObjectStar
+{
+    enum {
+        Defined = 0
+    };
+};
+
+template <typename T>
+struct QMetaTypeIdSharedPointerQObjectStar<T, /* IsPointerToTypeDerivedFromQObject */ true>
+{
+    enum {
+        Defined = 1
+    };
+    static int qt_metatype_id()
+    {
+        static QBasicAtomicInt metatype_id = Q_BASIC_ATOMIC_INITIALIZER(0);
+        if (!metatype_id.load()) {
+            metatype_id.storeRelease(qRegisterNormalizedMetaType< QSharedPointer<T> >( QByteArray("QSharedPointer<") + T::staticMetaObject.className() + ">",
+                        reinterpret_cast< QSharedPointer<T> *>(quintptr(-1))));
+        }
+        return metatype_id.loadAcquire();
+    }
+};
+
+template <typename T>
+struct QMetaTypeId< QSharedPointer<T> > : public QMetaTypeIdSharedPointerQObjectStar<T>
+{
+};
+
 inline QMetaType::QMetaType(const ExtensionFlag extensionFlags, const QMetaTypeInterface *info,
                             Creator creator,
                             Deleter deleter,
index da8c182..e1b58e6 100644 (file)
@@ -1171,8 +1171,6 @@ void tst_QMetaType::registerStreamBuiltin()
     qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
 }
 
-Q_DECLARE_METATYPE(QSharedPointer<QObject>)
-
 typedef QHash<int, uint> IntUIntHash;
 Q_DECLARE_METATYPE(IntUIntHash)
 typedef QMap<int, uint> IntUIntMap;
@@ -1228,6 +1226,18 @@ private:
     int m_int;
 };
 
+class MyObject : public QObject
+{
+  Q_OBJECT
+public:
+  MyObject(QObject *parent = 0)
+    : QObject(parent)
+  {
+  }
+};
+typedef MyObject* MyObjectPtr;
+Q_DECLARE_METATYPE(MyObjectPtr)
+
 void tst_QMetaType::automaticTemplateRegistration()
 {
   {
@@ -1418,8 +1428,7 @@ void tst_QMetaType::automaticTemplateRegistration()
         F(QVector, TYPE) \
         F(QQueue, TYPE) \
         F(QStack, TYPE) \
-        F(QSet, TYPE) \
-        F(QSharedPointer, TYPE)
+        F(QSet, TYPE)
 
     #define PRINT_1ARG_TEMPLATE(RealName, ...) \
         FOR_EACH_1ARG_TEMPLATE_TYPE(CREATE_AND_VERIFY_CONTAINER, RealName)
@@ -1453,6 +1462,20 @@ void tst_QMetaType::automaticTemplateRegistration()
 
 #endif // Q_COMPILER_VARIADIC_MACROS
 
+#define TEST_QSHAREDPOINTER(FULLTYPE) \
+    { \
+        FULLTYPE sp = FULLTYPE::create(); \
+        QVariant v = QVariant::fromValue(sp); \
+        QCOMPARE(v.typeName(), #FULLTYPE); \
+    }
+
+    TEST_QSHAREDPOINTER(QSharedPointer<QObject>)
+    TEST_QSHAREDPOINTER(QSharedPointer<QFile>)
+    TEST_QSHAREDPOINTER(QSharedPointer<QTemporaryFile>)
+    TEST_QSHAREDPOINTER(QSharedPointer<MyObject>)
+
+#undef TEST_QSHAREDPOINTER
+
 }
 
 template <typename T>