summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2025-05-31 17:34:54 +0300
committerAhmad Samir <a.samirh78@gmail.com>2025-09-08 22:27:52 +0300
commit67abb290be80024cd36cc4c78d4e74c3ff705dba (patch)
treea1a7e2800c57c8180f235480fcaf68bcd9989119
parent0cf90e65bfa1a0c19055b17519d333939bc23759 (diff)
QMetaObjectPrivate: use QSpan instead of passing a length and pointer
This is easier to read specially because the QArgumentTypeArray parameter is passed on to other methods down the line. Use std::equal to compare the signal's args with the slot's args, instead of hand-rolled for-loop. Change-Id: I38ad0aa02f08dbe86f4f24ed040ec36f3a147b49 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/kernel/qmetaobject.cpp74
-rw-r--r--src/corelib/kernel/qmetaobject_p.h28
-rw-r--r--src/corelib/kernel/qobject.cpp25
3 files changed, 63 insertions, 64 deletions
diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp
index 7c5d3da1653..a00a6d1aa42 100644
--- a/src/corelib/kernel/qmetaobject.cpp
+++ b/src/corelib/kernel/qmetaobject.cpp
@@ -10,6 +10,7 @@
#include "qobject_p.h"
#include <qcoreapplication.h>
+#include <QtCore/qspan.h>
#include <qvariant.h>
// qthread(_p).h uses QT_CONFIG(thread) internally and has a dummy
@@ -647,9 +648,10 @@ int QMetaObject::classInfoCount() const
// matches the given name, argument count and argument types, otherwise
// returns \c false.
bool QMetaObjectPrivate::methodMatch(const QMetaObject *m, const QMetaMethod &method,
- QByteArrayView name, int argc,
- const QArgumentType *types)
+ QByteArrayView name,
+ QSpan<const QArgumentType> types)
{
+ const qsizetype argc = types.size();
const QMetaMethod::Data &data = method.data;
auto priv = QMetaMethodPrivate::get(&method);
if (priv->parameterCount() != argc)
@@ -664,7 +666,7 @@ bool QMetaObjectPrivate::methodMatch(const QMetaObject *m, const QMetaMethod &me
const QtPrivate::QMetaTypeInterface * const *ifaces = priv->parameterMetaTypeInterfaces();
int paramsIndex = data.parameters() + 1;
- for (int i = 0; i < argc; ++i) {
+ for (qsizetype i = 0; i < argc; ++i) {
uint typeInfo = m->d.data[paramsIndex + i];
QMetaType mt = types[i].metaType();
if (mt.isValid()) {
@@ -709,8 +711,8 @@ QMetaMethod QMetaObjectPrivate::firstMethod(const QMetaObject *baseObject, QByte
*/
template<int MethodType>
inline int QMetaObjectPrivate::indexOfMethodRelative(const QMetaObject **baseObject,
- QByteArrayView name, int argc,
- const QArgumentType *types)
+ QByteArrayView name,
+ QSpan<const QArgumentType> types)
{
for (const QMetaObject *m = *baseObject; m; m = m->d.superdata) {
Q_ASSERT(priv(m->d.data)->revision >= 7);
@@ -721,7 +723,7 @@ inline int QMetaObjectPrivate::indexOfMethodRelative(const QMetaObject **baseObj
for (; i >= end; --i) {
auto data = QMetaMethod::fromRelativeMethodIndex(m, i);
- if (methodMatch(m, data, name, argc, types)) {
+ if (methodMatch(m, data, name, types)) {
*baseObject = m;
return i;
}
@@ -772,7 +774,7 @@ static int indexOfConstructor_helper(const QMetaObject *mo, const char *construc
{
QArgumentTypeArray types;
QByteArrayView name = QMetaObjectPrivate::decodeMethodSignature(constructor, types);
- return QMetaObjectPrivate::indexOfConstructor(mo, name, types.size(), types.constData());
+ return QMetaObjectPrivate::indexOfConstructor(mo, name, types);
}
int QMetaObject::indexOfConstructor(const char *constructor) const
@@ -800,7 +802,7 @@ static int indexOfMethod_helper(const QMetaObject *m, const char *method)
Q_ASSERT(priv(m->d.data)->revision >= 7);
QArgumentTypeArray types;
QByteArrayView name = QMetaObjectPrivate::decodeMethodSignature(method, types);
- i = QMetaObjectPrivate::indexOfMethodRelative<0>(&m, name, types.size(), types.constData());
+ i = QMetaObjectPrivate::indexOfMethodRelative<0>(&m, name, types);
if (i >= 0)
i += m->methodOffset();
return i;
@@ -851,7 +853,7 @@ static int indexOfSignal_helper(const QMetaObject *m, const char *signal)
Q_ASSERT(priv(m->d.data)->revision >= 7);
QArgumentTypeArray types;
QByteArrayView name = QMetaObjectPrivate::decodeMethodSignature(signal, types);
- i = QMetaObjectPrivate::indexOfSignalRelative(&m, name, types.size(), types.constData());
+ i = QMetaObjectPrivate::indexOfSignalRelative(&m, name, types);
if (i >= 0)
i += m->methodOffset();
return i;
@@ -871,14 +873,14 @@ int QMetaObject::indexOfSignal(const char *signal) const
\a baseObject will be adjusted to the enclosing QMetaObject, or \nullptr if the signal is not found
*/
int QMetaObjectPrivate::indexOfSignalRelative(const QMetaObject **baseObject,
- QByteArrayView name, int argc,
- const QArgumentType *types)
+ QByteArrayView name,
+ QSpan<const QArgumentType> types)
{
- int i = indexOfMethodRelative<MethodSignal>(baseObject, name, argc, types);
+ int i = indexOfMethodRelative<MethodSignal>(baseObject, name, types);
#ifndef QT_NO_DEBUG
const QMetaObject *m = *baseObject;
if (i >= 0 && m && m->d.superdata) {
- int conflict = indexOfMethod(m->d.superdata, name, argc, types);
+ int conflict = indexOfMethod(m->d.superdata, name, types);
if (conflict >= 0) {
QMetaMethod conflictMethod = m->d.superdata->method(conflict);
qWarning("QMetaObject::indexOfSignal: signal %s from %s redefined in %s",
@@ -907,7 +909,7 @@ static int indexOfSlot_helper(const QMetaObject *m, const char *slot)
Q_ASSERT(priv(m->d.data)->revision >= 7);
QArgumentTypeArray types;
QByteArrayView name = QMetaObjectPrivate::decodeMethodSignature(slot, types);
- i = QMetaObjectPrivate::indexOfSlotRelative(&m, name, types.size(), types.constData());
+ i = QMetaObjectPrivate::indexOfSlotRelative(&m, name, types);
if (i >= 0)
i += m->methodOffset();
return i;
@@ -924,45 +926,45 @@ int QMetaObject::indexOfSlot(const char *slot) const
// same as indexOfSignalRelative but for slots.
int QMetaObjectPrivate::indexOfSlotRelative(const QMetaObject **m,
- QByteArrayView name, int argc,
- const QArgumentType *types)
+ QByteArrayView name,
+ QSpan<const QArgumentType> types)
{
- return indexOfMethodRelative<MethodSlot>(m, name, argc, types);
+ return indexOfMethodRelative<MethodSlot>(m, name, types);
}
int QMetaObjectPrivate::indexOfSignal(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types)
+ QSpan<const QArgumentType> types)
{
- int i = indexOfSignalRelative(&m, name, argc, types);
+ int i = indexOfSignalRelative(&m, name, types);
if (i >= 0)
i += m->methodOffset();
return i;
}
int QMetaObjectPrivate::indexOfSlot(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types)
+ QSpan<const QArgumentType> types)
{
- int i = indexOfSlotRelative(&m, name, argc, types);
+ int i = indexOfSlotRelative(&m, name, types);
if (i >= 0)
i += m->methodOffset();
return i;
}
int QMetaObjectPrivate::indexOfMethod(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types)
+ QSpan<const QArgumentType> types)
{
- int i = indexOfMethodRelative<0>(&m, name, argc, types);
+ int i = indexOfMethodRelative<0>(&m, name, types);
if (i >= 0)
i += m->methodOffset();
return i;
}
int QMetaObjectPrivate::indexOfConstructor(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types)
+ QSpan<const QArgumentType> types)
{
for (int i = priv(m->d.data)->constructorCount-1; i >= 0; --i) {
const QMetaMethod method = QMetaMethod::fromRelativeConstructorIndex(m, i);
- if (methodMatch(m, method, name, argc, types))
+ if (methodMatch(m, method, name, types))
return i;
}
return -1;
@@ -1047,16 +1049,16 @@ QMetaMethod QMetaObjectPrivate::signal(const QMetaObject *m, int signal_index)
Returns \c true if the \a signalTypes and \a methodTypes are
compatible; otherwise returns \c false.
*/
-bool QMetaObjectPrivate::checkConnectArgs(int signalArgc, const QArgumentType *signalTypes,
- int methodArgc, const QArgumentType *methodTypes)
+bool QMetaObjectPrivate::checkConnectArgs(QSpan<const QArgumentType> signalTypes,
+ QSpan<const QArgumentType> methodTypes)
{
- if (signalArgc < methodArgc)
- return false;
- for (int i = 0; i < methodArgc; ++i) {
- if (signalTypes[i] != methodTypes[i])
- return false;
+ const qsizetype methodArgc = methodTypes.size();
+ if (signalTypes.size() >= methodArgc) {
+ signalTypes = signalTypes.first(methodArgc);
+ return std::equal(signalTypes.begin(), signalTypes.end(),
+ methodTypes.begin(), methodTypes.end());
}
- return true;
+ return false;
}
/*!
@@ -4268,12 +4270,12 @@ int QMetaProperty::notifySignalIndex() const
const QByteArrayView signalName = stringDataView(mobj, methodIndex);
const QMetaObject *m = mobj;
// try 0-arg signal
- int idx = QMetaObjectPrivate::indexOfMethodRelative<MethodSignal>(&m, signalName, 0, nullptr);
+ int idx = QMetaObjectPrivate::indexOfMethodRelative<MethodSignal>(&m, signalName, {});
if (idx >= 0)
return idx + m->methodOffset();
// try 1-arg signal
- QArgumentType argType(metaType());
- idx = QMetaObjectPrivate::indexOfMethodRelative<MethodSignal>(&m, signalName, 1, &argType);
+ QArgumentType argType[] = {metaType()};
+ idx = QMetaObjectPrivate::indexOfMethodRelative<MethodSignal>(&m, signalName, argType);
if (idx >= 0)
return idx + m->methodOffset();
qWarning("QMetaProperty::notifySignal: cannot find the NOTIFY signal %s in class %s for property '%s'",
diff --git a/src/corelib/kernel/qmetaobject_p.h b/src/corelib/kernel/qmetaobject_p.h
index e8df9b20f8d..a44ae29137a 100644
--- a/src/corelib/kernel/qmetaobject_p.h
+++ b/src/corelib/kernel/qmetaobject_p.h
@@ -134,19 +134,19 @@ struct QMetaObjectPrivate
static QByteArrayView decodeMethodSignature(QByteArrayView signature,
QArgumentTypeArray &types);
static int indexOfSignalRelative(const QMetaObject **baseObject,
- QByteArrayView name, int argc,
- const QArgumentType *types);
+ QByteArrayView name,
+ QSpan<const QArgumentType> types);
static int indexOfSlotRelative(const QMetaObject **m,
- QByteArrayView name, int argc,
- const QArgumentType *types);
+ QByteArrayView name,
+ QSpan<const QArgumentType> types);
static int indexOfSignal(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types);
+ QSpan<const QArgumentType> types);
static int indexOfSlot(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types);
+ QSpan<const QArgumentType> types);
static int indexOfMethod(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types);
+ QSpan<const QArgumentType> types);
static int indexOfConstructor(const QMetaObject *m, QByteArrayView name,
- int argc, const QArgumentType *types);
+ QSpan<const QArgumentType> types);
enum class Which { Name, Alias };
static int indexOfEnumerator(const QMetaObject *m, QByteArrayView name, Which which);
@@ -163,8 +163,8 @@ struct QMetaObjectPrivate
}
Q_CORE_EXPORT static int absoluteSignalCount(const QMetaObject *m);
Q_CORE_EXPORT static int signalIndex(const QMetaMethod &m);
- static bool checkConnectArgs(int signalArgc, const QArgumentType *signalTypes,
- int methodArgc, const QArgumentType *methodTypes);
+ static bool checkConnectArgs(QSpan<const QArgumentType> signalTypes,
+ QSpan<const QArgumentType> methodTypes);
static bool checkConnectArgs(const QMetaMethodPrivate *signal,
const QMetaMethodPrivate *method);
@@ -193,12 +193,12 @@ struct QMetaObjectPrivate
template<int MethodType>
static inline int indexOfMethodRelative(const QMetaObject **baseObject,
- QByteArrayView name, int argc,
- const QArgumentType *types);
+ QByteArrayView name,
+ QSpan<const QArgumentType> types);
static bool methodMatch(const QMetaObject *m, const QMetaMethod &method,
- QByteArrayView name, int argc,
- const QArgumentType *types);
+ QByteArrayView name,
+ QSpan<const QArgumentType> types);
Q_CORE_EXPORT static QMetaMethod firstMethod(const QMetaObject *baseObject, QByteArrayView name);
};
diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp
index a10c4258f53..6493f95c050 100644
--- a/src/corelib/kernel/qobject.cpp
+++ b/src/corelib/kernel/qobject.cpp
@@ -3064,8 +3064,7 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
QArgumentTypeArray signalTypes;
Q_ASSERT(QMetaObjectPrivate::get(smeta)->revision >= 7);
QByteArrayView signalName = QMetaObjectPrivate::decodeMethodSignature(signalView, signalTypes);
- int signal_index = QMetaObjectPrivate::indexOfSignalRelative(
- &smeta, signalName, signalTypes.size(), signalTypes.constData());
+ int signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signalName, signalTypes);
if (signal_index < 0) {
// check for normalized signatures
pinnedSignal = QMetaObjectPrivate::normalizedSignature(signalView);
@@ -3074,8 +3073,7 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
signalTypes.clear();
signalName = QMetaObjectPrivate::decodeMethodSignature(signalView, signalTypes);
smeta = sender->metaObject();
- signal_index = QMetaObjectPrivate::indexOfSignalRelative(
- &smeta, signalName, signalTypes.size(), signalTypes.constData());
+ signal_index = QMetaObjectPrivate::indexOfSignalRelative(&smeta, signalName, signalTypes);
}
if (signal_index < 0) {
err_method_notfound(sender, signal_arg, "connect");
@@ -3098,13 +3096,14 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
switch (membcode) {
case QSLOT_CODE:
method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(
- &rmeta, methodName, methodTypes.size(), methodTypes.constData());
+ &rmeta, methodName, methodTypes);
break;
case QSIGNAL_CODE:
method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(
- &rmeta, methodName, methodTypes.size(), methodTypes.constData());
+ &rmeta, methodName, methodTypes);
break;
}
+
if (method_index_relative < 0) {
// check for normalized methods
pinnedMethod = QMetaObjectPrivate::normalizedSignature(methodView);
@@ -3117,11 +3116,11 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
switch (membcode) {
case QSLOT_CODE:
method_index_relative = QMetaObjectPrivate::indexOfSlotRelative(
- &rmeta, methodName, methodTypes.size(), methodTypes.constData());
+ &rmeta, methodName, methodTypes);
break;
case QSIGNAL_CODE:
method_index_relative = QMetaObjectPrivate::indexOfSignalRelative(
- &rmeta, methodName, methodTypes.size(), methodTypes.constData());
+ &rmeta, methodName, methodTypes);
break;
}
}
@@ -3132,8 +3131,7 @@ QMetaObject::Connection QObject::connect(const QObject *sender, const char *sign
return QMetaObject::Connection(nullptr);
}
- if (!QMetaObjectPrivate::checkConnectArgs(signalTypes.size(), signalTypes.constData(),
- methodTypes.size(), methodTypes.constData())) {
+ if (!QMetaObjectPrivate::checkConnectArgs(signalTypes, methodTypes)) {
qCWarning(lcConnect,
"QObject::connect: Incompatible sender/receiver arguments"
"\n %s::%s --> %s::%s",
@@ -3397,7 +3395,7 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
int signal_index = -1;
if (signal) {
signal_index = QMetaObjectPrivate::indexOfSignalRelative(
- &smeta, signalName, signalTypes.size(), signalTypes.constData());
+ &smeta, signalName, signalTypes);
if (signal_index < 0)
break;
signal_index = QMetaObjectPrivate::originalClone(smeta, signal_index);
@@ -3411,7 +3409,7 @@ bool QObject::disconnect(const QObject *sender, const char *signal,
const QMetaObject *rmeta = receiver->metaObject();
do {
int method_index = QMetaObjectPrivate::indexOfMethod(
- rmeta, methodName, methodTypes.size(), methodTypes.constData());
+ rmeta, methodName, methodTypes);
if (method_index >= 0)
while (method_index < rmeta->methodOffset())
rmeta = rmeta->superClass();
@@ -4418,8 +4416,7 @@ int QObjectPrivate::signalIndex(const char *signalName,
Q_ASSERT(QMetaObjectPrivate::get(base)->revision >= 7);
QArgumentTypeArray types;
QByteArrayView name = QMetaObjectPrivate::decodeMethodSignature(signalName, types);
- int relative_index = QMetaObjectPrivate::indexOfSignalRelative(
- &base, name, types.size(), types.constData());
+ int relative_index = QMetaObjectPrivate::indexOfSignalRelative(&base, name, types);
if (relative_index < 0)
return relative_index;
relative_index = QMetaObjectPrivate::originalClone(base, relative_index);