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>
This commit is contained in:
Jędrzej Nowacki 2012-03-13 14:15:53 +01:00 committed by Qt by Nokia
parent e92ceba13d
commit 22494ea8e8
4 changed files with 44 additions and 40 deletions

View File

@ -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>

View File

@ -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>,

View File

@ -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 \
}

View File

@ -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()