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(const QQmlPropertyData &data, QByteArray *unknownTypeError) const;
56
57 /*!
58 \internal
59 Returns false if one of the types is unknown. Otherwise, fills \a argstorage with the
60 metatypes of the function.
61 */
62 template<typename ArgTypeStorage>
63 bool methodParameterTypes(
64 int index, ArgTypeStorage *argStorage, QByteArray *unknownTypeError) const
65 {
66 Q_ASSERT(_m && index >= 0);
67
68 QMetaMethod m = _m->method(index);
69 return methodParameterTypes(m, argStorage, unknownTypeError);
70 }
71
72 /*!
73 \internal
74 Returns false if one of the types is unknown. Otherwise, fills \a argstorage with the
75 metatypes of the function.
76 */
77 template<typename ArgTypeStorage>
78 bool constructorParameterTypes(
79 int index, ArgTypeStorage *dummy, QByteArray *unknownTypeError) const
80 {
81 QMetaMethod m = _m->constructor(index);
82 return methodParameterTypes(m, dummy, unknownTypeError);
83 }
84
85
86 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
87 {
88 Q_ASSERT(!from.isNull() && !to.isNull());
89 return from.metaObject()->inherits(to.metaObject());
90 }
91
92 // static_metacall (on Gadgets) doesn't call the base implementation and therefore
93 // we need a helper to find the correct meta object and property/method index.
94 static void resolveGadgetMethodOrPropertyIndex(
95 QMetaObject::Call type, const QMetaObject **metaObject, int *index);
96
97 template<typename ArgTypeStorage>
98 static bool methodParameterTypes(
99 const QMetaMethod &method, ArgTypeStorage *argStorage, QByteArray *unknownTypeError)
100 {
101 Q_ASSERT(argStorage);
102
103 const int argc = method.parameterCount();
104 argStorage->resize(argc);
105 for (int ii = 0; ii < argc; ++ii) {
106 if (!parameterType(method, ii, unknownTypeError, [argStorage](int ii, QMetaType &&type) {
107 argStorage->operator[](ii) = std::forward<QMetaType>(type);
108 })) {
109 return false;
110 }
111 }
112 return true;
113 }
114
115 template<typename ArgTypeStorage>
116 static bool methodReturnAndParameterTypes(
117 const QMetaMethod &method, ArgTypeStorage *argStorage, QByteArray *unknownTypeError)
118 {
119 Q_ASSERT(argStorage);
120
121 const int argc = method.parameterCount();
122 argStorage->resize(argc + 1);
123
124 QMetaType type = method.returnMetaType();
125 if (type.flags().testFlag(QMetaType::IsEnumeration))
126 type = type.underlyingType();
127
128 if (!type.isValid()) {
129 if (unknownTypeError)
130 *unknownTypeError = "return type";
131 return false;
132 }
133
134 argStorage->operator[](0) = type;
135
136 for (int ii = 0; ii < argc; ++ii) {
137 if (!parameterType(
138 method, ii, unknownTypeError, [argStorage](int ii, QMetaType &&type) {
139 argStorage->operator[](ii + 1) = std::forward<QMetaType>(type);
140 })) {
141 return false;
142 }
143 }
144
145 return true;
146 }
147
148protected:
149 template<typename Store>
150 static bool parameterType(
151 const QMetaMethod &method, int ii, QByteArray *unknownTypeError, const Store &store)
152 {
153 QMetaType type = method.parameterMetaType(ii);
154
155 // we treat enumerations as their underlying type
156 if (type.flags().testFlag(QMetaType::IsEnumeration))
157 type = type.underlyingType();
158
159 if (!type.isValid()) {
160 if (unknownTypeError)
161 *unknownTypeError = method.parameterTypeName(ii);
162 return false;
163 }
164
165 store(ii, std::move(type));
166 return true;
167 }
168
169
170 const QMetaObject *_m = nullptr;
171
172};
173
174QQmlMetaObject::QQmlMetaObject(const QObject *o)
175{
176 if (o)
177 _m = o->metaObject();
178}
179
180QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
181 : _m(m)
182{
183}
184
185QQmlMetaObject::QQmlMetaObject(const QQmlPropertyCache::ConstPtr &m)
186{
187 if (m)
188 _m = m->createMetaObject();
189}
190
191QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
192 : _m(o._m)
193{
194}
195
196QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
197{
198 _m = o._m;
199 return *this;
200}
201
202bool QQmlMetaObject::isNull() const
203{
204 return !_m;
205}
206
207const char *QQmlMetaObject::className() const
208{
209 if (!_m)
210 return nullptr;
211 return metaObject()->className();
212}
213
214int QQmlMetaObject::propertyCount() const
215{
216 if (!_m)
217 return 0;
218 return metaObject()->propertyCount();
219}
220
221const QMetaObject *QQmlMetaObject::metaObject() const
222{
223 return _m;
224}
225
226QT_END_NAMESPACE
227
228#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:183
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