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/qcbormap.h"
24#include "QtCore/qcborvalue.h"
25#include "QtCore/qmap.h"
26#include "QtCore/qobject.h"
27#include "QtCore/qplugin.h"
28
29QT_BEGIN_NAMESPACE
30
31class QJsonObject;
32class QLibraryPrivate;
33
34class QPluginParsedMetaData
35{
36 QCborValue data;
37 bool setError(const QString &errorString) Q_DECL_COLD_FUNCTION
38 {
39 data = errorString;
40 return false;
41 }
42public:
43 QPluginParsedMetaData() = default;
44 QPluginParsedMetaData(QByteArrayView input) { parse(input); }
45
46 bool isError() const { return !data.isMap(); }
47 QString errorString() const { return data.toString(); }
48
49 bool parse(QByteArrayView input);
50 bool parse(QPluginMetaData metaData)
51 { return parse(input: QByteArrayView(reinterpret_cast<const char *>(metaData.data), metaData.size)); }
52
53 QJsonObject toJson() const; // only for QLibrary & QPluginLoader
54
55 // if data is not a map, toMap() returns empty, so shall these functions
56 QCborMap toCbor() const { return data.toMap(); }
57 QCborValue value(QtPluginMetaDataKeys k) const { return data[int(k)]; }
58};
59
60class QFactoryLoaderPrivate;
61class Q_CORE_EXPORT QFactoryLoader : public QObject
62{
63 Q_OBJECT
64 Q_DECLARE_PRIVATE(QFactoryLoader)
65
66public:
67 explicit QFactoryLoader(const char *iid,
68 const QString &suffix = QString(),
69 Qt::CaseSensitivity = Qt::CaseSensitive);
70
71#if QT_CONFIG(library)
72 ~QFactoryLoader();
73
74 void update();
75 static void refreshAll();
76
77#if defined(Q_OS_UNIX) && !defined (Q_OS_DARWIN)
78 QLibraryPrivate *library(const QString &key) const;
79#endif // Q_OS_UNIX && !Q_OS_DARWIN
80#endif // QT_CONFIG(library)
81
82 void setExtraSearchPath(const QString &path);
83 QMultiMap<int, QString> keyMap() const;
84 int indexOf(const QString &needle) const;
85
86 using MetaDataList = QList<QPluginParsedMetaData>;
87
88 MetaDataList metaData() const;
89 QObject *instance(int index) const;
90};
91
92template <class PluginInterface, class FactoryInterface, typename ...Args>
93PluginInterface *qLoadPlugin(const QFactoryLoader *loader, const QString &key, Args &&...args)
94{
95 const int index = loader->indexOf(needle: key);
96 if (index != -1) {
97 QObject *factoryObject = loader->instance(index);
98 if (FactoryInterface *factory = qobject_cast<FactoryInterface *>(factoryObject))
99 if (PluginInterface *result = factory->create(key, std::forward<Args>(args)...))
100 return result;
101 }
102 return nullptr;
103}
104
105template <class PluginInterface, class FactoryInterface, typename Arg>
106Q_DECL_DEPRECATED PluginInterface *qLoadPlugin1(const QFactoryLoader *loader, const QString &key, Arg &&arg)
107{ return qLoadPlugin<PluginInterface, FactoryInterface>(loader, key, std::forward<Arg>(arg)); }
108
109QT_END_NAMESPACE
110
111#endif // QT_NO_QOBJECT
112
113#endif // QFACTORYLOADER_P_H
114

source code of qtbase/src/corelib/plugin/qfactoryloader_p.h