*** no comment ***
This commit is contained in:
parent
72e8a366a4
commit
6d529efa63
3 changed files with 68 additions and 23 deletions
60
balance.cc
60
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
|
} // namespace ledger
|
||||||
|
|
|
||||||
25
balance.h
25
balance.h
|
|
@ -143,11 +143,7 @@ class balance_t
|
||||||
}
|
}
|
||||||
|
|
||||||
// multiplication and divide
|
// multiplication and divide
|
||||||
balance_t& operator*=(const balance_t& bal) {
|
balance_t& operator*=(const balance_t& bal);
|
||||||
if (amounts.size() == 1 && bal.amounts.size() == 1)
|
|
||||||
return *this *= (*bal.amounts.begin()).second;
|
|
||||||
throw amount_error("It makes no sense to multiply two balances");
|
|
||||||
}
|
|
||||||
balance_t& operator*=(const amount_t& amt) {
|
balance_t& operator*=(const amount_t& amt) {
|
||||||
// Multiplying by the null commodity causes all amounts to be
|
// Multiplying by the null commodity causes all amounts to be
|
||||||
// increased by the same factor.
|
// increased by the same factor.
|
||||||
|
|
@ -172,11 +168,7 @@ class balance_t
|
||||||
return *this *= amount_t(val);
|
return *this *= amount_t(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
balance_t& operator/=(const balance_t& bal) {
|
balance_t& operator/=(const balance_t& bal);
|
||||||
if (amounts.size() == 1 && bal.amounts.size() == 1)
|
|
||||||
return *this /= (*bal.amounts.begin()).second;
|
|
||||||
throw amount_error("It makes no sense to divide two balances");
|
|
||||||
}
|
|
||||||
balance_t& operator/=(const amount_t& amt) {
|
balance_t& operator/=(const amount_t& amt) {
|
||||||
// Dividing by the null commodity causes all amounts to be
|
// Dividing by the null commodity causes all amounts to be
|
||||||
// increased by the same factor.
|
// increased by the same factor.
|
||||||
|
|
@ -406,15 +398,7 @@ class balance_t
|
||||||
}
|
}
|
||||||
|
|
||||||
// conversion operators
|
// conversion operators
|
||||||
operator amount_t() const {
|
operator amount_t() const;
|
||||||
if (amounts.size() == 1)
|
|
||||||
return (*amounts.begin()).second;
|
|
||||||
else if (amounts.size() == 0)
|
|
||||||
return amount_t();
|
|
||||||
else
|
|
||||||
throw amount_error("Cannot convert a balance with "
|
|
||||||
"multiple commodities to an amount");
|
|
||||||
}
|
|
||||||
operator bool() const {
|
operator bool() const {
|
||||||
for (amounts_map::const_iterator i = amounts.begin();
|
for (amounts_map::const_iterator i = amounts.begin();
|
||||||
i != amounts.end();
|
i != amounts.end();
|
||||||
|
|
@ -427,8 +411,7 @@ class balance_t
|
||||||
amount_t amount(const commodity_t& commodity) const;
|
amount_t amount(const commodity_t& commodity) const;
|
||||||
balance_t value(const std::time_t moment) const;
|
balance_t value(const std::time_t moment) const;
|
||||||
|
|
||||||
void write(std::ostream& out,
|
void write(std::ostream& out, const int first_width,
|
||||||
const int first_width,
|
|
||||||
const int latter_width = -1) const;
|
const int latter_width = -1) const;
|
||||||
|
|
||||||
void abs() {
|
void abs() {
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,8 @@ config_t::regexps_to_predicate(const std::string& command,
|
||||||
display_predicate += ")/";
|
display_predicate += ")/";
|
||||||
}
|
}
|
||||||
else if (! show_empty) {
|
else if (! show_empty) {
|
||||||
|
if (! display_predicate.empty())
|
||||||
|
display_predicate += "&";
|
||||||
display_predicate += "T";
|
display_predicate += "T";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue