1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2019 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qqmltypemodule_p_p.h" |
41 | |
42 | #include <private/qqmltype_p_p.h> |
43 | |
44 | #include <QtCore/qmutex.h> |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | QQmlTypeModule::QQmlTypeModule(const QString &module, int majorVersion) |
49 | : d(new QQmlTypeModulePrivate(module, majorVersion)) |
50 | { |
51 | } |
52 | |
53 | QQmlTypeModule::~QQmlTypeModule() |
54 | { |
55 | delete d; |
56 | } |
57 | |
58 | QString QQmlTypeModule::module() const |
59 | { |
60 | // No need to lock. d->module is const |
61 | return d->module; |
62 | } |
63 | |
64 | int QQmlTypeModule::majorVersion() const |
65 | { |
66 | // No need to lock. d->majorVersion is const |
67 | return d->majorVersion; |
68 | } |
69 | |
70 | int QQmlTypeModule::minimumMinorVersion() const |
71 | { |
72 | return d->minMinorVersion.loadRelaxed(); |
73 | } |
74 | |
75 | int QQmlTypeModule::maximumMinorVersion() const |
76 | { |
77 | return d->maxMinorVersion.loadRelaxed(); |
78 | } |
79 | |
80 | void QQmlTypeModule::addMinorVersion(int version) |
81 | { |
82 | for (int oldVersion = d->minMinorVersion.loadRelaxed(); |
83 | oldVersion > version && !d->minMinorVersion.testAndSetOrdered(expectedValue: oldVersion, newValue: version); |
84 | oldVersion = d->minMinorVersion.loadRelaxed()) { |
85 | } |
86 | |
87 | for (int oldVersion = d->maxMinorVersion.loadRelaxed(); |
88 | oldVersion < version && !d->maxMinorVersion.testAndSetOrdered(expectedValue: oldVersion, newValue: version); |
89 | oldVersion = d->maxMinorVersion.loadRelaxed()) { |
90 | } |
91 | } |
92 | |
93 | void QQmlTypeModule::add(QQmlTypePrivate *type) |
94 | { |
95 | QMutexLocker lock(&d->mutex); |
96 | addMinorVersion(version: type->version_min); |
97 | |
98 | QList<QQmlTypePrivate *> &list = d->typeHash[type->elementName]; |
99 | for (int ii = 0; ii < list.count(); ++ii) { |
100 | QQmlTypePrivate *in_list = list.at(i: ii); |
101 | Q_ASSERT(in_list); |
102 | if (in_list->version_min < type->version_min) { |
103 | list.insert(i: ii, t: type); |
104 | return; |
105 | } else if (in_list->version_min == type->version_min) { |
106 | list[ii] = type; |
107 | return; |
108 | } |
109 | } |
110 | list.append(t: type); |
111 | } |
112 | |
113 | void QQmlTypeModule::remove(const QQmlTypePrivate *type) |
114 | { |
115 | QMutexLocker lock(&d->mutex); |
116 | for (auto elementIt = d->typeHash.begin(); elementIt != d->typeHash.end();) { |
117 | QQmlMetaType::removeQQmlTypePrivate(container&: elementIt.value(), reference: type); |
118 | |
119 | #if 0 |
120 | if (list.isEmpty()) |
121 | elementIt = typeHash.erase(elementIt); |
122 | else |
123 | ++elementIt; |
124 | #else |
125 | ++elementIt; |
126 | #endif |
127 | } |
128 | } |
129 | |
130 | bool QQmlTypeModule::isLocked() const |
131 | { |
132 | return d->locked.loadRelaxed() != 0; |
133 | } |
134 | |
135 | void QQmlTypeModule::lock() |
136 | { |
137 | d->locked.storeRelaxed(newValue: 1); |
138 | } |
139 | |
140 | QQmlType QQmlTypeModule::type(const QHashedStringRef &name, int minor) const |
141 | { |
142 | QMutexLocker lock(&d->mutex); |
143 | QList<QQmlTypePrivate *> *types = d->typeHash.value(key: name); |
144 | if (types) { |
145 | for (int ii = 0; ii < types->count(); ++ii) |
146 | if (types->at(i: ii)->version_min <= minor) |
147 | return QQmlType(types->at(i: ii)); |
148 | } |
149 | |
150 | return QQmlType(); |
151 | } |
152 | |
153 | QQmlType QQmlTypeModule::type(const QV4::String *name, int minor) const |
154 | { |
155 | QMutexLocker lock(&d->mutex); |
156 | QList<QQmlTypePrivate *> *types = d->typeHash.value(string: name); |
157 | if (types) { |
158 | for (int ii = 0; ii < types->count(); ++ii) |
159 | if (types->at(i: ii)->version_min <= minor) |
160 | return QQmlType(types->at(i: ii)); |
161 | } |
162 | |
163 | return QQmlType(); |
164 | } |
165 | |
166 | void QQmlTypeModule::walkCompositeSingletons(const std::function<void(const QQmlType &)> &callback) const |
167 | { |
168 | QMutexLocker lock(&d->mutex); |
169 | for (auto typeCandidates = d->typeHash.begin(), end = d->typeHash.end(); |
170 | typeCandidates != end; ++typeCandidates) { |
171 | for (auto type: typeCandidates.value()) { |
172 | if (type->regType == QQmlType::CompositeSingletonType) |
173 | callback(QQmlType(type)); |
174 | } |
175 | } |
176 | } |
177 | |
178 | QT_END_NAMESPACE |
179 | |