diff options
author | John Wiegley <johnw@newartisans.com> | 2006-03-19 23:16:31 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 05:48:45 -0400 |
commit | f60717d3f4bd3f3c04e08edbe459495d00936d0c (patch) | |
tree | b573b4e23dbcec06d7f4b79835127a2d64db5db7 | |
parent | 7adb262823377bc40085ba5b66a6448a3d58db0b (diff) | |
download | fork-ledger-f60717d3f4bd3f3c04e08edbe459495d00936d0c.tar.gz fork-ledger-f60717d3f4bd3f3c04e08edbe459495d00936d0c.tar.bz2 fork-ledger-f60717d3f4bd3f3c04e08edbe459495d00936d0c.zip |
*** no comment ***
-rw-r--r-- | amount.cc | 73 | ||||
-rw-r--r-- | amount.h | 24 |
2 files changed, 57 insertions, 40 deletions
@@ -473,39 +473,50 @@ int amount_t::sign() const return quantity ? mpz_sgn(MPZ(quantity)) : 0; } -// comparisons between amounts -#define AMOUNT_CMP_AMOUNT(OP) \ -bool amount_t::operator OP(const amount_t& amt) const \ -{ \ - if (! quantity) \ - return 0 OP amt; \ - if (! amt.quantity) \ - return *this OP 0; \ - \ - if (commodity() && amt.commodity() && \ - commodity() != amt.commodity()) \ - return false; \ - \ - if (quantity->prec == amt.quantity->prec) { \ - return mpz_cmp(MPZ(quantity), MPZ(amt.quantity)) OP 0; \ - } \ - else if (quantity->prec < amt.quantity->prec) { \ - amount_t temp = *this; \ - temp._resize(amt.quantity->prec); \ - return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity)) OP 0; \ - } \ - else { \ - amount_t temp = amt; \ - temp._resize(quantity->prec); \ - return mpz_cmp(MPZ(quantity), MPZ(temp.quantity)) OP 0; \ - } \ +int amount_t::compare(const amount_t& amt) const +{ + if (! quantity) { + if (! amt.quantity) + return 0; + return - amt.sign(); + } + if (! amt.quantity) + return sign(); + + if (commodity() && amt.commodity() && + commodity() != amt.commodity()) + throw new amount_error + (std::string("Cannot compare amounts with different commodities: ") + + commodity().symbol + " and " + amt.commodity().symbol); + + if (quantity->prec == amt.quantity->prec) { + return mpz_cmp(MPZ(quantity), MPZ(amt.quantity)); + } + else if (quantity->prec < amt.quantity->prec) { + amount_t temp = *this; + temp._resize(amt.quantity->prec); + return mpz_cmp(MPZ(temp.quantity), MPZ(amt.quantity)); + } + else { + amount_t temp = amt; + temp._resize(quantity->prec); + return mpz_cmp(MPZ(quantity), MPZ(temp.quantity)); + } +} + +bool amount_t::operator==(const amount_t& amt) const +{ + if (commodity() != amt.commodity()) + return false; + return compare(amt) == 0; } -AMOUNT_CMP_AMOUNT(<) -AMOUNT_CMP_AMOUNT(<=) -AMOUNT_CMP_AMOUNT(>) -AMOUNT_CMP_AMOUNT(>=) -AMOUNT_CMP_AMOUNT(==) +bool amount_t::operator!=(const amount_t& amt) const +{ + if (commodity() != amt.commodity()) + return true; + return compare(amt) != 0; +} amount_t::operator bool() const { @@ -181,16 +181,22 @@ class amount_t operator double() const; // comparisons between amounts - 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; - bool operator==(const amount_t& amt) const; - bool operator!=(const amount_t& amt) const { - if (commodity_ != amt.commodity_) - return true; - return ! (*this == amt); + int compare(const amount_t& amt) const; + + 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 { + return compare(amt) >= 0; + } + bool operator==(const amount_t& amt) const; + bool operator!=(const amount_t& amt) const; template <typename T> void parse_num(T num) { |