From ce4343a52f68ce0759d712c79d3acd37ecd954b1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C4=99drzej=20Nowacki?= Date: Mon, 21 Nov 2011 14:08:52 +0100 Subject: [PATCH] Code cleanup in QVariant and QMetaType. QMetaTypeGuiHelper was generalized and renamed to QMetaTypeInterface. From now all types will have common interface which can be used for basic operations. Change-Id: I50d67f4a8081fa0f75c9d530a8211593ec37bc55 Reviewed-by: Olivier Goffart --- src/corelib/kernel/qmetatype.cpp | 34 +------- src/corelib/kernel/qmetatype_p.h | 66 ++++++++++++++ src/gui/kernel/qguivariant.cpp | 155 ++------------------------------- src/widgets/kernel/qwidgetsvariant.cpp | 80 ++--------------- 4 files changed, 84 insertions(+), 251 deletions(-) diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp index ebb4548..e3a8293 100644 --- a/src/corelib/kernel/qmetatype.cpp +++ b/src/corelib/kernel/qmetatype.cpp @@ -287,42 +287,14 @@ static const struct { const char * typeName; int typeNameLength; int type; } typ {0, 0, QMetaType::Void} }; -struct QMetaTypeGuiHelper -{ - QMetaType::Creator creator; - QMetaType::Deleter deleter; -#ifndef QT_NO_DATASTREAM - QMetaType::SaveOperator saveOp; - QMetaType::LoadOperator loadOp; -#endif - QMetaType::Constructor constructor; - QMetaType::Destructor destructor; - int size; -}; -Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeGuiHelper = 0; -Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeWidgetsHelper = 0; +Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeGuiHelper = 0; +Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeWidgetsHelper = 0; -class QCustomTypeInfo +class QCustomTypeInfo : public QMetaTypeInterface { public: - QCustomTypeInfo() : typeName(), creator(0), deleter(0) -#ifndef QT_NO_DATASTREAM - , saveOp(0), loadOp(0) -#endif - , constructor(0), destructor(0), size(0) - {} - QByteArray typeName; - QMetaType::Creator creator; - QMetaType::Deleter deleter; -#ifndef QT_NO_DATASTREAM - QMetaType::SaveOperator saveOp; - QMetaType::LoadOperator loadOp; -#endif int alias; - QMetaType::Constructor constructor; - QMetaType::Destructor destructor; - int size; }; Q_DECLARE_TYPEINFO(QCustomTypeInfo, Q_MOVABLE_TYPE); diff --git a/src/corelib/kernel/qmetatype_p.h b/src/corelib/kernel/qmetatype_p.h index 521e8ff..205516d 100644 --- a/src/corelib/kernel/qmetatype_p.h +++ b/src/corelib/kernel/qmetatype_p.h @@ -111,6 +111,72 @@ QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_DECLARE_WIDGETS_MODULE_TYPES_ITER) #undef QT_DECLARE_GUI_MODULE_TYPES_ITER #undef QT_DECLARE_WIDGETS_MODULE_TYPES_ITER +class QMetaTypeInterface +{ +private: + template + struct Impl { + static void *creator(const T *t) + { + if (t) + return new T(*t); + return new T(); + } + + static void deleter(T *t) { delete t; } + #ifndef QT_NO_DATASTREAM + static void saver(QDataStream &stream, const T *t) { stream << *t; } + static void loader(QDataStream &stream, T *t) { stream >> *t; } + #endif // QT_NO_DATASTREAM + static void destructor(T *t) + { + Q_UNUSED(t) // Silence MSVC that warns for POD types. + t->~T(); + } + static void *constructor(void *where, const T *t) + { + if (t) + return new (where) T(*static_cast(t)); + return new (where) T; + } + }; +public: + template + QMetaTypeInterface(T * = 0) + : creator(reinterpret_cast(Impl::creator)) + , deleter(reinterpret_cast(Impl::deleter)) + #ifndef QT_NO_DATASTREAM + , saveOp(reinterpret_cast(Impl::saver)) + , loadOp(reinterpret_cast(Impl::loader)) + #endif + , constructor(reinterpret_cast(Impl::constructor)) + , destructor(reinterpret_cast(Impl::destructor)) + , size(sizeof(T)) + {} + + QMetaTypeInterface() + : creator(0) + , deleter(0) + #ifndef QT_NO_DATASTREAM + , saveOp(0) + , loadOp(0) + #endif + , constructor(0) + , destructor(0) + , size(0) + {} + + QMetaType::Creator creator; + QMetaType::Deleter deleter; +#ifndef QT_NO_DATASTREAM + QMetaType::SaveOperator saveOp; + QMetaType::LoadOperator loadOp; +#endif + QMetaType::Constructor constructor; + QMetaType::Destructor destructor; + int size; +}; + QT_END_NAMESPACE #endif // QMETATYPE_P_H diff --git a/src/gui/kernel/qguivariant.cpp b/src/gui/kernel/qguivariant.cpp index f0baf70..9198e7f 100644 --- a/src/gui/kernel/qguivariant.cpp +++ b/src/gui/kernel/qguivariant.cpp @@ -474,158 +474,17 @@ const QVariant::Handler qt_gui_variant_handler = { #endif }; -struct QMetaTypeGuiHelper -{ - QMetaType::Creator creator; - QMetaType::Deleter deleter; -#ifndef QT_NO_DATASTREAM - QMetaType::SaveOperator saveOp; - QMetaType::LoadOperator loadOp; -#endif - QMetaType::Constructor constructor; - QMetaType::Destructor destructor; - int size; -}; - -extern Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeGuiHelper; - - -#ifdef QT_NO_DATASTREAM -# define Q_DECL_METATYPE_HELPER(TYPE) \ - typedef void *(*QCreate##TYPE)(const TYPE *); \ - static const QCreate##TYPE qCreate##TYPE = qMetaTypeCreateHelper; \ - typedef void (*QDelete##TYPE)(TYPE *); \ - static const QDelete##TYPE qDelete##TYPE = qMetaTypeDeleteHelper; \ - typedef void *(*QConstruct##TYPE)(void *, const TYPE *); \ - static const QConstruct##TYPE qConstruct##TYPE = qMetaTypeConstructHelper; \ - typedef void (*QDestruct##TYPE)(TYPE *); \ - static const QDestruct##TYPE qDestruct##TYPE = qMetaTypeDestructHelper; -#else -# define Q_DECL_METATYPE_HELPER(TYPE) \ - typedef void *(*QCreate##TYPE)(const TYPE *); \ - static const QCreate##TYPE qCreate##TYPE = qMetaTypeCreateHelper; \ - typedef void (*QDelete##TYPE)(TYPE *); \ - static const QDelete##TYPE qDelete##TYPE = qMetaTypeDeleteHelper; \ - typedef void *(*QConstruct##TYPE)(void *, const TYPE *); \ - static const QConstruct##TYPE qConstruct##TYPE = qMetaTypeConstructHelper; \ - typedef void (*QDestruct##TYPE)(TYPE *); \ - static const QDestruct##TYPE qDestruct##TYPE = qMetaTypeDestructHelper; \ - typedef void (*QSave##TYPE)(QDataStream &, const TYPE *); \ - static const QSave##TYPE qSave##TYPE = qMetaTypeSaveHelper; \ - typedef void (*QLoad##TYPE)(QDataStream &, TYPE *); \ - static const QLoad##TYPE qLoad##TYPE = qMetaTypeLoadHelper; -#endif - -Q_DECL_METATYPE_HELPER(QFont) -Q_DECL_METATYPE_HELPER(QPixmap) -Q_DECL_METATYPE_HELPER(QBrush) -Q_DECL_METATYPE_HELPER(QColor) -Q_DECL_METATYPE_HELPER(QPalette) -Q_DECL_METATYPE_HELPER(QImage) -Q_DECL_METATYPE_HELPER(QPolygon) -Q_DECL_METATYPE_HELPER(QRegion) -Q_DECL_METATYPE_HELPER(QBitmap) -#ifndef QT_NO_CURSOR -Q_DECL_METATYPE_HELPER(QCursor) -#endif -#ifndef QT_NO_SHORTCUT -Q_DECL_METATYPE_HELPER(QKeySequence) -#endif -Q_DECL_METATYPE_HELPER(QPen) -Q_DECL_METATYPE_HELPER(QTextLength) -Q_DECL_METATYPE_HELPER(QTextFormat) -Q_DECL_METATYPE_HELPER(QMatrix) -Q_DECL_METATYPE_HELPER(QTransform) -#ifndef QT_NO_MATRIX4X4 -Q_DECL_METATYPE_HELPER(QMatrix4x4) -#endif -#ifndef QT_NO_VECTOR2D -Q_DECL_METATYPE_HELPER(QVector2D) -#endif -#ifndef QT_NO_VECTOR3D -Q_DECL_METATYPE_HELPER(QVector3D) -#endif -#ifndef QT_NO_VECTOR4D -Q_DECL_METATYPE_HELPER(QVector4D) -#endif -#ifndef QT_NO_QUATERNION -Q_DECL_METATYPE_HELPER(QQuaternion) -#endif -Q_DECL_METATYPE_HELPER(QPolygonF) +extern Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeGuiHelper; -#ifdef QT_NO_DATASTREAM -# define Q_IMPL_METATYPE_HELPER(TYPE) \ - { reinterpret_cast(qCreate##TYPE), \ - reinterpret_cast(qDelete##TYPE), \ - reinterpret_cast(qConstruct##TYPE), \ - reinterpret_cast(qDestruct##TYPE), \ - sizeof(TYPE) \ - } -#else -# define Q_IMPL_METATYPE_HELPER(TYPE) \ - { reinterpret_cast(qCreate##TYPE), \ - reinterpret_cast(qDelete##TYPE), \ - reinterpret_cast(qSave##TYPE), \ - reinterpret_cast(qLoad##TYPE), \ - reinterpret_cast(qConstruct##TYPE), \ - reinterpret_cast(qDestruct##TYPE), \ - sizeof(TYPE) \ - } -#endif +#define QT_IMPL_METATYPEINTERFACE_GUI_TYPES(MetaTypeName, MetaTypeId, RealName) \ + QMetaTypeInterface(static_cast(0)), -static const QMetaTypeGuiHelper qVariantGuiHelper[] = { - Q_IMPL_METATYPE_HELPER(QFont), - Q_IMPL_METATYPE_HELPER(QPixmap), - Q_IMPL_METATYPE_HELPER(QBrush), - Q_IMPL_METATYPE_HELPER(QColor), - Q_IMPL_METATYPE_HELPER(QPalette), - Q_IMPL_METATYPE_HELPER(QImage), - Q_IMPL_METATYPE_HELPER(QPolygon), - Q_IMPL_METATYPE_HELPER(QRegion), - Q_IMPL_METATYPE_HELPER(QBitmap), -#ifdef QT_NO_CURSOR - {0, 0, 0, 0, 0, 0, 0}, -#else - Q_IMPL_METATYPE_HELPER(QCursor), -#endif -#ifdef QT_NO_SHORTCUT - {0, 0, 0, 0, 0, 0, 0}, -#else - Q_IMPL_METATYPE_HELPER(QKeySequence), -#endif - Q_IMPL_METATYPE_HELPER(QPen), - Q_IMPL_METATYPE_HELPER(QTextLength), - Q_IMPL_METATYPE_HELPER(QTextFormat), - Q_IMPL_METATYPE_HELPER(QMatrix), - Q_IMPL_METATYPE_HELPER(QTransform), -#ifndef QT_NO_MATRIX4X4 - Q_IMPL_METATYPE_HELPER(QMatrix4x4), -#else - {0, 0, 0, 0, 0, 0, 0}, -#endif -#ifndef QT_NO_VECTOR2D - Q_IMPL_METATYPE_HELPER(QVector2D), -#else - {0, 0, 0, 0, 0, 0, 0}, -#endif -#ifndef QT_NO_VECTOR3D - Q_IMPL_METATYPE_HELPER(QVector3D), -#else - {0, 0, 0, 0, 0, 0, 0}, -#endif -#ifndef QT_NO_VECTOR4D - Q_IMPL_METATYPE_HELPER(QVector4D), -#else - {0, 0, 0, 0, 0, 0, 0}, -#endif -#ifndef QT_NO_QUATERNION - Q_IMPL_METATYPE_HELPER(QQuaternion), -#else - {0, 0, 0, 0, 0, 0, 0}, -#endif - Q_IMPL_METATYPE_HELPER(QPolygonF) +static const QMetaTypeInterface qVariantGuiHelper[] = { + QT_FOR_EACH_STATIC_GUI_CLASS(QT_IMPL_METATYPEINTERFACE_GUI_TYPES) }; +#undef QT_IMPL_METATYPEINTERFACE_GUI_TYPES + static const QVariant::Handler *qt_guivariant_last_handler = 0; int qRegisterGuiVariant() { diff --git a/src/widgets/kernel/qwidgetsvariant.cpp b/src/widgets/kernel/qwidgetsvariant.cpp index dcffa0e..2e945d2 100644 --- a/src/widgets/kernel/qwidgetsvariant.cpp +++ b/src/widgets/kernel/qwidgetsvariant.cpp @@ -45,6 +45,7 @@ #include "qsizepolicy.h" #include "private/qvariant_p.h" +#include QT_BEGIN_NAMESPACE @@ -137,82 +138,17 @@ static const QVariant::Handler widgets_handler = { #endif }; -struct QMetaTypeGuiHelper -{ - QMetaType::Creator creator; - QMetaType::Deleter deleter; -#ifndef QT_NO_DATASTREAM - QMetaType::SaveOperator saveOp; - QMetaType::LoadOperator loadOp; -#endif - QMetaType::Constructor constructor; - QMetaType::Destructor destructor; - int size; -}; - -extern Q_CORE_EXPORT const QMetaTypeGuiHelper *qMetaTypeWidgetsHelper; - - -#ifdef QT_NO_DATASTREAM -# define Q_DECL_METATYPE_HELPER(TYPE) \ - typedef void *(*QCreate##TYPE)(const TYPE *); \ - static const QCreate##TYPE qCreate##TYPE = qMetaTypeCreateHelper; \ - typedef void (*QDelete##TYPE)(TYPE *); \ - static const QDelete##TYPE qDelete##TYPE = qMetaTypeDeleteHelper; \ - typedef void *(*QConstruct##TYPE)(void *, const TYPE *); \ - static const QConstruct##TYPE qConstruct##TYPE = qMetaTypeConstructHelper; \ - typedef void (*QDestruct##TYPE)(TYPE *); \ - static const QDestruct##TYPE qDestruct##TYPE = qMetaTypeDestructHelper; -#else -# define Q_DECL_METATYPE_HELPER(TYPE) \ - typedef void *(*QCreate##TYPE)(const TYPE *); \ - static const QCreate##TYPE qCreate##TYPE = qMetaTypeCreateHelper; \ - typedef void (*QDelete##TYPE)(TYPE *); \ - static const QDelete##TYPE qDelete##TYPE = qMetaTypeDeleteHelper; \ - typedef void *(*QConstruct##TYPE)(void *, const TYPE *); \ - static const QConstruct##TYPE qConstruct##TYPE = qMetaTypeConstructHelper; \ - typedef void (*QDestruct##TYPE)(TYPE *); \ - static const QDestruct##TYPE qDestruct##TYPE = qMetaTypeDestructHelper; \ - typedef void (*QSave##TYPE)(QDataStream &, const TYPE *); \ - static const QSave##TYPE qSave##TYPE = qMetaTypeSaveHelper; \ - typedef void (*QLoad##TYPE)(QDataStream &, TYPE *); \ - static const QLoad##TYPE qLoad##TYPE = qMetaTypeLoadHelper; -#endif +extern Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeWidgetsHelper; -#ifndef QT_NO_ICON -Q_DECL_METATYPE_HELPER(QIcon) -#endif -Q_DECL_METATYPE_HELPER(QSizePolicy) - -#ifdef QT_NO_DATASTREAM -# define Q_IMPL_METATYPE_HELPER(TYPE) \ - { reinterpret_cast(qCreate##TYPE), \ - reinterpret_cast(qDelete##TYPE), \ - reinterpret_cast(qConstruct##TYPE), \ - reinterpret_cast(qDestruct##TYPE), \ - sizeof(TYPE) \ - } -#else -# define Q_IMPL_METATYPE_HELPER(TYPE) \ - { reinterpret_cast(qCreate##TYPE), \ - reinterpret_cast(qDelete##TYPE), \ - reinterpret_cast(qSave##TYPE), \ - reinterpret_cast(qLoad##TYPE), \ - reinterpret_cast(qConstruct##TYPE), \ - reinterpret_cast(qDestruct##TYPE), \ - sizeof(TYPE) \ - } -#endif +#define QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES(MetaTypeName, MetaTypeId, RealName) \ + QMetaTypeInterface(static_cast(0)), -static const QMetaTypeGuiHelper qVariantWidgetsHelper[] = { -#ifdef QT_NO_ICON - {0, 0, 0, 0}, -#else - Q_IMPL_METATYPE_HELPER(QIcon), -#endif - Q_IMPL_METATYPE_HELPER(QSizePolicy), +static const QMetaTypeInterface qVariantWidgetsHelper[] = { + QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES) }; +#undef QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES + extern Q_GUI_EXPORT const QVariant::Handler *qt_widgets_variant_handler; int qRegisterWidgetsVariant() -- 2.7.4