15# include "qlibrary_p.h"
20using namespace Qt::StringLiterals;
25
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
77static constexpr QLibrary::LoadHints defaultLoadHints = QLibrary::PreventUnloadHint;
80
81
82QPluginLoader::QPluginLoader(QObject *parent)
83 : QObject(parent), d(
nullptr), did_load(
false)
88
89
90
91
92
93
94
95
96
97
98QPluginLoader::QPluginLoader(
const QString &fileName, QObject *parent)
99 : QObject(parent), d(
nullptr), did_load(
false)
101 setFileName(fileName);
102 setLoadHints(defaultLoadHints);
106
107
108
109
110
111
112
113QPluginLoader::~QPluginLoader()
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139QObject *QPluginLoader::instance()
141 if (!isLoaded() && !load())
143 return d->pluginInstance();
147
148
149
150
151
152
153
154
155
156QJsonObject QPluginLoader::metaData()
const
159 return QJsonObject();
160 return d->metaData.toJson();
164
165
166
167
168
169
170
171
172
173bool QPluginLoader::load()
175 if (!d || d->fileName.isEmpty())
178 return d->pHnd && d->instanceFactory.loadAcquire();
182 return d->loadPlugin();
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201bool QPluginLoader::unload()
208 d->errorString = tr(
"The plugin was not loaded.");
213
214
215
216
217bool QPluginLoader::isLoaded()
const
219 return d && d->pHnd && d->instanceFactory.loadRelaxed();
222#if defined(QT_SHARED)
223static QString locatePlugin(
const QString& fileName)
225 const bool isAbsolute = QDir::isAbsolutePath(fileName);
227 QFileInfo fi(fileName);
229 return fi.canonicalFilePath();
232 std::array<QStringView, 2> prefixes = { QStringView(), QLibraryPrivate::prefix_sys() };
233 QStringList suffixes = QLibraryPrivate::suffixes_sys(QString());
234 suffixes.prepend(QString());
237 const qsizetype slash = fileName.lastIndexOf(u'/');
238 const auto baseName = QStringView{fileName}.mid(slash + 1);
239 const auto basePath = isAbsolute ? QStringView() : QStringView{fileName}.left(slash + 1);
243 paths.append(fileName.left(slash));
245 paths = QCoreApplication::libraryPaths();
248 for (
const QString &path : std::as_const(paths)) {
249 for (QStringView prefix : prefixes) {
250 for (
const QString &suffix : std::as_const(suffixes)) {
253 QString pluginPath = basePath + prefix + baseName + suffix;
254 const QString fn = path +
"/lib"_L1 + pluginPath.replace(u'/', u'_');
255 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
256 if (QFileInfo(fn).isFile())
260 const QString fn = path + u'/' + basePath + prefix + baseName + suffix;
261 qCDebug(qt_lcDebugPlugins) <<
"Trying..." << fn;
262 if (QFileInfo(fn).isFile())
267 qCDebug(qt_lcDebugPlugins) << fileName <<
"not found";
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294void QPluginLoader::setFileName(
const QString &fileName)
296#if defined(QT_SHARED)
297 QLibrary::LoadHints lh = defaultLoadHints;
305 const QString fn = locatePlugin(fileName);
307 d = QLibraryPrivate::findOrCreate(fn, QString(), lh);
309 d->updatePluginState();
312 qCWarning(qt_lcDebugPlugins,
"Cannot load '%ls' into a statically linked Qt library.",
313 qUtf16Printable(fileName));
317QString QPluginLoader::fileName()
const
325
326
327
328
329QString QPluginLoader::errorString()
const
331 return (!d || d->errorString.isEmpty()) ? tr(
"Unknown error") : d->errorString;
335
336
337
338
339
340
341
342
343
344
345
346
348void QPluginLoader::setLoadHints(QLibrary::LoadHints loadHints)
351 d = QLibraryPrivate::findOrCreate({}, {}, loadHints);
352 d->errorString.clear();
354 d->setLoadHints(loadHints);
358QLibrary::LoadHints QPluginLoader::loadHints()
const
365 return d ? d->loadHints() : defaultLoadHints;
374
375
376
377
378
379
380void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin plugin)
384 StaticPluginList &plugins = *staticPluginList;
388 auto comparator = [=](
const QStaticPlugin &p1,
const QStaticPlugin &p2) {
389 using Less = std::less<
decltype(plugin.instance)>;
390 return Less{}(p1.instance, p2.instance);
392 auto pos = std::lower_bound(plugins.constBegin(), plugins.constEnd(), plugin, comparator);
393 if (pos == plugins.constEnd() || pos->instance != plugin.instance)
394 plugins.insert(pos, plugin);
398
399
400
401
402QObjectList QPluginLoader::staticInstances()
404 QObjectList instances;
405 if (staticPluginList.exists()) {
406 const StaticPluginList &plugins = *staticPluginList;
407 instances.reserve(plugins.size());
408 for (QStaticPlugin plugin : plugins)
409 instances += plugin.instance();
415
416
417
418
419
420
421QList<QStaticPlugin> QPluginLoader::staticPlugins()
423 StaticPluginList *plugins = staticPluginList();
426 return QList<QStaticPlugin>();
430
431
432
433
434
435
436
437
438
441
442
443
446
447
448
449
450
451
454
455
456
457
458QJsonObject QStaticPlugin::metaData()
const
460 QByteArrayView data(
static_cast<
const char *>(rawMetaData), rawMetaDataSize);
461 QPluginParsedMetaData parsed(data);
462 Q_ASSERT(!parsed.isError());
463 return parsed.toJson();
468#include "moc_qpluginloader.cpp"
Combined button and popup list for selecting options.
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
QList< QStaticPlugin > StaticPluginList