Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlmetaobject_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLMETAOBJECT_P_H
6#define QQMLMETAOBJECT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmlpropertycache_p.h>
20
21#include <QtQml/qtqmlglobal.h>
22#include <QtCore/qvarlengtharray.h>
23#include <QtCore/qmetaobject.h>
24
25QT_BEGIN_NAMESPACE
26
27// QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
28// This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
29// we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
30// QObject type used in assignment or when we don't have a QQmlEngine etc.
31//
32// This class does NOT reference the propertycache.
33class QQmlEnginePrivate;
35class Q_QML_EXPORT QQmlMetaObject
36{
37public:
38 template<qsizetype Prealloc>
39 using ArgTypeStorage = QVarLengthArray<QMetaType, Prealloc>;
40
41 inline QQmlMetaObject() = default;
42 inline QQmlMetaObject(const QObject *);
43 inline QQmlMetaObject(const QMetaObject *);
44 inline QQmlMetaObject(const QQmlPropertyCache::ConstPtr &);
45 inline QQmlMetaObject(const QQmlMetaObject &);
46
47 inline QQmlMetaObject &operator=(const QQmlMetaObject &);
48
49 inline bool isNull() const;
50
51 inline const char *className() const;
52 inline int propertyCount() const;
53
54 inline const QMetaObject *metaObject() const;
55
56 QMetaType methodReturnType(
57 const QQmlPropertyData &data, QString *unknownTypeError = nullptr) const;
58
59 /*!
60 \internal
61 Returns false if one of the types is unknown. Otherwise, fills \a argstorage with the
62 metatypes of the function.
63 */
64 template<typename ArgTypeStorage>
65 bool methodParameterTypes(
66 const QQmlPropertyData &data, ArgTypeStorage *argStorage,
67 QString *unknownTypeError = nullptr) const
68 {
69 Q_ASSERT(_m && data.coreIndex() >= 0);
70
71 QMetaMethod m = _m->method(data.coreIndex());
72 return methodParameterTypes(m, argStorage, unknownTypeError);
73 }
74
75 /*!
76 \internal
77 Returns false if one of the types is unknown. Otherwise, fills \a argstorage with the
78 metatypes of the function.
79 */
80 template<typename ArgTypeStorage>
81 bool methodReturnAndParameterTypes(
82 const QQmlPropertyData &data, ArgTypeStorage *argStorage,
83 QString *unknownTypeError = nullptr) const
84 {
85 Q_ASSERT(_m && data.coreIndex() >= 0);
86
87 QMetaMethod m = _m->method(data.coreIndex());
88 return methodReturnAndParameterTypes(data, m, argStorage, unknownTypeError);
89 }
90
91 /*!
92 \internal
93 Returns false if one of the types is unknown. Otherwise, fills \a argstorage with the
94 metatypes of the function.
95 */
96 template<typename ArgTypeStorage>
97 bool constructorParameterTypes(
98 const QQmlPropertyData &data, ArgTypeStorage *argStorarge,
99 QString *unknownTypeError = nullptr) const
100 {
101 Q_ASSERT(_m && data.coreIndex() >= 0);
102
103 QMetaMethod m = _m->constructor(data.coreIndex());
104 return methodParameterTypes(m, argStorarge, unknownTypeError);
105 }
106
107 /*!
108 \internal
109 Returns false if one of the types is unknown. Otherwise, fills \a argstorage with the
110 metatypes of the function.
111 */
112 template<typename ArgTypeStorage>
113 bool constructorReturnAndParameterTypes(
114 const QQmlPropertyData &data, ArgTypeStorage *storage,
115 QString *unknownTypeError = nullptr) const
116 {
117 Q_ASSERT(_m && data.coreIndex() >= 0);
118
119 QMetaMethod m = _m->constructor(data.coreIndex());
120 return methodReturnAndParameterTypes(data, m, storage, unknownTypeError);
121 }
122
123 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
124 {
125 Q_ASSERT(!from.isNull() && !to.isNull());
126 return from.metaObject()->inherits(to.metaObject());
127 }
128
129 // static_metacall (on Gadgets) doesn't call the base implementation and therefore
130 // we need a helper to find the correct meta object and property/method index.
131 static void resolveGadgetMethodOrPropertyIndex(
132 QMetaObject::Call type, const QMetaObject **metaObject, int *index);
133
134 template<typename ArgTypeStorage>
135 static bool methodParameterTypes(
136 const QMetaMethod &method, ArgTypeStorage *argStorage,
137 QString *unknownTypeError = nullptr)
138 {
139 Q_ASSERT(argStorage);
140
141 const int argc = method.parameterCount();
142 argStorage->resize(argc);
143 for (int ii = 0; ii < argc; ++ii) {
144 if (!parameterType(method, ii, unknownTypeError,
145 [argStorage](int ii, QMetaType &&type) {
146 argStorage->operator[](ii) = std::forward<QMetaType>(type);
147 })) {
148 return false;
149 }
150 }
151 return true;
152 }
153
154 template<typename ArgTypeStorage>
155 bool methodReturnAndParameterTypes(
156 const QQmlPropertyData &data, const QMetaMethod &method, ArgTypeStorage *argStorage,
157 QString *unknownTypeError = nullptr) const
158 {
159 Q_ASSERT(argStorage);
160
161 const int argc = method.parameterCount();
162 argStorage->resize(argc + 1);
163
164 QMetaType type = methodReturnType(data, unknownTypeError);
165 if (!type.isValid())
166 return false;
167
168 argStorage->operator[](0) = type;
169
170 for (int ii = 0; ii < argc; ++ii) {
171 if (!parameterType(
172 method, ii, unknownTypeError, [argStorage](int ii, QMetaType &&type) {
173 argStorage->operator[](ii + 1) = std::forward<QMetaType>(type);
174 })) {
175 return false;
176 }
177 }
178
179 return true;
180 }
181
182protected:
183 template<typename Store>
184 static bool parameterType(
185 const QMetaMethod &method, int ii, QString *unknownTypeError, const Store &store)
186 {
187 QMetaType type = method.parameterMetaType(ii);
188
189 // we treat enumerations as their underlying type
190 if (type.flags().testFlag(QMetaType::IsEnumeration))
191 type = type.underlyingType();
192
193 if (!type.isValid()) {
194 if (unknownTypeError)
195 *unknownTypeError = QLatin1String("Unknown method parameter type: ")
196 + QString::fromUtf8(method.parameterTypeName(ii));
197 return false;
198 }
199
200 store(ii, std::move(type));
201 return true;
202 }
203
204
205 const QMetaObject *_m = nullptr;
206
207};
208
209QQmlMetaObject::QQmlMetaObject(const QObject *o)
210{
211 if (o)
212 _m = o->metaObject();
213}
214
215QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
216 : _m(m)
217{
218}
219
220QQmlMetaObject::QQmlMetaObject(const QQmlPropertyCache::ConstPtr &m)
221{
222 if (m)
223 _m = m->createMetaObject();
224}
225
226QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
227 : _m(o._m)
228{
229}
230
231QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
232{
233 _m = o._m;
234 return *this;
235}
236
237bool QQmlMetaObject::isNull() const
238{
239 return !_m;
240}
241
242const char *QQmlMetaObject::className() const
243{
244 if (!_m)
245 return nullptr;
246 return metaObject()->className();
247}
248
249int QQmlMetaObject::propertyCount() const
250{
251 if (!_m)
252 return 0;
253 return metaObject()->propertyCount();
254}
255
256const QMetaObject *QQmlMetaObject::metaObject() const
257{
258 return _m;
259}
260
261QT_END_NAMESPACE
262
263#endif // QQMLMETAOBJECT_P_H
reference operator*() const
Definition qqmllist_p.h:105
pointer(const QQmlListIterator *iter)
Definition qqmllist_p.h:104
QQmlListIterator operator->() const
Definition qqmllist_p.h:106
reference(const QQmlListIterator *iter)
Definition qqmllist_p.h:71
reference(reference &&)=default
reference & operator=(reference &&value)
Definition qqmllist_p.h:89
reference & operator=(const reference &value)
Definition qqmllist_p.h:88
reference & operator=(T *value)
Definition qqmllist_p.h:83
reference(const reference &)=default
friend void swap(reference a, reference b)
Definition qqmllist_p.h:91
friend QQmlListIterator operator+(qsizetype i, const QQmlListIterator &j)
Definition qqmllist_p.h:204
friend bool operator<=(const QQmlListIterator &i, const QQmlListIterator &j)
Definition qqmllist_p.h:199
QQmlListIterator operator-(qsizetype j)
Definition qqmllist_p.h:158
reference operator*() const
Definition qqmllist_p.h:163
friend qsizetype operator-(const QQmlListIterator &i, const QQmlListIterator &j)
Definition qqmllist_p.h:209
pointer operator->() const
Definition qqmllist_p.h:168
friend bool operator==(const QQmlListIterator &a, const QQmlListIterator &b)
Definition qqmllist_p.h:174
QQmlListIterator(QQmlListProperty< T > *list, qsizetype i)
Definition qqmllist_p.h:113
friend bool operator>=(const QQmlListIterator &i, const QQmlListIterator &j)
Definition qqmllist_p.h:189
QQmlListIterator operator+(qsizetype j)
Definition qqmllist_p.h:153
QQmlListIterator & operator++()
Definition qqmllist_p.h:115
friend bool operator>(const QQmlListIterator &i, const QQmlListIterator &j)
Definition qqmllist_p.h:194
friend bool operator<(const QQmlListIterator &i, const QQmlListIterator &j)
Definition qqmllist_p.h:184
QQmlListIterator & operator-=(qsizetype j)
Definition qqmllist_p.h:147
QQmlListIterator operator++(int)
Definition qqmllist_p.h:121
friend bool operator!=(const QQmlListIterator &a, const QQmlListIterator &b)
Definition qqmllist_p.h:179
QQmlListIterator()=default
QQmlListIterator & operator--()
Definition qqmllist_p.h:128
QQmlListIterator & operator+=(qsizetype j)
Definition qqmllist_p.h:141
QQmlListIterator operator--(int)
Definition qqmllist_p.h:134
The QQmlListProperty class allows applications to expose list-like properties of QObject-derived clas...
Definition qqmllist.h:25
T *(*)(QQmlListProperty< T > *, qsizetype) AtFunction
Synonym for {T *(*)(QQmlListProperty<T> *property, qsizetype index)}.
Definition qqmllist.h:31
QQmlListProperty(QObject *o, QList< T * > *list)
Definition qqmllist.h:38
ClearFunction clear
Definition qqmllist.h:88
bool operator==(const QQmlListProperty &o) const
Returns true if this QQmlListProperty is equal to other, otherwise false.
Definition qqmllist.h:71
QQmlListProperty(QObject *o, void *d, AppendFunction a, CountFunction c, AtFunction t, ClearFunction r)
Construct a QQmlListProperty from a set of operation functions append, count, at, and clear.
Definition qqmllist.h:43
QQmlListProperty(QObject *o, void *d, CountFunction c, AtFunction a)
Construct a readonly QQmlListProperty from a set of operation functions count and at.
Definition qqmllist.h:67
void(*)(QQmlListProperty< T > *) RemoveLastFunction
Synonym for {void (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:34
AtFunction at
Definition qqmllist.h:87
QQmlListProperty(QObject *o, void *d, AppendFunction a, CountFunction c, AtFunction t, ClearFunction r, ReplaceFunction s, RemoveLastFunction p)
Construct a QQmlListProperty from a set of operation functions append, count, at, clear,...
Definition qqmllist.h:55
void(*)(QQmlListProperty< T > *, qsizetype, T *) ReplaceFunction
Synonym for {void (*)(QQmlListProperty<T> *property, qsizetype index, T *value)}.
Definition qqmllist.h:33
QObject * object
Definition qqmllist.h:82
RemoveLastFunction removeLast
Definition qqmllist.h:90
void(*)(QQmlListProperty< T > *, T *) AppendFunction
Synonym for {void (*)(QQmlListProperty<T> *property, T *value)}.
Definition qqmllist.h:29
CountFunction count
Definition qqmllist.h:86
AppendFunction append
Definition qqmllist.h:85
QQmlListProperty()=default
\macro QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_APPEND
ReplaceFunction replace
Definition qqmllist.h:89
void(*)(QQmlListProperty< T > *) ClearFunction
Synonym for {void (*)(QQmlListProperty<T> *property)}.
Definition qqmllist.h:32
static QQmlListReference init(const QQmlListProperty< QObject > &, QMetaType)
Definition qqmllist.cpp:26
const QMetaObject * elementType()
Definition qqmllist_p.h:47
QPointer< QObject > object
Definition qqmllist_p.h:35
QQmlListProperty< QObject > property
Definition qqmllist_p.h:36
static QQmlListReferencePrivate * get(QQmlListReference *ref)
Definition qqmllist_p.h:43
The QQmlListReference class allows the manipulation of \l QQmlListProperty properties.
Definition qqmllist.h:190
Combined button and popup list for selecting options.
static QT_BEGIN_NAMESPACE bool isObjectCompatible(QObject *object, QQmlListReferencePrivate *d)
Definition qqmllist.cpp:11
QQmlListIterator< T > end(QQmlListProperty< T > &list)
Definition qqmllist_p.h:225
QQmlListIterator< T > begin(QQmlListProperty< T > &list)
Definition qqmllist_p.h:219