*** no comment ***

This commit is contained in:
John Wiegley 2006-03-19 23:16:31 +00:00
parent 7adb262823
commit f60717d3f4
2 changed files with 57 additions and 40 deletions

View file

@ -473,39 +473,50 @@ int amount_t::sign() const
return quantity ? mpz_sgn(MPZ(quantity)) : 0; return quantity ? mpz_sgn(MPZ(quantity)) : 0;
} }
// comparisons between amounts int amount_t::compare(const amount_t& amt) const
#define AMOUNT_CMP_AMOUNT(OP) \ {
bool amount_t::operator OP(const amount_t& amt) const \ if (! quantity) {
{ \ if (! amt.quantity)
if (! quantity) \ return 0;
return 0 OP amt; \ return - amt.sign();
if (! amt.quantity) \ }
return *this OP 0; \ if (! amt.quantity)
\ return sign();
if (commodity() && amt.commodity() && \
commodity() != amt.commodity()) \ if (commodity() && amt.commodity() &&
return false; \ commodity() != amt.commodity())
\ throw new amount_error
if (quantity->prec == amt.quantity->prec) { \ (std::string("Cannot compare amounts with different commodities: ") +
return mpz_cmp(MPZ(quantity), MPZ(amt.quantity)) OP 0; \ commodity().symbol + " and " + amt.commodity().symbol);
} \
else if (quantity->prec < amt.quantity->prec) { \ if (quantity->prec == amt.quantity->prec) {
amount_t temp = *this; \ return mpz_cmp(MPZ(quantity), MPZ(amt.quantity));
temp._resize(amt.quantity->prec); \ }
return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity)) OP 0; \ else if (quantity->prec < amt.quantity->prec) {
} \ amount_t temp = *this;
else { \ temp._resize(amt.quantity->prec);
amount_t temp = amt; \ return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity));
temp._resize(quantity->prec); \ }
return mpz_cmp(MPZ(quantity), MPZ(temp.quantity)) OP 0; \ else {
} \ amount_t temp = amt;
temp._resize(quantity->prec);
return mpz_cmp(MPZ(quantity), MPZ(temp.quantity));
}
} }
AMOUNT_CMP_AMOUNT(<) bool amount_t::operator==(const amount_t& amt) const
AMOUNT_CMP_AMOUNT(<=) {
AMOUNT_CMP_AMOUNT(>) if (commodity() != amt.commodity())
AMOUNT_CMP_AMOUNT(>=) return false;
AMOUNT_CMP_AMOUNT(==) return compare(amt) == 0;
}
bool amount_t::operator!=(const amount_t& amt) const
{
if (commodity() != amt.commodity())
return true;
return compare(amt) != 0;
}
amount_t::operator bool() const amount_t::operator bool() const
{ {

View file

@ -181,16 +181,22 @@ class amount_t
operator double() const; operator double() const;
// comparisons between amounts // comparisons between amounts
bool operator<(const amount_t& amt) const; int compare(const amount_t& amt) const;
bool operator<=(const amount_t& amt) const;
bool operator>(const amount_t& amt) const; bool operator<(const amount_t& amt) const {
bool operator>=(const amount_t& amt) const; return compare(amt) < 0;
bool operator==(const amount_t& amt) const;
bool operator!=(const amount_t& amt) const {
if (commodity_ != amt.commodity_)
return true;
return ! (*this == amt);
} }
bool operator<=(const amount_t& amt) const {
return compare(amt) <= 0;
}
bool operator>(const amount_t& amt) const {
return compare(amt) > 0;
}
bool operator>=(const amount_t& amt) const {
return compare(amt) >= 0;
}
bool operator==(const amount_t& amt) const;
bool operator!=(const amount_t& amt) const;
template <typename T> template <typename T>
void parse_num(T num) { void parse_num(T num) {