Skip to content

Commit

Permalink
Further metaobject cleanups
Browse files Browse the repository at this point in the history
Remove QMetaProperty index member. Instead, add a function to compute it
on demand from the QMetaObject.
Also remove the remains of the signature function.

Original-patch-by: Lars Knoll <[email protected]>
Change-Id: I5b16d444e7474216a535e3e93a118825cd0e6090
Reviewed-by: Ulf Hermann <[email protected]>
Reviewed-by: Lars Knoll <[email protected]>
  • Loading branch information
Inkane committed Jun 11, 2020
1 parent f5e8f9c commit 9b383f3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
66 changes: 34 additions & 32 deletions src/corelib/kernel/qmetaobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,21 +565,21 @@ int QMetaObject::classInfoCount() const
return n;
}

// Returns \c true if the method defined by the given meta-object&handle
// Returns \c true if the method defined by the given meta-object&meta-method
// matches the given name, argument count and argument types, otherwise
// returns \c false.
static bool methodMatch(const QMetaObject *m, int handle,
bool QMetaObjectPrivate::methodMatch(const QMetaObject *m, const QMetaMethod &method,
const QByteArray &name, int argc,
const QArgumentType *types)
{
Q_ASSERT(priv(m->d.data)->revision >= 7);
if (int(m->d.data[handle + 1]) != argc)
const QMetaMethod::Data &data = method.data;
if (data.argc() != uint(argc))
return false;

if (rawStringData(m, m->d.data[handle]) != name)
if (stringData(m, data.name()) != name)
return false;

int paramsIndex = m->d.data[handle + 2] + 1;
int paramsIndex = data.parameters() + 1;
for (int i = 0; i < argc; ++i) {
uint typeInfo = m->d.data[paramsIndex + i];
if (types[i].type()) {
Expand All @@ -601,7 +601,7 @@ static bool methodMatch(const QMetaObject *m, int handle,
* \a MethodType might be MethodSignal or MethodSlot, or \nullptr to match everything.
*/
template<int MethodType>
static inline int indexOfMethodRelative(const QMetaObject **baseObject,
inline int QMetaObjectPrivate::indexOfMethodRelative(const QMetaObject **baseObject,
const QByteArray &name, int argc,
const QArgumentType *types)
{
Expand All @@ -613,8 +613,8 @@ static inline int indexOfMethodRelative(const QMetaObject **baseObject,
? (priv(m->d.data)->signalCount) : 0;

for (; i >= end; --i) {
int handle = priv(m->d.data)->methodData + QMetaObjectPrivate::IntsPerMethod*i;
if (methodMatch(m, handle, name, argc, types)) {
auto data = QMetaMethod::fromRelativeMethodIndex(m, i);
if (methodMatch(m, data, name, argc, types)) {
*baseObject = m;
return i;
}
Expand Down Expand Up @@ -657,7 +657,7 @@ int QMetaObject::indexOfMethod(const char *method) const
Q_ASSERT(priv(m->d.data)->revision >= 7);
QArgumentTypeArray types;
QByteArray name = QMetaObjectPrivate::decodeMethodSignature(method, types);
i = indexOfMethodRelative<0>(&m, name, types.size(), types.constData());
i = QMetaObjectPrivate::indexOfMethodRelative<0>(&m, name, types.size(), types.constData());
if (i >= 0)
i += m->methodOffset();
return i;
Expand Down Expand Up @@ -814,8 +814,8 @@ int QMetaObjectPrivate::indexOfConstructor(const QMetaObject *m, const QByteArra
int argc, const QArgumentType *types)
{
for (int i = priv(m->d.data)->constructorCount-1; i >= 0; --i) {
int handle = priv(m->d.data)->constructorData + QMetaObjectPrivate::IntsPerMethod*i;
if (methodMatch(m, handle, name, argc, types))
const QMetaMethod method = QMetaMethod::fromRelativeConstructorIndex(m, i);
if (methodMatch(m, method, name, argc, types))
return i;
}
return -1;
Expand Down Expand Up @@ -2941,7 +2941,7 @@ int QMetaProperty::userType() const
{
if (!mobj)
return QMetaType::UnknownType;
return QMetaType(mobj->d.metaTypes[idx]).id();
return QMetaType(mobj->d.metaTypes[data.index(mobj)]).id();
}

/*!
Expand All @@ -2955,7 +2955,12 @@ QMetaType QMetaProperty::metaType() const
{
if (!mobj)
return {};
return QMetaType(mobj->d.metaTypes[idx]);
return QMetaType(mobj->d.metaTypes[data.index(mobj)]);
}

int QMetaProperty::Data::index(const QMetaObject *mobj) const
{
return (d - mobj->d.data - priv(mobj->d.data)->propertyData)/Size;
}

/*!
Expand All @@ -2967,7 +2972,7 @@ int QMetaProperty::propertyIndex() const
{
if (!mobj)
return -1;
return idx + mobj->propertyOffset();
return data.index(mobj) + mobj->propertyOffset();
}

/*!
Expand All @@ -2979,7 +2984,7 @@ int QMetaProperty::relativePropertyIndex() const
{
if (!mobj)
return -1;
return idx;
return data.index(mobj);
}

/*!
Expand Down Expand Up @@ -3036,14 +3041,13 @@ int QMetaProperty::registerPropertyType() const
{
int registerResult = -1;
void *argv[] = { &registerResult };
mobj->static_metacall(QMetaObject::RegisterPropertyMetaType, idx, argv);
mobj->static_metacall(QMetaObject::RegisterPropertyMetaType, data.index(mobj), argv);
return registerResult == -1 ? QMetaType::UnknownType : registerResult;
}

QMetaProperty::QMetaProperty(const QMetaObject *mobj, int index)
: mobj(mobj),
data({ mobj->d.data + priv(mobj->d.data)->propertyData + index * Data::Size }),
idx(index)
data({ mobj->d.data + priv(mobj->d.data)->propertyData + index * Data::Size })
{
Q_ASSERT(index >= 0 && index < priv(mobj->d.data)->propertyCount);

Expand Down Expand Up @@ -3110,18 +3114,18 @@ QVariant QMetaProperty::read(const QObject *object) const
int status = -1;
QVariant value;
void *argv[] = { nullptr, &value, &status };
QMetaType t(mobj->d.metaTypes[idx]);
QMetaType t(mobj->d.metaTypes[data.index(mobj)]);
if (t == QMetaType::fromType<QVariant>()) {
argv[0] = &value;
} else {
value = QVariant(t, nullptr);
argv[0] = value.data();
}
if (priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall) {
mobj->d.static_metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty, idx, argv);
mobj->d.static_metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty, data.index(mobj), argv);
} else {
QMetaObject::metacall(const_cast<QObject*>(object), QMetaObject::ReadProperty,
idx + mobj->propertyOffset(), argv);
data.index(mobj) + mobj->propertyOffset(), argv);
}

if (status != -1)
Expand Down Expand Up @@ -3149,7 +3153,7 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
return false;

QVariant v = value;
QMetaType t(mobj->d.metaTypes[idx]);
QMetaType t(mobj->d.metaTypes[data.index(mobj)]);
if (t != QMetaType::fromType<QVariant>() && t != v.metaType()) {
if (isEnumType() && !t.metaObject() && v.userType() == QMetaType::QString) {
// Assigning a string to a property of type Q_ENUMS (instead of Q_ENUM)
Expand Down Expand Up @@ -3183,9 +3187,9 @@ bool QMetaProperty::write(QObject *object, const QVariant &value) const
else
argv[0] = v.data();
if (priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall)
mobj->d.static_metacall(object, QMetaObject::WriteProperty, idx, argv);
mobj->d.static_metacall(object, QMetaObject::WriteProperty, data.index(mobj), argv);
else
QMetaObject::metacall(object, QMetaObject::WriteProperty, idx + mobj->propertyOffset(), argv);
QMetaObject::metacall(object, QMetaObject::WriteProperty, data.index(mobj) + mobj->propertyOffset(), argv);

return status;
}
Expand All @@ -3204,9 +3208,9 @@ bool QMetaProperty::reset(QObject *object) const
return false;
void *argv[] = { nullptr };
if (priv(mobj->d.data)->flags & PropertyAccessInStaticMetaCall && mobj->d.static_metacall)
mobj->d.static_metacall(object, QMetaObject::ResetProperty, idx, argv);
mobj->d.static_metacall(object, QMetaObject::ResetProperty, data.index(mobj), argv);
else
QMetaObject::metacall(object, QMetaObject::ResetProperty, idx + mobj->propertyOffset(), argv);
QMetaObject::metacall(object, QMetaObject::ResetProperty, data.index(mobj) + mobj->propertyOffset(), argv);
return true;
}
/*!
Expand Down Expand Up @@ -3325,7 +3329,7 @@ int QMetaProperty::notifySignalIndex() const
methodIndex &= ~IsUnresolvedSignal;
const QByteArray signalName = stringData(mobj, methodIndex);
const QMetaObject *m = mobj;
const int idx = indexOfMethodRelative<MethodSignal>(&m, signalName, 0, nullptr);
const int idx = QMetaObjectPrivate::indexOfMethodRelative<MethodSignal>(&m, signalName, 0, nullptr);
if (idx >= 0) {
return idx + m->methodOffset();
} else {
Expand Down Expand Up @@ -3666,11 +3670,9 @@ const char* QMetaClassInfo::value() const
int QMetaObjectPrivate::originalClone(const QMetaObject *mobj, int local_method_index)
{
Q_ASSERT(local_method_index < get(mobj)->methodCount);
int handle = get(mobj)->methodData + QMetaObjectPrivate::IntsPerMethod * local_method_index;
while (mobj->d.data[handle + 4] & MethodCloned) {
while (QMetaMethod::fromRelativeMethodIndex(mobj, local_method_index).data.flags() & MethodCloned) {
Q_ASSERT(local_method_index > 0);
handle -= QMetaObjectPrivate::IntsPerMethod;
local_method_index--;
--local_method_index;
}
return local_method_index;
}
Expand Down
14 changes: 6 additions & 8 deletions src/corelib/kernel/qmetaobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,6 @@ class Q_CORE_EXPORT QMetaMethod
}

private:
#if QT_DEPRECATED_SINCE(5,0)
// signature() has been renamed to methodSignature() in Qt 5.
// Warning, that function returns a QByteArray; check the life time if
// you convert to char*.
char *signature(struct renamedInQt5_warning_checkTheLifeTime * = nullptr) = delete;
#endif
static QMetaMethod fromSignalImpl(const QMetaObject *, void **);
static QMetaMethod fromRelativeMethodIndex(const QMetaObject *mobj, int index);
static QMetaMethod fromRelativeConstructorIndex(const QMetaObject *mobj, int index);
Expand All @@ -201,6 +195,9 @@ class Q_CORE_EXPORT QMetaMethod

const uint *d;
};
constexpr QMetaMethod(const QMetaObject *metaObject, const Data &data_)
: mobj(metaObject), data(data_)
{}

const QMetaObject *mobj;
Data data;
Expand Down Expand Up @@ -276,7 +273,7 @@ Q_DECLARE_TYPEINFO(QMetaEnum, Q_MOVABLE_TYPE);
class Q_CORE_EXPORT QMetaProperty
{
public:
constexpr QMetaProperty() : mobj(nullptr), data({ nullptr }), idx(-1) {}
constexpr QMetaProperty() : mobj(nullptr), data({ nullptr }) {}

const char *name() const;
const char *typeName() const;
Expand Down Expand Up @@ -335,14 +332,15 @@ class Q_CORE_EXPORT QMetaProperty
uint notifyIndex() const { return d[3]; }
uint revision() const { return d[4]; }

int index(const QMetaObject *mobj) const;

const uint *d;
};

QMetaProperty(const QMetaObject *mobj, int index);

const QMetaObject *mobj;
Data data;
int idx;
QMetaEnum menum;
friend struct QMetaObject;
friend struct QMetaObjectPrivate;
Expand Down
10 changes: 10 additions & 0 deletions src/corelib/kernel/qmetaobject_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ struct QMetaObjectPrivate
const QObject *receiver, int method_index, void **slot,
QBasicMutex *senderMutex, DisconnectType = DisconnectAll);
#endif

template<int MethodType>
static inline int indexOfMethodRelative(const QMetaObject **baseObject,
const QByteArray &name, int argc,
const QArgumentType *types);

static bool methodMatch(const QMetaObject *m, const QMetaMethod &method,
const QByteArray &name, int argc,
const QArgumentType *types);

};

// For meta-object generators
Expand Down

0 comments on commit 9b383f3

Please sign in to comment.