summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorStephen Kelly <stephen.kelly@kdab.com>2012-07-01 22:55:54 +0200
committerQt by Nokia <qt-info@nokia.com>2012-07-03 01:08:05 +0200
commit13e1e30ec39e9039930a25d851bb525ec486a15d (patch)
tree1570f7f7700e0ec2763c5b6113db2ea08d966e0c /tests/auto
parent2ec9d34df6003107c865661b8abfb9a9fa38c146 (diff)
Add constexpr template specializations for built in metatypes.
This will make it possible (in Qt 6) to remove the enums listing metatype ids. As it is constexpr, it can be used in switch statements just like enums, as enum values, and as template specialization values. Change-Id: I51293674c403714e34cb8a8b8953522fc97a740a Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
index b61625714b..c73c26a9ef 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
@@ -107,6 +107,7 @@ private slots:
void saveAndLoadBuiltin();
void saveAndLoadCustom();
void metaObject();
+ void constexprMetaTypeIds();
};
struct Foo { int i; };
@@ -1632,6 +1633,46 @@ void tst_QMetaType::metaObject()
QCOMPARE(QMetaType(QMetaType::Int).metaObject(), static_cast<const QMetaObject *>(0));
}
+#define METATYPE_ID_FUNCTION(Type, MetaTypeId, Name) \
+ case ::qMetaTypeId< Name >(): metaType = MetaTypeIdStruct<MetaTypeId>::Value;
+
+template<int>
+struct MetaTypeIdStruct
+{
+};
+
+#define METATYPE_ID_STRUCT(Type, MetaTypeId, Name) \
+template<> \
+struct MetaTypeIdStruct< ::qMetaTypeId< Name >()> \
+{ \
+ enum { Value = ::qMetaTypeId< Name >() }; \
+};
+
+#if defined(Q_COMPILER_CONSTEXPR)
+QT_FOR_EACH_STATIC_TYPE(METATYPE_ID_STRUCT)
+
+template<int i = ::qMetaTypeId<int>()>
+struct MetaTypeIdStructDefaultTemplateValue
+{
+ enum { Value };
+};
+#endif
+
+void tst_QMetaType::constexprMetaTypeIds()
+{
+ int id = 0;
+ int metaType;
+
+ switch(id) {
+#if defined(Q_COMPILER_CONSTEXPR)
+ QT_FOR_EACH_STATIC_TYPE(METATYPE_ID_FUNCTION)
+ metaType = MetaTypeIdStructDefaultTemplateValue<>::Value;
+#endif
+ default:;
+ }
+ Q_UNUSED(metaType);
+}
+
// Compile-time test, it should be possible to register function pointer types
class Undefined;