Store the is-a QObject fact with the metatype declaration.

This is a source incompatible change for Q_DECLARE_METATYPE(T*),
which now requires T to be fully defined.

The consequences of this are:
 * Forward declared types can no longer be declared as a metatype.
     (though this is a very uncommon thing to do).

There is a trivial workaround where necessary.

Change-Id: Id74c40088b8c0b466fcd7c55abd616f69acc82c8
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Stephen Kelly 2011-12-30 12:00:09 +01:00 committed by Qt by Nokia
parent 6efefb3fe5
commit 4b8ceb41ae
11 changed files with 154 additions and 9 deletions

5
dist/changes-5.0.0 vendored
View File

@ -117,6 +117,11 @@ information about a particular change.
* QWidget *widget() has been removed and is replaced by QObject
*target() in order to avoid QWidget dependencies.
- QMetaType
* It is no longer possible to use Q_DECLARE_METATYPE(Foo*) where Foo is only
forward declared - it must be fully defined.
- QWindowSystemInterface:
* The signature of all handleTouchEvent() variants have changed,

View File

@ -208,6 +208,13 @@ typedef QTextStream & (*QTextStreamFunction)(QTextStream &);// manipulator funct
typedef void (QTextStream::*QTSMFI)(int); // manipulator w/int argument
typedef void (QTextStream::*QTSMFC)(QChar); // manipulator w/QChar argument
namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<QTextStreamFunction> {
enum { Value = false };
};
}
class Q_CORE_EXPORT QTextStreamManipulator
{
public:

View File

@ -1670,11 +1670,13 @@ class Flags
template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted>
struct FlagsImpl
{
static quint32 Flags(const int)
static quint32 Flags(const int type)
{
return (!QTypeInfo<T>::isStatic * QMetaType::MovableType)
| (QTypeInfo<T>::isComplex * QMetaType::NeedsConstruction)
| (QTypeInfo<T>::isComplex * QMetaType::NeedsDestruction);
| (QTypeInfo<T>::isComplex * QMetaType::NeedsDestruction)
| (type == QMetaType::QObjectStar ? QMetaType::PointerToQObject : 0)
| (type == QMetaType::QWidgetStar ? QMetaType::PointerToQObject : 0);
}
};
template<typename T>

View File

@ -207,7 +207,8 @@ public:
enum TypeFlag {
NeedsConstruction = 0x1,
NeedsDestruction = 0x2,
MovableType = 0x4
MovableType = 0x4,
PointerToQObject = 0x8
};
Q_DECLARE_FLAGS(TypeFlags, TypeFlag)
@ -315,6 +316,9 @@ struct QMetaTypeId2
static inline int qt_metatype_id() { return QMetaTypeId<T>::qt_metatype_id(); }
};
class QObject;
class QWidget;
namespace QtPrivate {
template <typename T, bool Defined = QMetaTypeId2<T>::Defined>
struct QMetaTypeIdHelper {
@ -325,6 +329,43 @@ namespace QtPrivate {
static inline int qt_metatype_id()
{ return -1; }
};
template<typename T>
struct IsPointerToTypeDerivedFromQObject
{
enum { Value = false };
};
// Specialize to avoid sizeof(void) warning
template<>
struct IsPointerToTypeDerivedFromQObject<void*>
{
enum { Value = false };
};
template<>
struct IsPointerToTypeDerivedFromQObject<QObject*>
{
enum { Value = true };
};
template<>
struct IsPointerToTypeDerivedFromQObject<QWidget*>
{
enum { Value = true };
};
template<typename T>
struct IsPointerToTypeDerivedFromQObject<T*>
{
typedef qint8 yes_type;
typedef qint64 no_type;
#ifndef QT_NO_QOBJECT
static yes_type checkType(QObject* );
#endif
static no_type checkType(...);
Q_STATIC_ASSERT_X(sizeof(T), "Type argument of Q_DECLARE_METATYPE(T*) must be fully defined");
enum { Value = sizeof(checkType(static_cast<T*>(0))) == sizeof(yes_type) };
};
}
template <typename T>
@ -354,6 +395,8 @@ int qRegisterMetaType(const char *typeName
flags |= QMetaType::NeedsConstruction;
flags |= QMetaType::NeedsDestruction;
}
if (QtPrivate::IsPointerToTypeDerivedFromQObject<T>::Value)
flags |= QMetaType::PointerToQObject;
return QMetaType::registerType(typeName, reinterpret_cast<QMetaType::Deleter>(dptr),
reinterpret_cast<QMetaType::Creator>(cptr),
@ -459,8 +502,6 @@ QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER)
#undef QT_FORWARD_DECLARE_STATIC_TYPES_ITER
class QWidget;
class QObject;
template <class T> class QList;
template <class T1, class T2> class QMap;
template <class T1, class T2> class QHash;

View File

@ -107,7 +107,18 @@
template <typename T>
inline void PQfreemem(T *t, int = 0) { free(t); }
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<PGconn*> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(PGconn*)
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<PGresult*> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(PGresult*)
QT_BEGIN_NAMESPACE

View File

@ -59,7 +59,18 @@
#include <sqlite3.h>
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<sqlite3*> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(sqlite3*)
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<sqlite3_stmt*> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(sqlite3_stmt*)
QT_BEGIN_NAMESPACE

View File

@ -127,6 +127,18 @@ QT_BEGIN_NAMESPACE
#define CS_PUBLIC
#endif
namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<LOGINREC*> {
enum { Value = false };
};
}
namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<DBPROCESS*> {
enum { Value = false };
};
}
QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, int errNo = -1)
{
return QSqlError(QLatin1String("QTDS: ") + err, QString(), type, errNo);

View File

@ -596,21 +596,50 @@ Q_DECLARE_TYPEINFO(CustomMovable, Q_MOVABLE_TYPE);
QT_END_NAMESPACE
Q_DECLARE_METATYPE(CustomMovable);
class CustomObject : public QObject
{
Q_OBJECT
public:
CustomObject(QObject *parent = 0)
: QObject(parent)
{
}
};
Q_DECLARE_METATYPE(CustomObject*);
struct SecondBase {};
class CustomMultiInheritanceObject : public QObject, SecondBase
{
Q_OBJECT
public:
CustomMultiInheritanceObject(QObject *parent = 0)
: QObject(parent)
{
}
};
Q_DECLARE_METATYPE(CustomMultiInheritanceObject*);
void tst_QMetaType::flags_data()
{
QTest::addColumn<int>("type");
QTest::addColumn<bool>("isMovable");
QTest::addColumn<bool>("isComplex");
QTest::addColumn<bool>("isPointerToQObject");
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
QTest::newRow(#RealType) << MetaTypeId << bool(!QTypeInfo<RealType>::isStatic) << bool(QTypeInfo<RealType>::isComplex);
QTest::newRow(#RealType) << MetaTypeId << bool(!QTypeInfo<RealType>::isStatic) << bool(QTypeInfo<RealType>::isComplex) << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<RealType>::Value);
QT_FOR_EACH_STATIC_CORE_CLASS(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_CORE_POINTER(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
QTest::newRow("TestSpace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << false << true;
QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << false << true;
QTest::newRow("CustomMovable") << ::qMetaTypeId<CustomMovable>() << true << true;
QTest::newRow("TestSpace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << false << true << false;
QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << false << true << false;
QTest::newRow("CustomMovable") << ::qMetaTypeId<CustomMovable>() << true << true << false;
QTest::newRow("CustomObject*") << ::qMetaTypeId<CustomObject*>() << true << false << true;
QTest::newRow("CustomMultiInheritanceObject*") << ::qMetaTypeId<CustomMultiInheritanceObject*>() << true << false << true;
}
void tst_QMetaType::flags()
@ -618,10 +647,12 @@ void tst_QMetaType::flags()
QFETCH(int, type);
QFETCH(bool, isMovable);
QFETCH(bool, isComplex);
QFETCH(bool, isPointerToQObject);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsConstruction), isComplex);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsDestruction), isComplex);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::MovableType), isMovable);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::PointerToQObject), isPointerToQObject);
}
void tst_QMetaType::construct_data()

