Prefer std::array over C-style array in base_uint

This commit is contained in:
Joe Loser
2018-05-05 10:00:19 -04:00
committed by Mike Ellery
parent 08382d866b
commit 0b2f33d23a
2 changed files with 17 additions and 22 deletions

View File

@@ -31,6 +31,7 @@
#include <ripple/basics/hardened_hash.h> #include <ripple/basics/hardened_hash.h>
#include <ripple/beast/utility/Zero.h> #include <ripple/beast/utility/Zero.h>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <array>
#include <functional> #include <functional>
#include <type_traits> #include <type_traits>
@@ -51,13 +52,13 @@ class base_uint
"The length of a base_uint in bits must be at least 64."); "The length of a base_uint in bits must be at least 64.");
protected: protected:
enum { WIDTH = Bits / 32 }; static constexpr std::size_t WIDTH = Bits / 32;
// This is really big-endian in byte order. // This is really big-endian in byte order.
// We sometimes use std::uint32_t for speed. // We sometimes use std::uint32_t for speed.
// NIKB TODO: migrate to std::array using array_type = std::array<std::uint32_t, WIDTH>;
std::uint32_t pn[WIDTH]; array_type pn;
public: public:
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
@@ -65,7 +66,8 @@ public:
// STL Container Interface // STL Container Interface
// //
static std::size_t const bytes = Bits / 8; static std::size_t constexpr bytes = Bits / 8;
static_assert(sizeof(array_type) == bytes, "");
using size_type = std::size_t; using size_type = std::size_t;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
@@ -80,8 +82,8 @@ public:
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using tag_type = Tag; using tag_type = Tag;
pointer data() { return reinterpret_cast<pointer>(pn); } pointer data() { return reinterpret_cast<pointer>(pn.data ()); }
const_pointer data() const { return reinterpret_cast<const_pointer>(pn); } const_pointer data() const { return reinterpret_cast<const_pointer>(pn.data ()); }
iterator begin() { return data(); } iterator begin() { return data(); }
iterator end() { return data()+bytes; } iterator end() { return data()+bytes; }
@@ -124,7 +126,7 @@ private:
explicit base_uint (void const* data, VoidHelper) explicit base_uint (void const* data, VoidHelper)
{ {
memcpy (&pn [0], data, bytes); memcpy (pn.data (), data, bytes);
} }
public: public:
@@ -143,7 +145,7 @@ public:
assert (vch.size () == size ()); assert (vch.size () == size ());
if (vch.size () == size ()) if (vch.size () == size ())
memcpy (pn, &vch[0], size ()); memcpy (pn.data (), vch.data (), size ());
else else
*this = beast::zero; *this = beast::zero;
} }
@@ -153,12 +155,10 @@ public:
*this = b; *this = b;
} }
base_uint (base_uint<Bits, Tag> const& other) = default;
template <class OtherTag> template <class OtherTag>
void copyFrom (base_uint<Bits, OtherTag> const& other) void copyFrom (base_uint<Bits, OtherTag> const& other)
{ {
memcpy (&pn [0], other.data(), bytes); memcpy (pn.data (), other.data(), bytes);
} }
/* Construct from a raw pointer. /* Construct from a raw pointer.
@@ -194,14 +194,6 @@ public:
return ret; return ret;
} }
base_uint& operator= (const base_uint& b)
{
for (int i = 0; i < WIDTH; i++)
pn[i] = b.pn[i];
return *this;
}
base_uint& operator= (std::uint64_t uHost) base_uint& operator= (std::uint64_t uHost)
{ {
*this = beast::zero; *this = beast::zero;
@@ -314,7 +306,7 @@ public:
Hasher& h, base_uint const& a) noexcept Hasher& h, base_uint const& a) noexcept
{ {
// Do not allow any endian transformations on this memory // Do not allow any endian transformations on this memory
h(a.pn, sizeof(a.pn)); h(a.pn.data (), sizeof(a.pn));
} }
/** Parse a hex string into a base_uint /** Parse a hex string into a base_uint
@@ -325,7 +317,6 @@ public:
{ {
unsigned char* pOut = begin (); unsigned char* pOut = begin ();
assert(sizeof(pn) == bytes);
for (int i = 0; i < sizeof (pn); ++i) for (int i = 0; i < sizeof (pn); ++i)
{ {
auto hi = charUnHex(*psz++); auto hi = charUnHex(*psz++);
@@ -419,7 +410,7 @@ public:
base_uint<Bits, Tag>& operator=(Zero) base_uint<Bits, Tag>& operator=(Zero)
{ {
memset (&pn[0], 0, sizeof (pn)); pn.fill(0);
return *this; return *this;
} }

View File

@@ -22,6 +22,8 @@
#include <ripple/beast/unit_test.h> #include <ripple/beast/unit_test.h>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <type_traits>
namespace ripple { namespace ripple {
namespace test { namespace test {
@@ -50,6 +52,8 @@ struct nonhash
struct base_uint_test : beast::unit_test::suite struct base_uint_test : beast::unit_test::suite
{ {
using test96 = base_uint<96>; using test96 = base_uint<96>;
static_assert(std::is_copy_constructible<test96>::value, "");
static_assert(std::is_copy_assignable<test96>::value, "");
void run() override void run() override
{ {