Better types and more comments in RippleCalc.

* Better automatic conversions to and from tagged uint160 varints.
* Start using tagged variants of uint160 for Currency, Account.
* Comments from 2014/6/11 RippleCalc session.
This commit is contained in:
Tom Ritchford
2014-06-10 18:18:03 -04:00
committed by Vinnie Falco
parent a23013abc1
commit e24cba8c35
42 changed files with 1849 additions and 1310 deletions

View File

@@ -28,7 +28,7 @@
#include <ripple/types/api/Blob.h>
#include <ripple/types/api/strHex.h>
#include <ripple/types/api/ByteOrder.h>
#include <beast/container/hardened_hash.h>
#include <beast/utility/Zero.h>
@@ -150,7 +150,18 @@ public:
SetHex (str);
}
base_uint (base_uint const& other) = default;
base_uint (base_uint<Bits, Tag> const& other) = default;
template <
class OtherTag,
class = std::enable_if_t <
std::is_same <OtherTag, void>::value !=
std::is_same <Tag, void>::value>
>
base_uint (base_uint<Bits, OtherTag> const& other)
{
memcpy (&pn [0], other.data(), Bits / 8);
}
/* Construct from a raw pointer.
The buffer pointed to by `data` must be at least Bits/8 bytes.
@@ -402,9 +413,21 @@ extern std::size_t hash_value (uint160 const&);
extern std::size_t hash_value (uint256 const&);
//------------------------------------------------------------------------------
template <std::size_t Bits, class Tag>
template <typename Tag1, typename Tag2>
struct is_tag_compatible
{
static bool const value =
std::is_same <Tag1, Tag2>::value ||
std::is_same <Tag1, void>::value ||
std::is_same <Tag2, void>::value;
};
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline int compare (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
auto ret = std::mismatch (a.cbegin (), a.cend (), b.cbegin ());
@@ -419,44 +442,56 @@ inline int compare (
return -1;
}
template <std::size_t Bits, class Tag>
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline bool operator< (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
return compare (a, b) < 0;
}
template <std::size_t Bits, class Tag>
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline bool operator<= (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
return compare (a, b) <= 0;
}
template <std::size_t Bits, class Tag>
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline bool operator> (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
return compare (a, b) > 0;
}
template <std::size_t Bits, class Tag>
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline bool operator>= (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
return compare (a, b) >= 0;
}
template <std::size_t Bits, class Tag>
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline bool operator== (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
return compare (a, b) == 0;
}
template <std::size_t Bits, class Tag>
template <std::size_t Bits, class Tag, class OtherTag,
class = std::enable_if_t <is_tag_compatible<Tag, OtherTag>::value>
>
inline bool operator!= (
base_uint<Bits, Tag> const& a, base_uint<Bits, Tag> const& b)
base_uint<Bits, Tag> const& a, base_uint<Bits, OtherTag> const& b)
{
return compare (a, b) != 0;
}
@@ -517,6 +552,6 @@ inline std::ostream& operator<< (
return out << to_string (u);
}
}
} // rippled
#endif