summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>2011-10-26 11:09:29 +0200
committerQt by Nokia <qt-info@nokia.com>2011-11-09 10:11:14 +0100
commit2b39be6dd5d111482e5df06ac6dea18ca338d9f0 (patch)
tree13f9b75bdb796db88330f6b67e38e0619254adaf /src/corelib
parenteda469d3a093b488d3cff64e0f8e3c69d34bf84a (diff)
Implement QMetaTypeSwitcher.
Currently one of the most common coding pattern, when working with QMetaType types, is to switch over all types ids to convert a given type id to a real c++ type. The pattern is not perfect, because of: - code duplication - to convert type id to a real type a user has to write own switch case - maintenance - adding new type to QMetaType::Types doesn't propagate to other parts of code. Proposed type switcher can solve the issue by switching type id and delegating found c++ type. The class is created for internal use only and was created to simplify common problems in QtDeclarative, QtScript and QVariant. Change-Id: I0567ef908024b3b05ee18126f98a73a74748fbd2 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/kernel/kernel.pri3
-rw-r--r--src/corelib/kernel/qmetatypeswitcher_p.h101
2 files changed, 103 insertions, 1 deletions
diff --git a/src/corelib/kernel/kernel.pri b/src/corelib/kernel/kernel.pri
index 9b656d8b9b..409c71076c 100644
--- a/src/corelib/kernel/kernel.pri
+++ b/src/corelib/kernel/kernel.pri
@@ -37,7 +37,8 @@ HEADERS += \
kernel/qfunctions_p.h \
kernel/qmath.h \
kernel/qsystemerror_p.h \
- kernel/qmetatype_p.h
+ kernel/qmetatype_p.h \
+ kernel/qmetatypeswitcher_p.h \
SOURCES += \
kernel/qabstracteventdispatcher.cpp \
diff --git a/src/corelib/kernel/qmetatypeswitcher_p.h b/src/corelib/kernel/qmetatypeswitcher_p.h
new file mode 100644
index 0000000000..2a443aa60e
--- /dev/null
+++ b/src/corelib/kernel/qmetatypeswitcher_p.h
@@ -0,0 +1,101 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
+**
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QMETATYPESWITCHER_P_H
+#define QMETATYPESWITCHER_P_H
+
+#include "qmetatype.h"
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+class QMetaTypeSwitcher {
+public:
+
+ typedef void *UnknownType;
+ template<class ReturnType, class DelegateObject>
+ static ReturnType switcher(DelegateObject &logic, int type, const void *data);
+};
+
+
+#define QT_METATYPE_SWICHER_CASE_PRIMITIVE(TypeName, TypeId, Name)\
+ case QMetaType::TypeName: return logic.delegate(static_cast<const Name *>(data));
+
+#define QT_METATYPE_SWICHER_CASE_PRIMITIVE_POINTER(TypeName, TypeId, Name)\
+ case QMetaType::TypeName: return logic.delegate(static_cast< Name * const *>(data));
+
+#define QT_METATYPE_SWICHER_CASE_POINTER(TypeName, TypeId, Name)\
+ case QMetaType::TypeName: return logic.delegate(static_cast< QT_PREPEND_NAMESPACE(Name) * const *>(data));
+
+#define QT_METATYPE_SWICHER_CASE_QCLASS(TypeName, TypeId, Name)\
+ case QMetaType::TypeName: return logic.delegate(static_cast<const QT_PREPEND_NAMESPACE(Name) *>(data));
+
+template<class ReturnType, class DelegateObject>
+ReturnType QMetaTypeSwitcher::switcher(DelegateObject &logic, int type, const void *data)
+{
+ switch (QMetaType::Type(type)) {
+ QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(QT_METATYPE_SWICHER_CASE_PRIMITIVE)
+ QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(QT_METATYPE_SWICHER_CASE_PRIMITIVE_POINTER)
+ QT_FOR_EACH_STATIC_CORE_POINTER(QT_METATYPE_SWICHER_CASE_POINTER)
+ QT_FOR_EACH_STATIC_CORE_CLASS(QT_METATYPE_SWICHER_CASE_QCLASS)
+ QT_FOR_EACH_STATIC_CORE_TEMPLATE(QT_METATYPE_SWICHER_CASE_QCLASS)
+ QT_FOR_EACH_STATIC_GUI_CLASS(QT_METATYPE_SWICHER_CASE_QCLASS)
+ QT_FOR_EACH_STATIC_WIDGETS_CLASS(QT_METATYPE_SWICHER_CASE_QCLASS)
+
+ default:
+ return logic.delegate(static_cast<const UnknownType *>(data));
+ }
+}
+
+#undef QT_METATYPE_SWICHER_CASE_PRIMITIVE
+#undef QT_METATYPE_SWICHER_CASE_PRIMITIVE_POINTER
+#undef QT_METATYPE_SWICHER_CASE_QCLASS
+#undef QT_METATYPE_SWICHER_CASE_POINTER
+
+#endif // QMETATYPESWITCHER_P_H