8edc34d29381655cf33c01950330b7214885eb60
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativetypenamecache_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVETYPENAMECACHE_P_H
43 #define QDECLARATIVETYPENAMECACHE_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/qdeclarativerefcount_p.h>
57 #include "qdeclarativecleanup_p.h"
58 #include "qdeclarativemetatype_p.h"
59
60 #include <private/qhashedstring_p.h>
61
62 #include <QtCore/qvector.h>
63
64 QT_BEGIN_NAMESPACE
65
66 class QDeclarativeType;
67 class QDeclarativeEngine;
68 class QDeclarativeTypeNameCache : public QDeclarativeRefCount
69 {
70 public:
71     QDeclarativeTypeNameCache();
72     virtual ~QDeclarativeTypeNameCache();
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(QDeclarativeType *type);
82         inline Result(int scriptIndex);
83         inline Result(const Result &);
84
85         inline bool isValid() const;
86
87         QDeclarativeType *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     QDeclarativeMetaType::ModuleApiInstance *moduleApi(const void *importNamespace);
96
97 private:
98     friend class QDeclarativeImports;
99
100     struct Import {
101         inline Import();
102         // Imported module
103         QDeclarativeMetaType::ModuleApiInstance *moduleApi;
104         QVector<QDeclarativeTypeModuleVersion> modules;
105
106         // Or, imported script
107         int scriptIndex;
108     };
109
110     template<typename Key>
111     Result query(const QStringHash<Import> &imports, Key key)
112     {
113         Import *i = imports.value(key);
114         if (i) {
115             if (i->scriptIndex != -1) {
116                 return Result(i->scriptIndex);
117             } else {
118                 return Result(static_cast<const void *>(i));
119             }
120         }
121
122         return Result();
123     }
124
125     template<typename Key>
126     Result typeSearch(const QVector<QDeclarativeTypeModuleVersion> &modules, Key key)
127     {
128         QVector<QDeclarativeTypeModuleVersion>::const_iterator end = modules.constEnd();
129         for (QVector<QDeclarativeTypeModuleVersion>::const_iterator it = modules.constBegin(); it != end; ++it) {
130             if (QDeclarativeType *type = it->type(key))
131                 return Result(type);
132         }
133
134         return Result();
135     }
136
137     QStringHash<Import> m_namedImports;
138     QMap<const Import *, QStringHash<Import> > m_namespacedImports;
139     QVector<QDeclarativeTypeModuleVersion> m_anonymousImports;
140
141     QDeclarativeEngine *engine;
142 };
143
144 QDeclarativeTypeNameCache::Result::Result()
145 : type(0), importNamespace(0), scriptIndex(-1)
146 {
147 }
148
149 QDeclarativeTypeNameCache::Result::Result(const void *importNamespace)
150 : type(0), importNamespace(importNamespace), scriptIndex(-1)
151 {
152 }
153
154 QDeclarativeTypeNameCache::Result::Result(QDeclarativeType *type)
155 : type(type), importNamespace(0), scriptIndex(-1)
156 {
157 }
158
159 QDeclarativeTypeNameCache::Result::Result(int scriptIndex)
160 : type(0), importNamespace(0), scriptIndex(scriptIndex)
161 {
162 }
163
164 QDeclarativeTypeNameCache::Result::Result(const Result &o)
165 : type(o.type), importNamespace(o.importNamespace), scriptIndex(o.scriptIndex)
166 {
167 }
168
169 bool QDeclarativeTypeNameCache::Result::isValid() const
170 {
171     return type || importNamespace || scriptIndex != -1;
172 }
173
174 QDeclarativeTypeNameCache::Import::Import()
175 : moduleApi(0), scriptIndex(-1)
176 {
177 }
178
179 bool QDeclarativeTypeNameCache::isEmpty() const
180 {
181     return m_namedImports.isEmpty() && m_anonymousImports.isEmpty();
182 }
183
184 QT_END_NAMESPACE
185
186 #endif // QDECLARATIVETYPENAMECACHE_P_H
187