| 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 | |
| 34 | QT_BEGIN_NAMESPACE |
| 35 | |
| 36 | class QJsonObject; |
| 37 | class QLibraryPrivate; |
| 38 | |
| 39 | class Q_CORE_EXPORT QFactoryLoader |
| 40 | { |
| 41 | Q_DECLARE_TR_FUNCTIONS(QFactoryLoader); |
| 42 | |
| 43 | public: |
| 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 (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; |
| 67 | QList<QCborArray> metaDataKeys() const; |
| 68 | QObject *instance(int index) const; |
| 69 | |
| 70 | private: |
| 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 ; |
| 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 | |
| 95 | template <class PluginInterface, class FactoryInterface, typename ...Args> |
| 96 | PluginInterface *qLoadPlugin(const QFactoryLoader *loader, const QString &key, Args &&...args) |
| 97 | { |
| 98 | const int index = loader->indexOf(needle: 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 | |
| 108 | template <class PluginInterface, class FactoryInterface, typename Arg> |
| 109 | Q_DECL_DEPRECATED PluginInterface *qLoadPlugin1(const QFactoryLoader *loader, const QString &key, Arg &&arg) |
| 110 | { return qLoadPlugin<PluginInterface, FactoryInterface>(loader, key, std::forward<Arg>(arg)); } |
| 111 | |
| 112 | QT_END_NAMESPACE |
| 113 | |
| 114 | #endif // QT_NO_QOBJECT |
| 115 | |
| 116 | #endif // QFACTORYLOADER_P_H |
| 117 | |