summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Teske <daniel.teske@digia.com>2014-05-06 18:33:10 +0200
committerDaniel Teske <daniel.teske@digia.com>2014-05-12 14:54:36 +0200
commitce1a691abe0475a6f75ff6d6059df8b25a775fe9 (patch)
treedfde08065b0562f4ceb47784ae99a8f65dd85dcc
parent248964789a9eb7a759be63cabf68a3df3e56c023 (diff)
downloadqt-creator-ce1a691abe0475a6f75ff6d6059df8b25a775fe9.tar.gz
PluginManager: Introduce a getObjects() function taking a predicate
Change-Id: I8a822d76a3dc358de48e96801e4e531f3bbb8669 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@digia.com> Reviewed-by: hjk <hjk121@nokiamail.com> Reviewed-by: Eike Ziller <eike.ziller@digia.com>
-rw-r--r--src/libs/extensionsystem/pluginmanager.cpp30
-rw-r--r--src/libs/extensionsystem/pluginmanager.h24
-rw-r--r--tests/auto/extensionsystem/pluginmanager/tst_pluginmanager.cpp16
3 files changed, 69 insertions, 1 deletions
diff --git a/src/libs/extensionsystem/pluginmanager.cpp b/src/libs/extensionsystem/pluginmanager.cpp
index 338d1578ea..a9550ffa63 100644
--- a/src/libs/extensionsystem/pluginmanager.cpp
+++ b/src/libs/extensionsystem/pluginmanager.cpp
@@ -220,7 +220,21 @@ enum { debugLeaks = 0 };
This function uses \c qobject_cast to determine the type of an object.
If there are more than one object of the given type in
- the object pool, this function will choose an arbitrary one of them.
+ the object pool, this function will arbitrarily choose one of them.
+
+ \sa addObject()
+*/
+
+/*!
+ \fn T *PluginManager::getObject(Predicate predicate)
+
+ Retrieves the object of a given type from the object pool that matches
+ the \a predicate.
+
+ This function uses \c qobject_cast to determine the type of an object.
+ The predicate must be a function taking T * and returning a bool.
+ If there is more than one object matching the type and predicate,
+ this function will arbitrarily choose one of them.
\sa addObject()
*/
@@ -235,6 +249,20 @@ enum { debugLeaks = 0 };
\sa addObject()
*/
+/*!
+ \fn QList<T *> PluginManager::getObjects(Predicate predicate)
+
+ Retrieves all objects of a given type from the object pool that
+ match the \a predicate.
+
+ This function uses \c qobject_cast to determine the type of an object.
+ The predicate should be a unary function taking a T* parameter and
+ returning a bool.
+
+ \sa addObject()
+*/
+
+
using namespace ExtensionSystem;
using namespace ExtensionSystem::Internal;
diff --git a/src/libs/extensionsystem/pluginmanager.h b/src/libs/extensionsystem/pluginmanager.h
index 312fc50b71..31f129980b 100644
--- a/src/libs/extensionsystem/pluginmanager.h
+++ b/src/libs/extensionsystem/pluginmanager.h
@@ -75,6 +75,19 @@ public:
}
return results;
}
+ template <typename T, typename Predicate>
+ static QList<T *> getObjects(Predicate predicate)
+ {
+ QReadLocker lock(listLock());
+ QList<T *> results;
+ QList<QObject *> all = allObjects();
+ foreach (QObject *obj, all) {
+ T *result = qobject_cast<T *>(obj);
+ if (result && predicate(result))
+ results += result;
+ }
+ return results;
+ }
template <typename T> static T *getObject()
{
QReadLocker lock(listLock());
@@ -85,6 +98,17 @@ public:
}
return 0;
}
+ template <typename T, typename Predicate> static T *getObject(Predicate predicate)
+ {
+ QReadLocker lock(listLock());
+ QList<QObject *> all = allObjects();
+ foreach (QObject *obj, all) {
+ if (T *result = qobject_cast<T *>(obj))
+ if (predicate(result))
+ return result;
+ }
+ return 0;
+ }
static QObject *getObjectByName(const QString &name);
static QObject *getObjectByClassName(const QString &className);
diff --git a/tests/auto/extensionsystem/pluginmanager/tst_pluginmanager.cpp b/tests/auto/extensionsystem/pluginmanager/tst_pluginmanager.cpp
index e8169795e0..9e3ce28cdb 100644
--- a/tests/auto/extensionsystem/pluginmanager/tst_pluginmanager.cpp
+++ b/tests/auto/extensionsystem/pluginmanager/tst_pluginmanager.cpp
@@ -138,6 +138,9 @@ void tst_PluginManager::getObject()
{
MyClass2 *object2 = new MyClass2;
MyClass11 *object11 = new MyClass11;
+ MyClass2 *object2b = new MyClass2;
+ const QString objectName = QLatin1String("OBJECTNAME");
+ object2b->setObjectName(objectName);
m_pm->addObject(object2);
QCOMPARE(m_pm->getObject<MyClass11>(), (MyClass11*)0);
QCOMPARE(m_pm->getObject<MyClass1>(), (MyClass1*)0);
@@ -146,10 +149,17 @@ void tst_PluginManager::getObject()
QCOMPARE(m_pm->getObject<MyClass11>(), object11);
QCOMPARE(m_pm->getObject<MyClass1>(), qobject_cast<MyClass1*>(object11));
QCOMPARE(m_pm->getObject<MyClass2>(), object2);
+ QCOMPARE(m_pm->getObjectByName(objectName), (QObject*)0);
+ m_pm->addObject(object2b);
+ QCOMPARE(m_pm->getObjectByName(objectName), object2b);
+ QCOMPARE(m_pm->getObject<MyClass2>(
+ [&objectName](MyClass2 *obj) { return obj->objectName() == objectName;}), object2b);
m_pm->removeObject(object2);
m_pm->removeObject(object11);
+ m_pm->removeObject(object2b);
delete object2;
delete object11;
+ delete object2b;
}
void tst_PluginManager::getObjects()
@@ -172,6 +182,12 @@ void tst_PluginManager::getObjects()
QCOMPARE(m_pm->getObjects<MyClass1>(), QList<MyClass1*>() << object11 << object1);
QCOMPARE(m_pm->getObjects<MyClass2>(), QList<MyClass2*>() << object2);
QCOMPARE(m_pm->allObjects(), QList<QObject*>() << object2 << object11 << object1);
+
+ QCOMPARE(m_pm->getObjects<MyClass1>(
+ [](MyClass1 *o){
+ return !qobject_cast<MyClass11 *>(o);} ),
+ QList<MyClass1 *>() << object1);
+
m_pm->removeObject(object2);
m_pm->removeObject(object11);
m_pm->removeObject(object1);