Tidy up some STAmount and base_uint operations:

* Constrain use of arithmetic operators in STAmount
* Prevent constructor conversion of uint256 to uint128 - make intent clear
* Prevent implicit conversion of uint64_t to uint256
* Prevent implicit conversion of uint64_t to uint160
This commit is contained in:
Nik Bougalis
2014-04-28 11:22:39 -07:00
committed by Vinnie Falco
parent 6ae329f4a6
commit 912d74e805
7 changed files with 27 additions and 22 deletions

View File

@@ -31,6 +31,12 @@ namespace ripple {
class uint128 : public base_uint128
{
private:
uint128 (value_type const* ptr)
{
memcpy (pn, ptr, bytes);
}
public:
typedef base_uint128 basetype;
@@ -52,15 +58,6 @@ public:
return *this;
}
// VFALCO NOTE This looks dangerous and wouldn't be obvious at call
// sites what is being performed here.
//
explicit uint128 (const base_uint256& b)
{
for (int i = 0; i < WIDTH; i++)
pn[i] = b.pn[i];
}
explicit uint128 (Blob const& vch)
{
if (vch.size () == size ())
@@ -69,6 +66,10 @@ public:
zero ();
}
static uint128 low128(base_uint256 const& b)
{
return uint128 (b.data());
}
};
}

View File

@@ -53,7 +53,7 @@ public:
return *this;
}
uint160 (std::uint64_t b)
explicit uint160 (std::uint64_t b)
{
*this = b;
}

View File

@@ -56,7 +56,7 @@ public:
return *this;
}
uint256 (std::uint64_t b)
explicit uint256 (std::uint64_t b)
{
*this = b;
}

View File

@@ -50,6 +50,9 @@ inline int Testuint256AdHoc (std::vector<std::string> vArg);
template<unsigned int BITS>
class base_uint
{
static_assert ((BITS % 32) == 0,
"The length of a base_uint must be a multiple of 32.");
protected:
enum { WIDTH = BITS / 32 };
@@ -129,9 +132,6 @@ protected:
*/
base_uint (void const* data, FromVoid)
{
// BITS must be a multiple of 32
static_bassert ((BITS % 32) == 0);
memcpy (&pn [0], data, BITS / 8);
}
public:

View File

@@ -70,7 +70,7 @@ public:
}
virtual uint160 getUint160 ()
{
return (0);
return uint160(0);
}
//virtual bool isCurrency(){ return(false); }

View File

@@ -30,7 +30,7 @@ uint128 CKey::PassPhraseToKey (const std::string& passPhrase)
s.addRaw (passPhrase);
uint256 hash256 = s.getSHA512Half ();
uint128 ret (hash256);
uint128 ret (uint128::low128(hash256));
s.secureErase ();

View File

@@ -765,19 +765,23 @@ STAmount& STAmount::operator= (std::uint64_t v)
STAmount& STAmount::operator+= (std::uint64_t v)
{
if (mIsNative)
setSNValue (getSNValue () + static_cast<std::int64_t> (v));
else *this += STAmount (mCurrency, v);
assert (mIsNative);
if (!mIsNative)
throw std::runtime_error ("not native");
setSNValue (getSNValue () + static_cast<std::int64_t> (v));
return *this;
}
STAmount& STAmount::operator-= (std::uint64_t v)
{
if (mIsNative)
setSNValue (getSNValue () - static_cast<std::int64_t> (v));
else *this -= STAmount (mCurrency, v);
assert (mIsNative);
if (!mIsNative)
throw std::runtime_error ("not native");
setSNValue (getSNValue () - static_cast<std::int64_t> (v));
return *this;
}