View File

@ -3324,6 +3324,11 @@ void tst_QVariant::colorInteger()
}
class Forward;
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<Forward*> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(Forward*);
void tst_QVariant::forwardDeclare()

View File

@ -99,6 +99,11 @@ struct AbstractTester
Q_DECLARE_METATYPE(AbstractTester *)
typedef void (*TestFunction)(QObject*);
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<TestFunction> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(TestFunction)
template <typename T>

View File

@ -1298,6 +1298,11 @@ static int ucstrncmp_ssse3_aligning2(const ushort *a, const ushort *b, int len)
#endif
typedef int (* UcstrncmpFunction)(const ushort *, const ushort *, int);
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<UcstrncmpFunction> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(UcstrncmpFunction)
void tst_QString::ucstrncmp_data() const
@ -1457,6 +1462,11 @@ void tst_QString::fromLatin1() const
}
typedef void (* FromLatin1Function)(ushort *, const char *, int);
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<FromLatin1Function> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(FromLatin1Function)
void fromLatin1_regular(ushort *dst, const char *str, int size)
@ -1907,6 +1917,11 @@ void tst_QString::fromLatin1Alternatives() const
}
typedef int (* FromUtf8Function)(ushort *, const char *, int);
QT_BEGIN_NAMESPACE namespace QtPrivate {
template <> struct IsPointerToTypeDerivedFromQObject<FromUtf8Function> {
enum { Value = false };
};
} QT_END_NAMESPACE
Q_DECLARE_METATYPE(FromUtf8Function)
extern QTextCodec::ConverterState *state;