aboutsummaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qdeclarativepropertycache_p.h
blob: 8a1da1929c7190de7c4d70639af5e2d889557406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QDECLARATIVEPROPERTYCACHE_P_H
#define QDECLARATIVEPROPERTYCACHE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "private/qdeclarativerefcount_p.h"
#include "private/qdeclarativecleanup_p.h"
#include "private/qdeclarativenotifier_p.h"

#include "private/qhashedstring_p.h"
#include <QtCore/qvector.h>

QT_BEGIN_NAMESPACE

class QDeclarativeEngine;
class QMetaProperty;
class QV8Engine;
class QV8QObjectWrapper;

class Q_DECLARATIVE_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
{
public:
    QDeclarativePropertyCache(QDeclarativeEngine *);
    QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
    virtual ~QDeclarativePropertyCache();

    struct Data {
        inline Data(); 
        inline bool operator==(const Data &);

        enum Flag { 
                    NoFlags           = 0x00000000,

                    // Can apply to all properties, except IsFunction
                    IsConstant        = 0x00000001, // Has CONST flag
                    IsWritable        = 0x00000002, // Has WRITE function
                    IsResettable      = 0x00000004, // Has RESET function
                    IsAlias           = 0x00000008, // Is a QML alias to another property
                    IsFinal           = 0x00000010, // Has FINAL flag
                    IsDirect          = 0x00000020, // Exists on a C++ QMetaObject

                    // These are mutualy exclusive
                    IsFunction        = 0x00000040, // Is an invokable
                    IsQObjectDerived  = 0x00000080, // Property type is a QObject* derived type
                    IsEnumType        = 0x00000100, // Property type is an enum
                    IsQList           = 0x00000200, // Property type is a QML list
                    IsQmlBinding      = 0x00000400, // Property type is a QDeclarativeBinding*
                    IsQJSValue    = 0x00000800, // Property type is a QScriptValue
                    IsV8Handle        = 0x00001000, // Property type is a QDeclarativeV8Handle

                    // Apply only to IsFunctions
                    IsVMEFunction     = 0x00002000, // Function was added by QML
                    HasArguments      = 0x00004000, // Function takes arguments
                    IsSignal          = 0x00008000, // Function is a signal
                    IsVMESignal       = 0x00010000, // Signal was added by QML
                    IsV8Function      = 0x00020000, // Function takes QDeclarativeV8Function* args

                    // Internal QDeclarativePropertyCache flags
                    NotFullyResolved  = 0x00040000  // True if the type data is to be lazily resolved 
        };
        Q_DECLARE_FLAGS(Flags, Flag)

        Flags getFlags() const { return flags; }
        void setFlags(Flags f) { flags = f; }

        bool isValid() const { return coreIndex != -1; } 

        bool isConstant() const { return flags & IsConstant; }
        bool isWritable() const { return flags & IsWritable; }
        bool isResettable() const { return flags & IsResettable; }
        bool isAlias() const { return flags & IsAlias; }
        bool isFinal() const { return flags & IsFinal; }
        bool isDirect() const { return flags & IsDirect; }
        bool isFunction() const { return flags & IsFunction; }
        bool isQObject() const { return flags & IsQObjectDerived; }
        bool isEnum() const { return flags & IsEnumType; }
        bool isQList() const { return flags & IsQList; }
        bool isQmlBinding() const { return flags & IsQmlBinding; }
        bool isQJSValue() const { return flags & IsQJSValue; }
        bool isV8Handle() const { return flags & IsV8Handle; }
        bool isVMEFunction() const { return flags & IsVMEFunction; }
        bool hasArguments() const { return flags & HasArguments; }
        bool isSignal() const { return flags & IsSignal; }
        bool isVMESignal() const { return flags & IsVMESignal; }
        bool isV8Function() const { return flags & IsV8Function; }

