Skip to content

Commit

Permalink
refactor: use range-based for loop to enhance readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech authored and Inokinoki committed Nov 8, 2024
1 parent bd9770e commit e598937
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
74 changes: 37 additions & 37 deletions qefidpeditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ QEFIDPEditorView::QEFIDPEditorView(QEFIDevicePath *dp, QWidget *parent)
// m_dpSubtypeSelector->setPlaceholderText(tr("Device Path subtype"));
if (dp != nullptr) {
QList<quint8> subtypes = enum_device_path_subtype(dp->type());
for (int i = 0; i < subtypes.size(); i++) {
for (const auto &subtype: std::as_const(subtypes)) {
m_dpSubtypeSelector->addItem(convert_device_path_subtype_to_name(
dp->type(), subtypes[i]),
QVariant(subtypes[i]));
dp->type(), subtype),
QVariant(subtype));
}
m_dpSubtypeSelected = m_dpSubtypeSelector->findData(QVariant(dp->subType()));
}
Expand All @@ -55,8 +55,8 @@ QEFIDPEditorView::QEFIDPEditorView(QEFIDevicePath *dp, QWidget *parent)

// Add edit view
m_currentWidgets = constructDPEditView(dp);
for (int i = 0; i < m_currentWidgets.size(); i++) {
m_topLevelLayout->addRow(m_currentWidgets[i].first, m_currentWidgets[i].second);
for (const auto &w: std::as_const(m_currentWidgets)) {
m_topLevelLayout->addRow(w.first, w.second);
}

setLayout(m_topLevelLayout);
Expand Down Expand Up @@ -160,37 +160,37 @@ QEFIDevicePath *QEFIDPEditorView::getDevicePath()
quint8 format;
quint8 signatureType;
QString filepath;
for (int i = 0; i < m_currentWidgets.size(); i++) {
qDebug() << m_currentWidgets[i].first;
for (const auto &w: std::as_const(m_currentWidgets)) {
qDebug() << w.first;
// TODO: Int range
if (m_currentWidgets[i].first == tr("Partition Num")) {
if (w.first == tr("Partition Num")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_Number, m_currentWidgets[i].second);
QEFIDPEditType::EditType_Number, w.second);
if (data.isNull()) return dp;
partitionNumber = data.toInt();
} else if (m_currentWidgets[i].first == tr("Start")) {
} else if (w.first == tr("Start")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_Number, m_currentWidgets[i].second);
QEFIDPEditType::EditType_Number, w.second);
if (data.isNull()) return dp;
start = data.toInt();
} else if (m_currentWidgets[i].first == tr("Size")) {
} else if (w.first == tr("Size")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_Number, m_currentWidgets[i].second);
QEFIDPEditType::EditType_Number, w.second);
if (data.isNull()) return dp;
size = data.toInt();
} else if (m_currentWidgets[i].first == tr("Format")) {
} else if (w.first == tr("Format")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_Enum, m_currentWidgets[i].second);
QEFIDPEditType::EditType_Enum, w.second);
if (data.isNull()) return dp;
format = data.toInt();
} else if (m_currentWidgets[i].first == tr("Signature Type")) {
} else if (w.first == tr("Signature Type")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_Enum, m_currentWidgets[i].second);
QEFIDPEditType::EditType_Enum, w.second);
if (data.isNull()) return dp;
signatureType = data.toInt();
} else if (m_currentWidgets[i].first == tr("Signature")) {
} else if (w.first == tr("Signature")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_HexData, m_currentWidgets[i].second);
QEFIDPEditType::EditType_HexData, w.second);
if (data.isNull() || data.type() != QVariant::Type::ByteArray) return dp;
QByteArray sig = data.toByteArray();
for (int i = 0; i < 16 && i < sig.size(); i++) {
Expand All @@ -209,10 +209,10 @@ QEFIDevicePath *QEFIDPEditorView::getDevicePath()
case QEFIDevicePathMediaSubType::MEDIA_File:
{
QString filepath;
for (int i = 0; i < m_currentWidgets.size(); i++) {
if (m_currentWidgets[i].first == tr("File")) {
for (const auto &w: std::as_const(m_currentWidgets)) {
if (w.first == tr("File")) {
QVariant data = retrieveDPEditComponent(
QEFIDPEditType::EditType_Path, m_currentWidgets[i].second);
QEFIDPEditType::EditType_Path, w.second);
if (data.isNull() || data.type() != QVariant::Type::String) return dp;
filepath = data.toString();
}
Expand Down Expand Up @@ -247,14 +247,14 @@ void QEFIDPEditorView::dpTypeComboBoxCurrentIndexChanged(int index)
if (m_dpSubtypeSelector != nullptr && m_dpTypeSelected != index) {
// Change the subtype
m_dpSubtypeSelector->clear();
int subtype = (m_dpTypeSelector != nullptr ?
int type = (m_dpTypeSelector != nullptr ?
m_dpTypeSelector->itemData(index).toInt() : 0);
QList<quint8> subtypes = enum_device_path_subtype(
(enum QEFIDevicePathType)subtype);
for (int i = 0; i < subtypes.size(); i++) {
(enum QEFIDevicePathType)type);
for (const auto &subtype: std::as_const(subtypes)) {
m_dpSubtypeSelector->addItem(convert_device_path_subtype_to_name(
(enum QEFIDevicePathType)subtype, subtypes[i]),
QVariant(subtypes[i]));
(enum QEFIDevicePathType)type, subtype),
QVariant(subtype));
}
}
m_dpTypeSelected = index;
Expand All @@ -272,8 +272,8 @@ void QEFIDPEditorView::dpSubtypeComboBoxCurrentIndexChanged(int index)
(quint8)subtype & 0xFF);
while (m_topLevelLayout->rowCount() > 2) m_topLevelLayout->removeRow(2);
m_currentWidgets = widgets;
for (int i = 0; i < m_currentWidgets.size(); i++) {
m_topLevelLayout->addRow(m_currentWidgets[i].first, m_currentWidgets[i].second);
for (const auto &w: std::as_const(m_currentWidgets)) {
m_topLevelLayout->addRow(w.first, w.second);
}
}
m_dpSubtypeSelected = index;
Expand Down Expand Up @@ -347,10 +347,10 @@ QList<QPair<QString, QWidget *> > QEFIDPEditorView::constructDPEditView(
QEFIDevicePathMediaHD hd(0, 0, 0, signature, 0, 0);
QList<QPair<QString, enum QEFIDPEditType>> types =
convert_device_path_types((QEFIDevicePath *)&hd);
for (int i = 0; i < types.size(); i++) {
QWidget *w = constructDPEditComponent(types[i].second);
widgets << QPair<QString, QWidget *>(types[i].first, w);
if (types[i].first == "Format") {
for (const auto &t: std::as_const(types)) {
QWidget *w = constructDPEditComponent(t.second);
widgets << QPair<QString, QWidget *>(t.first, w);
if (t.first == "Format") {
QComboBox *comboBox = dynamic_cast<QComboBox *>(w);
if (comboBox != nullptr) {
comboBox->addItem(tr("GPT"),
Expand All @@ -361,7 +361,7 @@ QList<QPair<QString, QWidget *> > QEFIDPEditorView::constructDPEditView(
QEFIDevicePathMediaHDFormat::PCAT));
}
}
if (types[i].first == "Signature Type") {
if (t.first == "Signature Type") {
QComboBox *comboBox = dynamic_cast<QComboBox *>(w);
if (comboBox != nullptr) {
comboBox->addItem(tr("None"),
Expand All @@ -387,9 +387,9 @@ QList<QPair<QString, QWidget *> > QEFIDPEditorView::constructDPEditView(
QEFIDevicePathMediaFile file(QStringLiteral(""));
QList<QPair<QString, enum QEFIDPEditType>> types =
convert_device_path_types((QEFIDevicePath *)&file);
for (int i = 0; i < types.size(); i++) {
QWidget *w = constructDPEditComponent(types[i].second);
widgets << QPair<QString, QWidget *>(types[i].first, w);
for (const auto &t: std::as_const(types)) {
QWidget *w = constructDPEditComponent(t.second);
widgets << QPair<QString, QWidget *>(t.first, w);
}
}
break;
Expand Down
9 changes: 4 additions & 5 deletions qefientrystaticlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ QEFIEntryStaticList *QEFIEntryStaticList::instance()

QEFIEntryStaticList::~QEFIEntryStaticList()
{
QMap<quint16, QEFILoadOption *>::const_iterator i = m_loadOptions.begin();
for (; i != m_loadOptions.end(); i++) {
if (*i) delete (*i);
for (const auto &option: std::as_const(m_loadOptions)) {
if (option) delete (option);
}
m_loadOptions.clear();
}
Expand Down Expand Up @@ -118,8 +117,8 @@ void QEFIEntryStaticList::setBootOrder(const QList<quint16> &newOrder)

QByteArray orderBuffer(newOrder.size() * 2, 0);
quint16 *p = (quint16 *)orderBuffer.data();
for (int i = 0; i < newOrder.size(); i++, p++) {
*p = qToLittleEndian<quint16>(newOrder[i]);
for (const auto &i: newOrder) {
*p = qToLittleEndian<quint16>(i);
}
qefi_set_variable(g_efiUuid,
QStringLiteral("BootOrder"), orderBuffer);
Expand Down
6 changes: 3 additions & 3 deletions qefientryview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ void QEFIEntryView::entryChanged(int currentRow)
void QEFIEntryView::resetClicked(bool checked)
{
m_entries->clear();
for (int i = 0; i < m_order.size(); i++) {
if (m_entryItems.contains(m_order[i])) {
QEFIEntry &entry = m_entryItems[m_order[i]];
for (const auto &i: std::as_const(m_order)) {
if (m_entryItems.contains(i)) {
QEFIEntry &entry = m_entryItems[i];
QString item = QStringLiteral("[%1] %2")
.arg(entry.id(), 4, 16, QLatin1Char('0'))
.arg(entry.name());
Expand Down
12 changes: 6 additions & 6 deletions qefiloadoptioneditorview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ QEFILoadOptionEditorView::~QEFILoadOptionEditorView()
if (m_idSpinBox != nullptr) m_idSpinBox->deleteLater();

// Clear and delete
for (int i = 0; i < m_dps.size(); i++) {
delete m_dps[i];
for (const auto &dp: std::as_const(m_dps)) {
delete dp;
}
m_dps.clear();
}
Expand Down Expand Up @@ -140,8 +140,8 @@ QByteArray QEFILoadOptionEditorView::generateLoadOption()
if (optionalData.size() * 2 != m_optionalDataTextEdit->text().length()) return data;
newLoadOption.setOptionalData(optionalData);
// Format DPs and clear it
for (int i = 0; i < m_dps.size(); i++) {
newLoadOption.addDevicePath(m_dps[i]);
for (const auto &dp: std::as_const(m_dps)) {
newLoadOption.addDevicePath(dp);
}
m_dps.clear();

Expand All @@ -157,8 +157,8 @@ quint16 QEFILoadOptionEditorView::getBootEntryID()
void QEFILoadOptionEditorView::clearDPClicked(bool checked)
{
Q_UNUSED(checked);
for (int i = 0; i < m_dps.size(); i++) {
delete m_dps[i];
for (const auto &dp: std::as_const(m_dps)) {
delete dp;
}
m_dps.clear();
}

0 comments on commit e598937

Please sign in to comment.