Merge PropertyCache::Data and ValueTypeData
[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 QT_BEGIN_NAMESPACE
64
65 class QDeclarativeEngine;
66 class QMetaProperty;
67 class QV8Engine;
68 class QV8QObjectWrapper;
69
70 class Q_DECLARATIVE_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
71 {
72 public:
73     QDeclarativePropertyCache(QDeclarativeEngine *);
74     QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
75     virtual ~QDeclarativePropertyCache();
76
77     // We have this somewhat aweful split between RawData and Data so that RawData can be
78     // used in unions.  In normal code, you should always use Data which initializes RawData
79     // to an invalid state on construction.
80     struct Data;
81     struct RawData {
82         enum Flag { 
83                     NoFlags           = 0x00000000,
84                     ValueTypeFlagMask = 0x0000FFFF, // Flags in valueTypeFlags must fit in this mask
85
86                     // Can apply to all properties, except IsFunction
87                     IsConstant         = 0x00000001, // Has CONST flag
88                     IsWritable         = 0x00000002, // Has WRITE function
89                     IsResettable       = 0x00000004, // Has RESET function
90                     IsAlias            = 0x00000008, // Is a QML alias to another property
91                     IsFinal            = 0x00000010, // Has FINAL flag
92                     IsDirect           = 0x00000020, // Exists on a C++ QMetaObject
93
94                     // These are mutualy exclusive
95                     IsFunction         = 0x00000040, // Is an invokable
96                     IsQObjectDerived   = 0x00000080, // Property type is a QObject* derived type
97                     IsEnumType         = 0x00000100, // Property type is an enum
98                     IsQList            = 0x00000200, // Property type is a QML list
99                     IsQmlBinding       = 0x00000400, // Property type is a QDeclarativeBinding*
100                     IsQJSValue         = 0x00000800, // Property type is a QScriptValue
101                     IsV8Handle         = 0x00001000, // Property type is a QDeclarativeV8Handle
102                     IsVMEProperty      = 0x00002000, // Property type is a "var" property of VMEMO
103                     IsValueTypeVirtual = 0x00004000, // Property is a value type "virtual" property
104
105                     // Apply only to IsFunctions
106                     IsVMEFunction      = 0x00008000, // Function was added by QML
107                     HasArguments       = 0x00010000, // Function takes arguments
108                     IsSignal           = 0x00020000, // Function is a signal
109                     IsVMESignal        = 0x00040000, // Signal was added by QML
110                     IsV8Function       = 0x00080000, // Function takes QDeclarativeV8Function* args
111                     IsSignalHandler    = 0x00100000, // Function is a signal handler
112
113                     // Internal QDeclarativePropertyCache flags
114                     NotFullyResolved   = 0x00200000  // True if the type data is to be lazily resolved
115         };
116         Q_DECLARE_FLAGS(Flags, Flag)
117
118         Flags getFlags() const { return Flag(flags); }
119         void setFlags(Flags f) { flags = f; }
120
121         bool isValid() const { return coreIndex != -1; } 
122
123         bool isConstant() const { return flags & IsConstant; }
124         bool isWritable() const { return flags & IsWritable; }
125         bool isResettable() const { return flags & IsResettable; }
126         bool isAlias() const { return flags & IsAlias; }
127         bool isFinal() const { return flags & IsFinal; }
128         bool isDirect() const { return flags & IsDirect; }
129         bool isFunction() const { return flags & IsFunction; }
130         bool isQObject() const { return flags & IsQObjectDerived; }
131         bool isEnum() const { return flags & IsEnumType; }
132         bool isQList() const { return flags & IsQList; }
133         bool isQmlBinding() const { return flags & IsQmlBinding; }
134         bool isQJSValue() const { return flags & IsQJSValue; }
135         bool isV8Handle() const { return flags & IsV8Handle; }
136         bool isVMEProperty() const { return flags & IsVMEProperty; }
137         bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
138         bool isVMEFunction() const { return flags & IsVMEFunction; }
139         bool hasArguments() const { return flags & HasArguments; }
140         bool isSignal() const { return flags & IsSignal; }
141         bool isVMESignal() const { return flags & IsVMESignal; }
142         bool isV8Function() const { return flags & IsV8Function; }
143         bool isSignalHandler() const { return flags & IsSignalHandler; }
144
145         union {
146             int propType;             // When !NotFullyResolved
147             const char *propTypeName; // When NotFullyResolved
148         };
149         int coreIndex;
150         union {
151             int notifyIndex;  // When !IsFunction
152             int relatedIndex; // When IsFunction
153         };
154         union {
155             struct { // When !IsValueTypeVirtual
156                 uint overrideIndexIsProperty : 1;
157                 signed int overrideIndex : 31;
158             };
159             struct { // When IsValueTypeVirtual
160                 quint16 valueTypeFlags; // flags of the access property on the value type proxy object
161                 quint8 valueTypePropType; // The QVariant::Type of access property on the value type 
162                                           // proxy object
163                 quint8 valueTypeCoreIndex; // The prop index of the access property on the value type 
164                                            //proxy object
165             };
166         };
167         int revision; 
168         int metaObjectOffset;
169
170     private:
171         friend struct Data;
172         friend class QDeclarativePropertyCache;
173         quint32 flags;
174     };
175
176     struct Data : public RawData {
177         inline Data(); 
178         inline Data(const RawData &); 
179
180         inline bool operator==(const RawData &);
181
182         static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
183         void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
184         void load(const QMetaMethod &);
185         QString name(QObject *);
186         QString name(const QMetaObject *);
187
188         // Returns -1 if not a value type virtual property
189         inline int getValueTypeCoreIndex() const;
190
191     private:
192         friend class QDeclarativePropertyCache;
193         void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
194         void lazyLoad(const QMetaMethod &);
195         bool notFullyResolved() const { return flags & NotFullyResolved; }
196     };
197
198     void update(QDeclarativeEngine *, const QMetaObject *);
199
200     QDeclarativePropertyCache *copy(int reserve = 0);
201     void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
202                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
203     void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
204                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
205
206     static Data create(const QMetaObject *, const QString &);
207
208     inline Data *property(const QHashedV8String &) const;
209     Data *property(const QHashedStringRef &) const;
210     Data *property(const QHashedCStringRef &) const;
211     Data *property(const QString &) const;
212     Data *property(int) const;
213     Data *method(int) const;
214     QStringList propertyNames() const;
215
216     inline Data *overrideData(Data *) const;
217     inline bool isAllowedInRevision(Data *) const;
218
219     inline QDeclarativeEngine *qmlEngine() const;
220     static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
221     static Data *property(QDeclarativeEngine *, QObject *, const QHashedV8String &, Data &);
222
223     static bool isDynamicMetaObject(const QMetaObject *);
224
225 protected:
226     virtual void destroy();
227     virtual void clear();
228
229 private:
230     friend class QDeclarativeEnginePrivate;
231     friend class QV8QObjectWrapper;
232
233     // Implemented in v8/qv8qobjectwrapper.cpp
234     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
235
236     typedef QVector<Data> IndexCache;
237     typedef QStringHash<Data *> StringCache;
238     typedef QVector<int> AllowedRevisionCache;
239
240     void resolve(Data *) const;
241     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
242
243     QDeclarativeEngine *engine;
244     
245     QDeclarativePropertyCache *parent;
246     int propertyIndexCacheStart;
247     int methodIndexCacheStart;
248
249     IndexCache propertyIndexCache;
250     IndexCache methodIndexCache;
251     IndexCache signalHandlerIndexCache;
252     StringCache stringCache;
253     AllowedRevisionCache allowedRevisionCache;
254     v8::Persistent<v8::Function> constructor;
255 };
256 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
257   
258 QDeclarativePropertyCache::Data::Data()
259 {
260     propType = 0;
261     coreIndex = -1;
262     notifyIndex = -1;
263     overrideIndexIsProperty = false;
264     overrideIndex = -1;
265     revision = 0;
266     metaObjectOffset = -1; 
267     flags = 0;
268 }
269
270 QDeclarativePropertyCache::Data::Data(const QDeclarativePropertyCache::RawData &d)
271 {
272     *(static_cast<RawData *>(this)) = d;
273 }
274
275 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::RawData &other)
276 {
277     return flags == other.flags &&
278            propType == other.propType &&
279            coreIndex == other.coreIndex &&
280            notifyIndex == other.notifyIndex &&
281            revision == other.revision &&
282            (!isValueTypeVirtual() || 
283             (valueTypeCoreIndex == other.valueTypeCoreIndex && 
284              valueTypePropType == other.valueTypePropType));
285 }
286
287 int QDeclarativePropertyCache::Data::getValueTypeCoreIndex() const
288 {
289     return isValueTypeVirtual()?valueTypeCoreIndex:-1;
290 }
291
292 QDeclarativePropertyCache::Data *
293 QDeclarativePropertyCache::overrideData(Data *data) const
294 {
295     if (data->overrideIndex < 0)
296         return 0;
297
298     if (data->overrideIndexIsProperty)
299         return property(data->overrideIndex);
300     else
301         return method(data->overrideIndex);
302 }
303
304 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
305 {
306     return (data->metaObjectOffset == -1 && data->revision == 0) ||
307            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
308 }
309
310 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
311 {
312     return engine;
313 }
314
315 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(const QHashedV8String &str) const
316 {
317     QDeclarativePropertyCache::Data **rv = stringCache.value(str);
318     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
319     return rv?*rv:0;
320 }
321
322 QT_END_NAMESPACE
323
324 #endif // QDECLARATIVEPROPERTYCACHE_P_H