Skip to content

Commit 8c21562

Browse files
author
MarcoFalke
committed
Merge bitcoin-core#166: refactor: Use enum type as switch argument in *TableModel
1d5d832 qt, refactor: Use enum type as switch argument in TransactionTableModel (Hennadii Stepanov) 52f122c qt, refactor: Use enum type as switch argument in PeerTableModel (Hennadii Stepanov) a35223f qt, refactor: Use enum type as switch argument in BanTableModel (Hennadii Stepanov) ab8a747 qt, refactor: Use enum type as switch argument in AddressTableModel (Hennadii Stepanov) Pull request description: This PR makes code more maintainable by leveraging `-Wswitch` compiler warnings. Only the `RecentRequestsTableModel` is not refactored, because its `enum ColumnIndex` contains additional `NUMBER_OF_COLUMNS` value. No behavior change. ACKs for top commit: hebasto: Do you mind mentioning the _top_ pr commit with your ACK, i.e., 1d5d832, not ab8a747? jarolrod: ACK 1d5d832, tested on macOS 11.1 Qt 5.15.2 leonardojobim: ACK bitcoin-core@1d5d832, tested on Ubuntu 20.04 Qt 5.12.8 promag: Code review ACK 1d5d832. Tree-SHA512: 0d474d226a2fa0069495d1aa5ec13b2470708ec7b8a6ab35402236c7bf57cb9939577677a30dfa54f5e3bc0477c6cfffd20ed6f19e4eb394a938569cc9347851
2 parents 1020b04 + 1d5d832 commit 8c21562

File tree

4 files changed

+65
-65
lines changed

4 files changed

+65
-65
lines changed

src/qt/addresstablemodel.cpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,42 +198,38 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
198198

199199
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
200200

201-
if(role == Qt::DisplayRole || role == Qt::EditRole)
202-
{
203-
switch(index.column())
204-
{
201+
const auto column = static_cast<ColumnIndex>(index.column());
202+
if (role == Qt::DisplayRole || role == Qt::EditRole) {
203+
switch (column) {
205204
case Label:
206-
if(rec->label.isEmpty() && role == Qt::DisplayRole)
207-
{
205+
if (rec->label.isEmpty() && role == Qt::DisplayRole) {
208206
return tr("(no label)");
209-
}
210-
else
211-
{
207+
} else {
212208
return rec->label;
213209
}
214210
case Address:
215211
return rec->address;
216-
}
217-
}
218-
else if (role == Qt::FontRole)
219-
{
220-
QFont font;
221-
if(index.column() == Address)
222-
{
223-
font = GUIUtil::fixedPitchFont();
224-
}
225-
return font;
226-
}
227-
else if (role == TypeRole)
228-
{
212+
} // no default case, so the compiler can warn about missing cases
213+
assert(false);
214+
} else if (role == Qt::FontRole) {
215+
switch (column) {
216+
case Label:
217+
return QFont();
218+
case Address:
219+
return GUIUtil::fixedPitchFont();
220+
} // no default case, so the compiler can warn about missing cases
221+
assert(false);
222+
} else if (role == TypeRole) {
229223
switch(rec->type)
230224
{
231225
case AddressTableEntry::Sending:
232226
return Send;
233227
case AddressTableEntry::Receiving:
234228
return Receive;
235-
default: break;
236-
}
229+
case AddressTableEntry::Hidden:
230+
return {};
231+
} // no default case, so the compiler can warn about missing cases
232+
assert(false);
237233
}
238234
return QVariant();
239235
}

src/qt/bantablemodel.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ bool BannedNodeLessThan::operator()(const CCombinedBan& left, const CCombinedBan
2323
if (order == Qt::DescendingOrder)
2424
std::swap(pLeft, pRight);
2525

26-
switch(column)
27-
{
26+
switch (static_cast<BanTableModel::ColumnIndex>(column)) {
2827
case BanTableModel::Address:
2928
return pLeft->subnet.ToString().compare(pRight->subnet.ToString()) < 0;
3029
case BanTableModel::Bantime:
3130
return pLeft->banEntry.nBanUntil < pRight->banEntry.nBanUntil;
32-
}
33-
34-
return false;
31+
} // no default case, so the compiler can warn about missing cases
32+
assert(false);
3533
}
3634

3735
// private implementation
@@ -119,16 +117,17 @@ QVariant BanTableModel::data(const QModelIndex &index, int role) const
119117

120118
CCombinedBan *rec = static_cast<CCombinedBan*>(index.internalPointer());
121119

120+
const auto column = static_cast<ColumnIndex>(index.column());
122121
if (role == Qt::DisplayRole) {
123-
switch(index.column())
124-
{
122+
switch (column) {
125123
case Address:
126124
return QString::fromStdString(rec->subnet.ToString());
127125
case Bantime:
128126
QDateTime date = QDateTime::fromMSecsSinceEpoch(0);
129127
date = date.addSecs(rec->banEntry.nBanUntil);
130128
return QLocale::system().toString(date, QLocale::LongFormat);
131-
}
129+
} // no default case, so the compiler can warn about missing cases
130+
assert(false);
132131
}
133132

