Skip to content

Commit 06c4320

Browse files
jonatackpromag
andcommitted
cli: use C++17 std::array class template argument deduction (CTAD)
Credit to João Barbosa (promag) for the suggestions. Co-authored-by: João Barbosa <[email protected]>
1 parent edf3167 commit 06c4320

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/bitcoin-cli.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,10 @@ class BaseRequestHandler
234234
class AddrinfoRequestHandler : public BaseRequestHandler
235235
{
236236
private:
237-
static constexpr uint8_t m_networks_size{5};
238-
const std::array<std::string, m_networks_size> m_networks{{"ipv4", "ipv6", "torv2", "torv3", "i2p"}};
237+
static constexpr std::array m_networks{"ipv4", "ipv6", "torv2", "torv3", "i2p"};
239238
int8_t NetworkStringToId(const std::string& str) const
240239
{
241-
for (uint8_t i = 0; i < m_networks_size; ++i) {
240+
for (size_t i = 0; i < m_networks.size(); ++i) {
242241
if (str == m_networks.at(i)) return i;
243242
}
244243
return UNKNOWN_NETWORK;
@@ -262,7 +261,7 @@ class AddrinfoRequestHandler : public BaseRequestHandler
262261
throw std::runtime_error("-addrinfo requires bitcoind server to be running v22.0 and up");
263262
}
264263
// Count the number of peers we know by network, including torv2 versus torv3.
265-
std::array<uint64_t, m_networks_size> counts{{}};
264+
std::array<uint64_t, m_networks.size()> counts{{}};
266265
for (const UniValue& node : nodes) {
267266
std::string network_name{node["network"].get_str()};
268267
if (network_name == "onion") {
@@ -275,7 +274,7 @@ class AddrinfoRequestHandler : public BaseRequestHandler
275274
// Prepare result to return to user.
276275
UniValue result{UniValue::VOBJ}, addresses{UniValue::VOBJ};
277276
uint64_t total{0}; // Total address count
278-
for (uint8_t i = 0; i < m_networks_size; ++i) {
277+
for (size_t i = 0; i < m_networks.size(); ++i) {
279278
addresses.pushKV(m_networks.at(i), counts.at(i));
280279
total += counts.at(i);
281280
}
@@ -356,15 +355,14 @@ class GetinfoRequestHandler: public BaseRequestHandler
356355
class NetinfoRequestHandler : public BaseRequestHandler
357356
{
358357
private:
359-
static constexpr uint8_t m_networks_size{4};
360358
static constexpr uint8_t MAX_DETAIL_LEVEL{4};
361-
const std::array<std::string, m_networks_size> m_networks{{"ipv4", "ipv6", "onion", "i2p"}};
362-
std::array<std::array<uint16_t, m_networks_size + 1>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total)
359+
static constexpr std::array m_networks{"ipv4", "ipv6", "onion", "i2p"};
360+
std::array<std::array<uint16_t, m_networks.size() + 1>, 3> m_counts{{{}}}; //!< Peer counts by (in/out/total, networks/total)
363361
uint8_t m_block_relay_peers_count{0};
364362
uint8_t m_manual_peers_count{0};
365363
int8_t NetworkStringToId(const std::string& str) const
366364
{
367-
for (uint8_t i = 0; i < m_networks_size; ++i) {
365+
for (size_t i = 0; i < m_networks.size(); ++i) {
368366
if (str == m_networks.at(i)) return i;
369367
}
370368
return UNKNOWN_NETWORK;
@@ -461,10 +459,10 @@ class NetinfoRequestHandler : public BaseRequestHandler
461459
const bool is_outbound{!peer["inbound"].get_bool()};
462460
const bool is_block_relay{!peer["relaytxes"].get_bool()};
463461
const std::string conn_type{peer["connection_type"].get_str()};
464-
++m_counts.at(is_outbound).at(network_id); // in/out by network
465-
++m_counts.at(is_outbound).at(m_networks_size); // in/out overall
466-
++m_counts.at(2).at(network_id); // total by network
467-
++m_counts.at(2).at(m_networks_size); // total overall
462+
++m_counts.at(is_outbound).at(network_id); // in/out by network
463+
++m_counts.at(is_outbound).at(m_networks.size()); // in/out overall
464+
++m_counts.at(2).at(network_id); // total by network
465+
++m_counts.at(2).at(m_networks.size()); // total overall
468466
if (conn_type == "block-relay-only") ++m_block_relay_peers_count;
469467
if (conn_type == "manual") ++m_manual_peers_count;
470468
if (DetailsRequested()) {
@@ -534,11 +532,11 @@ class NetinfoRequestHandler : public BaseRequestHandler
534532
if (any_i2p_peers) result += " i2p";
535533
result += " total block";
536534
if (m_manual_peers_count) result += " manual";
537-
const std::array<std::string, 3> rows{{"in", "out", "total"}};
535+
const std::array rows{"in", "out", "total"};
538536
for (uint8_t i = 0; i < 3; ++i) {
539537
result += strprintf("\n%-5s %5i %5i %5i", rows.at(i), m_counts.at(i).at(0), m_counts.at(i).at(1), m_counts.at(i).at(2)); // ipv4/ipv6/onion peers counts
540538
if (any_i2p_peers) result += strprintf(" %5i", m_counts.at(i).at(3)); // i2p peers count
541-
result += strprintf(" %5i", m_counts.at(i).at(m_networks_size)); // total peers count
539+
result += strprintf(" %5i", m_counts.at(i).at(m_networks.size())); // total peers count
542540
if (i == 1) { // the outbound row has two extra columns for block relay and manual peer counts
543541
result += strprintf(" %5i", m_block_relay_peers_count);
544542
if (m_manual_peers_count) result += strprintf(" %5i", m_manual_peers_count);

0 commit comments

Comments
 (0)