1/****************************************************************************
2**
3** Copyright (C) 2019 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLMETAOBJECT_P_H
41#define QQMLMETAOBJECT_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtQml/qtqmlglobal.h>
55
56#include <private/qflagpointer_p.h>
57#include <private/qqmlpropertycache_p.h>
58
59#include <QtCore/qvarlengtharray.h>
60#include <QtCore/qmetaobject.h>
61
62QT_BEGIN_NAMESPACE
63
64// QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
65// This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
66// we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
67// QObject type used in assignment or when we don't have a QQmlEngine etc.
68//
69// This class does NOT reference the propertycache.
70class QQmlEnginePrivate;
71class QQmlPropertyData;
72class Q_QML_EXPORT QQmlMetaObject
73{
74public:
75 typedef QVarLengthArray<int, 9> ArgTypeStorage;
76
77 inline QQmlMetaObject();
78 inline QQmlMetaObject(QObject *);
79 inline QQmlMetaObject(const QMetaObject *);
80 inline QQmlMetaObject(QQmlPropertyCache *);
81 inline QQmlMetaObject(const QQmlMetaObject &);
82
83 inline QQmlMetaObject &operator=(const QQmlMetaObject &);
84
85 inline bool isNull() const;
86
87 inline const char *className() const;
88 inline int propertyCount() const;
89
90 inline bool hasMetaObject() const;
91 inline const QMetaObject *metaObject() const;
92
93 QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
94
95 int methodReturnType(const QQmlPropertyData &data, QByteArray *unknownTypeError) const;
96 int *methodParameterTypes(int index, ArgTypeStorage *argStorage,
97 QByteArray *unknownTypeError) const;
98
99 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
100
101 // static_metacall (on Gadgets) doesn't call the base implementation and therefore
102 // we need a helper to find the correct meta object and property/method index.
103 static void resolveGadgetMethodOrPropertyIndex(QMetaObject::Call type, const QMetaObject **metaObject, int *index);
104
105protected:
106 QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
107 int *methodParameterTypes(const QMetaMethod &method, ArgTypeStorage *argStorage,
108 QByteArray *unknownTypeError) const;
109
110};
111
112QQmlMetaObject::QQmlMetaObject()
113{
114}
115
116QQmlMetaObject::QQmlMetaObject(QObject *o)
117{
118 if (o) {
119 QQmlData *ddata = QQmlData::get(object: o, create: false);
120 if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
121 else _m = o->metaObject();
122 }
123}
124
125QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
126 : _m(m)
127{
128}
129
130QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
131 : _m(m)
132{
133}
134
135QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
136 : _m(o._m)
137{
138}
139
140QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
141{
142 _m = o._m;
143 return *this;
144}
145
146bool QQmlMetaObject::isNull() const
147{
148 return _m.isNull();
149}
150
151const char *QQmlMetaObject::className() const
152{
153 if (_m.isNull()) {
154 return nullptr;
155 } else if (_m.isT1()) {
156 return _m.asT1()->className();
157 } else {
158 return _m.asT2()->className();
159 }
160}
161
162int QQmlMetaObject::propertyCount() const
163{
164 if (_m.isNull()) {
165 return 0;
166 } else if (_m.isT1()) {
167 return _m.asT1()->propertyCount();
168 } else {
169 return _m.asT2()->propertyCount();
170 }
171}
172
173bool QQmlMetaObject::hasMetaObject() const
174{
175 return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
176}
177
178const QMetaObject *QQmlMetaObject::metaObject() const
179{
180 if (_m.isNull()) return nullptr;
181 if (_m.isT1()) return _m.asT1()->createMetaObject();
182 else return _m.asT2();
183}
184
185QT_END_NAMESPACE
186
187#endif // QQMLMETAOBJECT_P_H
188

source code of qtdeclarative/src/qml/qml/qqmlmetaobject_p.h