Support enum return types in Q_INVOKABLE functions.
[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 int methodReturnType(QObject *, const QQmlPropertyData &data,
300                                 QByteArray *unknownTypeError);
301     static QList<QByteArray> signalParameterNames(QObject *, int index);
302
303     const char *className() const;
304
305     inline int propertyCount() const;
306     inline int propertyOffset() const;
307     inline int methodCount() const;
308     inline int methodOffset() const;
309     inline int signalCount() const;
310     inline int signalOffset() const;
311
312     static bool isDynamicMetaObject(const QMetaObject *);
313
314     void toMetaObjectBuilder(QMetaObjectBuilder &);
315
316 protected:
317     virtual void destroy();
318     virtual void clear();
319
320 private:
321     friend class QQmlEnginePrivate;
322     friend class QV8QObjectWrapper;
323     friend class QQmlCompiler;
324
325     inline QQmlPropertyCache *copy(int reserve);
326
327     void append(QQmlEngine *, const QMetaObject *, int revision,
328                 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
329                 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
330                 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
331
332     // Implemented in v8/qv8qobjectwrapper.cpp
333     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
334
335     typedef QVector<QQmlPropertyData> IndexCache;
336     typedef QStringHash<QQmlPropertyData *> StringCache;
337     typedef QVector<int> AllowedRevisionCache;
338
339     void resolve(QQmlPropertyData *) const;
340     void updateRecur(QQmlEngine *, const QMetaObject *);
341
342     QQmlEngine *engine;
343
344     QQmlPropertyCache *_parent;
345     int propertyIndexCacheStart;
346     int methodIndexCacheStart;
347     int signalHandlerIndexCacheStart;
348
349     IndexCache propertyIndexCache;
350     IndexCache methodIndexCache;
351     IndexCache signalHandlerIndexCache;
352     StringCache stringCache;
353     AllowedRevisionCache allowedRevisionCache;
354     v8::Persistent<v8::Function> constructor;
355
356     bool _ownMetaObject;
357     const QMetaObject *_metaObject;
358     QByteArray _dynamicClassName;
359     QByteArray _dynamicStringData;
360     QString _defaultPropertyName;
361     QQmlPropertyCacheMethodArguments *argumentsCache;
362 };
363
364 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
365 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
366 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
367 // QObject type used in assignment or when we don't have a QQmlEngine etc.
368 //
369 // This class does NOT reference the propertycache.
370 class QQmlEnginePrivate;
371 class Q_QML_EXPORT QQmlMetaObject
372 {
373 public:
374     inline QQmlMetaObject();
375     inline QQmlMetaObject(QObject *);
376     inline QQmlMetaObject(const QMetaObject *);
377     inline QQmlMetaObject(QQmlPropertyCache *);
378     inline QQmlMetaObject(const QQmlMetaObject &);
379
380     inline QQmlMetaObject &operator=(const QQmlMetaObject &);
381
382     inline bool isNull() const;
383
384     inline const char *className() const;
385     inline int propertyCount() const;
386
387     inline bool hasMetaObject() const;
388     inline const QMetaObject *metaObject() const;
389
390     QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
391
392     static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
393
394 private:
395     QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
396 };
397   
398 QQmlPropertyData::QQmlPropertyData()
399 {
400     propType = 0;
401     coreIndex = -1;
402     notifyIndex = -1;
403     overrideIndexIsProperty = false;
404     overrideIndex = -1;
405     revision = 0;
406     metaObjectOffset = -1; 
407     flags = 0;
408 }
409
410 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
411 {
412     *(static_cast<QQmlPropertyRawData *>(this)) = d;
413 }
414
415 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
416 {
417     return flags == other.flags &&
418            propType == other.propType &&
419            coreIndex == other.coreIndex &&
420            notifyIndex == other.notifyIndex &&
421            revision == other.revision &&
422            (!isValueTypeVirtual() || 
423             (valueTypeCoreIndex == other.valueTypeCoreIndex && 
424              valueTypePropType == other.valueTypePropType));
425 }
426
427 int QQmlPropertyRawData::getValueTypeCoreIndex() const
428 {
429     return isValueTypeVirtual()?valueTypeCoreIndex:-1;
430 }
431
432 int QQmlPropertyRawData::encodedIndex() const
433 {
434     return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 24)):coreIndex;
435 }
436
437 QQmlPropertyData *
438 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
439 {
440     if (!data->hasOverride())
441         return 0;
442
443     if (data->overrideIndexIsProperty)
444         return property(data->overrideIndex);
445     else
446         return method(data->overrideIndex);
447 }
448
449 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
450 {
451     return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
452            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
453 }
454
455 QQmlEngine *QQmlPropertyCache::qmlEngine() const
456 {
457     return engine;
458 }
459
460 QQmlPropertyData *QQmlPropertyCache::property(const QHashedV8String &str) const
461 {
462     QQmlPropertyData **rv = stringCache.value(str);
463     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
464     return rv?*rv:0;
465 }
466
467 int QQmlPropertyCache::propertyCount() const
468 {
469     return propertyIndexCacheStart + propertyIndexCache.count();
470 }
471
472 int QQmlPropertyCache::propertyOffset() const
473 {
474     return propertyIndexCacheStart;
475 }
476
477 int QQmlPropertyCache::methodCount() const
478 {
479     return methodIndexCacheStart + methodIndexCache.count();
480 }
481
482 int QQmlPropertyCache::methodOffset() const
483 {
484     return methodIndexCacheStart;
485 }
486
487 int QQmlPropertyCache::signalCount() const
488 {
489     return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
490 }
491
492 int QQmlPropertyCache::signalOffset() const
493 {
494     return signalHandlerIndexCacheStart;
495 }
496
497 QQmlMetaObject::QQmlMetaObject()
498 {
499 }
500
501 QQmlMetaObject::QQmlMetaObject(QObject *o)
502 {
503     if (o) {
504         QQmlData *ddata = QQmlData::get(o, false);
505         if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
506         else _m = o->metaObject();
507     }
508 }
509
510 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
511 : _m(m)
512 {
513 }
514
515 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
516 : _m(m)
517 {
518 }
519
520 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
521 : _m(o._m)
522 {
523 }
524
525 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
526 {
527     _m = o._m;
528     return *this;
529 }
530
531 bool QQmlMetaObject::isNull() const
532 {
533     return _m.isNull();
534 }
535
536 const char *QQmlMetaObject::className() const
537 {
538     if (_m.isNull()) {
539         return 0;
540     } else if (_m.isT1()) {
541         return _m.asT1()->className();
542     } else {
543         return _m.asT2()->className();
544     }
545 }
546
547 int QQmlMetaObject::propertyCount() const
548 {
549     if (_m.isNull()) {
550         return 0;
551     } else if (_m.isT1()) {
552         return _m.asT1()->propertyCount();
553     } else {
554         return _m.asT2()->propertyCount();
555     }
556 }
557
558 bool QQmlMetaObject::hasMetaObject() const
559 {
560     return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
561 }
562
563 const QMetaObject *QQmlMetaObject::metaObject() const
564 {
565     if (_m.isNull()) return 0;
566     if (_m.isT1()) return _m.asT1()->createMetaObject();
567     else return _m.asT2();
568 }
569
570 QT_END_NAMESPACE
571
572 #endif // QQMLPROPERTYCACHE_P_H