QPluginLoader: add 'staticPlugins' function

Currently there is no API to extract the meta data
from static plugins. This is needed in e.g QtDeclarative
for loading static module plugins.

This patch moves the 'staticPlugins' function from
QLibraryPrivate into QPluginLoader, and makes it public.
As such, we now also export QStaticPlugin.
Since an application developer cannot do much with raw
metadata, we add a new function
QStaticPlugin::metaData() that returns the QJsonObject
for the plugin. The old metaData function is
renamed 'rawMetaData'.

Change-Id: Idb0bf9ad8ebb13340565512e1998b26e762a357e
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Richard Moe Gustavsen 2013-08-22 13:16:34 +02:00 committed by The Qt Project
parent 500deb9a08
commit 8b7a8efe66
6 changed files with 67 additions and 16 deletions

View File

@ -235,13 +235,10 @@ QList<QJsonObject> QFactoryLoader::metaData() const
for (int i = 0; i < d->libraryList.size(); ++i)
metaData.append(d->libraryList.at(i)->metaData);
QVector<QStaticPlugin> staticPlugins = QLibraryPrivate::staticPlugins();
for (int i = 0; i < staticPlugins.count(); ++i) {
const char *rawMetaData = staticPlugins.at(i).metaData();
QJsonObject object = QLibraryPrivate::fromRawMetaData(rawMetaData).object();
foreach (const QStaticPlugin &plugin, QPluginLoader::staticPlugins()) {
const QJsonObject object = plugin.metaData();
if (object.value(QLatin1String("IID")) != QLatin1String(d->iid.constData(), d->iid.size()))
continue;
metaData.append(object);
}
return metaData;
@ -269,10 +266,9 @@ QObject *QFactoryLoader::instance(int index) const
}
index -= d->libraryList.size();
QVector<QStaticPlugin> staticPlugins = QLibraryPrivate::staticPlugins();
QVector<QStaticPlugin> staticPlugins = QPluginLoader::staticPlugins();
for (int i = 0; i < staticPlugins.count(); ++i) {
const char *rawMetaData = staticPlugins.at(i).metaData();
QJsonObject object = QLibraryPrivate::fromRawMetaData(rawMetaData).object();
const QJsonObject object = staticPlugins.at(i).metaData();
if (object.value(QLatin1String("IID")) != QLatin1String(d->iid.constData(), d->iid.size()))
continue;

View File

@ -98,9 +98,6 @@ public:
static QStringList suffixes_sys(const QString &fullVersion);
static QStringList prefixes_sys();
static QVector<QStaticPlugin> staticPlugins();
QPointer<QObject> inst;
QtPluginInstanceFunction instance;
QJsonObject metaData;

View File

@ -44,6 +44,7 @@
#include <QtCore/qobject.h>
#include <QtCore/qpointer.h>
#include <QtCore/qjsonobject.h>
QT_BEGIN_NAMESPACE
@ -59,11 +60,22 @@ QT_BEGIN_NAMESPACE
typedef QObject *(*QtPluginInstanceFunction)();
typedef const char *(*QtPluginMetaDataFunction)();
struct QStaticPlugin
struct Q_CORE_EXPORT QStaticPlugin
{
// Note: This struct is initialized using an initializer list.
// As such, it cannot have any new constructors or variables.
#ifndef Q_QDOC
QtPluginInstanceFunction instance;
QtPluginMetaDataFunction metaData;
QtPluginMetaDataFunction rawMetaData;
#else
// Since qdoc gets confused by the use of function
// pointers, we add these dummes for it to parse instead:
QObject *instance();
const char *rawMetaData();
#endif
QJsonObject metaData() const;
};
Q_DECLARE_TYPEINFO(QStaticPlugin, Q_PRIMITIVE_TYPE);
void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin staticPlugin);

View File

@ -106,6 +106,33 @@ QT_BEGIN_NAMESPACE
\sa QLibrary, {Plug & Paint Example}
*/
/*!
\class QStaticPlugin
\inmodule QtCore
\since 5.2
\brief QStaticPlugin is a struct containing a reference to a
static plugin instance together with its meta data.
\sa QPluginLoader, {How to Create Qt Plugins}
*/
/*!
\fn QObject *QStaticPlugin::instance()
Returns the plugin instance.
\sa QPluginLoader::staticInstances()
*/
/*!
\fn const char *QStaticPlugin::rawMetaData()
Returns the raw meta data for the plugin.
\sa metaData(), Q_PLUGIN_METADATA()
*/
/*!
Constructs a plugin loader with the given \a parent.
*/
@ -407,6 +434,7 @@ void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin plugin)
/*!
Returns a list of static plugin instances (root components) held
by the plugin loader.
\sa staticPlugins()
*/
QObjectList QPluginLoader::staticInstances()
{
@ -419,8 +447,14 @@ QObjectList QPluginLoader::staticInstances()
return instances;
}
QVector<QStaticPlugin> QLibraryPrivate::staticPlugins()
/*!
Returns a list of QStaticPlugins held by the plugin
loader. The function is similar to \l staticInstances()
with the addition that a QStaticPlugin also contains
meta data information.
\sa staticInstances()
*/
QVector<QStaticPlugin> QPluginLoader::staticPlugins()
{
StaticPluginList *plugins = staticPluginList();
if (plugins)
@ -428,6 +462,16 @@ QVector<QStaticPlugin> QLibraryPrivate::staticPlugins()
return QVector<QStaticPlugin>();
}
/*!
Returns a the meta data for the plugin as a QJsonObject.
\sa rawMetaData()
*/
QJsonObject QStaticPlugin::metaData() const
{
return QLibraryPrivate::fromRawMetaData(rawMetaData()).object();
}
QT_END_NAMESPACE
#endif // QT_NO_LIBRARY

View File

@ -43,6 +43,7 @@
#define QPLUGINLOADER_H
#include <QtCore/qlibrary.h>
#include <QtCore/qplugin.h>
#ifndef QT_NO_LIBRARY
@ -65,6 +66,7 @@ public:
QJsonObject metaData() const;
static QObjectList staticInstances();
static QVector<QStaticPlugin> staticPlugins();
bool load();
bool unload();

View File

@ -765,7 +765,7 @@ void tst_QGuiApplication::genericPluginsAndWindowSystemEvents()
QStaticPlugin testPluginInfo;
testPluginInfo.instance = qt_plugin_instance;
testPluginInfo.metaData = qt_plugin_query_metadata;
testPluginInfo.rawMetaData = qt_plugin_query_metadata;
qRegisterStaticPluginFunction(testPluginInfo);
int argc = 3;
char *argv[] = { const_cast<char*>("tst_qguiapplication"), const_cast<char*>("-plugin"), const_cast<char*>("testplugin") };