From 46ca473d569b92cdfc567c2096b1c4af3ba68f80 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 30 Jul 2009 10:52:40 +1000 Subject: Allow retrieval of interface IId from class type. This method is necessary for QML to support Qt interfaces, but probably shouldn't be used otherwise. Reviewed-by: Roberto Raggi --- src/corelib/kernel/qobject.h | 10 ++++++++++ tests/auto/qobject/tst_qobject.cpp | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/corelib/kernel/qobject.h b/src/corelib/kernel/qobject.h index 1fb216b..4715626 100644 --- a/src/corelib/kernel/qobject.h +++ b/src/corelib/kernel/qobject.h @@ -379,6 +379,9 @@ inline QList qFindChildren(const QObject *o, const QRegExp &re) #endif // Q_MOC_RUN +template inline const char * qobject_interface_iid() +{ return 0; } + template inline T qobject_cast_helper(QObject *object, T) { return static_cast(((T)0)->staticMetaObject.cast(object)); } @@ -395,6 +398,8 @@ inline T qobject_cast(const QObject *object) #ifndef Q_MOC_RUN # define Q_DECLARE_INTERFACE(IFace, IId) \ + template <> inline const char *qobject_interface_iid() \ + { return IId; } \ template <> inline IFace *qobject_cast_helper(QObject *object, IFace *) \ { return (IFace *)(object ? object->qt_metacast(IId) : 0); } \ template <> inline IFace *qobject_cast_helper(const QObject *object, IFace *) \ @@ -458,8 +463,13 @@ inline T qobject_cast(const QObject *object) } +template inline const char * qobject_interface_iid() +{ return 0; } + #ifndef Q_MOC_RUN # define Q_DECLARE_INTERFACE(IFace, IId) \ + template <> inline const char *qobject_interface_iid() \ + { return IId; } \ template <> inline IFace *qobject_cast(QObject *object) \ { return reinterpret_cast((object ? object->qt_metacast(IId) : 0)); } \ template <> inline IFace *qobject_cast(const QObject *object) \ diff --git a/tests/auto/qobject/tst_qobject.cpp b/tests/auto/qobject/tst_qobject.cpp index 4f25af6..3df83d7 100644 --- a/tests/auto/qobject/tst_qobject.cpp +++ b/tests/auto/qobject/tst_qobject.cpp @@ -118,6 +118,7 @@ private slots: void connectToSender(); void qobjectConstCast(); void uniqConnection(); + void interfaceIid(); protected: }; @@ -2887,5 +2888,15 @@ void tst_QObject::uniqConnection() delete r2; } +void tst_QObject::interfaceIid() +{ + QCOMPARE(QByteArray(qobject_interface_iid()), + QByteArray(Bleh_iid)); + QCOMPARE(QByteArray(qobject_interface_iid()), + QByteArray("com.qtest.foobar")); + QCOMPARE(QByteArray(qobject_interface_iid()), + QByteArray()); +} + QTEST_MAIN(tst_QObject) #include "tst_qobject.moc" -- cgit v0.12