| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2018 Intel Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #ifndef QPLUGIN_H |
| 42 | #define QPLUGIN_H |
| 43 | |
| 44 | #include <QtCore/qobject.h> |
| 45 | #include <QtCore/qpointer.h> |
| 46 | #include <QtCore/qjsonobject.h> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | |
| 51 | #ifndef Q_EXTERN_C |
| 52 | # ifdef __cplusplus |
| 53 | # define Q_EXTERN_C extern "C" |
| 54 | # else |
| 55 | # define Q_EXTERN_C extern |
| 56 | # endif |
| 57 | #endif |
| 58 | |
| 59 | inline constexpr unsigned char qPluginArchRequirements() |
| 60 | { |
| 61 | return 0 |
| 62 | #ifndef QT_NO_DEBUG |
| 63 | | 1 |
| 64 | #endif |
| 65 | #ifdef __AVX2__ |
| 66 | | 2 |
| 67 | # ifdef __AVX512F__ |
| 68 | | 4 |
| 69 | # endif |
| 70 | #endif |
| 71 | ; |
| 72 | } |
| 73 | |
| 74 | typedef QObject *(*QtPluginInstanceFunction)(); |
| 75 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 76 | typedef const char *(*QtPluginMetaDataFunction)(); |
| 77 | #else |
| 78 | struct QPluginMetaData |
| 79 | { |
| 80 | const uchar *data; |
| 81 | size_t size; |
| 82 | }; |
| 83 | typedef QPluginMetaData (*QtPluginMetaDataFunction)(); |
| 84 | #endif |
| 85 | |
| 86 | |
| 87 | struct Q_CORE_EXPORT QStaticPlugin |
| 88 | { |
| 89 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 90 | public: |
| 91 | constexpr QStaticPlugin(QtPluginInstanceFunction i, QtPluginMetaDataFunction m) |
| 92 | : instance(i), rawMetaData(m().data), rawMetaDataSize(m().size) |
| 93 | {} |
| 94 | QtPluginInstanceFunction instance; |
| 95 | private: |
| 96 | // ### Qt 6: revise, as this is not standard-layout |
| 97 | const void *rawMetaData; |
| 98 | qsizetype rawMetaDataSize; |
| 99 | public: |
| 100 | #elif !defined(Q_QDOC) |
| 101 | // Note: This struct is initialized using an initializer list. |
| 102 | // As such, it cannot have any new constructors or variables. |
| 103 | QtPluginInstanceFunction instance; |
| 104 | QtPluginMetaDataFunction rawMetaData; |
| 105 | #else |
| 106 | // Since qdoc gets confused by the use of function |
| 107 | // pointers, we add these dummes for it to parse instead: |
| 108 | QObject *instance(); |
| 109 | const char *rawMetaData(); |
| 110 | #endif |
| 111 | QJsonObject metaData() const; |
| 112 | }; |
| 113 | Q_DECLARE_TYPEINFO(QStaticPlugin, Q_PRIMITIVE_TYPE); |
| 114 | |
| 115 | void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin staticPlugin); |
| 116 | |
| 117 | #if (defined(Q_OF_ELF) || defined(Q_OS_WIN)) && (defined (Q_CC_GNU) || defined(Q_CC_CLANG)) |
| 118 | # define QT_PLUGIN_METADATA_SECTION \ |
| 119 | __attribute__ ((section (".qtmetadata"))) __attribute__((used)) |
| 120 | #elif defined(Q_OS_MAC) |
| 121 | // TODO: Implement section parsing on Mac |
| 122 | # define QT_PLUGIN_METADATA_SECTION \ |
| 123 | __attribute__ ((section ("__TEXT,qtmetadata"))) __attribute__((used)) |
| 124 | #elif defined(Q_CC_MSVC) |
| 125 | // TODO: Implement section parsing for MSVC |
| 126 | #pragma section(".qtmetadata",read,shared) |
| 127 | # define QT_PLUGIN_METADATA_SECTION \ |
| 128 | __declspec(allocate(".qtmetadata")) |
| 129 | #else |
| 130 | # define QT_PLUGIN_METADATA_SECTION |
| 131 | #endif |
| 132 | |
| 133 | |
| 134 | #define Q_IMPORT_PLUGIN(PLUGIN) \ |
| 135 | extern const QT_PREPEND_NAMESPACE(QStaticPlugin) qt_static_plugin_##PLUGIN(); \ |
| 136 | class Static##PLUGIN##PluginInstance{ \ |
| 137 | public: \ |
| 138 | Static##PLUGIN##PluginInstance() { \ |
| 139 | qRegisterStaticPluginFunction(qt_static_plugin_##PLUGIN()); \ |
| 140 | } \ |
| 141 | }; \ |
| 142 | static Static##PLUGIN##PluginInstance static##PLUGIN##Instance; |
| 143 | |
| 144 | #if defined(QT_PLUGIN_RESOURCE_INIT_FUNCTION) |
| 145 | # define QT_PLUGIN_RESOURCE_INIT \ |
| 146 | extern void QT_PLUGIN_RESOURCE_INIT_FUNCTION(); \ |
| 147 | QT_PLUGIN_RESOURCE_INIT_FUNCTION(); |
| 148 | #else |
| 149 | # define QT_PLUGIN_RESOURCE_INIT |
| 150 | #endif |
| 151 | |
| 152 | #define Q_PLUGIN_INSTANCE(IMPLEMENTATION) \ |
| 153 | { \ |
| 154 | static QT_PREPEND_NAMESPACE(QPointer)<QT_PREPEND_NAMESPACE(QObject)> _instance; \ |
| 155 | if (!_instance) { \ |
| 156 | QT_PLUGIN_RESOURCE_INIT \ |
| 157 | _instance = new IMPLEMENTATION; \ |
| 158 | } \ |
| 159 | return _instance; \ |
| 160 | } |
| 161 | |
| 162 | #if defined(QT_STATICPLUGIN) |
| 163 | |
| 164 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 165 | # define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS, PLUGINCLASSNAME) \ |
| 166 | static QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance_##PLUGINCLASSNAME() \ |
| 167 | Q_PLUGIN_INSTANCE(PLUGINCLASS) \ |
| 168 | static QPluginMetaData qt_plugin_query_metadata_##PLUGINCLASSNAME() \ |
| 169 | { return { qt_pluginMetaData, sizeof qt_pluginMetaData }; } \ |
| 170 | const QT_PREPEND_NAMESPACE(QStaticPlugin) qt_static_plugin_##PLUGINCLASSNAME() { \ |
| 171 | return { qt_plugin_instance_##PLUGINCLASSNAME, qt_plugin_query_metadata_##PLUGINCLASSNAME}; \ |
| 172 | } |
| 173 | #else |
| 174 | # define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS, PLUGINCLASSNAME) \ |
| 175 | static QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance_##PLUGINCLASSNAME() \ |
| 176 | Q_PLUGIN_INSTANCE(PLUGINCLASS) \ |
| 177 | static const char *qt_plugin_query_metadata_##PLUGINCLASSNAME() { return reinterpret_cast<const char *>(qt_pluginMetaData); } \ |
| 178 | const QT_PREPEND_NAMESPACE(QStaticPlugin) qt_static_plugin_##PLUGINCLASSNAME() { \ |
| 179 | QT_PREPEND_NAMESPACE(QStaticPlugin) plugin = { qt_plugin_instance_##PLUGINCLASSNAME, qt_plugin_query_metadata_##PLUGINCLASSNAME}; \ |
| 180 | return plugin; \ |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | #else |
| 185 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 186 | |
| 187 | # define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS, PLUGINCLASSNAME) \ |
| 188 | Q_EXTERN_C Q_DECL_EXPORT \ |
| 189 | QPluginMetaData qt_plugin_query_metadata() \ |
| 190 | { return { qt_pluginMetaData, sizeof qt_pluginMetaData }; } \ |
| 191 | Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance() \ |
| 192 | Q_PLUGIN_INSTANCE(PLUGINCLASS) |
| 193 | |
| 194 | #else |
| 195 | |
| 196 | # define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS, PLUGINCLASSNAME) \ |
| 197 | Q_EXTERN_C Q_DECL_EXPORT \ |
| 198 | const char *qt_plugin_query_metadata() \ |
| 199 | { return reinterpret_cast<const char *>(qt_pluginMetaData); } \ |
| 200 | Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance() \ |
| 201 | Q_PLUGIN_INSTANCE(PLUGINCLASS) |
| 202 | |
| 203 | #endif |
| 204 | |
| 205 | #endif |
| 206 | |
| 207 | #define Q_EXPORT_PLUGIN(PLUGIN) \ |
| 208 | Q_EXPORT_PLUGIN2(PLUGIN, PLUGIN) |
| 209 | # define Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS) \ |
| 210 | Q_STATIC_ASSERT_X(false, "Old plugin system used") |
| 211 | |
| 212 | # define Q_EXPORT_STATIC_PLUGIN2(PLUGIN, PLUGINCLASS) \ |
| 213 | Q_STATIC_ASSERT_X(false, "Old plugin system used") |
| 214 | |
| 215 | |
| 216 | QT_END_NAMESPACE |
| 217 | |
| 218 | #endif // Q_PLUGIN_H |
| 219 | |