If doing math between amount and balance, simplify

This commit is contained in:
John Wiegley 2009-10-27 08:30:15 -04:00
parent 63ae4531b8
commit 3ea7fbd5f6
2 changed files with 28 additions and 3 deletions

View file

@ -417,6 +417,9 @@ public:
bool is_empty() const { bool is_empty() const {
return amounts.size() == 0; return amounts.size() == 0;
} }
bool single_amount() const {
return amounts.size() == 1;
}
/** /**
* Conversion methods. A balance can be converted to an amount, but * Conversion methods. A balance can be converted to an amount, but

View file

@ -263,7 +263,7 @@ void value_t::in_place_simplify()
return; return;
} }
if (is_balance() && as_balance().amounts.size() == 1) { if (is_balance() && as_balance().single_amount()) {
DEBUG_("Reducing balance to amount"); DEBUG_("Reducing balance to amount");
DEBUG_("as a balance it looks like: " << *this); DEBUG_("as a balance it looks like: " << *this);
in_place_cast(AMOUNT); in_place_cast(AMOUNT);
@ -587,6 +587,12 @@ value_t& value_t::operator*=(const value_t& val)
case AMOUNT: case AMOUNT:
as_amount_lval() *= val.as_amount(); as_amount_lval() *= val.as_amount();
return *this; return *this;
case BALANCE:
if (val.as_balance().single_amount()) {
as_amount_lval() *= val.simplified().as_amount();
return *this;
}
break;
default: default:
break; break;
} }
@ -598,7 +604,12 @@ value_t& value_t::operator*=(const value_t& val)
as_balance_lval() *= val.as_long(); as_balance_lval() *= val.as_long();
return *this; return *this;
case AMOUNT: case AMOUNT:
if (! val.as_amount().has_commodity()) { if (as_balance().single_amount()) {
in_place_simplify();
as_amount_lval() *= val.as_amount();
return *this;
}
else if (! val.as_amount().has_commodity()) {
as_balance_lval() *= val.as_amount(); as_balance_lval() *= val.as_amount();
return *this; return *this;
} }
@ -645,6 +656,12 @@ value_t& value_t::operator/=(const value_t& val)
case AMOUNT: case AMOUNT:
as_amount_lval() /= val.as_amount(); as_amount_lval() /= val.as_amount();
return *this; return *this;
case BALANCE:
if (val.as_balance().single_amount()) {
as_amount_lval() /= val.simplified().as_amount();
return *this;
}
break;
default: default:
break; break;
} }
@ -656,7 +673,12 @@ value_t& value_t::operator/=(const value_t& val)
as_balance_lval() /= val.as_long(); as_balance_lval() /= val.as_long();
return *this; return *this;
case AMOUNT: case AMOUNT:
if (! val.as_amount().has_commodity()) { if (as_balance().single_amount()) {
in_place_simplify();
as_amount_lval() /= val.as_amount();
return *this;
}
else if (! val.as_amount().has_commodity()) {
as_balance_lval() /= val.as_amount(); as_balance_lval() /= val.as_amount();
return *this; return *this;
} }