Fix leak introduced in 0853343c33e394f35c31c161b019b2aed17f9256.
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlpropertycache_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLPROPERTYCACHE_P_H
43 #define QQMLPROPERTYCACHE_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/qqmlrefcount_p.h>
57 #include <private/qflagpointer_p.h>
58 #include "qqmlcleanup_p.h"
59 #include "qqmlnotifier_p.h"
60
61 #include <private/qhashedstring_p.h>
62 #include <QtCore/qvarlengtharray.h>
63 #include <QtCore/qvector.h>
64
65 QT_BEGIN_NAMESPACE
66
67 class QV8Engine;
68 class QMetaProperty;
69 class QV8QObjectWrapper;
70 class QQmlEngine;
71 class QQmlPropertyData;
72 class QQmlAccessors;
73 class QMetaObjectBuilder;
74 class QQmlPropertyCacheMethodArguments;
75 class QQmlVMEMetaObject;
76
77 // We have this somewhat awful 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 class QQmlPropertyRawData
81 {
82 public:
83     enum Flag {
84         NoFlags           = 0x00000000,
85         ValueTypeFlagMask = 0x0000FFFF, // Flags in valueTypeFlags must fit in this mask
86
87         // Can apply to all properties, except IsFunction
88         IsConstant         = 0x00000001, // Has CONST flag
89         IsWritable         = 0x00000002, // Has WRITE function
90         IsResettable       = 0x00000004, // Has RESET function
91         IsAlias            = 0x00000008, // Is a QML alias to another property
92         IsFinal            = 0x00000010, // Has FINAL flag
93         IsOverridden       = 0x00000020, // Is overridden by a extension property
94         IsDirect           = 0x00000040, // Exists on a C++ QMetaObject
95         HasAccessors       = 0x00000080, // Has property accessors
96
97         // These are mutualy exclusive
98         IsFunction         = 0x00000100, // Is an invokable
99         IsQObjectDerived   = 0x00000200, // Property type is a QObject* derived type
100         IsEnumType         = 0x00000400, // Property type is an enum
101         IsQList            = 0x00000800, // Property type is a QML list
102         IsQmlBinding       = 0x00001000, // Property type is a QQmlBinding*
103         IsQJSValue         = 0x00002000, // Property type is a QScriptValue
104         IsV8Handle         = 0x00004000, // Property type is a QQmlV8Handle
105         IsVarProperty      = 0x00008000, // Property type is a "var" property of VMEMO
106         IsValueTypeVirtual = 0x00010000, // Property is a value type "virtual" property
107         IsQVariant         = 0x00020000, // Property is a QVariant
108
109         // Apply only to IsFunctions
110         IsVMEFunction      = 0x00040000, // Function was added by QML
111         HasArguments       = 0x00080000, // Function takes arguments
112         IsSignal           = 0x00100000, // Function is a signal
113         IsVMESignal        = 0x00200000, // Signal was added by QML
114         IsV8Function       = 0x00400000, // Function takes QQmlV8Function* args
115         IsSignalHandler    = 0x00800000, // Function is a signal handler
116         IsOverload         = 0x01000000, // Function is an overload of another function
117         IsCloned           = 0x02000000, // The function was marked as cloned
118
119         // Internal QQmlPropertyCache flags
120         NotFullyResolved   = 0x04000000, // True if the type data is to be lazily resolved
121
122         // Flags that are set based on the propType field
123         PropTypeFlagMask = IsQObjectDerived | IsEnumType | IsQList | IsQmlBinding | IsQJSValue |
124                            IsV8Handle | IsQVariant,
125     };
126     Q_DECLARE_FLAGS(Flags, Flag)
127
128     Flags getFlags() const { return Flag(flags); }
129     void setFlags(Flags f) { flags = f; }
130
131     bool isValid() const { return coreIndex != -1; }
132
133     bool isConstant() const { return flags & IsConstant; }
134     bool isWritable() const { return flags & IsWritable; }
135     bool isResettable() const { return flags & IsResettable; }
136     bool isAlias() const { return flags & IsAlias; }
137     bool isFinal() const { return flags & IsFinal; }
138     bool isOverridden() const { return flags & IsOverridden; }
139     bool isDirect() const { return flags & IsDirect; }
140     bool hasAccessors() const { return flags & HasAccessors; }
141     bool isFunction() const { return flags & IsFunction; }
142     bool isQObject() const { return flags & IsQObjectDerived; }
143     bool isEnum() const { return flags & IsEnumType; }
144     bool isQList() const { return flags & IsQList; }
145     bool isQmlBinding() const { return flags & IsQmlBinding; }
146     bool isQJSValue() const { return flags & IsQJSValue; }
147     bool isV8Handle() const { return flags & IsV8Handle; }
148     bool isVarProperty() const { return flags & IsVarProperty; }
149     bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
150     bool isQVariant() const { return flags & IsQVariant; }
151     bool isVMEFunction() const { return flags & IsVMEFunction; }
152     bool hasArguments() const { return flags & HasArguments; }
153     bool isSignal() const { return flags & IsSignal; }
154     bool isVMESignal() const { return flags & IsVMESignal; }
155     bool isV8Function() const { return flags & IsV8Function; }
156     bool isSignalHandler() const { return flags & IsSignalHandler; }
157     bool isOverload() const { return flags & IsOverload; }
158     bool isCloned() const { return flags & IsCloned; }
159
160     bool hasOverride() const { return !(flags & IsValueTypeVirtual) &&
161                                       !(flags & HasAccessors) &&
162                                       overrideIndex >= 0; }
163     bool hasRevision() const { return !(flags & HasAccessors) && revision != 0; }
164
165     // Returns -1 if not a value type virtual property
166     inline int getValueTypeCoreIndex() const;
167
168     // Returns the "encoded" index for use with bindings.  Encoding is:
169     //     coreIndex | (valueTypeCoreIndex << 16)
170     inline int encodedIndex() const;
171
172     union {
173         int propType;             // When !NotFullyResolved
174         const char *propTypeName; // When NotFullyResolved
175     };
176     int coreIndex;
177     union {
178         // The notify index is in the range returned by QObjectPrivate::signalIndex().
179         // This is different from QMetaMethod::methodIndex().
180         int notifyIndex;  // When !IsFunction
181         void *arguments;  // When IsFunction && HasArguments
182     };
183
184     union {
185         struct { // When !HasAccessors
186             qint16 revision;
187             qint16 metaObjectOffset;
188
189             union {
190                 struct { // When IsValueTypeVirtual
191                     quint16 valueTypeFlags; // flags of the access property on the value type proxy
192                                             // object
193                     quint16 valueTypePropType; // The QVariant::Type of access property on the value
194                                                // type proxy object
195                     quint16 valueTypeCoreIndex; // The prop index of the access property on the value
196                                                 // type proxy object
197                 };
198
199                 struct { // When !IsValueTypeVirtual
200                     uint overrideIndexIsProperty : 1;
201                     signed int overrideIndex : 31;
202                 };
203             };
204         };
205         struct { // When HasAccessors
206             QQmlAccessors *accessors;
207             intptr_t accessorData;
208         };
209     };
210
211 private:
212     friend class QQmlPropertyData;
213     friend class QQmlPropertyCache;
214     quint32 flags;
215 };
216 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlPropertyRawData::Flags);
217
218 class QQmlPropertyData : public QQmlPropertyRawData
219 {
220 public:
221     inline QQmlPropertyData();
222     inline QQmlPropertyData(const QQmlPropertyRawData &);
223
224     inline bool operator==(const QQmlPropertyRawData &);
225
226     static Flags flagsForProperty(const QMetaProperty &, QQmlEngine *engine = 0);
227     void load(const QMetaProperty &, QQmlEngine *engine = 0);
228     void load(const QMetaMethod &);
229     QString name(QObject *);
230     QString name(const QMetaObject *);
231
232     void markAsOverrideOf(QQmlPropertyData *predecessor);
233
234 private:
235     friend class QQmlPropertyCache;
236     void lazyLoad(const QMetaProperty &, QQmlEngine *engine = 0);
237     void lazyLoad(const QMetaMethod &);
238     bool notFullyResolved() const { return flags & NotFullyResolved; }
239 };
240
241 class Q_QML_PRIVATE_EXPORT QQmlPropertyCache : public QQmlRefCount, public QQmlCleanup
242 {
243 public:
244     QQmlPropertyCache(QQmlEngine *);
245     QQmlPropertyCache(QQmlEngine *, const QMetaObject *);
246     virtual ~QQmlPropertyCache();
247
248     void update(QQmlEngine *, const QMetaObject *);
249
250     QQmlPropertyCache *copy();
251
252     QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *,
253                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
254                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
255                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
256     QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *, int revision,
257                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
258                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
259                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
260
261     QQmlPropertyCache *copyAndReserve(QQmlEngine *, int propertyCount,
262                                       int methodCount, int signalCount);
263     void appendProperty(const QString &,
264                         quint32 flags, int coreIndex, int propType, int notifyIndex);
265     void appendProperty(const QHashedCStringRef &,
266                         quint32 flags, int coreIndex, int propType, int notifyIndex);
267     void appendSignal(const QString &, quint32, int coreIndex, const int *types = 0,
268                       const QList<QByteArray> &names = QList<QByteArray>());
269     void appendSignal(const QHashedCStringRef &, quint32, int coreIndex, const int *types = 0,
270                       const QList<QByteArray> &names = QList<QByteArray>());
271     void appendMethod(const QString &, quint32 flags, int coreIndex,
272                       const QList<QByteArray> &names = QList<QByteArray>());
273     void appendMethod(const QHashedCStringRef &, quint32 flags, int coreIndex,
274                       const QList<QByteArray> &names = QList<QByteArray>());
275
276     const QMetaObject *metaObject() const;
277     const QMetaObject *createMetaObject();
278     const QMetaObject *firstCppMetaObject() const;
279
280     template<typename K>
281     QQmlPropertyData *property(const K &key, QObject *object, QQmlContextData *context) const
282     {
283         return findProperty(stringCache.find(key), object, context);
284     }
285
286     QQmlPropertyData *property(int) const;
287     QQmlPropertyData *method(int) const;
288     QQmlPropertyData *signal(int index) const { return signal(index, 0); }
289     int methodIndexToSignalIndex(int) const;
290     QStringList propertyNames() const;
291
292     QString defaultPropertyName() const;
293     QQmlPropertyData *defaultProperty() const;
294     QQmlPropertyCache *parent() const;
295
296     inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
297     inline bool isAllowedInRevision(QQmlPropertyData *) const;
298
299     inline QQmlEngine *qmlEngine() const;
300     static QQmlPropertyData *property(QQmlEngine *, QObject *, const QString &,
301                                               QQmlContextData *, QQmlPropertyData &);
302     static QQmlPropertyData *property(QQmlEngine *, QObject *, const QHashedV8String &,
303                                               QQmlContextData *, QQmlPropertyData &);
304     static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
305                                      QByteArray *unknownTypeError);
306     static int methodReturnType(QObject *, const QQmlPropertyData &data,
307                                 QByteArray *unknownTypeError);
308
309     //see QMetaObjectPrivate::originalClone
310     int originalClone(int index);
311     static int originalClone(QObject *, int index);
312
313     QList<QByteArray> signalParameterNames(int index) const;
314     QString signalParameterStringForJS(int index, int *count = 0, QString *errorString = 0);
315
316     const char *className() const;
317
318     inline int propertyCount() const;
319     inline int propertyOffset() const;
320     inline int methodCount() const;
321     inline int methodOffset() const;
322     inline int signalCount() const;
323     inline int signalOffset() const;
324
325     static bool isDynamicMetaObject(const QMetaObject *);
326
327     void toMetaObjectBuilder(QMetaObjectBuilder &);
328
329 protected:
330     virtual void destroy();
331     virtual void clear();
332
333 private:
334     friend class QQmlEnginePrivate;
335     friend class QV8QObjectWrapper;
336     friend class QQmlCompiler;
337
338     inline QQmlPropertyCache *copy(int reserve);
339
340     void append(QQmlEngine *, const QMetaObject *, int revision,
341                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
342                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
343                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
344
345     // Implemented in v8/qv8qobjectwrapper.cpp
346     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
347
348     QQmlPropertyData *signal(int, QQmlPropertyCache **) const;
349
350     typedef QVector<QQmlPropertyData> IndexCache;
351     typedef QStringMultiHash<QPair<int, QQmlPropertyData *> > StringCache;
352     typedef QVector<int> AllowedRevisionCache;
353
354     QQmlPropertyData *findProperty(StringCache::ConstIterator it, QObject *, QQmlContextData *) const;
355     QQmlPropertyData *findProperty(StringCache::ConstIterator it, const QQmlVMEMetaObject *, QQmlContextData *) const;
356
357     QQmlPropertyData *ensureResolved(QQmlPropertyData*) const;
358
359     void resolve(QQmlPropertyData *) const;
360     void updateRecur(QQmlEngine *, const QMetaObject *);
361
362     template<typename K>
363     QQmlPropertyData *findNamedProperty(const K &key)
364     {
365         StringCache::mapped_type *it = stringCache.value(key);
366         return it ? it->second : 0;
367     }
368
369     template<typename K>
370     void setNamedProperty(const K &key, int index, QQmlPropertyData *data, bool isOverride)
371     {
372         stringCache.insert(key, qMakePair(index, data));
373         _hasPropertyOverrides |= isOverride;
374     }
375
376     QQmlEngine *engine;
377
378     QQmlPropertyCache *_parent;
379     int propertyIndexCacheStart;
380     int methodIndexCacheStart;
381     int signalHandlerIndexCacheStart;
382
383     IndexCache propertyIndexCache;
384     IndexCache methodIndexCache;
385     IndexCache signalHandlerIndexCache;
386     StringCache stringCache;
387     AllowedRevisionCache allowedRevisionCache;
388     v8::Persistent<v8::Function> constructor;
389
390     bool _hasPropertyOverrides : 1;
391     bool _ownMetaObject : 1;
392     const QMetaObject *_metaObject;
393     QByteArray _dynamicClassName;
394     QByteArray _dynamicStringData;
395     QString _defaultPropertyName;
396     QQmlPropertyCacheMethodArguments *argumentsCache;
397 };
398
399 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
400 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
401 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
402 // QObject type used in assignment or when we don't have a QQmlEngine etc.
403 //
404 // This class does NOT reference the propertycache.
405 class QQmlEnginePrivate;
406 class Q_QML_EXPORT QQmlMetaObject
407 {
408 public:
409     inline QQmlMetaObject();
410     inline QQmlMetaObject(QObject *);
411     inline QQmlMetaObject(const QMetaObject *);
412     inline QQmlMetaObject(QQmlPropertyCache *);
413     inline QQmlMetaObject(const QQmlMetaObject &);
414
415     inline QQmlMetaObject &operator=(const QQmlMetaObject &);
416
417     inline bool isNull() const;
418
419     inline const char *className() const;
420     inline int propertyCount() const;
421
422     inline bool hasMetaObject() const;
423     inline const QMetaObject *metaObject() const;
424
425     QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
426
427     static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
428
429 private:
430     QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
431 };
432
433 QQmlPropertyData::QQmlPropertyData()
434 {
435     propType = 0;
436     coreIndex = -1;
437     notifyIndex = -1;
438     overrideIndexIsProperty = false;
439     overrideIndex = -1;
440     revision = 0;
441     metaObjectOffset = -1;
442     flags = 0;
443 }
444
445 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
446 {
447     *(static_cast<QQmlPropertyRawData *>(this)) = d;
448 }
449
450 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
451 {
452     return flags == other.flags &&
453            propType == other.propType &&
454            coreIndex == other.coreIndex &&
455            notifyIndex == other.notifyIndex &&
456            revision == other.revision &&
457            (!isValueTypeVirtual() ||
458             (valueTypeCoreIndex == other.valueTypeCoreIndex &&
459              valueTypePropType == other.valueTypePropType));
460 }
461
462 int QQmlPropertyRawData::getValueTypeCoreIndex() const
463 {
464     return isValueTypeVirtual()?valueTypeCoreIndex:-1;
465 }
466
467 int QQmlPropertyRawData::encodedIndex() const
468 {
469     return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 16)):coreIndex;
470 }
471
472 QQmlPropertyData *
473 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
474 {
475     if (!data->hasOverride())
476         return 0;
477
478     if (data->overrideIndexIsProperty)
479         return property(data->overrideIndex);
480     else
481         return method(data->overrideIndex);
482 }
483
484 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
485 {
486     return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
487            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
488 }
489
490 QQmlEngine *QQmlPropertyCache::qmlEngine() const
491 {
492     return engine;
493 }
494
495 int QQmlPropertyCache::propertyCount() const
496 {
497     return propertyIndexCacheStart + propertyIndexCache.count();
498 }
499
500 int QQmlPropertyCache::propertyOffset() const
501 {
502     return propertyIndexCacheStart;
503 }
504
505 int QQmlPropertyCache::methodCount() const
506 {
507     return methodIndexCacheStart + methodIndexCache.count();
508 }
509
510 int QQmlPropertyCache::methodOffset() const
511 {
512     return methodIndexCacheStart;
513 }
514
515 int QQmlPropertyCache::signalCount() const
516 {
517     return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
518 }
519
520 int QQmlPropertyCache::signalOffset() const
521 {
522     return signalHandlerIndexCacheStart;
523 }
524
525 QQmlMetaObject::QQmlMetaObject()
526 {
527 }
528
529 QQmlMetaObject::QQmlMetaObject(QObject *o)
530 {
531     if (o) {
532         QQmlData *ddata = QQmlData::get(o, false);
533         if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
534         else _m = o->metaObject();
535     }
536 }
537
538 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
539 : _m(m)
540 {
541 }
542
543 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
544 : _m(m)
545 {
546 }
547
548 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
549 : _m(o._m)
550 {
551 }
552
553 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
554 {
555     _m = o._m;
556     return *this;
557 }
558
559 bool QQmlMetaObject::isNull() const
560 {
561     return _m.isNull();
562 }
563
564 const char *QQmlMetaObject::className() const
565 {
566     if (_m.isNull()) {
567         return 0;
568     } else if (_m.isT1()) {
569         return _m.asT1()->className();
570     } else {
571         return _m.asT2()->className();
572     }
573 }
574
575 int QQmlMetaObject::propertyCount() const
576 {
577     if (_m.isNull()) {
578         return 0;
579     } else if (_m.isT1()) {
580         return _m.asT1()->propertyCount();
581     } else {
582         return _m.asT2()->propertyCount();
583     }
584 }
585
586 bool QQmlMetaObject::hasMetaObject() const
587 {
588     return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
589 }
590
591 const QMetaObject *QQmlMetaObject::metaObject() const
592 {
593     if (_m.isNull()) return 0;
594     if (_m.isT1()) return _m.asT1()->createMetaObject();
595     else return _m.asT2();
596 }
597
598 QT_END_NAMESPACE
599
600 #endif // QQMLPROPERTYCACHE_P_H