QMetaObjectBuilder: replace index-based for loops with C++11 range-for

...for the recently-introduced std::vector objects
(QVector would detach).

Also, as a drive-by, reorder two comparisons so
the cheaper one is first, twice.

Saves 1700B of text size on GCC 4.9 optimized C++11
AMD64 Linux builds.

Change-Id: I05a29ef1f2e67c98d26236c2cc40a13856a91af6
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2015-06-14 11:58:32 +02:00
parent d40839db2f
commit 3845d1b830

View File

@ -223,8 +223,8 @@ public:
bool QMetaObjectBuilderPrivate::hasRevisionedProperties() const bool QMetaObjectBuilderPrivate::hasRevisionedProperties() const
{ {
for (size_t i = 0; i < properties.size(); ++i) { for (const auto &property : properties) {
if (properties.at(i).revision) if (property.revision)
return true; return true;
} }
return false; return false;
@ -232,8 +232,8 @@ bool QMetaObjectBuilderPrivate::hasRevisionedProperties() const
bool QMetaObjectBuilderPrivate::hasRevisionedMethods() const bool QMetaObjectBuilderPrivate::hasRevisionedMethods() const
{ {
for (size_t i = 0; i < methods.size(); ++i) { for (const auto &method : methods) {
if (methods.at(i).revision) if (method.revision)
return true; return true;
} }
return false; return false;
@ -866,13 +866,13 @@ void QMetaObjectBuilder::removeMethod(int index)
{ {
if (uint(index) < d->methods.size()) { if (uint(index) < d->methods.size()) {
d->methods.erase(d->methods.begin() + index); d->methods.erase(d->methods.begin() + index);
for (size_t prop = 0; prop < d->properties.size(); ++prop) { for (auto &property : d->properties) {
// Adjust the indices of property notify signal references. // Adjust the indices of property notify signal references.
if (d->properties[prop].notifySignal == index) { if (property.notifySignal == index) {
d->properties[prop].notifySignal = -1; property.notifySignal = -1;
d->properties[prop].setFlag(Notify, false); property.setFlag(Notify, false);
} else if (d->properties[prop].notifySignal > index) } else if (property.notifySignal > index)
(d->properties[prop].notifySignal)--; property.notifySignal--;
} }
} }
} }
@ -957,9 +957,9 @@ void QMetaObjectBuilder::removeRelatedMetaObject(int index)
int QMetaObjectBuilder::indexOfMethod(const QByteArray& signature) int QMetaObjectBuilder::indexOfMethod(const QByteArray& signature)
{ {
QByteArray sig = QMetaObject::normalizedSignature(signature); QByteArray sig = QMetaObject::normalizedSignature(signature);
for (size_t index = 0; index < d->methods.size(); ++index) { for (const auto &method : d->methods) {
if (sig == d->methods[index].signature) if (sig == method.signature)
return int(index); return int(&method - &d->methods.front());
} }
return -1; return -1;
} }
@ -973,10 +973,9 @@ int QMetaObjectBuilder::indexOfMethod(const QByteArray& signature)
int QMetaObjectBuilder::indexOfSignal(const QByteArray& signature) int QMetaObjectBuilder::indexOfSignal(const QByteArray& signature)
{ {
QByteArray sig = QMetaObject::normalizedSignature(signature); QByteArray sig = QMetaObject::normalizedSignature(signature);
for (size_t index = 0; index < d->methods.size(); ++index) { for (const auto &method : d->methods) {
if (sig == d->methods[index].signature && if (method.methodType() == QMetaMethod::Signal && sig == method.signature)
d->methods[index].methodType() == QMetaMethod::Signal) return int(&method - &d->methods.front());
return int(index);
} }
return -1; return -1;
} }
@ -990,10 +989,9 @@ int QMetaObjectBuilder::indexOfSignal(const QByteArray& signature)
int QMetaObjectBuilder::indexOfSlot(const QByteArray& signature) int QMetaObjectBuilder::indexOfSlot(const QByteArray& signature)
{ {
QByteArray sig = QMetaObject::normalizedSignature(signature); QByteArray sig = QMetaObject::normalizedSignature(signature);
for (size_t index = 0; index < d->methods.size(); ++index) { for (const auto &method : d->methods) {
if (sig == d->methods[index].signature && if (method.methodType() == QMetaMethod::Slot && sig == method.signature)
d->methods[index].methodType() == QMetaMethod::Slot) return int(&method - &d->methods.front());
return int(index);
} }
return -1; return -1;
} }
@ -1007,9 +1005,9 @@ int QMetaObjectBuilder::indexOfSlot(const QByteArray& signature)
int QMetaObjectBuilder::indexOfConstructor(const QByteArray& signature) int QMetaObjectBuilder::indexOfConstructor(const QByteArray& signature)
{ {
QByteArray sig = QMetaObject::normalizedSignature(signature); QByteArray sig = QMetaObject::normalizedSignature(signature);
for (size_t index = 0; index < d->constructors.size(); ++index) { for (const auto &constructor : d->constructors) {
if (sig == d->constructors[index].signature) if (sig == constructor.signature)
return int(index); return int(&constructor - &d->constructors.front());
} }
return -1; return -1;
} }
@ -1022,9 +1020,9 @@ int QMetaObjectBuilder::indexOfConstructor(const QByteArray& signature)
*/ */
int QMetaObjectBuilder::indexOfProperty(const QByteArray& name) int QMetaObjectBuilder::indexOfProperty(const QByteArray& name)
{ {
for (size_t index = 0; index < d->properties.size(); ++index) { for (const auto &property : d->properties) {
if (name == d->properties[index].name) if (name == property.name)
return int(index); return int(&property - &d->properties.front());
} }
return -1; return -1;
} }
@ -1037,9 +1035,9 @@ int QMetaObjectBuilder::indexOfProperty(const QByteArray& name)
*/ */
int QMetaObjectBuilder::indexOfEnumerator(const QByteArray& name) int QMetaObjectBuilder::indexOfEnumerator(const QByteArray& name)
{ {
for (size_t index = 0; index < d->enumerators.size(); ++index) { for (const auto &enumerator : d->enumerators) {
if (name == d->enumerators[index].name) if (name == enumerator.name)
return int(index); return int(&enumerator - &d->enumerators.front());
} }
return -1; return -1;
} }
@ -1156,8 +1154,8 @@ void QMetaStringTable::writeBlob(char *out) const
static int aggregateParameterCount(const std::vector<QMetaMethodBuilderPrivate> &methods) static int aggregateParameterCount(const std::vector<QMetaMethodBuilderPrivate> &methods)
{ {
int sum = 0; int sum = 0;
for (size_t i = 0; i < methods.size(); ++i) for (const auto &method : methods)
sum += methods.at(i).parameterCount() + 1; // +1 for return type sum += method.parameterCount() + 1; // +1 for return type
return sum; return sum;
} }
@ -1198,8 +1196,8 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
= reinterpret_cast<QMetaObjectPrivate *>(buf + size); = reinterpret_cast<QMetaObjectPrivate *>(buf + size);
int pmetaSize = size; int pmetaSize = size;
dataIndex = MetaObjectPrivateFieldCount; dataIndex = MetaObjectPrivateFieldCount;
for (size_t index = 0; index < d->properties.size(); ++index) { for (const auto &property : d->properties) {
if (d->properties[index].notifySignal != -1) { if (property.notifySignal != -1) {
hasNotifySignals = true; hasNotifySignals = true;
break; break;
} }
@ -1261,10 +1259,8 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
// Allocate space for the enumerator key names and values. // Allocate space for the enumerator key names and values.
enumIndex = dataIndex; enumIndex = dataIndex;
for (size_t index = 0; index < d->enumerators.size(); ++index) { for (const auto &enumerator : d->enumerators)
QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]); dataIndex += 2 * enumerator.keys.size();
dataIndex += 2 * enumerator->keys.size();
}
// Zero terminator at the end of the data offset table. // Zero terminator at the end of the data offset table.
++dataIndex; ++dataIndex;
@ -1303,29 +1299,27 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
// Output the methods in the class. // Output the methods in the class.
Q_ASSERT(!buf || dataIndex == pmeta->methodData); Q_ASSERT(!buf || dataIndex == pmeta->methodData);
for (size_t index = 0; index < d->methods.size(); ++index) { for (const auto &method : d->methods) {
QMetaMethodBuilderPrivate *method = &(d->methods[index]); int name = strings.enter(method.name());
int name = strings.enter(method->name()); int argc = method.parameterCount();
int argc = method->parameterCount(); int tag = strings.enter(method.tag);
int tag = strings.enter(method->tag); int attrs = method.attributes;
int attrs = method->attributes;
if (buf) { if (buf) {
data[dataIndex] = name; data[dataIndex] = name;
data[dataIndex + 1] = argc; data[dataIndex + 1] = argc;
data[dataIndex + 2] = paramsIndex; data[dataIndex + 2] = paramsIndex;
data[dataIndex + 3] = tag; data[dataIndex + 3] = tag;
data[dataIndex + 4] = attrs; data[dataIndex + 4] = attrs;
if (method->methodType() == QMetaMethod::Signal) if (method.methodType() == QMetaMethod::Signal)
pmeta->signalCount++; pmeta->signalCount++;
} }
dataIndex += 5; dataIndex += 5;
paramsIndex += 1 + argc * 2; paramsIndex += 1 + argc * 2;
} }
if (hasRevisionedMethods) { if (hasRevisionedMethods) {
for (size_t index = 0; index < d->methods.size(); ++index) { for (const auto &method : d->methods) {
QMetaMethodBuilderPrivate *method = &(d->methods[index]);
if (buf) if (buf)
data[dataIndex] = method->revision; data[dataIndex] = method.revision;
++dataIndex; ++dataIndex;
} }
} }
@ -1335,12 +1329,11 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
+ (hasRevisionedMethods ? int(d->methods.size()) : 0)); + (hasRevisionedMethods ? int(d->methods.size()) : 0));
for (int x = 0; x < 2; ++x) { for (int x = 0; x < 2; ++x) {
const std::vector<QMetaMethodBuilderPrivate> &methods = (x == 0) ? d->methods : d->constructors; const std::vector<QMetaMethodBuilderPrivate> &methods = (x == 0) ? d->methods : d->constructors;
for (size_t index = 0; index < methods.size(); ++index) { for (const auto &method : methods) {
const QMetaMethodBuilderPrivate *method = &(methods[index]); const QList<QByteArray> paramTypeNames = method.parameterTypes();
QList<QByteArray> paramTypeNames = method->parameterTypes();
int paramCount = paramTypeNames.size(); int paramCount = paramTypeNames.size();
for (int i = -1; i < paramCount; ++i) { for (int i = -1; i < paramCount; ++i) {
const QByteArray &typeName = (i < 0) ? method->returnType : paramTypeNames.at(i); const QByteArray &typeName = (i < 0) ? method.returnType : paramTypeNames.at(i);
int typeInfo; int typeInfo;
if (QtPrivate::isBuiltinType(typeName)) if (QtPrivate::isBuiltinType(typeName))
typeInfo = QMetaType::type(typeName); typeInfo = QMetaType::type(typeName);
@ -1351,7 +1344,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
++dataIndex; ++dataIndex;
} }
QList<QByteArray> paramNames = method->parameterNames; QList<QByteArray> paramNames = method.parameterNames;
while (paramNames.size() < paramCount) while (paramNames.size() < paramCount)
paramNames.append(QByteArray()); paramNames.append(QByteArray());
for (int i = 0; i < paramCount; ++i) { for (int i = 0; i < paramCount; ++i) {
@ -1365,19 +1358,18 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
// Output the properties in the class. // Output the properties in the class.
Q_ASSERT(!buf || dataIndex == pmeta->propertyData); Q_ASSERT(!buf || dataIndex == pmeta->propertyData);
for (size_t index = 0; index < d->properties.size(); ++index) { for (const auto &prop : d->properties) {
QMetaPropertyBuilderPrivate *prop = &(d->properties[index]); int name = strings.enter(prop.name);
int name = strings.enter(prop->name);
int typeInfo; int typeInfo;
if (QtPrivate::isBuiltinType(prop->type)) if (QtPrivate::isBuiltinType(prop.type))
typeInfo = QMetaType::type(prop->type); typeInfo = QMetaType::type(prop.type);
else else
typeInfo = IsUnresolvedType | strings.enter(prop->type); typeInfo = IsUnresolvedType | strings.enter(prop.type);
int flags = prop->flags; int flags = prop.flags;
if (!QtPrivate::isBuiltinType(prop->type)) if (!QtPrivate::isBuiltinType(prop.type))
flags |= EnumOrFlag; flags |= EnumOrFlag;
if (buf) { if (buf) {
@ -1388,11 +1380,10 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
dataIndex += 3; dataIndex += 3;
} }
if (hasNotifySignals) { if (hasNotifySignals) {
for (size_t index = 0; index < d->properties.size(); ++index) { for (const auto &prop : d->properties) {
QMetaPropertyBuilderPrivate *prop = &(d->properties[index]);
if (buf) { if (buf) {
if (prop->notifySignal != -1) if (prop.notifySignal != -1)
data[dataIndex] = prop->notifySignal; data[dataIndex] = prop.notifySignal;
else else
data[dataIndex] = 0; data[dataIndex] = 0;
} }
@ -1400,21 +1391,19 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
} }
} }
if (hasRevisionedProperties) { if (hasRevisionedProperties) {
for (size_t index = 0; index < d->properties.size(); ++index) { for (const auto &prop : d->properties) {
QMetaPropertyBuilderPrivate *prop = &(d->properties[index]);
if (buf) if (buf)
data[dataIndex] = prop->revision; data[dataIndex] = prop.revision;
++dataIndex; ++dataIndex;
} }
} }
// Output the enumerators in the class. // Output the enumerators in the class.
Q_ASSERT(!buf || dataIndex == pmeta->enumeratorData); Q_ASSERT(!buf || dataIndex == pmeta->enumeratorData);
for (size_t index = 0; index < d->enumerators.size(); ++index) { for (const auto &enumerator : d->enumerators) {
QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]); int name = strings.enter(enumerator.name);
int name = strings.enter(enumerator->name); int isFlag = (int)(enumerator.isFlag);
int isFlag = (int)(enumerator->isFlag); int count = enumerator.keys.size();
int count = enumerator->keys.size();
int enumOffset = enumIndex; int enumOffset = enumIndex;
if (buf) { if (buf) {
data[dataIndex] = name; data[dataIndex] = name;
@ -1423,10 +1412,10 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
data[dataIndex + 3] = enumOffset; data[dataIndex + 3] = enumOffset;
} }
for (int key = 0; key < count; ++key) { for (int key = 0; key < count; ++key) {
int keyIndex = strings.enter(enumerator->keys[key]); int keyIndex = strings.enter(enumerator.keys[key]);
if (buf) { if (buf) {
data[enumOffset++] = keyIndex; data[enumOffset++] = keyIndex;
data[enumOffset++] = enumerator->values[key]; data[enumOffset++] = enumerator.values[key];
} }
} }
dataIndex += 4; dataIndex += 4;
@ -1435,12 +1424,11 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf,
// Output the constructors in the class. // Output the constructors in the class.
Q_ASSERT(!buf || dataIndex == pmeta->constructorData); Q_ASSERT(!buf || dataIndex == pmeta->constructorData);
for (size_t index = 0; index < d->constructors.size(); ++index) { for (const auto &ctor : d->constructors) {
QMetaMethodBuilderPrivate *method = &(d->constructors[index]); int name = strings.enter(ctor.name());
int name = strings.enter(method->name()); int argc = ctor.parameterCount();
int argc = method->parameterCount(); int tag = strings.enter(ctor.tag);
int tag = strings.enter(method->tag); int attrs = ctor.attributes;
int attrs = method->attributes;
if (buf) { if (buf) {
data[dataIndex] = name; data[dataIndex] = name;
data[dataIndex + 1] = argc; data[dataIndex + 1] = argc;
@ -1623,45 +1611,41 @@ void QMetaObjectBuilder::serialize(QDataStream& stream) const
} }
// Write the methods. // Write the methods.
for (size_t index = 0; index < d->methods.size(); ++index) { for (const auto &method : d->methods) {
const QMetaMethodBuilderPrivate *method = &(d->methods[index]); stream << method.signature;
stream << method->signature; stream << method.returnType;
stream << method->returnType; stream << method.parameterNames;
stream << method->parameterNames; stream << method.tag;
stream << method->tag; stream << method.attributes;
stream << method->attributes; if (method.revision)
if (method->revision) stream << method.revision;
stream << method->revision;
} }
// Write the properties. // Write the properties.
for (size_t index = 0; index < d->properties.size(); ++index) { for (const auto &property : d->properties) {
const QMetaPropertyBuilderPrivate *property = &(d->properties[index]); stream << property.name;
stream << property->name; stream << property.type;
stream << property->type; stream << property.flags;
stream << property->flags; stream << property.notifySignal;
stream << property->notifySignal; if (property.revision)
if (property->revision) stream << property.revision;
stream << property->revision;
} }
// Write the enumerators. // Write the enumerators.
for (size_t index = 0; index < d->enumerators.size(); ++index) { for (const auto &enumerator : d->enumerators) {
const QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]); stream << enumerator.name;
stream << enumerator->name; stream << enumerator.isFlag;
stream << enumerator->isFlag; stream << enumerator.keys;
stream << enumerator->keys; stream << enumerator.values;
stream << enumerator->values;
} }
// Write the constructors. // Write the constructors.
for (size_t index = 0; index < d->constructors.size(); ++index) { for (const auto &ctor : d->constructors) {
const QMetaMethodBuilderPrivate *method = &(d->constructors[index]); stream << ctor.signature;
stream << method->signature; stream << ctor.returnType;
stream << method->returnType; stream << ctor.parameterNames;
stream << method->parameterNames; stream << ctor.tag;
stream << method->tag; stream << ctor.attributes;
stream << method->attributes;
} }
// Write the related meta objects. // Write the related meta objects.
@ -1768,14 +1752,14 @@ void QMetaObjectBuilder::deserialize
return; return;
stream >> name; stream >> name;
addMethod(name); addMethod(name);
QMetaMethodBuilderPrivate *method = &(d->methods[index]); QMetaMethodBuilderPrivate &method = d->methods[index];
stream >> method->returnType; stream >> method.returnType;
stream >> method->parameterNames; stream >> method.parameterNames;
stream >> method->tag; stream >> method.tag;
stream >> method->attributes; stream >> method.attributes;
if (method->attributes & MethodRevisioned) if (method.attributes & MethodRevisioned)
stream >> method->revision; stream >> method.revision;
if (method->methodType() == QMetaMethod::Constructor) { if (method.methodType() == QMetaMethod::Constructor) {
// Cannot add a constructor in this set of methods. // Cannot add a constructor in this set of methods.
stream.setStatus(QDataStream::ReadCorruptData); stream.setStatus(QDataStream::ReadCorruptData);
return; return;
@ -1790,23 +1774,23 @@ void QMetaObjectBuilder::deserialize
stream >> name; stream >> name;
stream >> type; stream >> type;
addProperty(name, type); addProperty(name, type);
QMetaPropertyBuilderPrivate *property = &(d->properties[index]); QMetaPropertyBuilderPrivate &property = d->properties[index];
stream >> property->flags; stream >> property.flags;
stream >> property->notifySignal; stream >> property.notifySignal;
if (property->notifySignal < -1 || if (property.notifySignal < -1 ||
property->notifySignal >= int(d->methods.size())) { property.notifySignal >= int(d->methods.size())) {
// Notify signal method index is out of range. // Notify signal method index is out of range.
stream.setStatus(QDataStream::ReadCorruptData); stream.setStatus(QDataStream::ReadCorruptData);
return; return;
} }
if (property->notifySignal >= 0 && if (property.notifySignal >= 0 &&
d->methods[property->notifySignal].methodType() != QMetaMethod::Signal) { d->methods[property.notifySignal].methodType() != QMetaMethod::Signal) {
// Notify signal method index does not refer to a signal. // Notify signal method index does not refer to a signal.
stream.setStatus(QDataStream::ReadCorruptData); stream.setStatus(QDataStream::ReadCorruptData);
return; return;
} }
if (property->flags & Revisioned) if (property.flags & Revisioned)
stream >> property->revision; stream >> property.revision;
} }
// Read the enumerators. // Read the enumerators.
@ -1815,11 +1799,11 @@ void QMetaObjectBuilder::deserialize
return; return;
stream >> name; stream >> name;
addEnumerator(name); addEnumerator(name);
QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]); QMetaEnumBuilderPrivate &enumerator = d->enumerators[index];
stream >> enumerator->isFlag; stream >> enumerator.isFlag;
stream >> enumerator->keys; stream >> enumerator.keys;
stream >> enumerator->values; stream >> enumerator.values;
if (enumerator->keys.size() != enumerator->values.size()) { if (enumerator.keys.size() != enumerator.values.size()) {
// Mismatch between number of keys and number of values. // Mismatch between number of keys and number of values.
stream.setStatus(QDataStream::ReadCorruptData); stream.setStatus(QDataStream::ReadCorruptData);
return; return;
@ -1832,12 +1816,12 @@ void QMetaObjectBuilder::deserialize
return; return;
stream >> name; stream >> name;
addConstructor(name); addConstructor(name);
QMetaMethodBuilderPrivate *method = &(d->constructors[index]); QMetaMethodBuilderPrivate &method = d->constructors[index];
stream >> method->returnType; stream >> method.returnType;
stream >> method->parameterNames; stream >> method.parameterNames;
stream >> method->tag; stream >> method.tag;
stream >> method->attributes; stream >> method.attributes;
if (method->methodType() != QMetaMethod::Constructor) { if (method.methodType() != QMetaMethod::Constructor) {
// The type must be Constructor. // The type must be Constructor.
stream.setStatus(QDataStream::ReadCorruptData); stream.setStatus(QDataStream::ReadCorruptData);
return; return;