diff options
author | Jędrzej Nowacki <jedrzej.nowacki@nokia.com> | 2011-11-29 15:42:33 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-04 14:25:25 +0100 |
commit | 08863b6fdaa8383e2274826db3ec42c4d4f11576 (patch) | |
tree | a3cc772ada845bffa60f5b186defeb469bf1f5af /src/corelib/kernel/qvariant_p.h | |
parent | 52fc6694b87e93fe0828424bb26d9279785de3e7 (diff) |
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 <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/kernel/qvariant_p.h')
-rw-r--r-- | src/corelib/kernel/qvariant_p.h | 54 |
1 files changed, 11 insertions, 43 deletions
diff --git a/src/corelib/kernel/qvariant_p.h b/src/corelib/kernel/qvariant_p.h index a90164fa71..0436e9fe29 100644 --- a/src/corelib/kernel/qvariant_p.h +++ b/src/corelib/kernel/qvariant_p.h @@ -58,8 +58,6 @@ #include <QtCore/qglobal.h> #include <QtCore/qvariant.h> -#include <QtCore/private/qmetatype_p.h> - #include "qmetatypeswitcher_p.h" QT_BEGIN_NAMESPACE @@ -172,24 +170,7 @@ class QVariantComparator { }; template<typename T> struct FilteredComparator<T, /* IsAcceptedType = */ false> { - static bool compare(const QVariant::Private *m_a, const QVariant::Private *m_b) - { - const char *const typeName = QMetaType::typeName(m_a->type); - if (Q_UNLIKELY(!typeName) && Q_LIKELY(!QMetaType::isRegistered(m_a->type))) - qFatal("QVariant::compare: type %d unknown to QVariant.", m_a->type); - - const void *a_ptr = m_a->is_shared ? m_a->data.shared->ptr : &(m_a->data.ptr); - const void *b_ptr = m_b->is_shared ? m_b->data.shared->ptr : &(m_b->data.ptr); - - uint typeNameLen = qstrlen(typeName); - if (typeNameLen > 0 && typeName[typeNameLen - 1] == '*') - return *static_cast<void *const *>(a_ptr) == *static_cast<void *const *>(b_ptr); - - if (m_a->is_null && m_b->is_null) - return true; - - return !memcmp(a_ptr, b_ptr, QMetaType::sizeOf(m_a->type)); - } + static bool compare(const QVariant::Private *, const QVariant::Private *) { return false; } }; public: QVariantComparator(const QVariant::Private *a, const QVariant::Private *b) @@ -372,22 +353,8 @@ public: m_x->is_shared = false; return; } - const uint size = QMetaType::sizeOf(m_x->type); - if (!size) { - m_x->type = QVariant::Invalid; - return; - } - - // this logic should match with QVariantIntegrator::CanUseInternalSpace - if (size <= sizeof(QVariant::Private::Data) - && (QMetaType::typeFlags(m_x->type) & QMetaType::MovableType)) { - QMetaType::construct(m_x->type, &m_x->data.ptr, m_copy); - m_x->is_shared = false; - } else { - void *ptr = QMetaType::create(m_x->type, m_copy); - m_x->is_shared = true; - m_x->data.shared = new QVariant::PrivateShared(ptr); - } + qWarning("Trying to construct an instance of an invalid type, type id: %i", m_x->type); + m_x->type = QVariant::Invalid; } void delegate(const void*) @@ -436,13 +403,9 @@ public: void delegate(const QMetaTypeSwitcher::UnknownType*) { - // This is not a static type, so lets delegate everyting to QMetaType - if (!m_d->is_shared) { - QMetaType::destruct(m_d->type, &m_d->data.ptr); - } else { - QMetaType::destroy(m_d->type, m_d->data.shared->ptr); - delete m_d->data.shared; - } + if (m_d->type == QVariant::UserType) + return; + qWarning("Trying to destruct an instance of an invalid type, type id: %i", m_d->type); } // Ignore nonconstructible type void delegate(const void*) {} @@ -450,6 +413,11 @@ private: QVariant::Private *m_d; }; +namespace QVariantPrivate { +Q_CORE_EXPORT void registerHandler(const int /* Modules::Names */ name, const QVariant::Handler *handler); +Q_CORE_EXPORT void unregisterHandler(const int /* Modules::Names */ name); +} + QT_END_NAMESPACE #endif // QVARIANT_P_H |