I apologize for the disaster that was toUInt64. It has been nuked from orbit.

This commit is contained in:
JoelKatz
2012-10-12 18:24:32 -07:00
parent 19b518a0af
commit bfee7a3082
2 changed files with 28 additions and 19 deletions

View File

@@ -378,7 +378,7 @@ bool STAmount::setFullValue(const std::string& sAmount, const std::string& sCurr
void STAmount::canonicalize()
{
if (!mCurrency)
if (mCurrency.isZero())
{ // native currency amounts should always have an offset of zero
mIsNative = true;
@@ -401,7 +401,11 @@ void STAmount::canonicalize()
--mOffset;
}
assert(mValue <= cMaxNative);
if (mValue > cMaxNative)
{
assert(false);
throw std::runtime_error("Native currency amount out of range");
}
return;
}
@@ -430,9 +434,9 @@ void STAmount::canonicalize()
mValue /= 10;
++mOffset;
}
assert((mValue == 0) || ((mValue >= cMinValue) && (mValue <= cMaxValue)) );
assert((mValue == 0) || ((mOffset >= cMinOffset) && (mOffset <= cMaxOffset)) );
assert((mValue != 0) || (mOffset != -100) );
assert((mValue == 0) || ((mValue >= cMinValue) && (mValue <= cMaxValue)));
assert((mValue == 0) || ((mOffset >= cMinOffset) && (mOffset <= cMaxOffset)));
assert((mValue != 0) || (mOffset != -100));
}
void STAmount::add(Serializer& s) const
@@ -476,15 +480,20 @@ void STAmount::setValue(const STAmount &a)
mIsNegative = a.mIsNegative;
}
uint64 STAmount::toUInt64() const
{ // makes them sort easily
if (mIsNative)
return mValue;
if (mValue == 0)
return 0x4000000000000000ull;
if (mIsNegative)
return ((cMaxNative + 1) - mValue) | (static_cast<uint64>(mOffset + 97) << (64 - 10));
return mValue | (static_cast<uint64>(mOffset + 256 + 97) << (64 - 10));
int STAmount::compare(const STAmount& a) const
{ // Compares the value of a to the value of this STAmount, amounts must be comparable
if (mIsNegative != a.mIsNegative) return mIsNegative ? -1 : 1;
if (!mValue) return a.mValue ? 1 : 0;
if (!a.mValue) return 1;
if (mOffset > a.mOffset) return mIsNegative ? 1 : -1;
if (mOffset < a.mOffset) return mIsNegative ? -1 : 1;
if (mValue > a.mValue) return mIsNegative ? 1 : -1;
if (mValue < a.mValue) return mIsNegative ? -1 : 1;
return 0;
}
STAmount* STAmount::construct(SerializerIterator& sit, SField::ref name)
@@ -633,25 +642,25 @@ bool STAmount::operator!=(const STAmount& a) const
bool STAmount::operator<(const STAmount& a) const
{
throwComparable(a);
return toUInt64() < a.toUInt64();
return compare(a) < 0;
}
bool STAmount::operator>(const STAmount& a) const
{
throwComparable(a);
return toUInt64() > a.toUInt64();
return compare(a) > 0;
}
bool STAmount::operator<=(const STAmount& a) const
{
throwComparable(a);
return toUInt64() <= a.toUInt64();
return compare(a) <= 0;
}
bool STAmount::operator>=(const STAmount& a) const
{
throwComparable(a);
return toUInt64() >= a.toUInt64();
return compare(a) >= 0;
}
STAmount& STAmount::operator+=(const STAmount& a)

View File

@@ -224,7 +224,6 @@ protected:
: SerializedType(name), mCurrency(cur), mIssuer(iss), mValue(val), mOffset(off),
mIsNative(isNat), mIsNegative(isNeg) { ; }
uint64 toUInt64() const;
static uint64 muldiv(uint64, uint64, uint64);
public:
@@ -287,6 +286,7 @@ public:
void negate() { if (!isZero()) mIsNegative = !mIsNegative; }
void zero() { mOffset = mIsNative ? -100 : 0; mValue = 0; mIsNegative = false; }
int compare(const STAmount&) const;
const uint160& getIssuer() const { return mIssuer; }
void setIssuer(const uint160& uIssuer) { mIssuer = uIssuer; }