1/****************************************************************************
2**
3** Copyright (C) 2016 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#ifndef QQMLTYPENAMECACHE_P_H
41#define QQMLTYPENAMECACHE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <private/qqmlrefcount_p.h>
55#include "qqmlcleanup_p.h"
56#include "qqmlmetatype_p.h"
57
58#include <private/qstringhash_p.h>
59#include <private/qqmlimport_p.h>
60#include <private/qqmltypemoduleversion_p.h>
61
62#include <QtCore/qvector.h>
63
64QT_BEGIN_NAMESPACE
65
66struct QQmlImportRef {
67 inline QQmlImportRef()
68 : scriptIndex(-1)
69 {}
70 // Imported module
71 QVector<QQmlTypeModuleVersion> modules;
72
73 // Or, imported script
74 int scriptIndex;
75
76 // Or, imported compositeSingletons
77 QStringHash<QUrl> compositeSingletons;
78
79 // The qualifier of this import
80 QString m_qualifier;
81};
82
83class QQmlType;
84class QQmlEngine;
85class Q_QML_PRIVATE_EXPORT QQmlTypeNameCache : public QQmlRefCount
86{
87public:
88 QQmlTypeNameCache(const QQmlImports &imports);
89 ~QQmlTypeNameCache() override;
90
91 inline bool isEmpty() const;
92
93 void add(const QHashedString &name, int sciptIndex = -1, const QHashedString &nameSpace = QHashedString());
94 void add(const QHashedString &name, const QUrl &url, const QHashedString &nameSpace = QHashedString());
95
96 struct Result {
97 inline Result();
98 inline Result(const QQmlImportRef *importNamespace);
99 inline Result(const QQmlType &type);
100 inline Result(int scriptIndex);
101
102 inline bool isValid() const;
103
104 QQmlType type;
105 const QQmlImportRef *importNamespace;
106 int scriptIndex;
107 };
108 Result query(const QHashedStringRef &) const;
109 Result query(const QHashedStringRef &, const QQmlImportRef *importNamespace) const;
110 Result query(const QV4::String *, QQmlImport::RecursionRestriction recursionRestriction = QQmlImport::PreventRecursion) const;
111 Result query(const QV4::String *, const QQmlImportRef *importNamespace) const;
112
113private:
114 friend class QQmlImports;
115
116 template<typename Key>
117 Result query(const QStringHash<QQmlImportRef> &imports, Key key) const
118 {
119 QQmlImportRef *i = imports.value(key);
120 if (i) {
121 Q_ASSERT(!i->m_qualifier.isEmpty());
122 if (i->scriptIndex != -1) {
123 return Result(i->scriptIndex);
124 } else {
125 return Result(i);
126 }
127 }
128
129 return Result();
130 }
131
132 template<typename Key>
133 Result query(const QStringHash<QUrl> &urls, Key key) const
134 {
135 QUrl *url = urls.value(key);
136 if (url) {
137 QQmlType type = QQmlMetaType::qmlType(unNormalizedUrl: *url);
138 return Result(type);
139 }
140
141 return Result();
142 }
143
144 template<typename Key>
145 Result typeSearch(const QVector<QQmlTypeModuleVersion> &modules, Key key) const
146 {
147 QVector<QQmlTypeModuleVersion>::const_iterator end = modules.constEnd();
148 for (QVector<QQmlTypeModuleVersion>::const_iterator it = modules.constBegin(); it != end; ++it) {
149 QQmlType type = it->type(key);
150 if (type.isValid())
151 return Result(type);
152 }
153
154 return Result();
155 }
156
157 QStringHash<QQmlImportRef> m_namedImports;
158 QMap<const QQmlImportRef *, QStringHash<QQmlImportRef> > m_namespacedImports;
159 QVector<QQmlTypeModuleVersion> m_anonymousImports;
160 QStringHash<QUrl> m_anonymousCompositeSingletons;
161 QQmlImports m_imports;
162};
163
164QQmlTypeNameCache::Result::Result()
165: importNamespace(nullptr), scriptIndex(-1)
166{
167}
168
169QQmlTypeNameCache::Result::Result(const QQmlImportRef *importNamespace)
170: importNamespace(importNamespace), scriptIndex(-1)
171{
172}
173
174QQmlTypeNameCache::Result::Result(const QQmlType &type)
175: type(type), importNamespace(nullptr), scriptIndex(-1)
176{
177}
178
179QQmlTypeNameCache::Result::Result(int scriptIndex)
180: importNamespace(nullptr), scriptIndex(scriptIndex)
181{
182}
183
184bool QQmlTypeNameCache::Result::isValid() const
185{
186 return type.isValid() || importNamespace || scriptIndex != -1;
187}
188
189bool QQmlTypeNameCache::isEmpty() const
190{
191 return m_namedImports.isEmpty() && m_anonymousImports.isEmpty()
192 && m_anonymousCompositeSingletons.isEmpty();
193}
194
195QT_END_NAMESPACE
196
197#endif // QQMLTYPENAMECACHE_P_H
198
199

source code of qtdeclarative/src/qml/qml/qqmltypenamecache_p.h