134133
return QVariant();

src/qt/peertablemodel.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombine
2323
if (order == Qt::DescendingOrder)
2424
std::swap(pLeft, pRight);
2525

26-
switch(column)
27-
{
26+
switch (static_cast<PeerTableModel::ColumnIndex>(column)) {
2827
case PeerTableModel::NetNodeId:
2928
return pLeft->nodeid < pRight->nodeid;
3029
case PeerTableModel::Address:
@@ -41,9 +40,8 @@ bool NodeLessThan::operator()(const CNodeCombinedStats &left, const CNodeCombine
4140
return pLeft->nRecvBytes < pRight->nRecvBytes;
4241
case PeerTableModel::Subversion:
4342
return pLeft->cleanSubVer.compare(pRight->cleanSubVer) < 0;
44-
}
45-
46-
return false;
43+
} // no default case, so the compiler can warn about missing cases
44+
assert(false);
4745
}
4846

4947
// private implementation
@@ -157,9 +155,9 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
157155

158156
CNodeCombinedStats *rec = static_cast<CNodeCombinedStats*>(index.internalPointer());
159157

158+
const auto column = static_cast<ColumnIndex>(index.column());
160159
if (role == Qt::DisplayRole) {
161-
switch(index.column())
162-
{
160+
switch (column) {
163161
case NetNodeId:
164162
return (qint64)rec->nodeStats.nodeid;
165163
case Address:
@@ -177,19 +175,24 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
177175
return GUIUtil::formatBytes(rec->nodeStats.nRecvBytes);
178176
case Subversion:
179177
return QString::fromStdString(rec->nodeStats.cleanSubVer);
180-
}
178+
} // no default case, so the compiler can warn about missing cases
179+
assert(false);
181180
} else if (role == Qt::TextAlignmentRole) {
182-
switch (index.column()) {
183-
case ConnectionType:
184-
case Network:
185-
return QVariant(Qt::AlignCenter);
186-
case Ping:
187-
case Sent:
188-
case Received:
189-
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
190-
default:
191-
return QVariant();
192-
}
181+
switch (column) {
182+
case NetNodeId:
183+
case Address:
184+
return {};
185+
case ConnectionType:
186+
case Network:
187+
return QVariant(Qt::AlignCenter);
188+
case Ping:
189+
case Sent:
190+
case Received:
191+
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
192+
case Subversion:
193+
return {};
194+
} // no default case, so the compiler can warn about missing cases
195+
assert(false);
193196
} else if (role == StatsRole) {
194197
switch (index.column()) {
195198
case NetNodeId: return QVariant::fromValue(rec);

src/qt/transactiontablemodel.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -526,27 +526,30 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
526526
return QVariant();
527527
TransactionRecord *rec = static_cast<TransactionRecord*>(index.internalPointer());
528528

529-
switch(role)
530-
{
529+
const auto column = static_cast<ColumnIndex>(index.column());
530+
switch (role) {
531531
case RawDecorationRole:
532-
switch(index.column())
533-
{
532+
switch (column) {
534533
case Status:
535534
return txStatusDecoration(rec);
536535
case Watchonly:
537536
return txWatchonlyDecoration(rec);
537+
case Date: return {};
538+
case Type: return {};
538539
case ToAddress:
539540
return txAddressDecoration(rec);
540-
}
541-
break;
541+
case Amount: return {};
542+
} // no default case, so the compiler can warn about missing cases
543+
assert(false);
542544
case Qt::DecorationRole:
543545
{
544546
QIcon icon = qvariant_cast<QIcon>(index.data(RawDecorationRole));
545547
return platformStyle->TextColorIcon(icon);
546548
}
547549
case Qt::DisplayRole:
548-
switch(index.column())
549-
{
550+
switch (column) {
551+
case Status: return {};
552+
case Watchonly: return {};
550553
case Date:
551554
return formatTxDate(rec);
552555
case Type:
@@ -555,12 +558,11 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
555558
return formatTxToAddress(rec, false);
556559
case Amount:
557560
return formatTxAmount(rec, true, BitcoinUnits::SeparatorStyle::ALWAYS);
558-
}
559-
break;
561+
} // no default case, so the compiler can warn about missing cases
562+
assert(false);
560563
case Qt::EditRole:
561564
// Edit role is used for sorting, so return the unformatted values
562-
switch(index.column())
563-
{
565+
switch (column) {
564566
case Status:
565567
return QString::fromStdString(rec->status.sortKey);
566568
case Date:
@@ -573,8 +575,8 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
573575
return formatTxToAddress(rec, true);
574576
case Amount:
575577
return qint64(rec->credit + rec->debit);
576-
}
577-
break;
578+
} // no default case, so the compiler can warn about missing cases
579+
assert(false);
578580
case Qt::ToolTipRole:
579581
return formatTooltip(rec);
580582
case Qt::TextAlignmentRole:

0 commit comments

Comments
 (0)