QMetatype: Register the QMetaObject of a Q_ENUM or Q_FLAG

Change-Id: Id1343c3fb51d88a43d4d97e8859dbe2b0cfbea34
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
This commit is contained in:
Olivier Goffart 2014-08-10 14:11:26 +02:00 committed by Olivier Goffart (Woboq GmbH)
parent 9e6190cff3
commit d43d03529f
3 changed files with 32 additions and 3 deletions

View File

@ -282,7 +282,7 @@ struct DefinedTypesFilter {
\value NeedsDestruction This type has a non-trivial destructor. If the flag is not set calls to the destructor are not necessary before discarding objects.
\value MovableType An instance of a type having this attribute can be safely moved by memcpy.
\omitvalue SharedPointerToQObject
\omitvalue IsEnumeration
\value IsEnumeration This type is an enumeration
\value PointerToQObject This type is a pointer to a derived of QObject
\omitvalue WeakPointerToQObject
\omitvalue TrackingPointerToQObject
@ -376,6 +376,10 @@ struct DefinedTypesFilter {
QMetaObject. This can be used to retrieve QMetaMethod and QMetaProperty and use them on a
pointer of this type. (given by QVariant::data for example)
If the type is an enumaration, flags contains QMetaType::IsEnumeration, and this function
returns the QMetaObject of the enclosing object if the enum was registered as a Q_ENUM or 0
otherwise
\sa QMetaType::metaObjectForType(), QMetaType::flags()
*/

View File

@ -1363,12 +1363,25 @@ namespace QtPrivate
enum { Value = sizeof(checkType(static_cast<T*>(0))) == sizeof(void*) };
};
char qt_getEnumMetaObject(...);
char qt_getEnumMetaObject(); // Workaround bugs in MSVC.
template<typename T>
struct IsQEnumHelper {
static const T &declval();
enum { Value = sizeof(qt_getEnumMetaObject(declval())) == sizeof(QMetaObject*) };
};
template<typename T, typename Enable = void>
struct MetaObjectForType
{
static inline const QMetaObject *value() { return 0; }
};
template<>
struct MetaObjectForType<void>
{
static inline const QMetaObject *value() { return Q_NULLPTR; }
};
template<typename T>
struct MetaObjectForType<T*, typename QEnableIf<IsPointerToTypeDerivedFromQObject<T*>::Value>::Type>
{
@ -1379,6 +1392,11 @@ namespace QtPrivate
{
static inline const QMetaObject *value() { return &T::staticMetaObject; }
};
template<typename T>
struct MetaObjectForType<T, typename QEnableIf<IsQEnumHelper<T>::Value>::Type >
{
static inline const QMetaObject *value() { return qt_getEnumMetaObject(T()); }
};
template<typename T>
struct IsSharedPointerToTypeDerivedFromQObject

View File

@ -1835,12 +1835,17 @@ void tst_QMetaType::saveAndLoadCustom()
QCOMPARE(stream.status(), QDataStream::ReadPastEnd);
}
struct MyGadget {
class MyGadget {
Q_GADGET;
public:
enum MyEnum { Val1, Val2, Val3 };
Q_ENUM(MyEnum)
};
Q_DECLARE_METATYPE(MyGadget);
Q_DECLARE_METATYPE(const QMetaObject *);
Q_DECLARE_METATYPE(Qt::ScrollBarPolicy);
Q_DECLARE_METATYPE(MyGadget::MyEnum);
void tst_QMetaType::metaObject_data()
{
@ -1855,6 +1860,8 @@ void tst_QMetaType::metaObject_data()
QTest::newRow("int") << int(QMetaType::Int) << static_cast<const QMetaObject *>(0) << false << false;
QTest::newRow("QEasingCurve") << ::qMetaTypeId<QEasingCurve>() << &QEasingCurve::staticMetaObject << true << false;
QTest::newRow("MyGadget") << ::qMetaTypeId<MyGadget>() << &MyGadget::staticMetaObject << true << false;
QTest::newRow("MyEnum") << ::qMetaTypeId<MyGadget::MyEnum>() << &MyGadget::staticMetaObject << false << false;
QTest::newRow("Qt::ScrollBarPolicy") << ::qMetaTypeId<Qt::ScrollBarPolicy>() << &QObject::staticQtMetaObject << false << false;
}