summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJędrzej Nowacki <jedrzej.nowacki@nokia.com>2012-03-13 14:15:53 +0100
committerQt by Nokia <qt-info@nokia.com>2012-03-14 09:45:07 +0100
commit22494ea8e82d5d5588e67e8067f0273687ed9098 (patch)
treef0b7a4f6d498ba8e109677756183ff371f5f9171
parente92ceba13d751c151604726c3fa67ff4c918629f (diff)
Unify QMetaType::TypeFlags detection.
Duplicated code was removed. As an side effect: - one runtime flag check was replaced by a compile time check. - is enum flag can be used together with built-in types. Change-Id: I54173e7b07ce7e487d3cc21ba24dcccd28b5d049 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
-rw-r--r--src/corelib/kernel/qmetatype.cpp8
-rw-r--r--src/corelib/kernel/qmetatype.h24
-rw-r--r--src/corelib/kernel/qmetatype_p.h4
-rw-r--r--tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp48
4 files changed, 44 insertions, 40 deletions
diff --git a/src/corelib/kernel/qmetatype.cpp b/src/corelib/kernel/qmetatype.cpp
index 5032cf9590..98f53f7dd1 100644
--- a/src/corelib/kernel/qmetatype.cpp
+++ b/src/corelib/kernel/qmetatype.cpp
@@ -1578,13 +1578,9 @@ class Flags
template<typename T, bool IsAcceptedType = DefinedTypesFilter::Acceptor<T>::IsAccepted>
struct FlagsImpl
{
- static quint32 Flags(const int type)
+ static quint32 Flags(const int /* type */)
{
- return (!QTypeInfo<T>::isStatic * QMetaType::MovableType)
- | (QTypeInfo<T>::isComplex * QMetaType::NeedsConstruction)
- | (QTypeInfo<T>::isComplex * QMetaType::NeedsDestruction)
- | (type == QMetaType::QObjectStar ? QMetaType::PointerToQObject : 0)
- | (type == QMetaType::QWidgetStar ? QMetaType::PointerToQObject : 0);
+ return QtPrivate::QMetaTypeTypeFlags<T>::Flags;
}
};
template<typename T>
diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h
index 0010c27beb..bf1b126200 100644
--- a/src/corelib/kernel/qmetatype.h
+++ b/src/corelib/kernel/qmetatype.h
@@ -435,6 +435,17 @@ namespace QtPrivate {
template <class Result, class Arg0> struct IsPointerToTypeDerivedFromQObject<Result(*)(Arg0)> { enum { Value = false }; };
template <class Result, class Arg0, class Arg1> struct IsPointerToTypeDerivedFromQObject<Result(*)(Arg0, Arg1)> { enum { Value = false }; };
template <class Result, class Arg0, class Arg1, class Arg2> struct IsPointerToTypeDerivedFromQObject<Result(*)(Arg0, Arg1, Arg2)> { enum { Value = false }; };
+
+ template<typename T>
+ struct QMetaTypeTypeFlags
+ {
+ enum { Flags = (!QTypeInfo<T>::isStatic ? QMetaType::MovableType : 0)
+ | (QTypeInfo<T>::isComplex ? QMetaType::NeedsConstruction : 0)
+ | (QTypeInfo<T>::isComplex ? QMetaType::NeedsDestruction : 0)
+ | (IsPointerToTypeDerivedFromQObject<T>::Value ? QMetaType::PointerToQObject : 0)
+ | (Q_IS_ENUM(T) ? QMetaType::IsEnumeration : 0)
+ };
+ };
}
template <typename T>
@@ -448,18 +459,7 @@ int qRegisterMetaType(const char *typeName
if (typedefOf != -1)
return QMetaType::registerTypedef(typeName, typedefOf);
- QMetaType::TypeFlags flags;
- if (!QTypeInfo<T>::isStatic)
- flags |= QMetaType::MovableType;
- if (QTypeInfo<T>::isComplex) {
- flags |= QMetaType::NeedsConstruction;
- flags |= QMetaType::NeedsDestruction;
- }
- if (QtPrivate::IsPointerToTypeDerivedFromQObject<T>::Value)
- flags |= QMetaType::PointerToQObject;
- if (Q_IS_ENUM(T))
- flags |= QMetaType::IsEnumeration;
-
+ QMetaType::TypeFlags flags(QtPrivate::QMetaTypeTypeFlags<T>::Flags);
return QMetaType::registerType(typeName, qMetaTypeDeleteHelper<T>,
qMetaTypeCreateHelper<T>,
qMetaTypeDestructHelper<T>,
diff --git a/src/corelib/kernel/qmetatype_p.h b/src/corelib/kernel/qmetatype_p.h
index b50521c7a6..c73f8d0a20 100644
--- a/src/corelib/kernel/qmetatype_p.h
+++ b/src/corelib/kernel/qmetatype_p.h
@@ -155,9 +155,7 @@ public:
/*constructor*/(qMetaTypeConstructHelper<Type>), \
/*destructor*/(qMetaTypeDestructHelper<Type>), \
/*size*/(QTypeInfo<Type>::sizeOf), \
- /*flags*/(!QTypeInfo<Type>::isStatic * QMetaType::MovableType) \
- | (QTypeInfo<Type>::isComplex * QMetaType::NeedsConstruction) \
- | (QTypeInfo<Type>::isComplex * QMetaType::NeedsDestruction) \
+ /*flags*/QtPrivate::QMetaTypeTypeFlags<Type>::Flags \
}
diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
index bc5fa2716b..a3959f6d33 100644
--- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
+++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp
@@ -717,39 +717,47 @@ Q_DECLARE_METATYPE(QPairPC)
Q_DECLARE_METATYPE(QPairPM)
Q_DECLARE_METATYPE(QPairPP)
+enum FlagsDataEnum {};
+Q_DECLARE_METATYPE(FlagsDataEnum);
+
void tst_QMetaType::flags_data()
{
QTest::addColumn<int>("type");
QTest::addColumn<bool>("isMovable");
QTest::addColumn<bool>("isComplex");
QTest::addColumn<bool>("isPointerToQObject");
+ QTest::addColumn<bool>("isEnum");
#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \
- QTest::newRow(#RealType) << MetaTypeId << bool(!QTypeInfo<RealType>::isStatic) << bool(QTypeInfo<RealType>::isComplex) << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<RealType>::Value);
+ QTest::newRow(#RealType) << MetaTypeId \
+ << bool(!QTypeInfo<RealType>::isStatic) \
+ << bool(QTypeInfo<RealType>::isComplex) \
+ << bool(QtPrivate::IsPointerToTypeDerivedFromQObject<RealType>::Value) \
+ << bool(Q_IS_ENUM(RealType));
QT_FOR_EACH_STATIC_CORE_CLASS(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_PRIMITIVE_POINTER(ADD_METATYPE_TEST_ROW)
QT_FOR_EACH_STATIC_CORE_POINTER(ADD_METATYPE_TEST_ROW)
#undef ADD_METATYPE_TEST_ROW
- QTest::newRow("TestSpace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << false << true << false;
- QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << false << true << false;
- QTest::newRow("CustomMovable") << ::qMetaTypeId<CustomMovable>() << true << true << false;
- QTest::newRow("CustomObject*") << ::qMetaTypeId<CustomObject*>() << true << false << true;
- QTest::newRow("CustomMultiInheritanceObject*") << ::qMetaTypeId<CustomMultiInheritanceObject*>() << true << false << true;
- QTest::newRow("QPair<C,C>") << ::qMetaTypeId<QPair<C,C> >() << false << true << false;
- QTest::newRow("QPair<C,M>") << ::qMetaTypeId<QPair<C,M> >() << false << true << false;
- QTest::newRow("QPair<C,P>") << ::qMetaTypeId<QPair<C,P> >() << false << true << false;
- QTest::newRow("QPair<M,C>") << ::qMetaTypeId<QPair<M,C> >() << false << true << false;
- QTest::newRow("QPair<M,M>") << ::qMetaTypeId<QPair<M,M> >() << true << true << false;
- QTest::newRow("QPair<M,P>") << ::qMetaTypeId<QPair<M,P> >() << true << true << false;
- QTest::newRow("QPair<P,C>") << ::qMetaTypeId<QPair<P,C> >() << false << true << false;
- QTest::newRow("QPair<P,M>") << ::qMetaTypeId<QPair<P,M> >() << true << true << false;
- QTest::newRow("QPair<P,P>") << ::qMetaTypeId<QPair<P,P> >() << true << false << false;
+ QTest::newRow("TestSpace::Foo") << ::qMetaTypeId<TestSpace::Foo>() << false << true << false << false;
+ QTest::newRow("Whity<double>") << ::qMetaTypeId<Whity<double> >() << false << true << false << false;
+ QTest::newRow("CustomMovable") << ::qMetaTypeId<CustomMovable>() << true << true << false << false;
+ QTest::newRow("CustomObject*") << ::qMetaTypeId<CustomObject*>() << true << false << true << false;
+ QTest::newRow("CustomMultiInheritanceObject*") << ::qMetaTypeId<CustomMultiInheritanceObject*>() << true << false << true << false;
+ QTest::newRow("QPair<C,C>") << ::qMetaTypeId<QPair<C,C> >() << false << true << false << false;
+ QTest::newRow("QPair<C,M>") << ::qMetaTypeId<QPair<C,M> >() << false << true << false << false;
+ QTest::newRow("QPair<C,P>") << ::qMetaTypeId<QPair<C,P> >() << false << true << false << false;
+ QTest::newRow("QPair<M,C>") << ::qMetaTypeId<QPair<M,C> >() << false << true << false << false;
+ QTest::newRow("QPair<M,M>") << ::qMetaTypeId<QPair<M,M> >() << true << true << false << false;
+ QTest::newRow("QPair<M,P>") << ::qMetaTypeId<QPair<M,P> >() << true << true << false << false;
+ QTest::newRow("QPair<P,C>") << ::qMetaTypeId<QPair<P,C> >() << false << true << false << false;
+ QTest::newRow("QPair<P,M>") << ::qMetaTypeId<QPair<P,M> >() << true << true << false << false;
+ QTest::newRow("QPair<P,P>") << ::qMetaTypeId<QPair<P,P> >() << true << false << false << false;
+ QTest::newRow("FlagsDataEnum") << ::qMetaTypeId<FlagsDataEnum>() << false << true << false << true;
// invalid ids.
- QTest::newRow("-1") << -1 << false << false << false;
- QTest::newRow("-124125534") << -124125534 << false << false << false;
- QTest::newRow("124125534") << 124125534 << false << false << false;
-
+ QTest::newRow("-1") << -1 << false << false << false << false;
+ QTest::newRow("-124125534") << -124125534 << false << false << false << false;
+ QTest::newRow("124125534") << 124125534 << false << false << false << false;
}
void tst_QMetaType::flags()
@@ -758,11 +766,13 @@ void tst_QMetaType::flags()
QFETCH(bool, isMovable);
QFETCH(bool, isComplex);
QFETCH(bool, isPointerToQObject);
+ QFETCH(bool, isEnum);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsConstruction), isComplex);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::NeedsDestruction), isComplex);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::MovableType), isMovable);
QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::PointerToQObject), isPointerToQObject);
+ QCOMPARE(bool(QMetaType::typeFlags(type) & QMetaType::IsEnumeration), isEnum);
}
void tst_QMetaType::flagsStaticLess_data()