univalue: Avoid std::string copies

This commit is contained in:
MacroFake
2022-07-26 15:24:08 +02:00
parent e864f2e4af
commit 1111c7e3f1
2 changed files with 9 additions and 12 deletions

View File

@ -20,10 +20,7 @@ public:
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, }; enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
UniValue() { typ = VNULL; } UniValue() { typ = VNULL; }
UniValue(UniValue::VType initialType, const std::string& initialStr = "") { UniValue(UniValue::VType type, std::string str = {}) : typ{type}, val{std::move(str)} {}
typ = initialType;
val = initialStr;
}
template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>, template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,
std::enable_if_t<std::is_floating_point_v<T> || // setFloat std::enable_if_t<std::is_floating_point_v<T> || // setFloat
std::is_same_v<bool, T> || // setBool std::is_same_v<bool, T> || // setBool
@ -49,12 +46,12 @@ public:
void setNull(); void setNull();
void setBool(bool val); void setBool(bool val);
void setNumStr(const std::string& val); void setNumStr(std::string str);
void setInt(uint64_t val); void setInt(uint64_t val);
void setInt(int64_t val); void setInt(int64_t val);
void setInt(int val_) { return setInt(int64_t{val_}); } void setInt(int val_) { return setInt(int64_t{val_}); }
void setFloat(double val); void setFloat(double val);
void setStr(const std::string& val); void setStr(std::string str);
void setArray(); void setArray();
void setObject(); void setObject();

View File

@ -44,15 +44,15 @@ static bool validNumStr(const std::string& s)
return (tt == JTOK_NUMBER); return (tt == JTOK_NUMBER);
} }
void UniValue::setNumStr(const std::string& val_) void UniValue::setNumStr(std::string str)
{ {
if (!validNumStr(val_)) { if (!validNumStr(str)) {
throw std::runtime_error{"The string '" + val_ + "' is not a valid JSON number"}; throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
} }
clear(); clear();
typ = VNUM; typ = VNUM;
val = val_; val = std::move(str);
} }
void UniValue::setInt(uint64_t val_) void UniValue::setInt(uint64_t val_)
@ -82,11 +82,11 @@ void UniValue::setFloat(double val_)
return setNumStr(oss.str()); return setNumStr(oss.str());
} }
void UniValue::setStr(const std::string& val_) void UniValue::setStr(std::string str)
{ {
clear(); clear();
typ = VSTR; typ = VSTR;
val = val_; val = std::move(str);
} }
void UniValue::setArray() void UniValue::setArray()