From 7380da43ab403dacb41d2010093d11942bb7cec1 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 21 May 2007 20:42:05 +0000 Subject: Many changes. --- src/balpair.h | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) (limited to 'src/balpair.h') diff --git a/src/balpair.h b/src/balpair.h index 1198a82f..13e4857b 100644 --- a/src/balpair.h +++ b/src/balpair.h @@ -167,21 +167,50 @@ public: return *this = balance_t(str); } - // in-place arithmetic + /** + * Binary arithmetic operators. Balances support addition and + * subtraction of other balance pairs, balances or amounts, but + * multiplication and division are restricted to uncommoditized + * amounts only. + */ balance_pair_t& operator+=(const balance_pair_t& bal_pair) { - if (bal_pair.cost && ! cost) - cost = quantity; - quantity += bal_pair.quantity; - if (cost) - *cost += bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + balance_t::operator+=(bal_pair); + if (bal_pair.cost) { + if (! cost) + *cost = quantity(); + *cost += *bal_pair.cost; + } return *this; } balance_pair_t& operator-=(const balance_pair_t& bal_pair) { - if (bal_pair.cost && ! cost) - cost = quantity; - quantity -= bal_pair.quantity; + balance_t::operator+=(bal_pair); + if (bal_pair.cost) { + if (! cost) + *cost = quantity(); + *cost += *bal_pair.cost; + } + return *this; + } + + balance_pair_t& operator*=(const amount_t& amt) { + balance_t::operator*=(amt); + if (cost) + *cost *= amt; + return *this; + } + balance_pair_t& operator*=(const double val) { + return *this *= amount_t(val); + } + balance_pair_t& operator*=(const unsigned long val) { + return *this *= amount_t(val); + } + balance_pair_t& operator*=(const long val) { + return *this *= amount_t(val); + } + balance_pair_t& operator/=(const amount_t& amt) { + balance_t::operator/=(amt); if (cost) - *cost -= bal_pair.cost ? *bal_pair.cost : bal_pair.quantity; + *cost /= amt; return *this; } -- cgit v1.2.3