Adapt to connection-related changes in qtbase
[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
76 // We have this somewhat awful split between RawData and Data so that RawData can be
77 // used in unions.  In normal code, you should always use Data which initializes RawData
78 // to an invalid state on construction.
79 class QQmlPropertyRawData
80 {
81 public:
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         HasAccessors       = 0x00000040, // Has property accessors
94
95         // These are mutualy exclusive
96         IsFunction         = 0x00000080, // Is an invokable
97         IsQObjectDerived   = 0x00000100, // Property type is a QObject* derived type
98         IsEnumType         = 0x00000200, // Property type is an enum
99         IsQList            = 0x00000400, // Property type is a QML list
100         IsQmlBinding       = 0x00000800, // Property type is a QQmlBinding*
101         IsQJSValue         = 0x00001000, // Property type is a QScriptValue
102         IsV8Handle         = 0x00002000, // Property type is a QQmlV8Handle
103         IsVarProperty      = 0x00004000, // Property type is a "var" property of VMEMO
104         IsValueTypeVirtual = 0x00008000, // Property is a value type "virtual" property
105         IsQVariant         = 0x00010000, // Property is a QVariant
106
107         // Apply only to IsFunctions
108         IsVMEFunction      = 0x00020000, // Function was added by QML
109         HasArguments       = 0x00040000, // Function takes arguments
110         IsSignal           = 0x00080000, // Function is a signal
111         IsVMESignal        = 0x00100000, // Signal was added by QML
112         IsV8Function       = 0x00200000, // Function takes QQmlV8Function* args
113         IsSignalHandler    = 0x00400000, // Function is a signal handler
114         IsOverload         = 0x00800000, // Function is an overload of another function
115         IsCloned           = 0x01000000, // The function was marked as cloned
116
117         // Internal QQmlPropertyCache flags
118         NotFullyResolved   = 0x02000000, // True if the type data is to be lazily resolved
119
120         // Flags that are set based on the propType field
121         PropTypeFlagMask = IsQObjectDerived | IsEnumType | IsQList | IsQmlBinding | IsQJSValue |
122                            IsV8Handle | IsQVariant,
123     };
124     Q_DECLARE_FLAGS(Flags, Flag)
125
126     Flags getFlags() const { return Flag(flags); }
127     void setFlags(Flags f) { flags = f; }
128
129     bool isValid() const { return coreIndex != -1; }
130
131     bool isConstant() const { return flags & IsConstant; }
132     bool isWritable() const { return flags & IsWritable; }
133     bool isResettable() const { return flags & IsResettable; }
134     bool isAlias() const { return flags & IsAlias; }
135     bool isFinal() const { return flags & IsFinal; }
136     bool isDirect() const { return flags & IsDirect; }
137     bool hasAccessors() const { return flags & HasAccessors; }
138     bool isFunction() const { return flags & IsFunction; }
139     bool isQObject() const { return flags & IsQObjectDerived; }
140     bool isEnum() const { return flags & IsEnumType; }
141     bool isQList() const { return flags & IsQList; }
142     bool isQmlBinding() const { return flags & IsQmlBinding; }
143     bool isQJSValue() const { return flags & IsQJSValue; }
144     bool isV8Handle() const { return flags & IsV8Handle; }
145     bool isVarProperty() const { return flags & IsVarProperty; }
146     bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
147     bool isQVariant() const { return flags & IsQVariant; }
148     bool isVMEFunction() const { return flags & IsVMEFunction; }
149     bool hasArguments() const { return flags & HasArguments; }
150     bool isSignal() const { return flags & IsSignal; }
151     bool isVMESignal() const { return flags & IsVMESignal; }
152     bool isV8Function() const { return flags & IsV8Function; }
153     bool isSignalHandler() const { return flags & IsSignalHandler; }
154     bool isOverload() const { return flags & IsOverload; }
155     bool isCloned() const { return flags & IsCloned; }
156
157     bool hasOverride() const { return !(flags & IsValueTypeVirtual) &&
158                                       !(flags & HasAccessors) &&
159                                       overrideIndex >= 0; }
160     bool hasRevision() const { return !(flags & HasAccessors) && revision != 0; }
161
162     // Returns -1 if not a value type virtual property
163     inline int getValueTypeCoreIndex() const;
164
165     // Returns the "encoded" index for use with bindings.  Encoding is:
166     //     coreIndex | (valueTypeCoreIndex << 24)
167     inline int encodedIndex() const;
168
169     union {
170         int propType;             // When !NotFullyResolved
171         const char *propTypeName; // When NotFullyResolved
172     };
173     int coreIndex;
174     union {
175         // The notify index is in the range returned by QObjectPrivate::signalIndex().
176         // This is different from QMetaMethod::methodIndex().
177         int notifyIndex;  // When !IsFunction
178         void *arguments;  // When IsFunction && HasArguments
179     };
180
181     union {
182         struct { // When !HasAccessors
183             qint16 revision;
184             qint16 metaObjectOffset;
185
186             union {
187                 struct { // When IsValueTypeVirtual
188                     quint16 valueTypeFlags; // flags of the access property on the value type proxy
189                                             // object
190                     quint8 valueTypePropType; // The QVariant::Type of access property on the value
191                                               // type proxy object
192                     quint8 valueTypeCoreIndex; // The prop index of the access property on the value
193                                                // type proxy object
194                 };
195
196                 struct { // When !IsValueTypeVirtual
197                     uint overrideIndexIsProperty : 1;
198                     signed int overrideIndex : 31;
199                 };
200             };
201         };
202         struct { // When HasAccessors
203             QQmlAccessors *accessors;
204             intptr_t accessorData;
205         };
206     };
207
208 private:
209     friend class QQmlPropertyData;
210     friend class QQmlPropertyCache;
211     quint32 flags;
212 };
213 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlPropertyRawData::Flags);
214
215 class QQmlPropertyData : public QQmlPropertyRawData
216 {
217 public:
218     inline QQmlPropertyData();
219     inline QQmlPropertyData(const QQmlPropertyRawData &);
220
221     inline bool operator==(const QQmlPropertyRawData &);
222
223     static Flags flagsForProperty(const QMetaProperty &, QQmlEngine *engine = 0);
224     void load(const QMetaProperty &, QQmlEngine *engine = 0);
225     void load(const QMetaMethod &);
226     QString name(QObject *);
227     QString name(const QMetaObject *);
228
229 private:
230     friend class QQmlPropertyCache;
231     void lazyLoad(const QMetaProperty &, QQmlEngine *engine = 0);
232     void lazyLoad(const QMetaMethod &);
233     bool notFullyResolved() const { return flags & NotFullyResolved; }
234 };
235
236 class Q_QML_PRIVATE_EXPORT QQmlPropertyCache : public QQmlRefCount, public QQmlCleanup
237 {
238 public:
239     QQmlPropertyCache(QQmlEngine *);
240     QQmlPropertyCache(QQmlEngine *, const QMetaObject *);
241     virtual ~QQmlPropertyCache();
242
243     void update(QQmlEngine *, const QMetaObject *);
244
245     QQmlPropertyCache *copy();
246
247     QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *,
248                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
249                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
250                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
251     QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *, int revision,
252                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
253                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
254                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
255
256     QQmlPropertyCache *copyAndReserve(QQmlEngine *, int propertyCount,
257                                       int methodCount, int signalCount);
258     void appendProperty(const QString &,
259                         quint32 flags, int coreIndex, int propType, int notifyIndex);
260     void appendProperty(const QHashedCStringRef &,
261                         quint32 flags, int coreIndex, int propType, int notifyIndex);
262     void appendSignal(const QString &, quint32, int coreIndex, const int *types = 0,
263                       const QList<QByteArray> &names = QList<QByteArray>());
264     void appendSignal(const QHashedCStringRef &, quint32, int coreIndex, const int *types = 0,
265                       const QList<QByteArray> &names = QList<QByteArray>());
266     void appendMethod(const QString &, quint32 flags, int coreIndex,
267                       const QList<QByteArray> &names = QList<QByteArray>());
268     void appendMethod(const QHashedCStringRef &, quint32 flags, int coreIndex,
269                       const QList<QByteArray> &names = QList<QByteArray>());
270
271     const QMetaObject *metaObject() const;
272     const QMetaObject *createMetaObject();
273     const QMetaObject *firstCppMetaObject() const;
274
275     inline QQmlPropertyData *property(const QHashedV8String &) const;
276     QQmlPropertyData *property(const QHashedStringRef &) const;
277     QQmlPropertyData *property(const QHashedCStringRef &) const;
278     QQmlPropertyData *property(const QString &) const;
279     QQmlPropertyData *property(int) const;
280     QQmlPropertyData *method(int) const;
281     QQmlPropertyData *signal(int) const;
282     int methodIndexToSignalIndex(int) const;
283     QStringList propertyNames() const;
284
285     QString defaultPropertyName() const;
286     QQmlPropertyData *defaultProperty() const;
287     QQmlPropertyCache *parent() const;
288
289     inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
290     inline bool isAllowedInRevision(QQmlPropertyData *) const;
291
292     inline QQmlEngine *qmlEngine() const;
293     static QQmlPropertyData *property(QQmlEngine *, QObject *, const QString &,
294                                               QQmlPropertyData &);
295     static QQmlPropertyData *property(QQmlEngine *, QObject *, const QHashedV8String &,
296                                               QQmlPropertyData &);
297     static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
298                                      QByteArray *unknownTypeError);
299     static QList<QByteArray> signalParameterNames(QObject *, int index);
300
301     const char *className() const;
302
303     inline int propertyCount() const;
304     inline int propertyOffset() const;
305     inline int methodCount() const;
306     inline int methodOffset() const;
307     inline int signalCount() const;
308     inline int signalOffset() const;
309
310     static bool isDynamicMetaObject(const QMetaObject *);
311
312     void toMetaObjectBuilder(QMetaObjectBuilder &);
313
314 protected:
315     virtual void destroy();
316     virtual void clear();
317
318 private:
319     friend class QQmlEnginePrivate;
320     friend class QV8QObjectWrapper;
321     friend class QQmlCompiler;
322
323     inline QQmlPropertyCache *copy(int reserve);
324
325     void append(QQmlEngine *, const QMetaObject *, int revision,
326                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
327                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
328                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
329
330     // Implemented in v8/qv8qobjectwrapper.cpp
331     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
332
333     typedef QVector<QQmlPropertyData> IndexCache;
334     typedef QStringHash<QQmlPropertyData *> StringCache;
335     typedef QVector<int> AllowedRevisionCache;
336
337     void resolve(QQmlPropertyData *) const;
338     void updateRecur(QQmlEngine *, const QMetaObject *);
339
340     QQmlEngine *engine;
341
342     QQmlPropertyCache *_parent;
343     int propertyIndexCacheStart;
344     int methodIndexCacheStart;
345     int signalHandlerIndexCacheStart;
346
347     IndexCache propertyIndexCache;
348     IndexCache methodIndexCache;
349     IndexCache signalHandlerIndexCache;
350     StringCache stringCache;
351     AllowedRevisionCache allowedRevisionCache;
352     v8::Persistent<v8::Function> constructor;
353
354     bool _ownMetaObject;
355     const QMetaObject *_metaObject;
356     QByteArray _dynamicClassName;
357     QByteArray _dynamicStringData;
358     QString _defaultPropertyName;
359     QQmlPropertyCacheMethodArguments *argumentsCache;
360 };
361
362 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
363 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
364 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
365 // QObject type used in assignment or when we don't have a QQmlEngine etc.
366 //
367 // This class does NOT reference the propertycache.
368 class QQmlEnginePrivate;
369 class Q_QML_EXPORT QQmlMetaObject
370 {
371 public:
372     inline QQmlMetaObject();
373     inline QQmlMetaObject(QObject *);
374     inline QQmlMetaObject(const QMetaObject *);
375     inline QQmlMetaObject(QQmlPropertyCache *);
376     inline QQmlMetaObject(const QQmlMetaObject &);
377
378     inline QQmlMetaObject &operator=(const QQmlMetaObject &);
379
380     inline bool isNull() const;
381
382     inline const char *className() const;
383     inline int propertyCount() const;
384
385     inline bool hasMetaObject() const;
386     inline const QMetaObject *metaObject() const;
387
388     QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
389
390     static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
391
392 private:
393     QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
394 };
395   
396 QQmlPropertyData::QQmlPropertyData()
397 {
398     propType = 0;
399     coreIndex = -1;
400     notifyIndex = -1;
401     overrideIndexIsProperty = false;
402     overrideIndex = -1;
403     revision = 0;
404     metaObjectOffset = -1; 
405     flags = 0;
406 }
407
408 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
409 {
410     *(static_cast<QQmlPropertyRawData *>(this)) = d;
411 }
412
413 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
414 {
415     return flags == other.flags &&
416            propType == other.propType &&
417            coreIndex == other.coreIndex &&
418            notifyIndex == other.notifyIndex &&
419            revision == other.revision &&
420            (!isValueTypeVirtual() || 
421             (valueTypeCoreIndex == other.valueTypeCoreIndex && 
422              valueTypePropType == other.valueTypePropType));
423 }
424
425 int QQmlPropertyRawData::getValueTypeCoreIndex() const
426 {
427     return isValueTypeVirtual()?valueTypeCoreIndex:-1;
428 }
429
430 int QQmlPropertyRawData::encodedIndex() const
431 {
432     return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 24)):coreIndex;
433 }
434
435 QQmlPropertyData *
436 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
437 {
438     if (!data->hasOverride())
439         return 0;
440
441     if (data->overrideIndexIsProperty)
442         return property(data->overrideIndex);
443     else
444         return method(data->overrideIndex);
445 }
446
447 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
448 {
449     return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
450            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
451 }
452
453 QQmlEngine *QQmlPropertyCache::qmlEngine() const
454 {
455     return engine;
456 }
457
458 QQmlPropertyData *QQmlPropertyCache::property(const QHashedV8String &str) const
459 {
460     QQmlPropertyData **rv = stringCache.value(str);
461     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
462     return rv?*rv:0;
463 }
464
465 int QQmlPropertyCache::propertyCount() const
466 {
467     return propertyIndexCacheStart + propertyIndexCache.count();
468 }
469
470 int QQmlPropertyCache::propertyOffset() const
471 {
472     return propertyIndexCacheStart;
473 }
474
475 int QQmlPropertyCache::methodCount() const
476 {
477     return methodIndexCacheStart + methodIndexCache.count();
478 }
479
480 int QQmlPropertyCache::methodOffset() const
481 {
482     return methodIndexCacheStart;
483 }
484
485 int QQmlPropertyCache::signalCount() const
486 {
487     return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
488 }
489
490 int QQmlPropertyCache::signalOffset() const
491 {
492     return signalHandlerIndexCacheStart;
493 }
494
495 QQmlMetaObject::QQmlMetaObject()
496 {
497 }
498
499 QQmlMetaObject::QQmlMetaObject(QObject *o)
500 {
501     if (o) {
502         QQmlData *ddata = QQmlData::get(o, false);
503         if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
504         else _m = o->metaObject();
505     }
506 }
507
508 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
509 : _m(m)
510 {
511 }
512
513 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
514 : _m(m)
515 {
516 }
517
518 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
519 : _m(o._m)
520 {
521 }
522
523 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
524 {
525     _m = o._m;
526     return *this;
527 }
528
529 bool QQmlMetaObject::isNull() const
530 {
531     return _m.isNull();
532 }
533
534 const char *QQmlMetaObject::className() const
535 {
536     if (_m.isNull()) {
537         return 0;
538     } else if (_m.isT1()) {
539         return _m.asT1()->className();
540     } else {
541         return _m.asT2()->className();
542     }
543 }
544
545 int QQmlMetaObject::propertyCount() const
546 {
547     if (_m.isNull()) {
548         return 0;
549     } else if (_m.isT1()) {
550         return _m.asT1()->propertyCount();
551     } else {
552         return _m.asT2()->propertyCount();
553     }
554 }
555
556 bool QQmlMetaObject::hasMetaObject() const
557 {
558     return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
559 }
560
561 const QMetaObject *QQmlMetaObject::metaObject() const
562 {
563     if (_m.isNull()) return 0;
564     if (_m.isT1()) return _m.asT1()->createMetaObject();
565     else return _m.asT2();
566 }
567
568 QT_END_NAMESPACE
569
570 #endif // QQMLPROPERTYCACHE_P_H