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();
183 return d->loadPlugin();
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202bool QPluginLoader::unload()
209 d->errorString = tr(
"The plugin was not loaded.");
214
215
216
217
218bool QPluginLoader::isLoaded()
const
220 return d && d->pHnd && d->instanceFactory.loadRelaxed();
223#if defined(QT_SHARED)
224static QString locatePlugin(
const QString& fileName)
226 const bool isAbsolute = QDir::isAbsolutePath(fileName);
228 QFileInfo fi(fileName);
230 return fi.canonicalFilePath();
233 std::array<QStringView, 2> prefixes = { QStringView(), QLibraryPrivate::prefix_sys() };
234 QStringList suffixes = QLibraryPrivate::suffixes_sys(QString());
235 suffixes.prepend(QString());
238 const qsizetype slash = fileName.lastIndexOf(u'/');
239 const auto baseName = QStringView{fileName}.mid(slash + 1);
240 const auto basePath = isAbsolute ? QStringView() : QStringView{fileName}.left(slash + 1);
244 paths.append(fileName.left(slash));
246 paths = QCoreApplication::libraryPaths();
249 for (
const QString &path : std::as_const(paths)) {
250 for (QStringView prefix : prefixes) {
251 for (
const QString &suffix : std::as_const(suffixes)) {
254 QString pluginPath = basePath + prefix + baseName + suffix;
255 const QString fn = path +
"/lib"_L1 + pluginPath.replace(u'/', u'_');
256 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
257 if (QFileInfo(fn).isFile())
261 const QString fn = path + u'/' + basePath + prefix + baseName + suffix;
262 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
263 if (QFileInfo(fn).isFile())
268 qCDebug(qt_lcDebugPlugins) << fileName <<
"not found";
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295void QPluginLoader::setFileName(
const QString &fileName)
297#if defined(QT_SHARED)
298 QLibrary::LoadHints lh = defaultLoadHints;
306 const QString fn = locatePlugin(fileName);
308 d = QLibraryPrivate::findOrCreate(fn, QString(), lh);
310 d->updatePluginState();
313 qCWarning(qt_lcDebugPlugins,
"Cannot load '%ls' into a statically linked Qt library.",
314 qUtf16Printable(fileName));
318QString QPluginLoader::fileName()
const
326
327
328
329
330QString QPluginLoader::errorString()
const
332 return (!d || d->errorString.isEmpty()) ? tr(
"Unknown error") : d->errorString;
336
337
338
339
340
341
342
343
344
345
346
347
349void QPluginLoader::setLoadHints(QLibrary::LoadHints loadHints)
352 d = QLibraryPrivate::findOrCreate({}, {}, loadHints);
353 d->errorString.clear();
355 d->setLoadHints(loadHints);
359QLibrary::LoadHints QPluginLoader::loadHints()
const
366 return d ? d->loadHints() : defaultLoadHints;
375
376
377
378
379
380
381void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin plugin)
385 StaticPluginList &plugins = *staticPluginList;
389 auto comparator = [=](
const QStaticPlugin &p1,
const QStaticPlugin &p2) {
390 using Less = std::less<
decltype(plugin.instance)>;
391 return Less{}(p1.instance, p2.instance);
393 auto pos = std::lower_bound(plugins.constBegin(), plugins.constEnd(), plugin, comparator);
394 if (pos == plugins.constEnd() || pos->instance != plugin.instance)
395 plugins.insert(pos, plugin);
399
400
401
402
403QObjectList QPluginLoader::staticInstances()
405 QObjectList instances;
406 if (staticPluginList.exists()) {
407 const StaticPluginList &plugins = *staticPluginList;
408 instances.reserve(plugins.size());
409 for (QStaticPlugin plugin : plugins)
410 instances += plugin.instance();
416
417
418
419
420
421
422QList<QStaticPlugin> QPluginLoader::staticPlugins()
424 StaticPluginList *plugins = staticPluginList();
427 return QList<QStaticPlugin>();
431
432
433
434
435
436
437
438
439
442
443
444
447
448
449
450
451
452
455
456
457
458
459QJsonObject QStaticPlugin::metaData()
const
461 QByteArrayView data(
static_cast<
const char *>(rawMetaData), rawMetaDataSize);
462 QPluginParsedMetaData parsed(data);
463 Q_ASSERT(!parsed.isError());
464 return parsed.toJson();
469#include "moc_qpluginloader.cpp"
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QList< QStaticPlugin > StaticPluginList