cache the arguments in property cache data
[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 class Q_AUTOTEST_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
68 {
69 public:
70     QDeclarativePropertyCache(QDeclarativeEngine *);
71     QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
72     virtual ~QDeclarativePropertyCache();
73
74     struct Data {
75         inline Data(); 
76
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         QVector<int> paramTypes;
119         static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
120         int enumType(const QMetaObject *meta, const QString &strname);
121         void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
122         void load(const QMetaMethod &);
123         QString name(QObject *);
124         QString name(const QMetaObject *);
125     };
126
127     struct ValueTypeData {
128         inline ValueTypeData();
129         inline bool operator==(const ValueTypeData &);
130         Data::Flags flags;     // flags of the access property on the value type proxy object
131         int valueTypeCoreIdx;  // The prop index of the access property on the value type proxy object
132         int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
133     };
134
135     void update(QDeclarativeEngine *, const QMetaObject *);
136
137     QDeclarativePropertyCache *copy() const;
138     void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
139                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
140     void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
141                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
142
143     static Data create(const QMetaObject *, const QString &);
144
145     inline Data *property(const QScriptDeclarativeClass::Identifier &id) const;
146     Data *property(const QString &) const;
147     Data *property(int) const;
148     Data *method(int) const;
149     QStringList propertyNames() const;
150
151     inline Data *overrideData(Data *) const;
152     inline bool isAllowedInRevision(Data *) const;
153
154     inline QDeclarativeEngine *qmlEngine() const;
155     static Data *property(QDeclarativeEngine *, QObject *, const QScriptDeclarativeClass::Identifier &, Data &);
156     static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
157
158 protected:
159     virtual void clear();
160
161 private:
162     friend class QDeclarativeEnginePrivate;
163
164     struct RData : public Data, public QDeclarativeRefCount { 
165         QScriptDeclarativeClass::PersistentIdentifier identifier;
166     };
167
168     typedef QVector<RData *> IndexCache;
169     typedef QHash<QString, RData *> StringCache;
170     typedef QHash<QScriptDeclarativeClass::Identifier, RData *> IdentifierCache;
171     typedef QVector<int> AllowedRevisionCache;
172
173     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
174
175     QDeclarativeEngine *engine;
176     IndexCache indexCache;
177     IndexCache methodIndexCache;
178     StringCache stringCache;
179     IdentifierCache identifierCache;
180     AllowedRevisionCache allowedRevisionCache;
181 };
182 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
183   
184 QDeclarativePropertyCache::Data::Data()
185 : flags(0), propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
186   revision(0), metaObjectOffset(-1)
187 {
188 }
189
190 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
191 {
192     return flags == other.flags &&
193            propType == other.propType &&
194            coreIndex == other.coreIndex &&
195            notifyIndex == other.notifyIndex &&
196            revision == other.revision;
197 }
198
199 QDeclarativePropertyCache::Data *
200 QDeclarativePropertyCache::overrideData(Data *data) const
201 {
202     if (data->overrideIndex < 0)
203         return 0;
204
205     if (data->overrideIndexIsProperty)
206         return indexCache.at(data->overrideIndex);
207     else
208         return methodIndexCache.at(data->overrideIndex);
209 }
210
211 QDeclarativePropertyCache::Data *
212 QDeclarativePropertyCache::property(const QScriptDeclarativeClass::Identifier &id) const 
213 {
214     return identifierCache.value(id);
215 }
216
217 QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
218 : flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0) 
219 {
220 }
221
222 bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o) 
223
224     return flags == o.flags &&
225            valueTypeCoreIdx == o.valueTypeCoreIdx &&
226            valueTypePropType == o.valueTypePropType; 
227 }
228
229 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
230 {
231     return (data->metaObjectOffset == -1 && data->revision == 0) ||
232            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
233 }
234
235 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
236 {
237     return engine;
238 }
239
240 QT_END_NAMESPACE
241
242 #endif // QDECLARATIVEPROPERTYCACHE_P_H