diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2012-02-17 22:12:05 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-20 11:28:38 +0100 |
commit | 5640b0b4439941c621b4f0efec541e387abb7daf (patch) | |
tree | 4f038d4270c54a56415d34dc141520be8dac4ade | |
parent | 9c1680a7eda7f465d1db0fe1298d7aabd744bd15 (diff) |
Add QMetaMethod::isValid() function
This function provides a proper way of determining whether a function
returned by QMetaObject::method() is valid. (Checking whether
signature() returns a 0 pointer, which e.g. testlib does, is not an
ideal API -- especially given that signature() will soon be removed
and replaced by a function that returns a QByteArray.)
Change-Id: I644f476b09904925f2042945f5d0ad744482b682
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
-rw-r--r-- | src/corelib/kernel/qmetaobject.cpp | 8 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobject.h | 2 | ||||
-rw-r--r-- | tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp | 15 |
3 files changed, 25 insertions, 0 deletions
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index 09d6713a43..03fc90b620 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -1249,6 +1249,14 @@ bool QMetaObject::invokeMethod(QObject *obj, */ /*! + \fn bool QMetaMethod::isValid() const + \since 5.0 + + Returns true if this method is valid (can be introspected and + invoked), otherwise returns false. +*/ + +/*! \fn const QMetaObject *QMetaMethod::enclosingMetaObject() const \internal */ diff --git a/src/corelib/kernel/qmetaobject.h b/src/corelib/kernel/qmetaobject.h index 650369164c..9e51af7556 100644 --- a/src/corelib/kernel/qmetaobject.h +++ b/src/corelib/kernel/qmetaobject.h @@ -134,6 +134,8 @@ public: val0, val1, val2, val3, val4, val5, val6, val7, val8, val9); } + inline bool isValid() const { return mobj != 0; } + private: const QMetaObject *mobj; uint handle; diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp index cb34d411fd..1651d00738 100644 --- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp +++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp @@ -52,6 +52,8 @@ class tst_QMetaMethod : public QObject private slots: void method_data(); void method(); + + void invalidMethod(); }; struct CustomType { }; @@ -597,6 +599,7 @@ void tst_QMetaMethod::method() QVERIFY(index != -1); QMetaMethod method = (methodType == QMetaMethod::Constructor) ? mo->constructor(index) : mo->method(index); + QVERIFY(method.isValid()); QCOMPARE(method.methodType(), methodType); QCOMPARE(method.access(), access); @@ -611,5 +614,17 @@ void tst_QMetaMethod::method() QCOMPARE(method.parameterNames(), parameterNames); } +void tst_QMetaMethod::invalidMethod() +{ + QMetaMethod method; + QVERIFY(!method.isValid()); + + QMetaMethod method2 = staticMetaObject.method(staticMetaObject.methodCount()); + QVERIFY(!method2.isValid()); + + QMetaMethod method3 = staticMetaObject.method(-1); + QVERIFY(!method3.isValid()); +} + QTEST_MAIN(tst_QMetaMethod) #include "tst_qmetamethod.moc" |