diff options
author | John Wiegley <johnw@newartisans.com> | 2006-02-28 04:04:43 +0000 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2008-04-13 02:41:27 -0400 |
commit | 73b9d060c0c295d162e8f8812519e2b55d3cf129 (patch) | |
tree | 3aa5d9e0ed450c34aecd56261682f569eb6b1cab /balance.cc | |
parent | a2423f99db3eb80c5fdb7208f90a3933e3716112 (diff) | |
download | fork-ledger-73b9d060c0c295d162e8f8812519e2b55d3cf129.tar.gz fork-ledger-73b9d060c0c295d162e8f8812519e2b55d3cf129.tar.bz2 fork-ledger-73b9d060c0c295d162e8f8812519e2b55d3cf129.zip |
*** empty log message ***
Diffstat (limited to 'balance.cc')
-rw-r--r-- | balance.cc | 73 |
1 files changed, 73 insertions, 0 deletions
@@ -97,4 +97,77 @@ void balance_t::write(std::ostream& out, } } +balance_t& balance_t::operator*=(const balance_t& bal) +{ + if (! *this || ! bal) + return (*this = 0L); + else if (amounts.size() == 1 && bal.amounts.size() == 1) + return *this *= (*bal.amounts.begin()).second; + else { + std::string msg; + std::ostringstream errmsg(msg); + errmsg << "It makes no sense to multiply two balances: " + << *this << " * " << bal; + throw amount_error(errmsg.str()); + } +} + +balance_t& balance_t::operator/=(const balance_t& bal) +{ + if (! *this) { + return (*this = 0L); + } + else if (! bal) { + std::string msg; + std::ostringstream errmsg(msg); + errmsg << "Attempt to divide by zero: " << *this << " / " << bal; + throw amount_error(errmsg.str()); + } + else if (amounts.size() == 1 && bal.amounts.size() == 1) { + return *this /= (*bal.amounts.begin()).second; + } + else if (*this == bal) { + return (*this = 1L); + } + else { + std::string msg; + std::ostringstream errmsg(msg); + errmsg << "It makes no sense to divide two balances: " + << *this << " / " << bal; + throw amount_error(errmsg.str()); + } +} + +balance_t::operator amount_t() const +{ + if (amounts.size() == 1) { + return (*amounts.begin()).second; + } + else if (amounts.size() == 0) { + return amount_t(); + } + else { + std::string msg; + std::ostringstream errmsg(msg); + errmsg << "Cannot convert a balance with " + << "multiple commodities to an amount: " << *this; + throw amount_error(errmsg.str()); + } +} + +balance_pair_t& balance_pair_t::operator/=(const balance_pair_t& bal_pair) +{ + if (bal_pair.cost && ! cost) + cost = new balance_t(quantity); + quantity /= bal_pair.quantity; + if (cost) + *cost /= bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + + if (bal_pair.price && *bal_pair.price) { + if (price) + *price /= *bal_pair.price; + } + return *this; +} + } // namespace ledger |