Skip to content

Commit 32958da

Browse files
committed
merge bitcoin#25611: Avoid brittle, narrowing and verbose integral type confusions
1 parent 7802752 commit 32958da

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

src/rpc/net.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static RPCHelpMan getconnectioncount()
6666
const NodeContext& node = EnsureAnyNodeContext(request.context);
6767
const CConnman& connman = EnsureConnman(node);
6868

69-
return (int)connman.GetNodeCount(ConnectionDirection::Both);
69+
return connman.GetNodeCount(ConnectionDirection::Both);
7070
},
7171
};
7272
}
@@ -684,12 +684,12 @@ static RPCHelpMan getnetworkinfo()
684684
obj.pushKV("timeoffset", GetTimeOffset());
685685
if (node.connman) {
686686
obj.pushKV("networkactive", node.connman->GetNetworkActive());
687-
obj.pushKV("connections", (int)node.connman->GetNodeCount(ConnectionDirection::Both));
688-
obj.pushKV("connections_in", (int)node.connman->GetNodeCount(ConnectionDirection::In));
689-
obj.pushKV("connections_out", (int)node.connman->GetNodeCount(ConnectionDirection::Out));
690-
obj.pushKV("connections_mn", (int)node.connman->GetNodeCount(ConnectionDirection::Verified));
691-
obj.pushKV("connections_mn_in", (int)node.connman->GetNodeCount(ConnectionDirection::VerifiedIn));
692-
obj.pushKV("connections_mn_out", (int)node.connman->GetNodeCount(ConnectionDirection::VerifiedOut));
687+
obj.pushKV("connections", node.connman->GetNodeCount(ConnectionDirection::Both));
688+
obj.pushKV("connections_in", node.connman->GetNodeCount(ConnectionDirection::In));
689+
obj.pushKV("connections_out", node.connman->GetNodeCount(ConnectionDirection::Out));
690+
obj.pushKV("connections_mn", node.connman->GetNodeCount(ConnectionDirection::Verified));
691+
obj.pushKV("connections_mn_in", node.connman->GetNodeCount(ConnectionDirection::VerifiedIn));
692+
obj.pushKV("connections_mn_out", node.connman->GetNodeCount(ConnectionDirection::VerifiedOut));
693693
std::string_view sem_str = SEMToString(node.connman->GetSocketEventsMode());
694694
CHECK_NONFATAL(sem_str != "unknown");
695695
obj.pushKV("socketevents", std::string(sem_str));

src/univalue/include/univalue.h

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,25 @@ class UniValue {
2424
typ = initialType;
2525
val = initialStr;
2626
}
27-
UniValue(uint64_t val_) {
28-
setInt(val_);
29-
}
30-
UniValue(int64_t val_) {
31-
setInt(val_);
32-
}
33-
UniValue(bool val_) {
34-
setBool(val_);
35-
}
36-
UniValue(int val_) {
37-
setInt(val_);
38-
}
39-
UniValue(double val_) {
40-
setFloat(val_);
41-
}
42-
UniValue(const std::string& val_) {
43-
setStr(val_);
44-
}
45-
UniValue(const char *val_) {
46-
std::string s(val_);
47-
setStr(s);
27+
template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,
28+
std::enable_if_t<std::is_floating_point_v<T> || // setFloat
29+
std::is_same_v<bool, T> || // setBool
30+
std::is_signed_v<T> || std::is_unsigned_v<T> || // setInt
31+
std::is_constructible_v<std::string, T>, // setStr
32+
bool> = true>
33+
UniValue(Ref&& val)
34+
{
35+
if constexpr (std::is_floating_point_v<T>) {
36+
setFloat(val);
37+
} else if constexpr (std::is_same_v<bool, T>) {
38+
setBool(val);
39+
} else if constexpr (std::is_signed_v<T>) {
40+
setInt(int64_t{val});
41+
} else if constexpr (std::is_unsigned_v<T>) {
42+
setInt(uint64_t{val});
43+
} else {
44+
setStr(std::string{std::forward<Ref>(val)});
45+
}
4846
}
4947

5048
void clear();

src/wallet/rpc/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ static RPCHelpMan upgradewallet()
858858
"\nUpgrade the wallet. Upgrades to the latest version if no version number is specified.\n"
859859
"New keys may be generated and a new wallet backup will need to be made.",
860860
{
861-
{"version", RPCArg::Type::NUM, RPCArg::Default{FEATURE_LATEST}, "The version number to upgrade to. Default is the latest wallet version."}
861+
{"version", RPCArg::Type::NUM, RPCArg::Default{int{FEATURE_LATEST}}, "The version number to upgrade to. Default is the latest wallet version."}
862862
},
863863
RPCResult{
864864
RPCResult::Type::OBJ, "", "",

0 commit comments

Comments
 (0)