Refactor singleton type registration code
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmltypenamecache_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLTYPENAMECACHE_P_H
43 #define QQMLTYPENAMECACHE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <private/qqmlrefcount_p.h>
57 #include "qqmlcleanup_p.h"
58 #include "qqmlmetatype_p.h"
59
60 #include <private/qhashedstring_p.h>
61
62 #include <QtCore/qvector.h>
63
64 QT_BEGIN_NAMESPACE
65
66 class QQmlType;
67 class QQmlEngine;
68 class QQmlTypeNameCache : public QQmlRefCount
69 {
70 public:
71     QQmlTypeNameCache();
72     virtual ~QQmlTypeNameCache();
73
74     inline bool isEmpty() const;
75
76     void add(const QHashedString &name, int sciptIndex = -1, const QHashedString &nameSpace = QHashedString());
77
78     struct Result {
79         inline Result();
80         inline Result(const void *importNamespace);
81         inline Result(QQmlType *type);
82         inline Result(int scriptIndex);
83         inline Result(const Result &);
84
85         inline bool isValid() const;
86
87         QQmlType *type;
88         const void *importNamespace;
89         int scriptIndex;
90     };
91     Result query(const QHashedStringRef &);
92     Result query(const QHashedStringRef &, const void *importNamespace);
93     Result query(const QHashedV8String &);
94     Result query(const QHashedV8String &, const void *importNamespace);
95
96 private:
97     friend class QQmlImports;
98
99     struct Import {
100         inline Import();
101         // Imported module
102         QVector<QQmlTypeModuleVersion> modules;
103
104         // Or, imported script
105         int scriptIndex;
106     };
107
108     template<typename Key>
109     Result query(const QStringHash<Import> &imports, Key key)
110     {
111         Import *i = imports.value(key);
112         if (i) {
113             if (i->scriptIndex != -1) {
114                 return Result(i->scriptIndex);
115             } else {
116                 return Result(static_cast<const void *>(i));
117             }
118         }
119
120         return Result();
121     }
122
123     template<typename Key>
124     Result typeSearch(const QVector<QQmlTypeModuleVersion> &modules, Key key)
125     {
126         QVector<QQmlTypeModuleVersion>::const_iterator end = modules.constEnd();
127         for (QVector<QQmlTypeModuleVersion>::const_iterator it = modules.constBegin(); it != end; ++it) {
128             if (QQmlType *type = it->type(key))
129                 return Result(type);
130         }
131
132         return Result();
133     }
134
135     QStringHash<Import> m_namedImports;
136     QMap<const Import *, QStringHash<Import> > m_namespacedImports;
137     QVector<QQmlTypeModuleVersion> m_anonymousImports;
138
139     QQmlEngine *engine;
140 };
141
142 QQmlTypeNameCache::Result::Result()
143 : type(0), importNamespace(0), scriptIndex(-1)
144 {
145 }
146
147 QQmlTypeNameCache::Result::Result(const void *importNamespace)
148 : type(0), importNamespace(importNamespace), scriptIndex(-1)
149 {
150 }
151
152 QQmlTypeNameCache::Result::Result(QQmlType *type)
153 : type(type), importNamespace(0), scriptIndex(-1)
154 {
155 }
156
157 QQmlTypeNameCache::Result::Result(int scriptIndex)
158 : type(0), importNamespace(0), scriptIndex(scriptIndex)
159 {
160 }
161
162 QQmlTypeNameCache::Result::Result(const Result &o)
163 : type(o.type), importNamespace(o.importNamespace), scriptIndex(o.scriptIndex)
164 {
165 }
166
167 bool QQmlTypeNameCache::Result::isValid() const
168 {
169     return type || importNamespace || scriptIndex != -1;
170 }
171
172 QQmlTypeNameCache::Import::Import()
173 : scriptIndex(-1)
174 {
175 }
176
177 bool QQmlTypeNameCache::isEmpty() const
178 {
179     return m_namedImports.isEmpty() && m_anonymousImports.isEmpty();
180 }
181
182 QT_END_NAMESPACE
183
184 #endif // QQMLTYPENAMECACHE_P_H
185