        union {
            int propType;             // When !NotFullyResolved
            const char *propTypeName; // When NotFullyResolved
        };
        int coreIndex;
        union {
            int notifyIndex; // When !IsFunction
            int relatedIndex; // When IsFunction
        };
        uint overrideIndexIsProperty : 1;
        int overrideIndex : 31;
        int revision; 
        int metaObjectOffset;

        static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
        void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
        void load(const QMetaMethod &);
        QString name(QObject *);
        QString name(const QMetaObject *);

    private:
        void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
        void lazyLoad(const QMetaMethod &);
        bool notFullyResolved() const { return flags & NotFullyResolved; }
        friend class QDeclarativePropertyCache;
        Flags flags;
    };

    struct ValueTypeData {
        inline ValueTypeData();
        inline bool operator==(const ValueTypeData &);
        Data::Flags flags;     // flags of the access property on the value type proxy object
        int valueTypeCoreIdx;  // The prop index of the access property on the value type proxy object
        int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
    };

    void update(QDeclarativeEngine *, const QMetaObject *);

    QDeclarativePropertyCache *copy(int reserve = 0);
    void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
                Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
    void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
                Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);

    static Data create(const QMetaObject *, const QString &);

    inline Data *property(const QHashedV8String &) const;
    Data *property(const QHashedStringRef &) const;
    Data *property(const QHashedCStringRef &) const;
    Data *property(const QString &) const;
    Data *property(int) const;
    Data *method(int) const;
    QStringList propertyNames() const;

    inline Data *overrideData(Data *) const;
    inline bool isAllowedInRevision(Data *) const;

    inline QDeclarativeEngine *qmlEngine() const;
    static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
    static Data *property(QDeclarativeEngine *, QObject *, const QHashedV8String &, Data &);

    static bool isDynamicMetaObject(const QMetaObject *);
protected:
    virtual void clear();

private:
    friend class QDeclarativeEnginePrivate;
    friend class QV8QObjectWrapper;

    // Implemented in v8/qv8qobjectwrapper.cpp
    v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);

    typedef QVector<Data> IndexCache;
    typedef QStringHash<Data *> StringCache;
    typedef QVector<int> AllowedRevisionCache;

    void resolve(Data *) const;
    void updateRecur(QDeclarativeEngine *, const QMetaObject *);

    QDeclarativeEngine *engine;
    
    QDeclarativePropertyCache *parent;
    int propertyIndexCacheStart;
    int methodIndexCacheStart;

    IndexCache propertyIndexCache;
    IndexCache methodIndexCache;
    StringCache stringCache;
    AllowedRevisionCache allowedRevisionCache;
    v8::Persistent<v8::Function> constructor;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
  
QDeclarativePropertyCache::Data::Data()
: propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
  revision(0), metaObjectOffset(-1), flags(0)
{
}

bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
{
    return flags == other.flags &&
           propType == other.propType &&
           coreIndex == other.coreIndex &&
           notifyIndex == other.notifyIndex &&
           revision == other.revision;
}

QDeclarativePropertyCache::Data *
QDeclarativePropertyCache::overrideData(Data *data) const
{
    if (data->overrideIndex < 0)
        return 0;

    if (data->overrideIndexIsProperty)
        return property(data->overrideIndex);
    else
        return method(data->overrideIndex);
}

QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
: flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0) 
{
}

bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o) 
{ 
    return flags == o.flags &&
           valueTypeCoreIdx == o.valueTypeCoreIdx &&
           valueTypePropType == o.valueTypePropType; 
}

bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
{
    return (data->metaObjectOffset == -1 && data->revision == 0) ||
           (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
}

QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
{
    return engine;
}

QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(const QHashedV8String &str) const
{
    QDeclarativePropertyCache::Data **rv = stringCache.value(str);
    if (rv && (*rv)->notFullyResolved()) resolve(*rv);
    return rv?*rv:0;
}

QT_END_NAMESPACE

#endif // QDECLARATIVEPROPERTYCACHE_P_H