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// Qt-Security score:significant reason:default
5
6#ifndef QFACTORYLOADER_P_H
7#define QFACTORYLOADER_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include "QtCore/qglobal.h"
21#ifndef QT_NO_QOBJECT
22
23#include "QtCore/private/qplugin_p.h"
24#include "QtCore/private/qduplicatetracker_p.h"
25#include "QtCore/qcoreapplication.h"
26#include "QtCore/qmap.h"
27#include "QtCore/qmutex.h"
28#include "QtCore/qobject.h"
29#include "QtCore/qplugin.h"
30
31#if QT_CONFIG(library)
32# include "QtCore/private/qlibrary_p.h"
33#endif
34
35#include <optional>
36
37QT_BEGIN_NAMESPACE
38
39class QJsonObject;
40class QLibraryPrivate;
41
42class Q_CORE_EXPORT QFactoryLoader
43{
44 Q_DECLARE_TR_FUNCTIONS(QFactoryLoader);
45
46public:
47 explicit QFactoryLoader(const char *iid,
48 const QString &suffix = QString(),
49 Qt::CaseSensitivity = Qt::CaseSensitive);
50
51#if QT_CONFIG(library)
52 ~QFactoryLoader();
53
54 void setLoadHints(QLibrary::LoadHints hints);
55 void update();
56 static void refreshAll();
57
58#if defined(Q_OS_UNIX) && !defined (Q_OS_DARWIN)
59 QLibraryPrivate *library(const QString &key) const;
60#endif // Q_OS_UNIX && !Q_OS_DARWIN
61#endif // QT_CONFIG(library)
62
63 void setExtraSearchPath(const QString &path);
64 QMultiMap<int, QString> keyMap() const;
65 int indexOf(const QString &needle) const;
66
67 using MetaDataList = QList<QPluginParsedMetaData>;
68
69 MetaDataList metaData() const;
71 QObject *instance(int index) const;
72
73private:
74 struct Private {
75 QByteArray iid;
76 mutable QMutex mutex;
77 mutable QList<QtPluginInstanceFunction> usedStaticInstances;
78#if QT_CONFIG(library)
79 std::optional<QDuplicateTracker<QString>> loadedPaths{std::in_place};
80 std::vector<QLibraryPrivate::UniquePtr> libraries;
81 mutable QList<bool> loadedLibraries;
82 std::map<QString, QLibraryPrivate*> keyMap;
83 QString suffix;
84 QString extraSearchPath;
85 Qt::CaseSensitivity cs;
86 QLibrary::LoadHints loadHints;
87 void updateSinglePath(const QString &pluginDir);
88#endif
89
90 // for compat when we d was a pointer
91 auto operator->() { return this; }
92 auto operator->() const { return this; }
93 } d;
94
95 inline QObject *instanceHelper_locked(int index) const;
96};
97
98template <class PluginInterface, class FactoryInterface, typename ...Args>
99PluginInterface *qLoadPlugin(const QFactoryLoader *loader, const QString &key, Args &&...args)
100{
101 const int index = loader->indexOf(key);
102 if (index != -1) {
103 QObject *factoryObject = loader->instance(index);
104 if (FactoryInterface *factory = qobject_cast<FactoryInterface *>(factoryObject))
105 if (PluginInterface *result = factory->create(key, std::forward<Args>(args)...))
106 return result;
107 }
108 return nullptr;
109}
110
111template <class PluginInterface, class FactoryInterface, typename Arg>
112Q_DECL_DEPRECATED PluginInterface *qLoadPlugin1(const QFactoryLoader *loader, const QString &key, Arg &&arg)
113{ return qLoadPlugin<PluginInterface, FactoryInterface>(loader, key, std::forward<Arg>(arg)); }
114
115QT_END_NAMESPACE
116
117#endif // QT_NO_QOBJECT
118
119#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
Combined button and popup list for selecting options.
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)