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 inline Result(const Result &);
102
103 inline bool isValid() const;
104
105 QQmlType type;
106 const QQmlImportRef *importNamespace;
107 int scriptIndex;
108 };
109 Result query(const QHashedStringRef &) const;
110 Result query(const QHashedStringRef &, const QQmlImportRef *importNamespace) const;
111 Result query(const QV4::String *, QQmlImport::RecursionRestriction recursionRestriction = QQmlImport::PreventRecursion) const;
112 Result query(const QV4::String *, const QQmlImportRef *importNamespace) const;
113
114private:
115 friend class QQmlImports;
116
117 template<typename Key>
118 Result query(const QStringHash<QQmlImportRef> &imports, Key key) const
119 {
120 QQmlImportRef *i = imports.value(key);
121 if (i) {
122 Q_ASSERT(!i->m_qualifier.isEmpty());
123 if (i->scriptIndex != -1) {
124 return Result(i->scriptIndex);
125 } else {
126 return Result(i);
127 }
128 }
129
130 return Result();
131 }
132
133 template<typename Key>
134 Result query(const QStringHash<QUrl> &urls, Key key) const
135 {
136 QUrl *url = urls.value(key);
137 if (url) {
138 QQmlType type = QQmlMetaType::qmlType(*url);
139 return Result(type);
140 }
141
142 return Result();
143 }
144
145 template<typename Key>
146 Result typeSearch(const QVector<QQmlTypeModuleVersion> &modules, Key key) const
147 {
148 QVector<QQmlTypeModuleVersion>::const_iterator end = modules.constEnd();
149 for (QVector<QQmlTypeModuleVersion>::const_iterator it = modules.constBegin(); it != end; ++it) {
150 QQmlType type = it->type(key);
151 if (type.isValid())
152 return Result(type);
153 }
154
155 return Result();
156 }
157
158 QStringHash<QQmlImportRef> m_namedImports;
159 QMap<const QQmlImportRef *, QStringHash<QQmlImportRef> > m_namespacedImports;
160 QVector<QQmlTypeModuleVersion> m_anonymousImports;
161 QStringHash<QUrl> m_anonymousCompositeSingletons;
162 QQmlImports m_imports;
163};
164
165QQmlTypeNameCache::Result::Result()
166: importNamespace(nullptr), scriptIndex(-1)
167{
168}
169
170QQmlTypeNameCache::Result::Result(const QQmlImportRef *importNamespace)
171: importNamespace(importNamespace), scriptIndex(-1)
172{
173}
174
175QQmlTypeNameCache::Result::Result(const QQmlType &type)
176: type(type), importNamespace(nullptr), scriptIndex(-1)
177{
178}
179
180QQmlTypeNameCache::Result::Result(int scriptIndex)
181: importNamespace(nullptr), scriptIndex(scriptIndex)
182{
183}
184
185QQmlTypeNameCache::Result::Result(const Result &o)
186: type(o.type), importNamespace(o.importNamespace), scriptIndex(o.scriptIndex)
187{
188}
189
190bool QQmlTypeNameCache::Result::isValid() const
191{
192 return type.isValid() || importNamespace || scriptIndex != -1;
193}
194
195bool QQmlTypeNameCache::isEmpty() const
196{
197 return m_namedImports.isEmpty() && m_anonymousImports.isEmpty()
198 && m_anonymousCompositeSingletons.isEmpty();
199}
200
201QT_END_NAMESPACE
202
203#endif // QQMLTYPENAMECACHE_P_H
204
205