Fix automatic declaration of QSharedPointer<T> metatypes.

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>
This commit is contained in:
Stephen Kelly 2012-05-24 14:23:54 +02:00 committed by Qt by Nokia
parent 7462033cc5
commit 32bc019ac1
2 changed files with 57 additions and 5 deletions

View File

@ -752,13 +752,42 @@ Q_DECLARE_METATYPE_TEMPLATE_1ARG(QVector)
Q_DECLARE_METATYPE_TEMPLATE_1ARG(QQueue) Q_DECLARE_METATYPE_TEMPLATE_1ARG(QQueue)
Q_DECLARE_METATYPE_TEMPLATE_1ARG(QStack) Q_DECLARE_METATYPE_TEMPLATE_1ARG(QStack)
Q_DECLARE_METATYPE_TEMPLATE_1ARG(QSet) Q_DECLARE_METATYPE_TEMPLATE_1ARG(QSet)
Q_DECLARE_METATYPE_TEMPLATE_1ARG(QSharedPointer)
Q_DECLARE_METATYPE_TEMPLATE_1ARG(QLinkedList) Q_DECLARE_METATYPE_TEMPLATE_1ARG(QLinkedList)
Q_DECLARE_METATYPE_TEMPLATE_2ARG(QHash) Q_DECLARE_METATYPE_TEMPLATE_2ARG(QHash)
Q_DECLARE_METATYPE_TEMPLATE_2ARG(QMap) Q_DECLARE_METATYPE_TEMPLATE_2ARG(QMap)
Q_DECLARE_METATYPE_TEMPLATE_2ARG(QPair) 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, inline QMetaType::QMetaType(const ExtensionFlag extensionFlags, const QMetaTypeInterface *info,
Creator creator, Creator creator,
Deleter deleter, Deleter deleter,

View File

@ -1171,8 +1171,6 @@ void tst_QMetaType::registerStreamBuiltin()
qRegisterMetaTypeStreamOperators<QVariant>("QVariant"); qRegisterMetaTypeStreamOperators<QVariant>("QVariant");
} }
Q_DECLARE_METATYPE(QSharedPointer<QObject>)
typedef QHash<int, uint> IntUIntHash; typedef QHash<int, uint> IntUIntHash;
Q_DECLARE_METATYPE(IntUIntHash) Q_DECLARE_METATYPE(IntUIntHash)
typedef QMap<int, uint> IntUIntMap; typedef QMap<int, uint> IntUIntMap;
@ -1228,6 +1226,18 @@ private:
int m_int; 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() void tst_QMetaType::automaticTemplateRegistration()
{ {
{ {
@ -1418,8 +1428,7 @@ void tst_QMetaType::automaticTemplateRegistration()
F(QVector, TYPE) \ F(QVector, TYPE) \
F(QQueue, TYPE) \ F(QQueue, TYPE) \
F(QStack, TYPE) \ F(QStack, TYPE) \
F(QSet, TYPE) \ F(QSet, TYPE)
F(QSharedPointer, TYPE)
#define PRINT_1ARG_TEMPLATE(RealName, ...) \ #define PRINT_1ARG_TEMPLATE(RealName, ...) \
FOR_EACH_1ARG_TEMPLATE_TYPE(CREATE_AND_VERIFY_CONTAINER, RealName) FOR_EACH_1ARG_TEMPLATE_TYPE(CREATE_AND_VERIFY_CONTAINER, RealName)
@ -1453,6 +1462,20 @@ void tst_QMetaType::automaticTemplateRegistration()
#endif // Q_COMPILER_VARIADIC_MACROS #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> template <typename T>