Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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.
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 *);
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
62 template<typename ArgTypeStorage>
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
77 template<typename ArgTypeStorage>
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(
96
97 template<typename ArgTypeStorage>
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>
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
175{
176 if (o)
177 _m = o->metaObject();
178}
179
181 : _m(m)
182{
183}
184
186{
187 if (m)
188 _m = m->createMetaObject();
189}
190
192 : _m(o._m)
193{
194}
195
197{
198 _m = o._m;
199 return *this;
200}
201
203{
204 return !_m;
205}
206
207const char *QQmlMetaObject::className() const
208{
209 if (!_m)
210 return nullptr;
211 return metaObject()->className();
212}
213
215{
216 if (!_m)
217 return 0;
218 return metaObject()->propertyCount();
219}
220
222{
223 return _m;
224}
225
227
228#endif // QQMLMETAOBJECT_P_H
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qmetaobject.h:19
\inmodule QtCore
Definition qmetatype.h:341
@ IsEnumeration
Definition qmetatype.h:407
\inmodule QtCore
Definition qobject.h:103
bool methodParameterTypes(int index, ArgTypeStorage *argStorage, QByteArray *unknownTypeError) const
bool isNull() const
const char * className() const
static bool methodParameterTypes(const QMetaMethod &method, ArgTypeStorage *argStorage, QByteArray *unknownTypeError)
static bool parameterType(const QMetaMethod &method, int ii, QByteArray *unknownTypeError, const Store &store)
bool constructorParameterTypes(int index, ArgTypeStorage *dummy, QByteArray *unknownTypeError) const
QQmlMetaObject & operator=(const QQmlMetaObject &)
const QMetaObject * metaObject() const
static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
static bool methodReturnAndParameterTypes(const QMetaMethod &method, ArgTypeStorage *argStorage, QByteArray *unknownTypeError)
QVarLengthArray< QMetaType, Prealloc > ArgTypeStorage
QQmlMetaObject()=default
const QMetaObject * _m
int propertyCount() const
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char * method
const GLfloat * m
GLuint index
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
const char className[16]
[1]
Definition qwizard.cpp:100
obj metaObject() -> className()
\inmodule QtCore
int propertyCount() const
Returns the number of properties in this class, including the number of properties provided by each b...
const char * className() const
Returns the class name.
bool inherits(const QMetaObject *metaObject) const noexcept
Returns true if the class described by this QMetaObject inherits the type described by metaObject; ot...