Skip to content

Commit c33afea

Browse files
committed
merge bitcoin-core/gui#166: Use enum type as switch argument in *TableModel
1 parent 5a67b52 commit c33afea

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
@@ -195,42 +195,38 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
195195

196196
AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
197197

198-
if(role == Qt::DisplayRole || role == Qt::EditRole)
199-
{
200-
switch(index.column())
201-
{
198+
const auto column = static_cast<ColumnIndex>(index.column());
199+
if (role == Qt::DisplayRole || role == Qt::EditRole) {
200+
switch (column) {
202201
case Label:
203-
if(rec->label.isEmpty() && role == Qt::DisplayRole)
204-
{
202+
if (rec->label.isEmpty() && role == Qt::DisplayRole) {
205203
return tr("(no label)");
206-
}
207-
else
208-
{
204+
} else {
209205
return rec->label;
210206
}
211207
case Address:
212208
return rec->address;
213-
}
214-
}
215-
else if (role == Qt::FontRole)
216-
{
217-
QFont font;
218-
if(index.column() == Address)
219-
{
220-
font = GUIUtil::getFontNormal();
221-
}
222-
return font;
223-
}
224-
else if (role == TypeRole)
225-
{
209+
} // no default case, so the compiler can warn about missing cases
210+
assert(false);
211+
} else if (role == Qt::FontRole) {
212+
switch (column) {
213+
case Label:
214+
return QFont();
215+
case Address:
216+
return GUIUtil::getFontNormal();
217+
} // no default case, so the compiler can warn about missing cases
218+
assert(false);
219+
} else if (role == TypeRole) {
226220
switch(rec->type)
227221
{
228222
case AddressTableEntry::Sending:
229223
return Send;
230224
case AddressTableEntry::Receiving:
231225
return Receive;
232-
default: break;
233-
}
226+
case AddressTableEntry::Hidden:
227+
return {};
228+
} // no default case, so the compiler can warn about missing cases
229+
assert(false);
234230
}
235231
return QVariant();
236232
}

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
@@ -158,9 +156,9 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
158156

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

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

src/qt/transactiontablemodel.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -607,26 +607,29 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
607607
return QVariant();
608608
TransactionRecord *rec = static_cast<TransactionRecord*>(index.internalPointer());
609609

610-
switch(role)
611-
{
610+
const auto column = static_cast<ColumnIndex>(index.column());
611+
switch (role) {
612612
case RawDecorationRole:
613-
switch(index.column())
614-
{
613+
switch (column) {
615614
case Status:
616615
return txStatusDecoration(rec);
617616
case Watchonly:
618617
return txWatchonlyDecoration(rec);
618+
case Date: return {};
619+
case Type: return {};
619620
case ToAddress:
620621
return txAddressDecoration(rec);
621-
}
622-
break;
622+
case Amount: return {};
623+
} // no default case, so the compiler can warn about missing cases
624+
assert(false);
623625
case Qt::DecorationRole:
624626
{
625627
return qvariant_cast<QIcon>(index.data(RawDecorationRole));
626628
}
627629
case Qt::DisplayRole:
628-
switch(index.column())
629-
{
630+
switch (column) {
631+
case Status: return {};
632+
case Watchonly: return {};
630633
case Date:
631634
return formatTxDate(rec);
632635
case Type:
@@ -635,12 +638,11 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
635638
return formatTxToAddress(rec, false);
636639
case Amount:
637640
return formatTxAmount(rec, true, BitcoinUnits::SeparatorStyle::ALWAYS);
638-
}
639-
break;
641+
} // no default case, so the compiler can warn about missing cases
642+
assert(false);
640643
case Qt::EditRole:
641644
// Edit role is used for sorting, so return the unformatted values
642-
switch(index.column())
643-
{
645+
switch (column) {
644646
case Status:
645647
return QString::fromStdString(rec->status.sortKey);
646648
case Date:
@@ -653,8 +655,8 @@ QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
653655
return formatTxToAddress(rec, true);
654656
case Amount:
655657
return qint64(rec->credit + rec->debit);
656-
}
657-
break;
658+
} // no default case, so the compiler can warn about missing cases
659+
assert(false);
658660
case Qt::ToolTipRole:
659661
return formatTooltip(rec);
660662
case Qt::TextAlignmentRole:

0 commit comments

Comments
 (0)