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