Merge branch 'master' into v8
[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 ** 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 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 "private/qhashedstring_p.h"
61 #include <QtCore/qvector.h>
62
63 #include <QtScript/private/qscriptdeclarativeclass_p.h>
64 QT_BEGIN_NAMESPACE
65
66 class QDeclarativeEngine;
67 class QMetaProperty;
68 class QV8Engine;
69 class QV8QObjectWrapper;
70
71 class Q_AUTOTEST_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
72 {
73 public:
74     QDeclarativePropertyCache(QDeclarativeEngine *);
75     QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
76     virtual ~QDeclarativePropertyCache();
77
78     struct Data {
79         inline Data(); 
80         inline bool operator==(const Data &);
81
82         enum Flag { 
83                     NoFlags           = 0x00000000,
84
85                     // Can apply to all properties, except IsFunction
86                     IsConstant        = 0x00000001,
87                     IsWritable        = 0x00000002,
88                     IsResettable      = 0x00000004,
89                     IsAlias           = 0x00000008,
90                     IsFinal           = 0x00000010,
91
92                     // These are mutualy exclusive
93                     IsFunction        = 0x00000020,
94                     IsQObjectDerived  = 0x00000040,
95                     IsEnumType        = 0x00000080,
96                     IsQList           = 0x00000100,
97                     IsQmlBinding      = 0x00000200,
98                     IsQScriptValue    = 0x00000400,
99                     IsV8Handle        = 0x00000800,
100
101                     // Apply only to IsFunctions
102                     IsVMEFunction     = 0x00001000,
103                     HasArguments      = 0x00002000,
104                     IsSignal          = 0x00004000,
105                     IsVMESignal       = 0x00008000,
106                     IsV8Function      = 0x00010000
107         };
108         Q_DECLARE_FLAGS(Flags, Flag)
109
110         bool isValid() const { return coreIndex != -1; } 
111         bool isConstant() const { return flags & IsConstant; }
112         bool isWritable() const { return flags & IsWritable; }
113         bool isResettable() const { return flags & IsResettable; }
114         bool isAlias() const { return flags & IsAlias; }
115         bool isFinal() const { return flags & IsFinal; }
116         bool isFunction() const { return flags & IsFunction; }
117         bool isQObject() const { return flags & IsQObjectDerived; }
118         bool isEnum() const { return flags & IsEnumType; }
119         bool isQList() const { return flags & IsQList; }
120         bool isQmlBinding() const { return flags & IsQmlBinding; }
121         bool isQScriptValue() const { return flags & IsQScriptValue; }
122         bool isV8Handle() const { return flags & IsV8Handle; }
123
124         Flags flags;
125         int propType;
126         int coreIndex;
127         union {
128             int notifyIndex; // When !IsFunction
129             int relatedIndex; // When IsFunction
130         };
131         uint overrideIndexIsProperty : 1;
132         int overrideIndex : 31;
133         int revision; 
134         int metaObjectOffset;
135
136         static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
137         void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
138         void load(const QMetaMethod &);
139         QString name(QObject *);
140         QString name(const QMetaObject *);
141     };
142
143     struct ValueTypeData {
144         inline ValueTypeData();
145         inline bool operator==(const ValueTypeData &);
146         Data::Flags flags;     // flags of the access property on the value type proxy object
147         int valueTypeCoreIdx;  // The prop index of the access property on the value type proxy object
148         int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
149     };
150
151     void update(QDeclarativeEngine *, const QMetaObject *);
152
153     QDeclarativePropertyCache *copy() const;
154     void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
155                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
156     void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
157                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
158
159     static Data create(const QMetaObject *, const QString &);
160
161     inline Data *property(v8::Handle<v8::String>) const;
162     Data *property(const QString &) const;
163     Data *property(int) const;
164     Data *method(int) const;
165     QStringList propertyNames() const;
166
167     inline Data *overrideData(Data *) const;
168     inline bool isAllowedInRevision(Data *) const;
169
170     inline QDeclarativeEngine *qmlEngine() const;
171     static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
172     static Data *property(QDeclarativeEngine *, QObject *, v8::Handle<v8::String>, Data &);
173
174 protected:
175     virtual void clear();
176
177 private:
178     friend class QDeclarativeEnginePrivate;
179     friend class QV8QObjectWrapper;
180
181     // Implemented in v8/qv8qobjectwrapper.cpp
182     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
183
184     // XXX is this worth it anymore?
185     struct RData : public Data, public QDeclarativeRefCount { 
186     };
187
188     typedef QVector<RData *> IndexCache;
189     typedef QStringHash<RData *> StringCache;
190     typedef QVector<int> AllowedRevisionCache;
191
192     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
193
194     QDeclarativeEngine *engine;
195     IndexCache indexCache;
196     IndexCache methodIndexCache;
197     StringCache stringCache;
198     AllowedRevisionCache allowedRevisionCache;
199     v8::Persistent<v8::Function> constructor;
200 };
201 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
202   
203 QDeclarativePropertyCache::Data::Data()
204 : flags(0), propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
205   revision(0), metaObjectOffset(-1)
206 {
207 }
208
209 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
210 {
211     return flags == other.flags &&
212            propType == other.propType &&
213            coreIndex == other.coreIndex &&
214            notifyIndex == other.notifyIndex &&
215            revision == other.revision;
216 }
217
218 QDeclarativePropertyCache::Data *
219 QDeclarativePropertyCache::overrideData(Data *data) const
220 {
221     if (data->overrideIndex < 0)
222         return 0;
223
224     if (data->overrideIndexIsProperty)
225         return indexCache.at(data->overrideIndex);
226     else
227         return methodIndexCache.at(data->overrideIndex);
228 }
229
230 QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
231 : flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0) 
232 {
233 }
234
235 bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o) 
236
237     return flags == o.flags &&
238            valueTypeCoreIdx == o.valueTypeCoreIdx &&
239            valueTypePropType == o.valueTypePropType; 
240 }
241
242 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
243 {
244     return (data->metaObjectOffset == -1 && data->revision == 0) ||
245            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
246 }
247
248 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
249 {
250     return engine;
251 }
252
253 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(v8::Handle<v8::String> str) const
254 {
255     QDeclarativePropertyCache::RData **rv = stringCache.value(str);
256     return rv?*rv:0;
257 }
258
259 QT_END_NAMESPACE
260
261 #endif // QDECLARATIVEPROPERTYCACHE_P_H