forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user