Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfactoryloader_p.h
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// Copyright (C) 2022 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#ifndef QFACTORYLOADER_P_H
6#define QFACTORYLOADER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include "QtCore/qglobal.h"
20#ifndef QT_NO_QOBJECT
21
22#include "QtCore/private/qplugin_p.h"
23#include "QtCore/private/qduplicatetracker_p.h"
24#include "QtCore/qcoreapplication.h"
25#include "QtCore/qmap.h"
26#include "QtCore/qmutex.h"
27#include "QtCore/qobject.h"
28#include "QtCore/qplugin.h"
29
30#if QT_CONFIG(library)
31# include "QtCore/private/qlibrary_p.h"
32#endif
33
34QT_BEGIN_NAMESPACE
35
36class QJsonObject;
37class QLibraryPrivate;
38
39class Q_CORE_EXPORT QFactoryLoader
40{
41 Q_DECLARE_TR_FUNCTIONS(QFactoryLoader);
42
43public:
44 explicit QFactoryLoader(const char *iid,
45 const QString &suffix = QString(),
46 Qt::CaseSensitivity = Qt::CaseSensitive);
47
48#if QT_CONFIG(library)
49 ~QFactoryLoader();
50
51 void setLoadHints(QLibrary::LoadHints hints);
52 void update();
53 static void refreshAll();
54
55#if defined(Q_OS_UNIX) && !defined (Q_OS_DARWIN)
56 QLibraryPrivate *library(const QString &key) const;
57#endif // Q_OS_UNIX && !Q_OS_DARWIN
58#endif // QT_CONFIG(library)
59
60 void setExtraSearchPath(const QString &path);
61 QMultiMap<int, QString> keyMap() const;
62 int indexOf(const QString &needle) const;
63
64 using MetaDataList = QList<QPluginParsedMetaData>;
65
66 MetaDataList metaData() const;
68 QObject *instance(int index) const;
69
70private:
71 struct Private {
72 QByteArray iid;
73 mutable QMutex mutex;
74 mutable QList<QtPluginInstanceFunction> usedStaticInstances;
75#if QT_CONFIG(library)
76 QDuplicateTracker<QString> loadedPaths;
77 std::vector<QLibraryPrivate::UniquePtr> libraries;
78 mutable QList<bool> loadedLibraries;
79 std::map<QString, QLibraryPrivate*> keyMap;
80 QString suffix;
81 QString extraSearchPath;
82 Qt::CaseSensitivity cs;
83 QLibrary::LoadHints loadHints;
84 void updateSinglePath(const QString &pluginDir);
85#endif
86
87 // for compat when we d was a pointer
88 auto operator->() { return this; }
89 auto operator->() const { return this; }
90 } d;
91
92 inline QObject *instanceHelper_locked(int index) const;
93};
94
95template <class PluginInterface, class FactoryInterface, typename ...Args>
96PluginInterface *qLoadPlugin(const QFactoryLoader *loader, const QString &key, Args &&...args)
97{
98 const int index = loader->indexOf(key);
99 if (index != -1) {
100 QObject *factoryObject = loader->instance(index);
101 if (FactoryInterface *factory = qobject_cast<FactoryInterface *>(factoryObject))
102 if (PluginInterface *result = factory->create(key, std::forward<Args>(args)...))
103 return result;
104 }
105 return nullptr;
106}
107
108template <class PluginInterface, class FactoryInterface, typename Arg>
109Q_DECL_DEPRECATED PluginInterface *qLoadPlugin1(const QFactoryLoader *loader, const QString &key, Arg &&arg)
110{ return qLoadPlugin<PluginInterface, FactoryInterface>(loader, key, std::forward<Arg>(arg)); }
111
112QT_END_NAMESPACE
113
114#endif // QT_NO_QOBJECT
115
116#endif // QFACTORYLOADER_P_H
void setExtraSearchPath(const QString &path)
int indexOf(const QString &needle) const
QMultiMap< int, QString > keyMap() const
MetaDataList metaData() const
QList< QCborArray > metaDataKeys() const
QObject * instance(int index) const
Q_TRACE_POINT(qtcore, QFactoryLoader_update, const QString &fileName)
static bool isIidMatch(QByteArrayView raw, QLatin1StringView iid)
static IterationResult iterateInPluginMetaData(QByteArrayView raw, F &&f)
PluginInterface * qLoadPlugin(const QFactoryLoader *loader, const QString &key, Args &&...args)