1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2016 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 QLIBRARY_P_H
42 #define QLIBRARY_P_H
43 
44 //
45 //  W A R N I N G
46 //  -------------
47 //
48 // This file is not part of the Qt API.  It exists for the convenience
49 // of the QLibrary class.  This header file may change from
50 // version to version without notice, or even be removed.
51 //
52 // We mean it.
53 //
54 
55 #include <QtCore/private/qglobal_p.h>
56 #include "QtCore/qlibrary.h"
57 #include "QtCore/qmutex.h"
58 #include "QtCore/qpointer.h"
59 #include "QtCore/qstringlist.h"
60 #include "QtCore/qplugin.h"
61 #ifdef Q_OS_WIN
62 #  include "QtCore/qt_windows.h"
63 #endif
64 
65 QT_REQUIRE_CONFIG(library);
66 
67 QT_BEGIN_NAMESPACE
68 
69 bool qt_debug_component();
70 
71 class QLibraryStore;
72 class QLibraryPrivate
73 {
74 public:
75 #ifdef Q_OS_WIN
76     using Handle = HINSTANCE;
77 #else
78     using Handle = void *;
79 #endif
80     enum UnloadFlag { UnloadSys, NoUnloadSys };
81 
82     const QString fileName;
83     const QString fullVersion;
84 
85     bool load();
86     QtPluginInstanceFunction loadPlugin(); // loads and resolves instance
87     bool unload(UnloadFlag flag = UnloadSys);
88     void release();
89     QFunctionPointer resolve(const char *);
90 
loadHints()91     QLibrary::LoadHints loadHints() const
92     { return QLibrary::LoadHints(loadHintsInt.loadRelaxed()); }
93     void setLoadHints(QLibrary::LoadHints lh);
94     QObject *pluginInstance();
95 
96     static QLibraryPrivate *findOrCreate(const QString &fileName, const QString &version = QString(),
97                                          QLibrary::LoadHints loadHints = { });
98     static QStringList suffixes_sys(const QString &fullVersion);
99     static QStringList prefixes_sys();
100 
101     QAtomicPointer<std::remove_pointer<QtPluginInstanceFunction>::type> instanceFactory;
102     QAtomicPointer<std::remove_pointer<Handle>::type> pHnd;
103 
104     // the mutex protects the fields below
105     QMutex mutex;
106     QPointer<QObject> inst;         // used by QFactoryLoader
107     QJsonObject metaData;
108     QString errorString;
109     QString qualifiedFileName;
110 
111     void updatePluginState();
112     bool isPlugin();
113 
114 private:
115     explicit QLibraryPrivate(const QString &canonicalFileName, const QString &version, QLibrary::LoadHints loadHints);
116     ~QLibraryPrivate();
117     void mergeLoadHints(QLibrary::LoadHints loadHints);
118 
119     bool load_sys();
120     bool unload_sys();
121     QFunctionPointer resolve_sys(const char *);
122 
123     QAtomicInt loadHintsInt;
124 
125     /// counts how many QLibrary or QPluginLoader are attached to us, plus 1 if it's loaded
126     QAtomicInt libraryRefCount;
127     /// counts how many times load() or loadPlugin() were called
128     QAtomicInt libraryUnloadCount;
129 
130     enum { IsAPlugin, IsNotAPlugin, MightBeAPlugin } pluginState;
131     friend class QLibraryStore;
132 };
133 
134 QT_END_NAMESPACE
135 
136 #endif // QLIBRARY_P_H
137