Clean up QMetaEnum code

Change-Id: I9c1fcfd72890fb3d69d2d9caed7f3cff931c3ff6
Reviewed-by: Simon Hausmann <hausmann@gmail.com>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Lars Knoll 2019-04-30 10:16:12 +02:00 committed by Fabian Kosmale
parent ee709e3236
commit 679492ecc6
4 changed files with 55 additions and 61 deletions

View File

@ -983,9 +983,8 @@ int QMetaObject::indexOfEnumerator(const char *name) const
const QMetaObject *m = this; const QMetaObject *m = this;
while (m) { while (m) {
const QMetaObjectPrivate *d = priv(m->d.data); const QMetaObjectPrivate *d = priv(m->d.data);
const int intsPerEnum = d->revision >= 8 ? 5 : 4;
for (int i = d->enumeratorCount - 1; i >= 0; --i) { for (int i = d->enumeratorCount - 1; i >= 0; --i) {
const char *prop = rawStringData(m, m->d.data[d->enumeratorData + intsPerEnum * i]); const char *prop = rawStringData(m, m->d.data[d->enumeratorData + QMetaObjectPrivate::IntsPerEnum * i]);
if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) { if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
i += m->enumeratorOffset(); i += m->enumeratorOffset();
return i; return i;
@ -997,9 +996,8 @@ int QMetaObject::indexOfEnumerator(const char *name) const
m = this; m = this;
while (m) { while (m) {
const QMetaObjectPrivate *d = priv(m->d.data); const QMetaObjectPrivate *d = priv(m->d.data);
const int intsPerEnum = d->revision >= 8 ? 5 : 4;
for (int i = d->enumeratorCount - 1; i >= 0; --i) { for (int i = d->enumeratorCount - 1; i >= 0; --i) {
const char *prop = rawStringData(m, m->d.data[d->enumeratorData + intsPerEnum * i + 1]); const char *prop = rawStringData(m, m->d.data[d->enumeratorData + QMetaObjectPrivate::IntsPerEnum * i + 1]);
if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) { if (name[0] == prop[0] && strcmp(name + 1, prop + 1) == 0) {
i += m->enumeratorOffset(); i += m->enumeratorOffset();
return i; return i;
@ -1107,13 +1105,9 @@ QMetaEnum QMetaObject::enumerator(int index) const
if (i < 0 && d.superdata) if (i < 0 && d.superdata)
return d.superdata->enumerator(index); return d.superdata->enumerator(index);
const int intsPerEnum = priv(d.data)->revision >= 8 ? 5 : 4; if (i >= 0 && i < priv(d.data)->enumeratorCount)
QMetaEnum result; return QMetaEnum(this, i);
if (i >= 0 && i < priv(d.data)->enumeratorCount) { return QMetaEnum();
result.mobj = this;
result.handle = priv(d.data)->enumeratorData + intsPerEnum * i;
}
return result;
} }
/*! /*!
@ -2622,7 +2616,7 @@ const char *QMetaEnum::name() const
{ {
if (!mobj) if (!mobj)
return nullptr; return nullptr;
return rawStringData(mobj, mobj->d.data[handle]); return rawStringData(mobj, data.name());
} }
/*! /*!
@ -2641,10 +2635,7 @@ const char *QMetaEnum::enumName() const
{ {
if (!mobj) if (!mobj)
return nullptr; return nullptr;
const bool rev8p = priv(mobj->d.data)->revision >= 8; return rawStringData(mobj, data.alias());
if (rev8p)
return rawStringData(mobj, mobj->d.data[handle + 1]);
return name();
} }
/*! /*!
@ -2656,8 +2647,7 @@ int QMetaEnum::keyCount() const
{ {
if (!mobj) if (!mobj)
return 0; return 0;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2; return data.keyCount();
return mobj->d.data[handle + offset];
} }
/*! /*!
@ -2669,11 +2659,8 @@ const char *QMetaEnum::key(int index) const
{ {
if (!mobj) if (!mobj)
return nullptr; return nullptr;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2; if (index >= 0 && index < int(data.keyCount()))
int count = mobj->d.data[handle + offset]; return rawStringData(mobj, mobj->d.data[data.data() + 2*index]);
int data = mobj->d.data[handle + offset + 1];
if (index >= 0 && index < count)
return rawStringData(mobj, mobj->d.data[data + 2*index]);
return nullptr; return nullptr;
} }
@ -2687,11 +2674,8 @@ int QMetaEnum::value(int index) const
{ {
if (!mobj) if (!mobj)
return 0; return 0;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2; if (index >= 0 && index < int(data.keyCount()))
int count = mobj->d.data[handle + offset]; return mobj->d.data[data.data() + 2*index + 1];
int data = mobj->d.data[handle + offset + 1];
if (index >= 0 && index < count)
return mobj->d.data[data + 2*index + 1];
return -1; return -1;
} }
@ -2709,8 +2693,7 @@ bool QMetaEnum::isFlag() const
{ {
if (!mobj) if (!mobj)
return false; return false;
const int offset = priv(mobj->d.data)->revision >= 8 ? 2 : 1; return data.flags() & EnumIsFlag;
return mobj->d.data[handle + offset] & EnumIsFlag;
} }
/*! /*!
@ -2723,8 +2706,7 @@ bool QMetaEnum::isScoped() const
{ {
if (!mobj) if (!mobj)
return false; return false;
const int offset = priv(mobj->d.data)->revision >= 8 ? 2 : 1; return data.flags() & EnumIsScoped;
return mobj->d.data[handle + offset] & EnumIsScoped;
} }
/*! /*!
@ -2766,16 +2748,13 @@ int QMetaEnum::keyToValue(const char *key, bool *ok) const
scope = s - key - 1; scope = s - key - 1;
key += scope + 2; key += scope + 2;
} }
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2; for (int i = 0; i < int(data.keyCount()); ++i) {
int count = mobj->d.data[handle + offset];
int data = mobj->d.data[handle + offset + 1];
for (int i = 0; i < count; ++i) {
const QByteArray className = stringData(mobj, priv(mobj->d.data)->className); const QByteArray className = stringData(mobj, priv(mobj->d.data)->className);
if ((!scope || (className.size() == int(scope) && strncmp(qualified_key, className.constData(), scope) == 0)) if ((!scope || (className.size() == int(scope) && strncmp(qualified_key, className.constData(), scope) == 0))
&& strcmp(key, rawStringData(mobj, mobj->d.data[data + 2*i])) == 0) { && strcmp(key, rawStringData(mobj, mobj->d.data[data.data() + 2*i])) == 0) {
if (ok != nullptr) if (ok != nullptr)
*ok = true; *ok = true;
return mobj->d.data[data + 2*i + 1]; return mobj->d.data[data.data() + 2*i + 1];
} }
} }
return -1; return -1;
@ -2793,12 +2772,9 @@ const char* QMetaEnum::valueToKey(int value) const
{ {
if (!mobj) if (!mobj)
return nullptr; return nullptr;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2; for (int i = 0; i < int(data.keyCount()); ++i)
int count = mobj->d.data[handle + offset]; if (value == (int)mobj->d.data[data.data() + 2*i + 1])
int data = mobj->d.data[handle + offset + 1]; return rawStringData(mobj, mobj->d.data[data.data() + 2*i]);
for (int i = 0; i < count; ++i)
if (value == (int)mobj->d.data[data + 2*i + 1])
return rawStringData(mobj, mobj->d.data[data + 2*i]);
return nullptr; return nullptr;
} }
@ -2826,9 +2802,6 @@ int QMetaEnum::keysToValue(const char *keys, bool *ok) const
return 0; return 0;
// ### TODO write proper code: do not allocate memory, so we can go nothrow // ### TODO write proper code: do not allocate memory, so we can go nothrow
int value = 0; int value = 0;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2;
int count = mobj->d.data[handle + offset];
int data = mobj->d.data[handle + offset + 1];
for (QStringView untrimmed : splitKeys) { for (QStringView untrimmed : splitKeys) {
const QStringView trimmed = untrimmed.trimmed(); const QStringView trimmed = untrimmed.trimmed();
QByteArray qualified_key = trimmed.toLatin1(); QByteArray qualified_key = trimmed.toLatin1();
@ -2842,11 +2815,11 @@ int QMetaEnum::keysToValue(const char *keys, bool *ok) const
key += scope + 2; key += scope + 2;
} }
int i; int i;
for (i = count-1; i >= 0; --i) { for (i = data.keyCount() - 1; i >= 0; --i) {
const QByteArray className = stringData(mobj, priv(mobj->d.data)->className); const QByteArray className = stringData(mobj, priv(mobj->d.data)->className);
if ((!scope || (className.size() == int(scope) && strncmp(qualified_key.constData(), className.constData(), scope) == 0)) if ((!scope || (className.size() == int(scope) && strncmp(qualified_key.constData(), className.constData(), scope) == 0))
&& strcmp(key, rawStringData(mobj, mobj->d.data[data + 2*i])) == 0) { && strcmp(key, rawStringData(mobj, mobj->d.data[data.data() + 2*i])) == 0) {
value |= mobj->d.data[data + 2*i + 1]; value |= mobj->d.data[data.data() + 2*i + 1];
break; break;
} }
} }
@ -2870,23 +2843,29 @@ QByteArray QMetaEnum::valueToKeys(int value) const
QByteArray keys; QByteArray keys;
if (!mobj) if (!mobj)
return keys; return keys;
const int offset = priv(mobj->d.data)->revision >= 8 ? 3 : 2;
int count = mobj->d.data[handle + offset];
int data = mobj->d.data[handle + offset + 1];
int v = value; int v = value;
// reverse iterate to ensure values like Qt::Dialog=0x2|Qt::Window are processed first. // reverse iterate to ensure values like Qt::Dialog=0x2|Qt::Window are processed first.
for (int i = count - 1; i >= 0; --i) { for (int i = data.keyCount() - 1; i >= 0; --i) {
int k = mobj->d.data[data + 2*i + 1]; int k = mobj->d.data[data.data() + 2*i + 1];
if ((k != 0 && (v & k) == k ) || (k == value)) { if ((k != 0 && (v & k) == k ) || (k == value)) {
v = v & ~k; v = v & ~k;
if (!keys.isEmpty()) if (!keys.isEmpty())
keys.prepend('|'); keys.prepend('|');
keys.prepend(stringData(mobj, mobj->d.data[data + 2*i])); keys.prepend(stringData(mobj, mobj->d.data[data.data() + 2*i]));
} }
} }
return keys; return keys;
} }
/*!
\internal
*/
QMetaEnum::QMetaEnum(const QMetaObject *mobj, int index)
: mobj(mobj), data({ mobj->d.data + priv(mobj->d.data)->enumeratorData + index * Data::Size })
{
Q_ASSERT(index >= 0 && index < priv(mobj->d.data)->enumeratorCount);
}
/*! /*!
\fn QMetaEnum QMetaEnum::fromType() \fn QMetaEnum QMetaEnum::fromType()
\since 5.5 \since 5.5

View File

@ -221,7 +221,7 @@ inline bool operator!=(const QMetaMethod &m1, const QMetaMethod &m2)
class Q_CORE_EXPORT QMetaEnum class Q_CORE_EXPORT QMetaEnum
{ {
public: public:
Q_DECL_CONSTEXPR inline QMetaEnum() : mobj(nullptr), handle(0) {} Q_DECL_CONSTEXPR inline QMetaEnum() : mobj(nullptr), data({ nullptr }) {}
const char *name() const; const char *name() const;
const char *enumName() const; const char *enumName() const;
@ -253,9 +253,23 @@ public:
} }
private: private:
struct Data {
enum { Size = 5 };
quint32 name() const { return d[0]; }
quint32 alias() const { return d[1]; }
quint32 flags() const { return d[2]; }
qint32 keyCount() const { return static_cast<qint32>(d[3]); }
quint32 data() const { return d[4]; }
const uint *d;
};
QMetaEnum(const QMetaObject *mobj, int index);
const QMetaObject *mobj; const QMetaObject *mobj;
uint handle; Data data;
friend struct QMetaObject; friend struct QMetaObject;
friend struct QMetaObjectPrivate;
}; };
Q_DECLARE_TYPEINFO(QMetaEnum, Q_MOVABLE_TYPE); Q_DECLARE_TYPEINFO(QMetaEnum, Q_MOVABLE_TYPE);

View File

@ -180,6 +180,7 @@ struct QMetaObjectPrivate
// revision 9 is Qt 6.0: It adds the metatype of properties and methods // revision 9 is Qt 6.0: It adds the metatype of properties and methods
enum { OutputRevision = 9 }; // Used by moc, qmetaobjectbuilder and qdbus enum { OutputRevision = 9 }; // Used by moc, qmetaobjectbuilder and qdbus
enum { IntsPerMethod = QMetaMethod::Data::Size}; enum { IntsPerMethod = QMetaMethod::Data::Size};
enum { IntsPerEnum = QMetaEnum::Data::Size };
int revision; int revision;
int className; int className;

View File

@ -1244,7 +1244,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
pmeta->enumeratorCount = int(d->enumerators.size()); pmeta->enumeratorCount = int(d->enumerators.size());
pmeta->enumeratorData = dataIndex; pmeta->enumeratorData = dataIndex;
dataIndex += 5 * int(d->enumerators.size()); dataIndex += QMetaObjectPrivate::IntsPerEnum * int(d->enumerators.size());
pmeta->constructorCount = int(d->constructors.size()); pmeta->constructorCount = int(d->constructors.size());
pmeta->constructorData = dataIndex; pmeta->constructorData = dataIndex;
@ -1261,7 +1261,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
dataIndex += int(d->properties.size()); dataIndex += int(d->properties.size());
if (hasRevisionedProperties) if (hasRevisionedProperties)
dataIndex += int(d->properties.size()); dataIndex += int(d->properties.size());
dataIndex += 5 * int(d->enumerators.size()); dataIndex += QMetaObjectPrivate::IntsPerEnum * int(d->enumerators.size());
dataIndex += QMetaObjectPrivate::IntsPerMethod * int(d->constructors.size()); dataIndex += QMetaObjectPrivate::IntsPerMethod * int(d->constructors.size());
} }
@ -1432,7 +1432,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
data[enumOffset++] = enumerator.values[key]; data[enumOffset++] = enumerator.values[key];
} }
} }
dataIndex += 5; dataIndex += QMetaObjectPrivate::IntsPerEnum;
enumIndex += 2 * count; enumIndex += 2 * count;
} }