Replace base_uint(string) with from_hex_text<> (RIPD-897)

Removes the base_uint constructor that took a string.  Replaces
that functionality with two free functions named from_hex_text<>.
Use of from_hex_text<> looks like this:

auto v = from_hex_text<uint256>("AAA555");
static_assert (std::is_same<decltype(v), uint256>::value, "Huh!");

from_hex_text<> only operates on base_uint types.  At the moment the
list of those types include:

 o uint128,
 o uint160,
 o uint256,
 o Directory,
 o Account,
 o Currency, and
 o NodeID.

Using from_hex_text<> with any other types will not compile due to
an enable_if.
This commit is contained in:
Scott Schurr
2015-05-29 10:31:13 -07:00
committed by Vinnie Falco
parent d7def5509d
commit 4515ac0bca
9 changed files with 48 additions and 25 deletions

View File

@@ -141,13 +141,6 @@ public:
*this = b;
}
// NIKB TODO remove the need for this constructor - have a free function
// to handle the hex string parsing.
explicit base_uint (std::string const& str)
{
SetHex (str);
}
base_uint (base_uint<Bits, Tag> const& other) = default;
template <class OtherTag>
@@ -513,6 +506,27 @@ inline std::string to_string (base_uint<Bits, Tag> const& a)
return strHex (a.begin (), a.size ());
}
// Function templates that return a base_uint given text in hexadecimal.
// Invoke like:
// auto i = from_hex_text<uint256>("AAAAA");
template <typename T>
auto from_hex_text (char const* text) -> std::enable_if_t<
std::is_same<T, base_uint<T::bytes*8, typename T::tag_type>>::value, T>
{
T ret;
ret.SetHex (text);
return ret;
}
template <typename T>
auto from_hex_text (std::string const& text) -> std::enable_if_t<
std::is_same<T, base_uint<T::bytes*8, typename T::tag_type>>::value, T>
{
T ret;
ret.SetHex (text);
return ret;
}
template <std::size_t Bits, class Tag>
inline std::ostream& operator<< (
std::ostream& out, base_uint<Bits, Tag> const& u)