From 08863b6fdaa8383e2274826db3ec42c4d4f11576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Tue, 29 Nov 2011 15:42:33 +0100 Subject: Refactor QVariant handlers. QVariant implementation is based on delegation to a handler. The handler has rather simple construction, it is a set of function that implements a switch statement over known types and redirects calls to a right method of an encapsulated types instance. Unfortunately after qt modularization project, it is not easy to use types directly from different modules, as they can be undefined or completely unaccessible. Which means that each module has to implement own handler to cooperate correctly with QVariant. We can suspect that list of modules known to QVariant will grow and it is not limited to GUI, Widgets and Core, therefore it would be nice to have an unified, from performance and source code point of view, way of working with handlers. This patch is an attempt to cleanup handlers. Keynotes: - Each handler is working only on types defined in the same module - Core handler implements handling of primitive types too - Custom types have an own handler - Each handler is independent which means that dispatch between handlers is done on QVariant level - Handlers might be registered / unregistered using same interface Change-Id: Ib096df65e2c4ce464bc7a684aade5af7d1264c24 Reviewed-by: Olivier Goffart --- src/widgets/kernel/qwidgetsvariant.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/widgets') diff --git a/src/widgets/kernel/qwidgetsvariant.cpp b/src/widgets/kernel/qwidgetsvariant.cpp index 97a8238ca7..92c8d9e6c2 100644 --- a/src/widgets/kernel/qwidgetsvariant.cpp +++ b/src/widgets/kernel/qwidgetsvariant.cpp @@ -49,7 +49,7 @@ QT_BEGIN_NAMESPACE - +namespace { static void construct(QVariant::Private *x, const void *copy) { switch (x->type) { @@ -97,10 +97,8 @@ static bool isNull(const QVariant::Private *d) case QVariant::Icon: return v_cast(d)->isNull(); #endif - default: - Q_ASSERT(false); } - return true; + return false; } static bool compare(const QVariant::Private *a, const QVariant::Private *b) @@ -119,6 +117,15 @@ static bool compare(const QVariant::Private *a, const QVariant::Private *b) return false; } +static bool convert(const QVariant::Private *d, QVariant::Type type, void *result, bool *ok) +{ + Q_UNUSED(d); + Q_UNUSED(type); + Q_UNUSED(result); + if (ok) + *ok = false; + return false; +} static const QVariant::Handler widgets_handler = { construct, @@ -129,7 +136,7 @@ static const QVariant::Handler widgets_handler = { 0, #endif compare, - 0, + convert, 0, #if !defined(QT_NO_DEBUG_STREAM) && !defined(Q_BROKEN_DEBUG_STREAM) 0 @@ -138,8 +145,6 @@ static const QVariant::Handler widgets_handler = { #endif }; -extern Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeWidgetsHelper; - #define QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES(MetaTypeName, MetaTypeId, RealName) \ QT_METATYPE_INTERFACE_INIT(RealName), @@ -149,18 +154,20 @@ static const QMetaTypeInterface qVariantWidgetsHelper[] = { #undef QT_IMPL_METATYPEINTERFACE_WIDGETS_TYPES -extern Q_GUI_EXPORT const QVariant::Handler *qt_widgets_variant_handler; +} // namespace + +extern Q_CORE_EXPORT const QMetaTypeInterface *qMetaTypeWidgetsHelper; void qRegisterWidgetsVariant() { - qt_widgets_variant_handler = &widgets_handler; qMetaTypeWidgetsHelper = qVariantWidgetsHelper; + QVariantPrivate::registerHandler(QModulesPrivate::Widgets, &widgets_handler); } Q_CONSTRUCTOR_FUNCTION(qRegisterWidgetsVariant) void qUnregisterWidgetsVariant() { - qt_widgets_variant_handler = 0; + QVariantPrivate::unregisterHandler(QModulesPrivate::Widgets); qMetaTypeWidgetsHelper = 0; } Q_DESTRUCTOR_FUNCTION(qUnregisterWidgetsVariant) -- cgit v1.2.3