Merged master into qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativepropertycache_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEPROPERTYCACHE_P_H
43 #define QDECLARATIVEPROPERTYCACHE_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 "private/qdeclarativecleanup_p.h"
58 #include "private/qdeclarativenotifier_p.h"
59
60 #include <QtCore/qvector.h>
61
62 #include <QtScript/private/qscriptdeclarativeclass_p.h>
63 QT_BEGIN_NAMESPACE
64
65 class QDeclarativeEngine;
66 class QMetaProperty;
67
68 class Q_AUTOTEST_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
69 {
70 public:
71     QDeclarativePropertyCache(QDeclarativeEngine *);
72     QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
73     virtual ~QDeclarativePropertyCache();
74
75     struct Data {
76         inline Data(); 
77         inline bool operator==(const Data &);
78
79         enum Flag { 
80                     NoFlags           = 0x00000000,
81
82                     // Can apply to all properties, except IsFunction
83                     IsConstant        = 0x00000001,
84                     IsWritable        = 0x00000002,
85                     IsResettable      = 0x00000004,
86                     IsAlias           = 0x00000008,
87                     IsFinal           = 0x00000010,
88
89                     // These are mutualy exclusive
90                     IsFunction        = 0x00000020,
91                     IsQObjectDerived  = 0x00000040,
92                     IsEnumType        = 0x00000080,
93                     IsQList           = 0x00000100,
94                     IsQmlBinding      = 0x00000200,
95                     IsQScriptValue    = 0x00000400,
96
97                     // Apply only to IsFunctions
98                     IsVMEFunction     = 0x00000800,
99                     HasArguments      = 0x00001000,
100                     IsSignal          = 0x00002000,
101                     IsVMESignal       = 0x00004000
102         };
103         Q_DECLARE_FLAGS(Flags, Flag)
104
105         bool isValid() const { return coreIndex != -1; } 
106
107         Flags flags;
108         int propType;
109         int coreIndex;
110         union {
111             int notifyIndex; // When !IsFunction
112             int relatedIndex; // When IsFunction
113         };
114         uint overrideIndexIsProperty : 1;
115         int overrideIndex : 31;
116         int revision; 
117         int metaObjectOffset;
118
119         static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
120         void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
121         void load(const QMetaMethod &);
122         QString name(QObject *);
123         QString name(const QMetaObject *);
124     };
125
126     struct ValueTypeData {
127         inline ValueTypeData();
128         inline bool operator==(const ValueTypeData &);
129         Data::Flags flags;     // flags of the access property on the value type proxy object
130         int valueTypeCoreIdx;  // The prop index of the access property on the value type proxy object
131         int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
132     };
133
134     void update(QDeclarativeEngine *, const QMetaObject *);
135
136     QDeclarativePropertyCache *copy() const;
137     void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
138                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
139     void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
140                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
141
142     static Data create(const QMetaObject *, const QString &);
143
144     inline Data *property(const QScriptDeclarativeClass::Identifier &id) const;
145     Data *property(const QString &) const;
146     Data *property(int) const;
147     Data *method(int) const;
148     QStringList propertyNames() const;
149
150     inline Data *overrideData(Data *) const;
151     inline bool isAllowedInRevision(Data *) const;
152
153     inline QDeclarativeEngine *qmlEngine() const;
154     static Data *property(QDeclarativeEngine *, QObject *, const QScriptDeclarativeClass::Identifier &, Data &);
155     static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
156
157 protected:
158     virtual void clear();
159
160 private:
161     friend class QDeclarativeEnginePrivate;
162
163     struct RData : public Data, public QDeclarativeRefCount { 
164         QScriptDeclarativeClass::PersistentIdentifier identifier;
165     };
166
167     typedef QVector<RData *> IndexCache;
168     typedef QHash<QString, RData *> StringCache;
169     typedef QHash<QScriptDeclarativeClass::Identifier, RData *> IdentifierCache;
170     typedef QVector<int> AllowedRevisionCache;
171
172     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
173
174     QDeclarativeEngine *engine;
175     IndexCache indexCache;
176     IndexCache methodIndexCache;
177     StringCache stringCache;
178     IdentifierCache identifierCache;
179     AllowedRevisionCache allowedRevisionCache;
180 };
181 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
182   
183 QDeclarativePropertyCache::Data::Data()
184 : flags(0), propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
185   revision(0), metaObjectOffset(-1)
186 {
187 }
188
189 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
190 {
191     return flags == other.flags &&
192            propType == other.propType &&
193            coreIndex == other.coreIndex &&
194            notifyIndex == other.notifyIndex &&
195            revision == other.revision;
196 }
197
198 QDeclarativePropertyCache::Data *
199 QDeclarativePropertyCache::overrideData(Data *data) const
200 {
201     if (data->overrideIndex < 0)
202         return 0;
203
204     if (data->overrideIndexIsProperty)
205         return indexCache.at(data->overrideIndex);
206     else
207         return methodIndexCache.at(data->overrideIndex);
208 }
209
210 QDeclarativePropertyCache::Data *
211 QDeclarativePropertyCache::property(const QScriptDeclarativeClass::Identifier &id) const 
212 {
213     return identifierCache.value(id);
214 }
215
216 QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
217 : flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0) 
218 {
219 }
220
221 bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o) 
222
223     return flags == o.flags &&
224            valueTypeCoreIdx == o.valueTypeCoreIdx &&
225            valueTypePropType == o.valueTypePropType; 
226 }
227
228 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
229 {
230     return (data->metaObjectOffset == -1 && data->revision == 0) ||
231            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
232 }
233
234 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
235 {
236     return engine;
237 }
238
239 QT_END_NAMESPACE
240
241 #endif // QDECLARATIVEPROPERTYCACHE_P_H