From: Jędrzej Nowacki Date: Fri, 14 Oct 2011 07:22:46 +0000 (+0200) Subject: Refactor QMetaType types. X-Git-Tag: qt-v5.0.0-alpha1~2820 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7f35d5849e7233efac16c12271c58a049923b1c0;p=profile%2Fivi%2Fqtbase.git Refactor QMetaType types. QMetaType::Type enum is the main source of type ids. Currently there are many tasks that can be replaced by a smart macro that can iterate over all types. The patch introduces series of FOR_EACH_ macros that may be used for code generation. As the first step the macro was used for Q_DECLARE_BUILTIN_METATYPE to make sure that no type was forgotten. Second step was to use created macros in autotest to improve tests coverage. Change-Id: I34e9ad7bacf02b44b028bc1aad20b1241aacebd3 Reviewed-by: Olivier Goffart --- diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index b95b71d..e462be5 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -231,9 +231,14 @@ QT_BEGIN_NAMESPACE #define QT_ADD_STATIC_METATYPE(STR, TP) \ { STR, sizeof(STR) - 1, TP } +#define QT_ADD_STATIC_METATYPE_ALIASES_ITER(MetaTypeName, TypeId, AliasingName, RealNameStr) \ + QT_ADD_STATIC_METATYPE(RealNameStr, QMetaType::MetaTypeName), + +#define QT_ADD_STATIC_METATYPE_HACKS_ITER(MetaTypeName, TypeId, Name) \ + QT_ADD_STATIC_METATYPE(#Name, MetaTypeName), + /* Note: these MUST be in the order of the enums */ static const struct { const char * typeName; int typeNameLength; int type; } types[] = { - /* All Core types */ QT_ADD_STATIC_METATYPE("void", QMetaType::Void), QT_ADD_STATIC_METATYPE("bool", QMetaType::Bool), @@ -307,27 +312,8 @@ static const struct { const char * typeName; int typeNameLength; int type; } typ QT_ADD_STATIC_METATYPE("QVariant", QMetaType::QVariant), /* Type aliases - order doesn't matter */ - QT_ADD_STATIC_METATYPE("unsigned long", QMetaType::ULong), - QT_ADD_STATIC_METATYPE("unsigned int", QMetaType::UInt), - QT_ADD_STATIC_METATYPE("unsigned short", QMetaType::UShort), - QT_ADD_STATIC_METATYPE("unsigned char", QMetaType::UChar), - QT_ADD_STATIC_METATYPE("long long", QMetaType::LongLong), - QT_ADD_STATIC_METATYPE("unsigned long long", QMetaType::ULongLong), - QT_ADD_STATIC_METATYPE("qint8", QMetaType::Char), - QT_ADD_STATIC_METATYPE("signed char", QMetaType::Char), - QT_ADD_STATIC_METATYPE("quint8", QMetaType::UChar), - QT_ADD_STATIC_METATYPE("qint16", QMetaType::Short), - QT_ADD_STATIC_METATYPE("quint16", QMetaType::UShort), - QT_ADD_STATIC_METATYPE("qint32", QMetaType::Int), - QT_ADD_STATIC_METATYPE("quint32", QMetaType::UInt), - QT_ADD_STATIC_METATYPE("qint64", QMetaType::LongLong), - QT_ADD_STATIC_METATYPE("quint64", QMetaType::ULongLong), - QT_ADD_STATIC_METATYPE("QList", QMetaType::QVariantList), - QT_ADD_STATIC_METATYPE("QMap", QMetaType::QVariantMap), - QT_ADD_STATIC_METATYPE("QHash", QMetaType::QVariantHash), - // let QMetaTypeId2 figure out the type at compile time - QT_ADD_STATIC_METATYPE("qreal", QMetaTypeId2::MetaType), - + QT_FOR_EACH_STATIC_ALIAS_TYPE(QT_ADD_STATIC_METATYPE_ALIASES_ITER) + QT_FOR_EACH_STATIC_HACKS_TYPE(QT_ADD_STATIC_METATYPE_HACKS_ITER) {0, 0, QMetaType::Void} }; diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 24802ee..23f35ce 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -61,36 +61,134 @@ QT_BEGIN_NAMESPACE QT_MODULE(Core) +// F is a tuple: (QMetaType::TypeName, QMetaType::TypeNameID, RealType) +#define QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F)\ + F(Void, 0, void) \ + F(Bool, 1, bool) \ + F(Int, 2, int) \ + F(UInt, 3, uint) \ + F(LongLong, 4, qlonglong) \ + F(ULongLong, 5, qulonglong) \ + F(Double, 6, double) \ + F(Long, 129, long) \ + F(Short, 130, short) \ + F(Char, 131, char) \ + F(ULong, 132, ulong) \ + F(UShort, 133, ushort) \ + F(UChar, 134, uchar) \ + F(Float, 135, float) \ + +#define QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(F)\ + F(VoidStar, 128, void*) \ + +#define QT_FOR_EACH_STATIC_CORE_CLASS(F)\ + F(QChar, 7, QChar) \ + F(QString, 10, QString) \ + F(QStringList, 11, QStringList) \ + F(QByteArray, 12, QByteArray) \ + F(QBitArray, 13, QBitArray) \ + F(QDate, 14, QDate) \ + F(QTime, 15, QTime) \ + F(QDateTime, 16, QDateTime) \ + F(QUrl, 17, QUrl) \ + F(QLocale, 18, QLocale) \ + F(QRect, 19, QRect) \ + F(QRectF, 20, QRectF) \ + F(QSize, 21, QSize) \ + F(QSizeF, 22, QSizeF) \ + F(QLine, 23, QLine) \ + F(QLineF, 24, QLineF) \ + F(QPoint, 25, QPoint) \ + F(QPointF, 26, QPointF) \ + F(QRegExp, 27, QRegExp) \ + F(QEasingCurve, 29, QEasingCurve) \ + F(QVariant, 138, QVariant) \ + +#define QT_FOR_EACH_STATIC_CORE_POINTER(F)\ + F(QObjectStar, 136, QObject*) \ + F(QWidgetStar, 137, QWidget*) \ + +#define QT_FOR_EACH_STATIC_CORE_TEMPLATE(F)\ + F(QVariantMap, 8, QVariantMap) \ + F(QVariantList, 9, QVariantList) \ + F(QVariantHash, 28, QVariantHash) \ + +#define QT_FOR_EACH_STATIC_GUI_CLASS(F)\ + F(QFont, 64, QFont) \ + F(QPixmap, 65, QPixmap) \ + F(QBrush, 66, QBrush) \ + F(QColor, 67, QColor) \ + F(QPalette, 68, QPalette) \ + F(QImage, 69, QImage) \ + F(QPolygon, 70, QPolygon) \ + F(QRegion, 71, QRegion) \ + F(QBitmap, 72, QBitmap) \ + F(QCursor, 73, QCursor) \ + F(QKeySequence, 74, QKeySequence) \ + F(QPen, 75, QPen) \ + F(QTextLength, 76, QTextLength) \ + F(QTextFormat, 77, QTextFormat) \ + F(QMatrix, 78, QMatrix) \ + F(QTransform, 79, QTransform) \ + F(QMatrix4x4, 80, QMatrix4x4) \ + F(QVector2D, 81, QVector2D) \ + F(QVector3D, 82, QVector3D) \ + F(QVector4D, 83, QVector4D) \ + F(QQuaternion, 84, QQuaternion) \ + +#define QT_FOR_EACH_STATIC_WIDGETS_CLASS(F)\ + F(QIcon, 120, QIcon) \ + F(QSizePolicy, 121, QSizePolicy) \ + +// ### FIXME kill that set +#define QT_FOR_EACH_STATIC_HACKS_TYPE(F)\ + F(QMetaTypeId2::MetaType, -1, qreal) + +// F is a tuple: (QMetaType::TypeName, QMetaType::TypeNameID, AliasingType, "RealType") +#define QT_FOR_EACH_STATIC_ALIAS_TYPE(F)\ + F(ULong, -1, ulong, "unsigned long") \ + F(UInt, -1, uint, "unsigned int") \ + F(UShort, -1, ushort, "unsigned short") \ + F(UChar, -1, uchar, "unsigned char") \ + F(LongLong, -1, qlonglong, "long long") \ + F(ULongLong, -1, qulonglong, "unsigned long long") \ + F(Char, -1, char, "qint8") \ + F(Char, -1, char, "signed char") \ + F(UChar, -1, uchar, "quint8") \ + F(Short, -1, short, "qint16") \ + F(UShort, -1, ushort, "quint16") \ + F(Int, -1, int, "qint32") \ + F(UInt, -1, uint, "quint32") \ + F(LongLong, -1, qlonglong, "qint64") \ + F(ULongLong, -1, qulonglong, "quint64") \ + F(QVariantList, -1, QVariantList, "QList") \ + F(QVariantMap, -1, QVariantMap, "QMap") \ + F(QVariantHash, -1, QVariantHash, "QHash") \ + +#define QT_FOR_EACH_STATIC_TYPE(F)\ + QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F)\ + QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(F)\ + QT_FOR_EACH_STATIC_CORE_CLASS(F)\ + QT_FOR_EACH_STATIC_CORE_POINTER(F)\ + QT_FOR_EACH_STATIC_CORE_TEMPLATE(F)\ + QT_FOR_EACH_STATIC_GUI_CLASS(F)\ + QT_FOR_EACH_STATIC_WIDGETS_CLASS(F)\ + +#define QT_DEFINE_METATYPE_ID(TypeName, Id, Name) \ + TypeName = Id, + class Q_CORE_EXPORT QMetaType { public: enum Type { // these are merged with QVariant - Void = 0, Bool = 1, Int = 2, UInt = 3, LongLong = 4, ULongLong = 5, - Double = 6, QChar = 7, QVariantMap = 8, QVariantList = 9, - QString = 10, QStringList = 11, QByteArray = 12, - QBitArray = 13, QDate = 14, QTime = 15, QDateTime = 16, QUrl = 17, - QLocale = 18, QRect = 19, QRectF = 20, QSize = 21, QSizeF = 22, - QLine = 23, QLineF = 24, QPoint = 25, QPointF = 26, QRegExp = 27, - QVariantHash = 28, QEasingCurve = 29, LastCoreType = QEasingCurve, - - FirstGuiType = 64 /* QFont */, - - QFont = 64, QPixmap = 65, QBrush = 66, QColor = 67, QPalette = 68, - QImage = 69, QPolygon = 70, QRegion = 71, QBitmap = 72, - QCursor = 73, QKeySequence = 74, QPen = 75, - QTextLength = 76, QTextFormat = 77, QMatrix = 78, QTransform = 79, - QMatrix4x4 = 80, QVector2D = 81, QVector3D = 82, QVector4D = 83, - QQuaternion = 84, - LastGuiType = QQuaternion, + QT_FOR_EACH_STATIC_TYPE(QT_DEFINE_METATYPE_ID) - FirstWidgetsType = 120, /* QIcon */ - QIcon = 120, QSizePolicy = 121, + LastCoreType = QEasingCurve, + FirstGuiType = QFont, + LastGuiType = QQuaternion, + FirstWidgetsType = QIcon, LastWidgetsType = QSizePolicy, - - FirstCoreExtType = 128 /* VoidStar */, - VoidStar = 128, Long = 129, Short = 130, Char = 131, ULong = 132, - UShort = 133, UChar = 134, Float = 135, QObjectStar = 136, QWidgetStar = 137, - QVariant = 138, + FirstCoreExtType = VoidStar, LastCoreExtType = QVariant, // This logic must match the one in qglobal.h @@ -147,6 +245,8 @@ public: #endif }; +#undef QT_DEFINE_METATYPE_ID + template void qMetaTypeDeleteHelper(T *t) { @@ -329,122 +429,36 @@ inline int qRegisterMetaTypeStreamOperators() }; \ QT_END_NAMESPACE -class QString; -class QByteArray; -class QChar; -class QStringList; -class QBitArray; -class QDate; -class QTime; -class QDateTime; -class QUrl; -class QLocale; -class QRect; -class QRectF; -class QSize; -class QSizeF; -class QLine; -class QLineF; -class QPoint; -class QPointF; -#ifndef QT_NO_REGEXP -class QRegExp; -#endif -class QEasingCurve; +#define QT_FORWARD_DECLARE_STATIC_TYPES_ITER(TypeName, TypeId, Name) \ + class Name; + +QT_FOR_EACH_STATIC_CORE_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER) +QT_FOR_EACH_STATIC_GUI_CLASS(QT_FORWARD_DECLARE_STATIC_TYPES_ITER) +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 QList; +template class QMap; +template class QHash; +typedef QList QVariantList; +typedef QMap QVariantMap; +typedef QHash QVariantHash; -class QFont; -class QPixmap; -class QBrush; -class QColor; -class QPalette; -class QIcon; -class QImage; -class QPolygon; -class QRegion; -class QBitmap; -class QCursor; -class QSizePolicy; -class QKeySequence; -class QPen; -class QTextLength; -class QTextFormat; -class QMatrix; -class QTransform; -class QMatrix4x4; -class QVector2D; -class QVector3D; -class QVector4D; -class QQuaternion; -class QVariant; QT_END_NAMESPACE -Q_DECLARE_BUILTIN_METATYPE(QString, QString) -Q_DECLARE_BUILTIN_METATYPE(int, Int) -Q_DECLARE_BUILTIN_METATYPE(uint, UInt) -Q_DECLARE_BUILTIN_METATYPE(bool, Bool) -Q_DECLARE_BUILTIN_METATYPE(double, Double) -Q_DECLARE_BUILTIN_METATYPE(QByteArray, QByteArray) -Q_DECLARE_BUILTIN_METATYPE(QChar, QChar) -Q_DECLARE_BUILTIN_METATYPE(long, Long) -Q_DECLARE_BUILTIN_METATYPE(short, Short) -Q_DECLARE_BUILTIN_METATYPE(char, Char) + +#define QT_DECLARE_BUILTIN_METATYPE_ITER(MetaTypeName, MetaTypeId, Name) \ + Q_DECLARE_BUILTIN_METATYPE(Name, MetaTypeName) + +QT_FOR_EACH_STATIC_TYPE(QT_DECLARE_BUILTIN_METATYPE_ITER) Q_DECLARE_BUILTIN_METATYPE(signed char, Char) -Q_DECLARE_BUILTIN_METATYPE(ulong, ULong) -Q_DECLARE_BUILTIN_METATYPE(ushort, UShort) -Q_DECLARE_BUILTIN_METATYPE(uchar, UChar) -Q_DECLARE_BUILTIN_METATYPE(float, Float) -Q_DECLARE_BUILTIN_METATYPE(QObject *, QObjectStar) -Q_DECLARE_BUILTIN_METATYPE(QWidget *, QWidgetStar) -Q_DECLARE_BUILTIN_METATYPE(void *, VoidStar) -Q_DECLARE_BUILTIN_METATYPE(qlonglong, LongLong) -Q_DECLARE_BUILTIN_METATYPE(qulonglong, ULongLong) -Q_DECLARE_BUILTIN_METATYPE(QStringList, QStringList) -Q_DECLARE_BUILTIN_METATYPE(QBitArray, QBitArray) -Q_DECLARE_BUILTIN_METATYPE(QDate, QDate) -Q_DECLARE_BUILTIN_METATYPE(QTime, QTime) -Q_DECLARE_BUILTIN_METATYPE(QDateTime, QDateTime) -Q_DECLARE_BUILTIN_METATYPE(QUrl, QUrl) -Q_DECLARE_BUILTIN_METATYPE(QLocale, QLocale) -Q_DECLARE_BUILTIN_METATYPE(QRect, QRect) -Q_DECLARE_BUILTIN_METATYPE(QRectF, QRectF) -Q_DECLARE_BUILTIN_METATYPE(QSize, QSize) -Q_DECLARE_BUILTIN_METATYPE(QSizeF, QSizeF) -Q_DECLARE_BUILTIN_METATYPE(QLine, QLine) -Q_DECLARE_BUILTIN_METATYPE(QLineF, QLineF) -Q_DECLARE_BUILTIN_METATYPE(QPoint, QPoint) -Q_DECLARE_BUILTIN_METATYPE(QPointF, QPointF) -#ifndef QT_NO_REGEXP -Q_DECLARE_BUILTIN_METATYPE(QRegExp, QRegExp) -#endif -Q_DECLARE_BUILTIN_METATYPE(QEasingCurve, QEasingCurve) - -Q_DECLARE_BUILTIN_METATYPE(QFont, QFont) -Q_DECLARE_BUILTIN_METATYPE(QPixmap, QPixmap) -Q_DECLARE_BUILTIN_METATYPE(QBrush, QBrush) -Q_DECLARE_BUILTIN_METATYPE(QColor, QColor) -Q_DECLARE_BUILTIN_METATYPE(QPalette, QPalette) -Q_DECLARE_BUILTIN_METATYPE(QIcon, QIcon) -Q_DECLARE_BUILTIN_METATYPE(QImage, QImage) -Q_DECLARE_BUILTIN_METATYPE(QPolygon, QPolygon) -Q_DECLARE_BUILTIN_METATYPE(QRegion, QRegion) -Q_DECLARE_BUILTIN_METATYPE(QBitmap, QBitmap) -Q_DECLARE_BUILTIN_METATYPE(QCursor, QCursor) -Q_DECLARE_BUILTIN_METATYPE(QSizePolicy, QSizePolicy) -Q_DECLARE_BUILTIN_METATYPE(QKeySequence, QKeySequence) -Q_DECLARE_BUILTIN_METATYPE(QPen, QPen) -Q_DECLARE_BUILTIN_METATYPE(QTextLength, QTextLength) -Q_DECLARE_BUILTIN_METATYPE(QTextFormat, QTextFormat) -Q_DECLARE_BUILTIN_METATYPE(QMatrix, QMatrix) -Q_DECLARE_BUILTIN_METATYPE(QTransform, QTransform) -Q_DECLARE_BUILTIN_METATYPE(QMatrix4x4, QMatrix4x4) -Q_DECLARE_BUILTIN_METATYPE(QVector2D, QVector2D) -Q_DECLARE_BUILTIN_METATYPE(QVector3D, QVector3D) -Q_DECLARE_BUILTIN_METATYPE(QVector4D, QVector4D) -Q_DECLARE_BUILTIN_METATYPE(QQuaternion, QQuaternion) -Q_DECLARE_BUILTIN_METATYPE(QVariant, QVariant) + +#undef QT_DECLARE_BUILTIN_METATYPE_ITER + QT_END_HEADER diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 61dc48a..fbf5a84 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -392,10 +392,6 @@ public: inline DataPtr &data_ptr() { return d; } }; -typedef QList QVariantList; -typedef QMap QVariantMap; -typedef QHash QVariantHash; - inline bool qvariant_cast_helper(const QVariant &v, QVariant::Type tp, void *ptr) { return QVariant::handler->convert(&v.d, tp, ptr, 0); } @@ -522,10 +518,6 @@ Q_CORE_EXPORT QDebug operator<<(QDebug, const QVariant::Type); QT_END_NAMESPACE -Q_DECLARE_BUILTIN_METATYPE(QVariantList, QVariantList) -Q_DECLARE_BUILTIN_METATYPE(QVariantMap, QVariantMap) -Q_DECLARE_BUILTIN_METATYPE(QVariantHash, QVariantHash) - QT_END_HEADER #endif // QVARIANT_H diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 8574c14..32d5b0b 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -254,19 +254,19 @@ void tst_QMetaType::normalizedTypes() QCOMPARE(qRegisterMetaType >("Whity "), WhityDoubleId); } +#define TYPENAME_DATA(MetaTypeName, MetaTypeId, RealType)\ + QTest::newRow(#RealType) << QMetaType::MetaTypeName << #RealType; + +#define TYPENAME_DATA_ALIAS(MetaTypeName, MetaTypeId, AliasType, RealTypeString)\ + QTest::newRow(RealTypeString) << QMetaType::MetaTypeName << #AliasType; + void tst_QMetaType::typeName_data() { QTest::addColumn("aType"); QTest::addColumn("aTypeName"); - QTest::newRow("void") << QMetaType::Void << "void"; - QTest::newRow("int") << QMetaType::Int << "int"; - QTest::newRow("double") << QMetaType::Double << "double"; - QTest::newRow("qlonglong") << QMetaType::LongLong << "qlonglong"; - QTest::newRow("QRegExp") << QMetaType::QRegExp << "QRegExp"; - QTest::newRow("void*") << QMetaType::VoidStar << "void*"; - QTest::newRow("ulong") << QMetaType::ULong << "ulong"; - QTest::newRow("QWidget*") << QMetaType::QWidgetStar << "QWidget*"; + QT_FOR_EACH_STATIC_TYPE(TYPENAME_DATA) + QT_FOR_EACH_STATIC_ALIAS_TYPE(TYPENAME_DATA_ALIAS) } void tst_QMetaType::typeName() @@ -278,63 +278,23 @@ void tst_QMetaType::typeName() } #define FOR_EACH_PRIMITIVE_METATYPE(F) \ - F(int, Int) \ - F(uint, UInt) \ - F(bool, Bool) \ - F(double, Double) \ - F(long, Long) \ - F(short, Short) \ - F(char, Char) \ - F(ulong, ULong) \ - F(ushort, UShort) \ - F(uchar, UChar) \ - F(float, Float) \ - F(QObject *, QObjectStar) \ - F(QWidget *, QWidgetStar) \ - F(void *, VoidStar) \ - F(qlonglong, LongLong) \ - F(qulonglong, ULongLong) + QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F) \ + QT_FOR_EACH_STATIC_CORE_POINTER(F) \ #define FOR_EACH_COMPLEX_CORE_METATYPE(F) \ - F(QString, QString) \ - F(QByteArray, QByteArray) \ - F(QChar, QChar) \ - F(QStringList, QStringList) \ - F(QBitArray, QBitArray) \ - F(QDate, QDate) \ - F(QTime, QTime) \ - F(QDateTime, QDateTime) \ - F(QUrl, QUrl) \ - F(QLocale, QLocale) \ - F(QRect, QRect) \ - F(QRectF, QRectF) \ - F(QSize, QSize) \ - F(QSizeF, QSizeF) \ - F(QLine, QLine) \ - F(QLineF, QLineF) \ - F(QPoint, QPoint) \ - F(QPointF, QPointF) \ - F(QEasingCurve, QEasingCurve) - -#ifndef QT_NO_REGEXP -# define FOR_EACH_COMPLEX_CORE_METATYPE2(F) \ - F(QRegExp, QRegExp) -#else -# define FOR_EACH_COMPLEX_CORE_METATYPE2(F) -#endif + QT_FOR_EACH_STATIC_CORE_CLASS(F) #define FOR_EACH_CORE_METATYPE(F) \ FOR_EACH_PRIMITIVE_METATYPE(F) \ FOR_EACH_COMPLEX_CORE_METATYPE(F) \ - FOR_EACH_COMPLEX_CORE_METATYPE2(F) template struct MetaEnumToType {}; -#define DEFINE_META_ENUM_TO_TYPE(TYPE, ID) \ +#define DEFINE_META_ENUM_TO_TYPE(MetaTypeName, MetaTypeId, RealType) \ template<> \ -struct MetaEnumToType { \ - typedef TYPE Type; \ +struct MetaEnumToType { \ + typedef RealType Type; \ }; FOR_EACH_CORE_METATYPE(DEFINE_META_ENUM_TO_TYPE) #undef DEFINE_META_ENUM_TO_TYPE @@ -346,6 +306,13 @@ struct DefaultValueFactory static Type *create() { return new Type; } }; +template <> +struct DefaultValueFactory +{ + typedef MetaEnumToType::Type Type; + static Type *create() { return 0; } +}; + template struct DefaultValueTraits { @@ -354,8 +321,8 @@ struct DefaultValueTraits enum { IsInitialized = true }; }; -#define DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS(TYPE, ID) \ -template<> struct DefaultValueTraits { \ +#define DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS(MetaTypeName, MetaTypeId, RealType) \ +template<> struct DefaultValueTraits { \ enum { IsInitialized = false }; \ }; // Primitive types (int et al) aren't initialized @@ -365,6 +332,10 @@ FOR_EACH_PRIMITIVE_METATYPE(DEFINE_NON_INITIALIZED_DEFAULT_VALUE_TRAITS) template struct TestValueFactory {}; +template<> struct TestValueFactory { + static void *create() { return 0; } +}; + template<> struct TestValueFactory { static QString *create() { return new QString(QString::fromLatin1("QString")); } }; @@ -470,17 +441,25 @@ template<> struct TestValueFactory { template<> struct TestValueFactory { static QEasingCurve *create() { return new QEasingCurve(QEasingCurve::InOutElastic); } }; -#ifndef QT_NO_REGEXP template<> struct TestValueFactory { - static QRegExp *create() { return new QRegExp("A*"); } -}; + static QRegExp *create() + { +#ifndef QT_NO_REGEXP + return new QRegExp("A*"); +#else + return 0; #endif + } +}; +template<> struct TestValueFactory { + static QVariant *create() { return new QVariant(QStringList(QStringList() << "Q" << "t")); } +}; void tst_QMetaType::create_data() { QTest::addColumn("type"); -#define ADD_METATYPE_TEST_ROW(TYPE, ID) \ - QTest::newRow(QMetaType::typeName(QMetaType::ID)) << QMetaType::ID; +#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ + QTest::newRow(QMetaType::typeName(QMetaType::MetaTypeName)) << QMetaType::MetaTypeName; FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) #undef ADD_METATYPE_TEST_ROW } @@ -498,6 +477,18 @@ static void testCreateHelper() QMetaType::destroy(ID, actual); } +template<> +void testCreateHelper() +{ + typedef MetaEnumToType::Type Type; + void *actual = QMetaType::create(QMetaType::Void); + if (DefaultValueTraits::IsInitialized) { + QVERIFY(DefaultValueFactory::create()); + } + QMetaType::destroy(QMetaType::Void, actual); +} + + typedef void (*TypeTestFunction)(); void tst_QMetaType::create() @@ -507,9 +498,9 @@ void tst_QMetaType::create() static TypeTestFunction get(int type) { switch (type) { -#define RETURN_CREATE_FUNCTION(TYPE, ID) \ - case QMetaType::ID: \ - return testCreateHelper; +#define RETURN_CREATE_FUNCTION(MetaTypeName, MetaTypeId, RealType) \ + case QMetaType::MetaTypeName: \ + return testCreateHelper; FOR_EACH_CORE_METATYPE(RETURN_CREATE_FUNCTION) #undef RETURN_CREATE_FUNCTION } @@ -532,6 +523,16 @@ static void testCreateCopyHelper() delete expected; } +template<> +void testCreateCopyHelper() +{ + typedef MetaEnumToType::Type Type; + Type *expected = TestValueFactory::create(); + void *actual = QMetaType::create(QMetaType::Void, expected); + QCOMPARE(static_cast(actual), expected); + QMetaType::destroy(QMetaType::Void, actual); +} + void tst_QMetaType::createCopy_data() { create_data(); @@ -544,9 +545,9 @@ void tst_QMetaType::createCopy() static TypeTestFunction get(int type) { switch (type) { -#define RETURN_CREATE_COPY_FUNCTION(TYPE, ID) \ - case QMetaType::ID: \ - return testCreateCopyHelper; +#define RETURN_CREATE_COPY_FUNCTION(MetaTypeName, MetaTypeId, RealType) \ + case QMetaType::MetaTypeName: \ + return testCreateCopyHelper; FOR_EACH_CORE_METATYPE(RETURN_CREATE_COPY_FUNCTION) #undef RETURN_CREATE_COPY_FUNCTION } @@ -558,12 +559,15 @@ FOR_EACH_CORE_METATYPE(RETURN_CREATE_COPY_FUNCTION) TypeTestFunctionGetter::get(type)(); } +template struct SafeSizeOf { enum {Size = sizeof(T)}; }; +template<> struct SafeSizeOf { enum {Size = 0}; }; + void tst_QMetaType::sizeOf_data() { QTest::addColumn("type"); QTest::addColumn("size"); -#define ADD_METATYPE_TEST_ROW(TYPE, ID) \ - QTest::newRow(QMetaType::typeName(QMetaType::ID)) << QMetaType::ID << int(sizeof(TYPE)); +#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ + QTest::newRow(#RealType) << QMetaType::MetaTypeName << int(SafeSizeOf::Size); FOR_EACH_CORE_METATYPE(ADD_METATYPE_TEST_ROW) #undef ADD_METATYPE_TEST_ROW } @@ -626,6 +630,24 @@ static void testConstructHelper() QMetaType::destruct(ID, 0); } +template<> +void testConstructHelper() +{ + typedef MetaEnumToType::Type Type; + /*int size = */ QMetaType::sizeOf(QMetaType::Void); + void *storage = 0; + void *actual = QMetaType::construct(QMetaType::Void, storage, /*copy=*/0); + QCOMPARE(actual, storage); + if (DefaultValueTraits::IsInitialized) { + /*Type *expected = */ DefaultValueFactory::create(); + } + QMetaType::destruct(QMetaType::Void, actual); + qFreeAligned(storage); + + QVERIFY(QMetaType::construct(QMetaType::Void, 0, /*copy=*/0) == 0); + QMetaType::destruct(QMetaType::Void, 0); +} + void tst_QMetaType::construct() { struct TypeTestFunctionGetter @@ -633,9 +655,9 @@ void tst_QMetaType::construct() static TypeTestFunction get(int type) { switch (type) { -#define RETURN_CONSTRUCT_FUNCTION(TYPE, ID) \ - case QMetaType::ID: \ - return testConstructHelper; +#define RETURN_CONSTRUCT_FUNCTION(MetaTypeName, MetaTypeId, RealType) \ + case QMetaType::MetaTypeName: \ + return testConstructHelper; FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_FUNCTION) #undef RETURN_CONSTRUCT_FUNCTION } @@ -665,6 +687,21 @@ static void testConstructCopyHelper() delete expected; } +template<> +void testConstructCopyHelper() +{ + typedef MetaEnumToType::Type Type; + Type *expected = TestValueFactory::create(); + /* int size = */QMetaType::sizeOf(QMetaType::Void); + void *storage = 0; + void *actual = QMetaType::construct(QMetaType::Void, storage, expected); + QCOMPARE(actual, storage); + QMetaType::destruct(QMetaType::Void, actual); + qFreeAligned(storage); + + QVERIFY(QMetaType::construct(QMetaType::Void, 0, expected) == 0); +} + void tst_QMetaType::constructCopy_data() { create_data(); @@ -677,9 +714,9 @@ void tst_QMetaType::constructCopy() static TypeTestFunction get(int type) { switch (type) { -#define RETURN_CONSTRUCT_COPY_FUNCTION(TYPE, ID) \ - case QMetaType::ID: \ - return testConstructCopyHelper; +#define RETURN_CONSTRUCT_COPY_FUNCTION(MetaTypeName, MetaTypeId, RealType) \ + case QMetaType::MetaTypeName: \ + return testConstructCopyHelper; FOR_EACH_CORE_METATYPE(RETURN_CONSTRUCT_COPY_FUNCTION) #undef RETURN_CONSTRUCT_COPY_FUNCTION }