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
46QT_BEGIN_NAMESPACE
47
48QQmlTypeModule::QQmlTypeModule(const QString &module, int majorVersion)
49 : d(new QQmlTypeModulePrivate(module, majorVersion))
50{
51}
52
53QQmlTypeModule::~QQmlTypeModule()
54{
55 delete d;
56}
57
58QString QQmlTypeModule::module() const
59{
60 // No need to lock. d->module is const
61 return d->module;
62}
63
64int QQmlTypeModule::majorVersion() const
65{
66 // No need to lock. d->majorVersion is const
67 return d->majorVersion;
68}
69
70int QQmlTypeModule::minimumMinorVersion() const
71{
72 return d->minMinorVersion.loadRelaxed();
73}
74
75int QQmlTypeModule::maximumMinorVersion() const
76{
77 return d->maxMinorVersion.loadRelaxed();
78}
79
80void QQmlTypeModule::addMinorVersion(int version)
81{
82 for (int oldVersion = d->minMinorVersion.loadRelaxed();
83 oldVersion > version && !d->minMinorVersion.testAndSetOrdered(oldVersion, version);
84 oldVersion = d->minMinorVersion.loadRelaxed()) {
85 }
86
87 for (int oldVersion = d->maxMinorVersion.loadRelaxed();
88 oldVersion < version && !d->maxMinorVersion.testAndSetOrdered(oldVersion, version);
89 oldVersion = d->maxMinorVersion.loadRelaxed()) {
90 }
91}
92
93void QQmlTypeModule::add(QQmlTypePrivate *type)
94{
95 QMutexLocker lock(&d->mutex);
96 addMinorVersion(type->version_min);
97
98 QList<QQmlTypePrivate *> &list = d->typeHash[type->elementName];
99 for (int ii = 0; ii < list.count(); ++ii) {
100 Q_ASSERT(list.at(ii));
101 if (list.at(ii)->version_min < type->version_min) {
102 list.insert(ii, type);
103 return;
104 }
105 }
106 list.append(type);
107}
108
109void QQmlTypeModule::remove(const QQmlTypePrivate *type)
110{
111 QMutexLocker lock(&d->mutex);
112 for (auto elementIt = d->typeHash.begin(); elementIt != d->typeHash.end();) {
113 QQmlMetaType::removeQQmlTypePrivate(elementIt.value(), type);
114
115#if 0
116 if (list.isEmpty())
117 elementIt = typeHash.erase(elementIt);
118 else
119 ++elementIt;
120#else
121 ++elementIt;
122#endif
123 }
124}
125
126bool QQmlTypeModule::isLocked() const
127{
128 return d->locked.loadRelaxed() != 0;
129}
130
131void QQmlTypeModule::lock()
132{
133 d->locked.storeRelaxed(1);
134}
135
136QQmlType QQmlTypeModule::type(const QHashedStringRef &name, int minor) const
137{
138 QMutexLocker lock(&d->mutex);
139 QList<QQmlTypePrivate *> *types = d->typeHash.value(name);
140 if (types) {
141 for (int ii = 0; ii < types->count(); ++ii)
142 if (types->at(ii)->version_min <= minor)
143 return QQmlType(types->at(ii));
144 }
145
146 return QQmlType();
147}
148
149QQmlType QQmlTypeModule::type(const QV4::String *name, int minor) const
150{
151 QMutexLocker lock(&d->mutex);
152 QList<QQmlTypePrivate *> *types = d->typeHash.value(name);
153 if (types) {
154 for (int ii = 0; ii < types->count(); ++ii)
155 if (types->at(ii)->version_min <= minor)
156 return QQmlType(types->at(ii));
157 }
158
159 return QQmlType();
160}
161
162void QQmlTypeModule::walkCompositeSingletons(const std::function<void(const QQmlType &)> &callback) const
163{
164 QMutexLocker lock(&d->mutex);
165 for (auto typeCandidates = d->typeHash.begin(), end = d->typeHash.end();
166 typeCandidates != end; ++typeCandidates) {
167 for (auto type: typeCandidates.value()) {
168 if (type->regType == QQmlType::CompositeSingletonType)
169 callback(QQmlType(type));
170 }
171 }
172}
173
174QT_END_NAMESPACE
175