Initial import from the monolithic Qt.
[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
88                     // These are mutualy exclusive
89                     IsFunction        = 0x00000010,
90                     IsQObjectDerived  = 0x00000020,
91                     IsEnumType        = 0x00000040,
92                     IsQList           = 0x00000080,
93                     IsQmlBinding      = 0x00000100,
94                     IsQScriptValue    = 0x00000200,
95
96                     // Apply only to IsFunctions
97                     IsVMEFunction     = 0x00000400,
98                     HasArguments      = 0x00000800,
99                     IsSignal          = 0x00001000,
100                     IsVMESignal       = 0x00002000
101         };
102         Q_DECLARE_FLAGS(Flags, Flag)
103
104         bool isValid() const { return coreIndex != -1; } 
105
106         Flags flags;
107         int propType;
108         int coreIndex;
109         union {
110             int notifyIndex; // When !IsFunction
111             int relatedIndex; // When IsFunction
112         };
113         uint overrideIndexIsProperty : 1;
114         int overrideIndex : 31;
115         int revision; 
116         int metaObjectOffset;
117
118         static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
119         void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
120         void load(const QMetaMethod &);
121         QString name(QObject *);
122         QString name(const QMetaObject *);
123     };
124
125     struct ValueTypeData {
126         inline ValueTypeData();
127         inline bool operator==(const ValueTypeData &);
128         Data::Flags flags;     // flags of the access property on the value type proxy object
129         int valueTypeCoreIdx;  // The prop index of the access property on the value type proxy object
130         int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
131     };
132
133     void update(QDeclarativeEngine *, const QMetaObject *);
134
135     QDeclarativePropertyCache *copy() const;
136     void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
137                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
138     void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
139                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
140
141     static Data create(const QMetaObject *, const QString &);
142
143     inline Data *property(const QScriptDeclarativeClass::Identifier &id) const;
144     Data *property(const QString &) const;
145     Data *property(int) const;
146     Data *method(int) const;
147     QStringList propertyNames() const;
148
149     inline Data *overrideData(Data *) const;
150     inline bool isAllowedInRevision(Data *) const;
151
152     inline QDeclarativeEngine *qmlEngine() const;
153     static Data *property(QDeclarativeEngine *, QObject *, const QScriptDeclarativeClass::Identifier &, Data &);
154     static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
155
156 protected:
157     virtual void clear();
158
159 private:
160     friend class QDeclarativeEnginePrivate;
161
162     struct RData : public Data, public QDeclarativeRefCount { 
163         QScriptDeclarativeClass::PersistentIdentifier identifier;
164     };
165
166     typedef QVector<RData *> IndexCache;
167     typedef QHash<QString, RData *> StringCache;
168     typedef QHash<QScriptDeclarativeClass::Identifier, RData *> IdentifierCache;
169     typedef QVector<int> AllowedRevisionCache;
170
171     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
172
173     QDeclarativeEngine *engine;
174     IndexCache indexCache;
175     IndexCache methodIndexCache;
176     StringCache stringCache;
177     IdentifierCache identifierCache;
178     AllowedRevisionCache allowedRevisionCache;
179 };
180 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
181   
182 QDeclarativePropertyCache::Data::Data()
183 : flags(0), propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
184   revision(0), metaObjectOffset(-1)
185 {
186 }
187
188 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
189 {
190     return flags == other.flags &&
191            propType == other.propType &&
192            coreIndex == other.coreIndex &&
193            notifyIndex == other.notifyIndex &&
194            revision == other.revision;
195 }
196
197 QDeclarativePropertyCache::Data *
198 QDeclarativePropertyCache::overrideData(Data *data) const
199 {
200     if (data->overrideIndex < 0)
201         return 0;
202
203     if (data->overrideIndexIsProperty)
204         return indexCache.at(data->overrideIndex);
205     else
206         return methodIndexCache.at(data->overrideIndex);
207 }
208
209 QDeclarativePropertyCache::Data *
210 QDeclarativePropertyCache::property(const QScriptDeclarativeClass::Identifier &id) const 
211 {
212     return identifierCache.value(id);
213 }
214
215 QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
216 : flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0) 
217 {
218 }
219
220 bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o) 
221
222     return flags == o.flags &&
223            valueTypeCoreIdx == o.valueTypeCoreIdx &&
224            valueTypePropType == o.valueTypePropType; 
225 }
226
227 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
228 {
229     return (data->metaObjectOffset == -1 && data->revision == 0) ||
230            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
231 }
232
233 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
234 {
235     return engine;
236 }
237
238 QT_END_NAMESPACE
239
240 #endif // QDECLARATIVEPROPERTYCACHE_P_H