Skip to content

Commit eb8cd55

Browse files
Replace PAIRTYPE macro with C++20 structured bindings
PAIRTYPE(t1, t2) was a pre-C++11 convenience macro for std::pair. Replace all 34 usages with structured bindings and remove the macro definition from utilstrencodings.h. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e87b181 commit eb8cd55

11 files changed

Lines changed: 76 additions & 90 deletions

File tree

src/alert.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ bool CAlert::ProcessAlert(const std::vector<unsigned char>& alertKey, bool fThre
221221
}
222222

223223
// Check if this alert has been cancelled
224-
for (PAIRTYPE(const uint256, CAlert)& item : mapAlerts)
224+
for (auto& [hash, alert] : mapAlerts)
225225
{
226-
const CAlert& alert = item.second;
227226
if (alert.Cancels(*this))
228227
{
229228
LogPrint("alert", "alert already cancelled by %d\n", alert.nID);

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,12 @@ void CleanupBlockRevFiles()
650650
// keeping a separate counter. Once we hit a gap (or if 0 doesn't exist)
651651
// start removing block files.
652652
int nContigCounter = 0;
653-
for (const PAIRTYPE(string, path)& item : mapBlockFiles) {
654-
if (atoi(item.first) == nContigCounter) {
653+
for (const auto& [fileIndex, filePath] : mapBlockFiles) {
654+
if (atoi(fileIndex) == nContigCounter) {
655655
nContigCounter++;
656656
continue;
657657
}
658-
remove(item.second);
658+
remove(filePath);
659659
}
660660
}
661661

src/main.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6051,15 +6051,13 @@ bool static LoadBlockIndexDB()
60516051
// Calculate nChainWork
60526052
vector<pair<int, CBlockIndex*> > vSortedByHeight;
60536053
vSortedByHeight.reserve(mapBlockIndex.size());
6054-
for (const PAIRTYPE(uint256, CBlockIndex*)& item : mapBlockIndex)
6054+
for (const auto& [hash, pindex] : mapBlockIndex)
60556055
{
6056-
CBlockIndex* pindex = item.second;
60576056
vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
60586057
}
60596058
sort(vSortedByHeight.begin(), vSortedByHeight.end());
6060-
for (const PAIRTYPE(int, CBlockIndex*)& item : vSortedByHeight)
6059+
for (const auto& [height, pindex] : vSortedByHeight)
60616060
{
6062-
CBlockIndex* pindex = item.second;
60636061
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
60646062
// We can link the chain of blocks for which we've received transactions at some point.
60656063
// Pruned nodes may have deleted the block.
@@ -6143,9 +6141,8 @@ bool static LoadBlockIndexDB()
61436141
// Check presence of blk files
61446142
LogPrintf("Checking all blk files are present...\n");
61456143
set<int> setBlkDataFiles;
6146-
for (const PAIRTYPE(uint256, CBlockIndex*)& item : mapBlockIndex)
6144+
for (const auto& [hash, pindex] : mapBlockIndex)
61476145
{
6148-
CBlockIndex* pindex = item.second;
61496146
if (pindex->nStatus & BLOCK_HAVE_DATA) {
61506147
setBlkDataFiles.insert(pindex->nFile);
61516148
}
@@ -6181,9 +6178,8 @@ bool static LoadBlockIndexDB()
61816178
fTimestampIndex = fInsightExplorer;
61826179

61836180
// Fill in-memory data
6184-
for (const PAIRTYPE(uint256, CBlockIndex*)& item : mapBlockIndex)
6181+
for (const auto& [hash, pindex] : mapBlockIndex)
61856182
{
6186-
CBlockIndex* pindex = item.second;
61876183
// - This relationship will always be true even if pprev has multiple
61886184
// children, because hashSproutAnchor is technically a property of pprev,
61896185
// not its children.
@@ -6873,9 +6869,8 @@ std::string GetWarnings(const std::string& strFor)
68736869
// Alerts
68746870
{
68756871
LOCK(cs_mapAlerts);
6876-
for (PAIRTYPE(const uint256, CAlert)& item : mapAlerts)
6872+
for (auto& [alertHash, alert] : mapAlerts)
68776873
{
6878-
const CAlert& alert = item.second;
68796874
if (alert.AppliesToMe() && alert.nPriority > nPriority)
68806875
{
68816876
nPriority = alert.nPriority;
@@ -7228,8 +7223,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
72287223
// Relay alerts
72297224
{
72307225
LOCK(cs_mapAlerts);
7231-
for (PAIRTYPE(const uint256, CAlert)& item : mapAlerts)
7232-
item.second.RelayTo(pfrom);
7226+
for (auto& [alertHash, alert] : mapAlerts)
7227+
alert.RelayTo(pfrom);
72337228
}
72347229

72357230
pfrom->fSuccessfullyConnected = true;

src/rpc/net.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,12 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
485485
UniValue localAddresses(UniValue::VARR);
486486
{
487487
LOCK(cs_mapLocalHost);
488-
for (const PAIRTYPE(CNetAddr, LocalServiceInfo) &item : mapLocalHost)
488+
for (const auto& [addr, serviceInfo] : mapLocalHost)
489489
{
490490
UniValue rec(UniValue::VOBJ);
491-
rec.pushKV("address", item.first.ToString());
492-
rec.pushKV("port", item.second.nPort);
493-
rec.pushKV("score", item.second.nScore);
491+
rec.pushKV("address", addr.ToString());
492+
rec.pushKV("port", serviceInfo.nPort);
493+
rec.pushKV("score", serviceInfo.nScore);
494494
localAddresses.push_back(rec);
495495
}
496496
}

src/rpc/server.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ void RPCTypeCheckObj(const UniValue& o,
9090
const map<string, UniValue::VType>& typesExpected,
9191
bool fAllowNull)
9292
{
93-
for (const PAIRTYPE(string, UniValue::VType)& t : typesExpected)
93+
for (const auto& [fieldName, expectedType] : typesExpected)
9494
{
95-
const UniValue& v = find_value(o, t.first);
95+
const UniValue& v = find_value(o, fieldName);
9696
if (!fAllowNull && v.isNull())
97-
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
97+
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", fieldName));
9898

99-
if (!((v.type() == t.second) || (fAllowNull && (v.isNull()))))
99+
if (!((v.type() == expectedType) || (fAllowNull && (v.isNull()))))
100100
{
101101
string err = strprintf("Expected type %s for %s, got %s",
102-
uvTypeName(t.second), t.first, uvTypeName(v.type()));
102+
uvTypeName(expectedType), fieldName, uvTypeName(v.type()));
103103
throw JSONRPCError(RPC_TYPE_ERROR, err);
104104
}
105105
}
@@ -171,9 +171,8 @@ std::string CRPCTable::help(const std::string& strCommand) const
171171
vCommands.push_back(make_pair(mi->second->category + mi->first, mi->second));
172172
sort(vCommands.begin(), vCommands.end());
173173

174-
for (const PAIRTYPE(string, const CRPCCommand*)& command : vCommands)
174+
for (const auto& [sortKey, pcmd] : vCommands)
175175
{
176-
const CRPCCommand *pcmd = command.second;
177176
string strMethod = pcmd->name;
178177
// We already filter duplicates, but these deprecated screw up the sort order
179178
if (strMethod.find("label") != string::npos)

src/script/standard.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsi
7070

7171
// Scan templates
7272
const CScript& script1 = scriptPubKey;
73-
for (const PAIRTYPE(txnouttype, CScript)& tplate : mTemplates)
73+
for (const auto& [txType, script2] : mTemplates)
7474
{
75-
const CScript& script2 = tplate.second;
7675
vSolutionsRet.clear();
7776

7877
opcodetype opcode1, opcode2;
@@ -86,7 +85,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsi
8685
if (pc1 == script1.end() && pc2 == script2.end())
8786
{
8887
// Found a match
89-
typeRet = tplate.first;
88+
typeRet = txType;
9089
if (typeRet == TX_MULTISIG)
9190
{
9291
// Additional checks for TX_MULTISIG:

src/sync.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,38 +76,38 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
7676

7777
LogPrintf("POTENTIAL DEADLOCK DETECTED\n");
7878
LogPrintf("Previous lock order was:\n");
79-
for (const PAIRTYPE(void*, CLockLocation) & i : s2) {
80-
if (i.first == mismatch.first) {
79+
for (const auto& [lockPtr, lockLoc] :s2) {
80+
if (lockPtr == mismatch.first) {
8181
LogPrintf(" (1)");
82-
if (!firstLocked && secondLocked && i.second.fTry)
82+
if (!firstLocked && secondLocked && lockLoc.fTry)
8383
onlyMaybeDeadlock = true;
8484
firstLocked = true;
8585
}
86-
if (i.first == mismatch.second) {
86+
if (lockPtr == mismatch.second) {
8787
LogPrintf(" (2)");
88-
if (!secondLocked && firstLocked && i.second.fTry)
88+
if (!secondLocked && firstLocked && lockLoc.fTry)
8989
onlyMaybeDeadlock = true;
9090
secondLocked = true;
9191
}
92-
LogPrintf(" %s\n", i.second.ToString());
92+
LogPrintf(" %s\n", lockLoc.ToString());
9393
}
9494
firstLocked = false;
9595
secondLocked = false;
9696
LogPrintf("Current lock order is:\n");
97-
for (const PAIRTYPE(void*, CLockLocation) & i : s1) {
98-
if (i.first == mismatch.first) {
97+
for (const auto& [lockPtr, lockLoc] :s1) {
98+
if (lockPtr == mismatch.first) {
9999
LogPrintf(" (1)");
100-
if (!firstLocked && secondLocked && i.second.fTry)
100+
if (!firstLocked && secondLocked && lockLoc.fTry)
101101
onlyMaybeDeadlock = true;
102102
firstLocked = true;
103103
}
104-
if (i.first == mismatch.second) {
104+
if (lockPtr == mismatch.second) {
105105
LogPrintf(" (2)");
106-
if (!secondLocked && firstLocked && i.second.fTry)
106+
if (!secondLocked && firstLocked && lockLoc.fTry)
107107
onlyMaybeDeadlock = true;
108108
secondLocked = true;
109109
}
110-
LogPrintf(" %s\n", i.second.ToString());
110+
LogPrintf(" %s\n", lockLoc.ToString());
111111
}
112112
assert(onlyMaybeDeadlock);
113113
}
@@ -122,16 +122,16 @@ static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
122122
(*lockstack).push_back(std::make_pair(c, locklocation));
123123

124124
if (!fTry) {
125-
for (const PAIRTYPE(void*, CLockLocation) & i : (*lockstack)) {
126-
if (i.first == c)
125+
for (const auto& [lockPtr, lockLoc] :(*lockstack)) {
126+
if (lockPtr == c)
127127
break;
128128

129-
std::pair<void*, void*> p1 = std::make_pair(i.first, c);
129+
std::pair<void*, void*> p1 = std::make_pair(lockPtr, c);
130130
if (lockorders.count(p1))
131131
continue;
132132
lockorders[p1] = (*lockstack);
133133

134-
std::pair<void*, void*> p2 = std::make_pair(c, i.first);
134+
std::pair<void*, void*> p2 = std::make_pair(c, lockPtr);
135135
if (lockorders.count(p2))
136136
potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]);
137137
}
@@ -159,15 +159,15 @@ void LeaveCritical()
159159
std::string LocksHeld()
160160
{
161161
std::string result;
162-
for (const PAIRTYPE(void*, CLockLocation) & i : *lockstack)
163-
result += i.second.ToString() + std::string("\n");
162+
for (const auto& [lockPtr, lockLoc] :*lockstack)
163+
result += lockLoc.ToString() + std::string("\n");
164164
return result;
165165
}
166166

167167
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
168168
{
169-
for (const PAIRTYPE(void*, CLockLocation) & i : *lockstack)
170-
if (i.first == cs)
169+
for (const auto& [lockPtr, lockLoc] :*lockstack)
170+
if (lockPtr == cs)
171171
return;
172172
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
173173
abort();

src/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,10 @@ void ParseParameters(int argc, const char* const argv[])
390390
}
391391

392392
// New 0.6 features:
393-
for (const PAIRTYPE(string,string)& entry : mapArgs)
393+
for (const auto& [key, value] : mapArgs)
394394
{
395395
// interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set
396-
InterpretNegativeSetting(entry.first, mapArgs);
396+
InterpretNegativeSetting(key, mapArgs);
397397
}
398398
}
399399

src/utilstrencodings.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#define UEND(a) ((unsigned char*)&((&(a))[1]))
2121
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
2222

23-
/** This is needed because the foreach macro can't get over the comma in pair<t1, t2> */
24-
#define PAIRTYPE(t1, t2) std::pair<t1, t2>
2523

2624
/** Used by SanitizeString() */
2725
enum SafeChars

src/wallet/rpcwallet.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ void WalletTxToJSON(const CWalletTx& wtx, UniValue& entry)
103103
entry.pushKV("walletconflicts", conflicts);
104104
entry.pushKV("time", wtx.GetTxTime());
105105
entry.pushKV("timereceived", (int64_t)wtx.nTimeReceived);
106-
for (const PAIRTYPE(string,string)& item : wtx.mapValue)
107-
entry.pushKV(item.first, item.second);
106+
for (const auto& [key, value] : wtx.mapValue)
107+
entry.pushKV(key, value);
108108

109109
entry.pushKV("vJoinSplit", TxJoinSplitToJSON(wtx));
110110
}
@@ -1740,9 +1740,9 @@ UniValue listaccounts(const UniValue& params, bool fHelp)
17401740
includeWatchonly = includeWatchonly | ISMINE_WATCH_ONLY;
17411741

17421742
map<string, CAmount> mapAccountBalances;
1743-
for (const PAIRTYPE(CTxDestination, CAddressBookData)& entry : pwalletMain->mapAddressBook) {
1744-
if (IsMine(*pwalletMain, entry.first) & includeWatchonly) // This address belongs to me
1745-
mapAccountBalances[entry.second.name] = 0;
1743+
for (const auto& [dest, addrBookData] : pwalletMain->mapAddressBook) {
1744+
if (IsMine(*pwalletMain, dest) & includeWatchonly) // This address belongs to me
1745+
mapAccountBalances[addrBookData.name] = 0;
17461746
}
17471747

17481748
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
@@ -1775,8 +1775,8 @@ UniValue listaccounts(const UniValue& params, bool fHelp)
17751775
mapAccountBalances[entry.strAccount] += entry.nCreditDebit;
17761776

17771777
UniValue ret(UniValue::VOBJ);
1778-
for (const PAIRTYPE(string, CAmount)& accountBalance : mapAccountBalances) {
1779-
ret.pushKV(accountBalance.first, ValueFromAmount(accountBalance.second));
1778+
for (const auto& [accountName, balance] : mapAccountBalances) {
1779+
ret.pushKV(accountName, ValueFromAmount(balance));
17801780
}
17811781
return ret;
17821782
}

0 commit comments

Comments
 (0)