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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
122static constexpr QLibrary::LoadHints defaultLoadHints = QLibrary::PreventUnloadHint;
125
126
127QPluginLoader::QPluginLoader(QObject *parent)
128 : QObject(parent), d(
nullptr), did_load(
false)
133
134
135
136
137
138
139
140
141
142
143QPluginLoader::QPluginLoader(
const QString &fileName, QObject *parent)
144 : QObject(parent), d(
nullptr), did_load(
false)
146 setFileName(fileName);
147 setLoadHints(defaultLoadHints);
151
152
153
154
155
156
157
158QPluginLoader::~QPluginLoader()
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184QObject *QPluginLoader::instance()
186 if (!isLoaded() && !load())
188 return d->pluginInstance();
192
193
194
195
196
197
198
199
200
201QJsonObject QPluginLoader::metaData()
const
204 return QJsonObject();
205 return d->metaData.toJson();
209
210
211
212
213
214
215
216
217
218bool QPluginLoader::load()
220 if (!d || d->fileName.isEmpty())
223 return d->pHnd && d->instanceFactory.loadAcquire();
224#if !defined(Q_OS_WASM)
232 return d->loadPlugin();
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251bool QPluginLoader::unload()
258 d->errorString = tr(
"The plugin was not loaded.");
263
264
265
266
267bool QPluginLoader::isLoaded()
const
269 return d && d->pHnd && d->instanceFactory.loadRelaxed();
272#if defined(QT_SHARED)
273# if defined(Q_OS_WASM)
277static QString locatePlugin(
const QString &fileName)
282static QString locatePlugin(
const QString& fileName)
284 const bool isAbsolute = QDir::isAbsolutePath(fileName);
286 QFileInfo fi(fileName);
288 return fi.canonicalFilePath();
291 std::array<QStringView, 2> prefixes = { QStringView(), QLibraryPrivate::prefix_sys() };
292 QStringList suffixes = QLibraryPrivate::suffixes_sys(QString());
293 suffixes.prepend(QString());
296 const qsizetype slash = fileName.lastIndexOf(u'/');
297 const auto baseName = QStringView{fileName}.mid(slash + 1);
298 const auto basePath = isAbsolute ? QStringView() : QStringView{fileName}.left(slash + 1);
302 paths.append(fileName.left(slash));
304 paths = QCoreApplication::libraryPaths();
307 for (
const QString &path : std::as_const(paths)) {
308 for (QStringView prefix : prefixes) {
309 for (
const QString &suffix : std::as_const(suffixes)) {
312 QString pluginPath = basePath + prefix + baseName + suffix;
313 const QString fn = path +
"/lib"_L1 + pluginPath.replace(u'/', u'_');
314 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
315 if (QFileInfo(fn).isFile())
319 const QString fn = path + u'/' + basePath + prefix + baseName + suffix;
320 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
321 if (QFileInfo(fn).isFile())
326 qCDebug(qt_lcDebugPlugins) << fileName <<
"not found";
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354void QPluginLoader::setFileName(
const QString &fileName)
356#if defined(QT_SHARED)
357 QLibrary::LoadHints lh = defaultLoadHints;
365 const QString fn = locatePlugin(fileName);
367 d = QLibraryPrivate::findOrCreate(fn, QString(), lh);
369 d->updatePluginState();
372 qCWarning(qt_lcDebugPlugins,
"Cannot load '%ls' into a statically linked Qt library.",
373 qUtf16Printable(fileName));
377QString QPluginLoader::fileName()
const
385
386
387
388
389QString QPluginLoader::errorString()
const
391 return (!d || d->errorString.isEmpty()) ? tr(
"Unknown error") : d->errorString;
395
396
397
398
399
400
401
402
403
404
405
406
408void QPluginLoader::setLoadHints(QLibrary::LoadHints loadHints)
411 d = QLibraryPrivate::findOrCreate({}, {}, loadHints);
412 d->errorString.clear();
414 d->setLoadHints(loadHints);
418QLibrary::LoadHints QPluginLoader::loadHints()
const
425 return d ? d->loadHints() : defaultLoadHints;
434
435
436
437
438
439
440void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin plugin)
444 StaticPluginList &plugins = *staticPluginList;
448 auto comparator = [=](
const QStaticPlugin &p1,
const QStaticPlugin &p2) {
449 using Less = std::less<
decltype(plugin.instance)>;
450 return Less{}(p1.instance, p2.instance);
452 auto pos = std::lower_bound(plugins.constBegin(), plugins.constEnd(), plugin, comparator);
453 if (pos == plugins.constEnd() || pos->instance != plugin.instance)
454 plugins.insert(pos, plugin);
458
459
460
461
462QObjectList QPluginLoader::staticInstances()
464 QObjectList instances;
465 if (staticPluginList.exists()) {
466 const StaticPluginList &plugins = *staticPluginList;
467 instances.reserve(plugins.size());
468 for (QStaticPlugin plugin : plugins)
469 instances += plugin.instance();
475
476
477
478
479
480
481QList<QStaticPlugin> QPluginLoader::staticPlugins()
483 StaticPluginList *plugins = staticPluginList();
486 return QList<QStaticPlugin>();
490
491
492
493
494
495
496
497
498
501
502
503
506
507
508
509
510
511
514
515
516
517
518QJsonObject QStaticPlugin::metaData()
const
520 QByteArrayView data(
static_cast<
const char *>(rawMetaData), rawMetaDataSize);
521 QPluginParsedMetaData parsed(data);
522 Q_ASSERT(!parsed.isError());
523 return parsed.toJson();
528#include "moc_qpluginloader.cpp"
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QList< QStaticPlugin > StaticPluginList