From fa23c197509f692a815193acc1b50bad2fcbedfe Mon Sep 17 00:00:00 2001 From: MacroFake Date: Thu, 14 Jul 2022 11:20:31 +0200 Subject: [PATCH] univalue: Avoid narrowing and verbose int constructors As UniValue provides several constructors for integral types, the compiler is unable to select one if the passed type does not exactly match. This is unintuitive for developers and forces them to write verbose and brittle code. For example, there are many places where an unsigned int is cast to a signed int. While the cast is safe in practice, it is still needlessly verbose and confusing as the value can never be negative. In fact it might even be unsafe if the unsigned value is large enough to map to a negative signed one. --- src/rpc/net.cpp | 8 +++---- src/univalue/include/univalue.h | 40 ++++++++++++++++----------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index fad92629c59..69bbae96c17 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -60,7 +60,7 @@ static RPCHelpMan getconnectioncount() NodeContext& node = EnsureAnyNodeContext(request.context); const CConnman& connman = EnsureConnman(node); - return (int)connman.GetNodeCount(ConnectionDirection::Both); + return connman.GetNodeCount(ConnectionDirection::Both); }, }; } @@ -639,9 +639,9 @@ static RPCHelpMan getnetworkinfo() obj.pushKV("timeoffset", GetTimeOffset()); if (node.connman) { obj.pushKV("networkactive", node.connman->GetNetworkActive()); - obj.pushKV("connections", (int)node.connman->GetNodeCount(ConnectionDirection::Both)); - obj.pushKV("connections_in", (int)node.connman->GetNodeCount(ConnectionDirection::In)); - obj.pushKV("connections_out", (int)node.connman->GetNodeCount(ConnectionDirection::Out)); + obj.pushKV("connections", node.connman->GetNodeCount(ConnectionDirection::Both)); + obj.pushKV("connections_in", node.connman->GetNodeCount(ConnectionDirection::In)); + obj.pushKV("connections_out", node.connman->GetNodeCount(ConnectionDirection::Out)); } obj.pushKV("networks", GetNetworksInfo()); obj.pushKV("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK())); diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h index 7f9a6aaffd9..1fb746b96d0 100644 --- a/src/univalue/include/univalue.h +++ b/src/univalue/include/univalue.h @@ -24,27 +24,25 @@ public: typ = initialType; val = initialStr; } - UniValue(uint64_t val_) { - setInt(val_); - } - UniValue(int64_t val_) { - setInt(val_); - } - UniValue(bool val_) { - setBool(val_); - } - UniValue(int val_) { - setInt(val_); - } - UniValue(double val_) { - setFloat(val_); - } - UniValue(const std::string& val_) { - setStr(val_); - } - UniValue(const char *val_) { - std::string s(val_); - setStr(s); + template >, + std::enable_if_t || // setFloat + std::is_same_v || // setBool + std::is_signed_v || std::is_unsigned_v || // setInt + std::is_constructible_v, // setStr + bool> = true> + UniValue(Ref&& val) + { + if constexpr (std::is_floating_point_v) { + setFloat(val); + } else if constexpr (std::is_same_v) { + setBool(val); + } else if constexpr (std::is_signed_v) { + setInt(int64_t{val}); + } else if constexpr (std::is_unsigned_v) { + setInt(uint64_t{val}); + } else { + setStr(std::string{std::forward(val)}); + } } void clear();