QFactoryLoader: Add API for supporting the metadata containing keys.

- Add a method returning a QMultiMap<int index, QString key>
  to QFactoryLoader, determined from metadata(), correctly
  reflecting the data structure ('Keys' being a list)
- Add convenience templates to create plugins via factory
  interfaces

Change-Id: I247749aa3245f635e476605db1c4cd9c74b74dea
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
Friedemann Kleint 2012-05-04 15:57:21 +02:00 committed by Qt by Nokia
parent 864f930ba9
commit 053576e880
2 changed files with 65 additions and 0 deletions

View File

@ -370,6 +370,39 @@ void QFactoryLoader::refreshAll()
}
}
QMultiMap<int, QString> QFactoryLoader::keyMap() const
{
QMultiMap<int, QString> result;
const QString metaDataKey = QStringLiteral("MetaData");
const QString keysKey = QStringLiteral("Keys");
const QList<QJsonObject> metaDataList = metaData();
for (int i = 0; i < metaDataList.size(); ++i) {
const QJsonObject metaData = metaDataList.at(i).value(metaDataKey).toObject();
const QJsonArray keys = metaData.value(keysKey).toArray();
const int keyCount = keys.size();
for (int k = 0; k < keyCount; ++k)
result.insert(i, keys.at(k).toString());
}
return result;
}
int QFactoryLoader::indexOf(const QString &needle) const
{
const QString metaDataKey = QStringLiteral("MetaData");
const QString keysKey = QStringLiteral("Keys");
const QList<QJsonObject> metaDataList = metaData();
for (int i = 0; i < metaDataList.size(); ++i) {
const QJsonObject metaData = metaDataList.at(i).value(metaDataKey).toObject();
const QJsonArray keys = metaData.value(keysKey).toArray();
const int keyCount = keys.size();
for (int k = 0; k < keyCount; ++k) {
if (!keys.at(k).toString().compare(needle, Qt::CaseInsensitive))
return i;
}
}
return -1;
}
QT_END_NAMESPACE
#endif // QT_NO_LIBRARY

View File

@ -56,6 +56,7 @@
#include "QtCore/qobject.h"
#include "QtCore/qstringlist.h"
#include "QtCore/qjsonobject.h"
#include "QtCore/qmap.h"
#include "private/qlibrary_p.h"
#ifndef QT_NO_LIBRARY
@ -84,11 +85,42 @@ public:
QLibraryPrivate *library(const QString &key) const;
#endif
QMultiMap<int, QString> keyMap() const;
int indexOf(const QString &needle) const;
void update();
static void refreshAll();
};
template <class PluginInterface, class FactoryInterface>
PluginInterface *qLoadPlugin(const QFactoryLoader *loader, const QString &key)
{
const int index = loader->indexOf(key);
if (index != -1) {
QObject *factoryObject = loader->instance(index);
if (FactoryInterface *factory = qobject_cast<FactoryInterface *>(factoryObject))
if (PluginInterface *result = factory->create(key))
return result;
}
return 0;
}
template <class PluginInterface, class FactoryInterface, class Parameter1>
PluginInterface *qLoadPlugin1(const QFactoryLoader *loader,
const QString &key,
const Parameter1 &parameter1)
{
const int index = loader->indexOf(key);
if (index != -1) {
QObject *factoryObject = loader->instance(index);
if (FactoryInterface *factory = qobject_cast<FactoryInterface *>(factoryObject))
if (PluginInterface *result = factory->create(key, parameter1))
return result;
}
return 0;
}
QT_END_NAMESPACE
#endif // QT_NO_LIBRARY