Add missing QT_{BEGIN,END}_NAMESPACE
[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     void addSingletonType(const QHashedString &name, QQmlMetaType::SingletonInstance *apiInstance, const QHashedString &nameSpace = QHashedString());
78
79     struct Result {
80         inline Result();
81         inline Result(const void *importNamespace);
82         inline Result(QQmlType *type);
83         inline Result(int scriptIndex);
84         inline Result(const Result &);
85
86         inline bool isValid() const;
87
88         QQmlType *type;
89         const void *importNamespace;
90         int scriptIndex;
91     };
92     Result query(const QHashedStringRef &);
93     Result query(const QHashedStringRef &, const void *importNamespace);
94     Result query(const QHashedV8String &);
95     Result query(const QHashedV8String &, const void *importNamespace);
96     QQmlMetaType::SingletonInstance *singletonType(const void *importNamespace);
97
98 private:
99     friend class QQmlImports;
100
101     struct Import {
102         inline Import();
103         // Imported module
104         QQmlMetaType::SingletonInstance *singletonType;
105         QVector<QQmlTypeModuleVersion> modules;
106
107         // Or, imported script
108         int scriptIndex;
109     };
110
111     template<typename Key>
112     Result query(const QStringHash<Import> &imports, Key key)
113     {
114         Import *i = imports.value(key);
115         if (i) {
116             if (i->scriptIndex != -1) {
117                 return Result(i->scriptIndex);
118             } else {
119                 return Result(static_cast<const void *>(i));
120             }
121         }
122
123         return Result();
124     }
125
126     template<typename Key>
127     Result typeSearch(const QVector<QQmlTypeModuleVersion> &modules, Key key)
128     {
129         QVector<QQmlTypeModuleVersion>::const_iterator end = modules.constEnd();
130         for (QVector<QQmlTypeModuleVersion>::const_iterator it = modules.constBegin(); it != end; ++it) {
131             if (QQmlType *type = it->type(key))
132                 return Result(type);
133         }
134
135         return Result();
136     }
137
138     QStringHash<Import> m_namedImports;
139     QMap<const Import *, QStringHash<Import> > m_namespacedImports;
140     QVector<QQmlTypeModuleVersion> m_anonymousImports;
141
142     QQmlEngine *engine;
143 };
144
145 QQmlTypeNameCache::Result::Result()
146 : type(0), importNamespace(0), scriptIndex(-1)
147 {
148 }
149
150 QQmlTypeNameCache::Result::Result(const void *importNamespace)
151 : type(0), importNamespace(importNamespace), scriptIndex(-1)
152 {
153 }
154
155 QQmlTypeNameCache::Result::Result(QQmlType *type)
156 : type(type), importNamespace(0), scriptIndex(-1)
157 {
158 }
159
160 QQmlTypeNameCache::Result::Result(int scriptIndex)
161 : type(0), importNamespace(0), scriptIndex(scriptIndex)
162 {
163 }
164
165 QQmlTypeNameCache::Result::Result(const Result &o)
166 : type(o.type), importNamespace(o.importNamespace), scriptIndex(o.scriptIndex)
167 {
168 }
169
170 bool QQmlTypeNameCache::Result::isValid() const
171 {
172     return type || importNamespace || scriptIndex != -1;
173 }
174
175 QQmlTypeNameCache::Import::Import()
176 : singletonType(0), scriptIndex(-1)
177 {
178 }
179
180 bool QQmlTypeNameCache::isEmpty() const
181 {
182     return m_namedImports.isEmpty() && m_anonymousImports.isEmpty();
183 }
184
185 QT_END_NAMESPACE
186
187 #endif // QQMLTYPENAMECACHE_P_H
188