summaryrefslogtreecommitdiff
path: root/balance.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2006-02-28 10:24:21 +0000
committerJohn Wiegley <johnw@newartisans.com>2008-04-13 05:48:43 -0400
commit6d529efa63f5f2020b126c8476fe0ea03516869d (patch)
treec8699424992378dbd5e787f5ffc9ea5942edaa17 /balance.cc
parent72e8a366a49451bd46a10d0b04c16b9ff5aba354 (diff)
downloadfork-ledger-6d529efa63f5f2020b126c8476fe0ea03516869d.tar.gz
fork-ledger-6d529efa63f5f2020b126c8476fe0ea03516869d.tar.bz2
fork-ledger-6d529efa63f5f2020b126c8476fe0ea03516869d.zip
*** no comment ***
Diffstat (limited to 'balance.cc')
-rw-r--r--balance.cc60
1 files changed, 60 insertions, 0 deletions
diff --git a/balance.cc b/balance.cc
index 6305eb95..604d9d82 100644
--- a/balance.cc
+++ b/balance.cc
@@ -85,4 +85,64 @@ 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());
+ }
+}
+
} // namespace ledger