16# include "qlibrary_p.h"
21using namespace Qt::StringLiterals;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
78static constexpr QLibrary::LoadHints defaultLoadHints = QLibrary::PreventUnloadHint;
81
82
83QPluginLoader::QPluginLoader(QObject *parent)
84 : QObject(parent), d(
nullptr), did_load(
false)
89
90
91
92
93
94
95
96
97
98
99QPluginLoader::QPluginLoader(
const QString &fileName, QObject *parent)
100 : QObject(parent), d(
nullptr), did_load(
false)
102 setFileName(fileName);
103 setLoadHints(defaultLoadHints);
107
108
109
110
111
112
113
114QPluginLoader::~QPluginLoader()
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140QObject *QPluginLoader::instance()
142 if (!isLoaded() && !load())
144 return d->pluginInstance();
148
149
150
151
152
153
154
155
156
157QJsonObject QPluginLoader::metaData()
const
160 return QJsonObject();
161 return d->metaData.toJson();
165
166
167
168
169
170
171
172
173
174bool QPluginLoader::load()
176 if (!d || d->fileName.isEmpty())
179 return d->pHnd && d->instanceFactory.loadAcquire();
180#if !defined(Q_OS_WASM)
188 return d->loadPlugin();
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207bool QPluginLoader::unload()
214 d->errorString = tr(
"The plugin was not loaded.");
219
220
221
222
223bool QPluginLoader::isLoaded()
const
225 return d && d->pHnd && d->instanceFactory.loadRelaxed();
228#if defined(QT_SHARED)
229# if defined(Q_OS_WASM)
233static QString locatePlugin(
const QString &fileName)
238static QString locatePlugin(
const QString& fileName)
240 const bool isAbsolute = QDir::isAbsolutePath(fileName);
242 QFileInfo fi(fileName);
244 return fi.canonicalFilePath();
247 std::array<QStringView, 2> prefixes = { QStringView(), QLibraryPrivate::prefix_sys() };
248 QStringList suffixes = QLibraryPrivate::suffixes_sys(QString());
249 suffixes.prepend(QString());
252 const qsizetype slash = fileName.lastIndexOf(u'/');
253 const auto baseName = QStringView{fileName}.mid(slash + 1);
254 const auto basePath = isAbsolute ? QStringView() : QStringView{fileName}.left(slash + 1);
258 paths.append(fileName.left(slash));
260 paths = QCoreApplication::libraryPaths();
263 for (
const QString &path : std::as_const(paths)) {
264 for (QStringView prefix : prefixes) {
265 for (
const QString &suffix : std::as_const(suffixes)) {
268 QString pluginPath = basePath + prefix + baseName + suffix;
269 const QString fn = path +
"/lib"_L1 + pluginPath.replace(u'/', u'_');
270 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
271 if (QFileInfo(fn).isFile())
275 const QString fn = path + u'/' + basePath + prefix + baseName + suffix;
276 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
277 if (QFileInfo(fn).isFile())
282 qCDebug(qt_lcDebugPlugins) << fileName <<
"not found";
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310void QPluginLoader::setFileName(
const QString &fileName)
312#if defined(QT_SHARED)
313 QLibrary::LoadHints lh = defaultLoadHints;
321 const QString fn = locatePlugin(fileName);
323 d = QLibraryPrivate::findOrCreate(fn, QString(), lh);
325 d->updatePluginState();
328 qCWarning(qt_lcDebugPlugins,
"Cannot load '%ls' into a statically linked Qt library.",
329 qUtf16Printable(fileName));
333QString QPluginLoader::fileName()
const
341
342
343
344
345QString QPluginLoader::errorString()
const
347 return (!d || d->errorString.isEmpty()) ? tr(
"Unknown error") : d->errorString;
351
352
353
354
355
356
357
358
359
360
361
362
364void QPluginLoader::setLoadHints(QLibrary::LoadHints loadHints)
367 d = QLibraryPrivate::findOrCreate({}, {}, loadHints);
368 d->errorString.clear();
370 d->setLoadHints(loadHints);
374QLibrary::LoadHints QPluginLoader::loadHints()
const
381 return d ? d->loadHints() : defaultLoadHints;
390
391
392
393
394
395
396void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin plugin)
400 StaticPluginList &plugins = *staticPluginList;
404 auto comparator = [=](
const QStaticPlugin &p1,
const QStaticPlugin &p2) {
405 using Less = std::less<
decltype(plugin.instance)>;
406 return Less{}(p1.instance, p2.instance);
408 auto pos = std::lower_bound(plugins.constBegin(), plugins.constEnd(), plugin, comparator);
409 if (pos == plugins.constEnd() || pos->instance != plugin.instance)
410 plugins.insert(pos, plugin);
414
415
416
417
418QObjectList QPluginLoader::staticInstances()
420 QObjectList instances;
421 if (staticPluginList.exists()) {
422 const StaticPluginList &plugins = *staticPluginList;
423 instances.reserve(plugins.size());
424 for (QStaticPlugin plugin : plugins)
425 instances += plugin.instance();
431
432
433
434
435
436
437QList<QStaticPlugin> QPluginLoader::staticPlugins()
439 StaticPluginList *plugins = staticPluginList();
442 return QList<QStaticPlugin>();
446
447
448
449
450
451
452
453
454
457
458
459
462
463
464
465
466
467
470
471
472
473
474QJsonObject QStaticPlugin::metaData()
const
476 QByteArrayView data(
static_cast<
const char *>(rawMetaData), rawMetaDataSize);
477 QPluginParsedMetaData parsed(data);
478 Q_ASSERT(!parsed.isError());
479 return parsed.toJson();
484#include "moc_qpluginloader.cpp"
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QList< QStaticPlugin > StaticPluginList