QPluginLoader: pave the way for Qt 6 plugin meta data

We want to store the metadata size and get it from the plugin in Qt 6.

Change-Id: Ieb48f7c0dd0e4e0fb35efffd153bebc2914d9a3c
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Thiago Macieira 2018-06-26 22:01:59 -07:00
parent 5a03b75c50
commit 330290e049
7 changed files with 116 additions and 2 deletions

View File

@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -56,13 +57,27 @@ QT_BEGIN_NAMESPACE
#endif
typedef QObject *(*QtPluginInstanceFunction)();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
typedef const char *(*QtPluginMetaDataFunction)();
#else
typedef QPair<const uchar *, size_t> (*QtPluginMetaDataFunction)();
#endif
struct Q_CORE_EXPORT QStaticPlugin
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
public:
constexpr QStaticPlugin(QtPluginInstanceFunction i, QtPluginMetaDataFunction m)
: instance(i), rawMetaData(m().first), rawMetaDataSize(m().second)
QtPluginInstanceFunction instance;
private:
// ### Qt 6: revise, as this is not standard-layout
const void *rawMetaData;
qsizetype rawMetaDataSize
#elif !defined(Q_QDOC)
// 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 rawMetaData;
#else
@ -133,6 +148,15 @@ void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin staticPlugin);
return plugin; \
}
#elif QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
# define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS, PLUGINCLASSNAME) \
Q_EXTERN_C Q_DECL_EXPORT \
auto qt_plugin_query_metadata() \
{ return qMakePair<const void *, size_t>(qt_pluginMetaData, sizeof qt_pluginMetaData); } \
Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance() \
Q_PLUGIN_INSTANCE(PLUGINCLASS)
#else
# define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS, PLUGINCLASSNAME) \

View File

@ -3,6 +3,7 @@ TEMPLATE = subdirs
tst.depends = lib theplugin
SUBDIRS = lib \
staticplugin \
theplugin \
tst
!android:!win32:!darwin {

View File

@ -0,0 +1,3 @@
*staticplugin.prl
libstaticplugin.a
staticplugin.lib

View File

@ -0,0 +1,39 @@
/****************************************************************************
**
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtPlugin>
#include <QObject>
class StaticPlugin : public QObject
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "SomeIID")
public:
StaticPlugin() {}
};
#include "main.moc"

View File

@ -0,0 +1,7 @@
TEMPLATE = lib
CONFIG += plugin static
SOURCES = main.cpp
QT = core
# Add extra metadata to the plugin
QMAKE_MOC_OPTIONS += -M ExtraMetaData=StaticPlugin -M ExtraMetaData=foo

View File

@ -8,9 +8,14 @@ HEADERS = ../theplugin/plugininterface.h
win32 {
CONFIG(debug, debug|release) {
TARGET = ../../debug/tst_qpluginloader
LIBS += -L../staticplugin/debug
} else {
TARGET = ../../release/tst_qpluginloader
LIBS += -L../staticplugin/release
}
} else {
LIBS += -L../staticplugin
}
LIBS += -lstaticplugin
TESTDATA += ../elftest ../machtest

View File

@ -1,7 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Copyright (C) 2018 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
@ -121,8 +121,11 @@ private slots:
void reloadPlugin();
void preloadedPlugin_data();
void preloadedPlugin();
void staticPlugins();
};
Q_IMPORT_PLUGIN(StaticPlugin)
void tst_QPluginLoader::cleanup()
{
// check if the library/plugin was leaked
@ -512,5 +515,37 @@ void tst_QPluginLoader::preloadedPlugin()
QVERIFY(lib.unload());
}
void tst_QPluginLoader::staticPlugins()
{
const QObjectList instances = QPluginLoader::staticInstances();
QVERIFY(instances.size());
bool found = false;
for (QObject *obj : instances) {
found = obj->metaObject()->className() == QLatin1String("StaticPlugin");
if (found)
break;
}
QVERIFY(found);
const auto plugins = QPluginLoader::staticPlugins();
QCOMPARE(plugins.size(), instances.size());
// find the metadata
QJsonObject metaData;
for (const auto &p : plugins) {
metaData = p.metaData();
found = metaData.value("className").toString() == QLatin1String("StaticPlugin");
if (found)
break;
}
QVERIFY(found);
QCOMPARE(metaData.value("version").toInt(), QT_VERSION);
QCOMPARE(metaData.value("IID").toString(), "SomeIID");
QCOMPARE(metaData.value("ExtraMetaData"), QJsonArray({ "StaticPlugin", "foo" }));
}
QTEST_MAIN(tst_QPluginLoader)
#include "tst_qpluginloader.moc"