From f9b833b685251e0787a8282a256588328bd68ad7 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Mon, 2 Nov 2009 04:40:51 -0500 Subject: Corrected balance_t's arithmetic interface --- src/balance.h | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'src/balance.h') diff --git a/src/balance.h b/src/balance.h index 5452510b..ad340aa5 100644 --- a/src/balance.h +++ b/src/balance.h @@ -207,7 +207,7 @@ public: template bool operator==(const T& val) const { - return *this == balance_t(val); + return *this == amount_t(val); } /** @@ -217,11 +217,29 @@ public: */ balance_t& operator+=(const balance_t& bal); balance_t& operator+=(const amount_t& amt); + balance_t& operator+=(const double val) { + return *this += amount_t(val); + } + balance_t& operator+=(const unsigned long val) { + return *this += amount_t(val); + } + balance_t& operator+=(const long val) { + return *this += amount_t(val); + } + balance_t& operator-=(const balance_t& bal); balance_t& operator-=(const amount_t& amt); + balance_t& operator-=(const double val) { + return *this -= amount_t(val); + } + balance_t& operator-=(const unsigned long val) { + return *this -= amount_t(val); + } + balance_t& operator-=(const long val) { + return *this -= amount_t(val); + } balance_t& operator*=(const amount_t& amt); - balance_t& operator*=(const double val) { return *this *= amount_t(val); } @@ -233,7 +251,6 @@ public: } balance_t& operator/=(const amount_t& amt); - balance_t& operator/=(const double val) { return *this /= amount_t(val); } @@ -278,7 +295,7 @@ public: * in_place_reduce() * in_place_unreduce() */ - balance_t negate() const { + balance_t negated() const { balance_t temp(*this); temp.in_place_negate(); return temp; @@ -288,7 +305,7 @@ public: pair.second.in_place_negate(); } balance_t operator-() const { - return negate(); + return negated(); } balance_t abs() const { @@ -388,6 +405,13 @@ public: * it. */ operator bool() const { + return is_nonzero(); + } + + bool is_nonzero() const { + if (is_empty()) + return false; + foreach (const amounts_map::value_type& pair, amounts) if (pair.second.is_nonzero()) return true; @@ -433,6 +457,7 @@ public: else throw_(balance_error, _("Cannot convert a balance with multiple commodities to an amount")); + return amount_t(); } /** -- cgit v1.2.3