From ab4198a50f7fe476d9d1c7d56fb09669e7839bf5 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 27 Mar 2020 18:20:44 -0700 Subject: Look up annotated amounts within balances by their displayed name --- src/balance.cc | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'src/balance.cc') diff --git a/src/balance.cc b/src/balance.cc index f0065dc5..0a2a3fec 100644 --- a/src/balance.cc +++ b/src/balance.cc @@ -76,7 +76,9 @@ balance_t& balance_t::operator+=(const amount_t& amt) if (amt.is_realzero()) return *this; - amounts_map::iterator i = amounts.find(&amt.commodity()); + amounts_map::iterator i = + amt.commodity().has_annotation() ? + find_by_name(amt.commodity()) : amounts.find(&amt.commodity()); if (i != amounts.end()) i->second += amt; else @@ -101,7 +103,9 @@ balance_t& balance_t::operator-=(const amount_t& amt) if (amt.is_realzero()) return *this; - amounts_map::iterator i = amounts.find(&amt.commodity()); + amounts_map::iterator i = + amt.commodity().has_annotation() ? + find_by_name(amt.commodity()) : amounts.find(&amt.commodity()); if (i != amounts.end()) { i->second -= amt; if (i->second.is_realzero()) @@ -202,6 +206,30 @@ balance_t::value(const datetime_t& moment, return resolved ? temp : optional(); } +balance_t::amounts_map::iterator +balance_t::find_by_name(const commodity_t& comm) +{ + for (amounts_map::iterator i = amounts.begin(); + i != amounts.end(); + i++) { + if (*(*i).first == comm) + return i; + } + return amounts.end(); +} + +balance_t::amounts_map::const_iterator +balance_t::find_by_name(const commodity_t& comm) const +{ + for (amounts_map::const_iterator i = amounts.begin(); + i != amounts.end(); + i++) { + if (*(*i).first == comm) + return i; + } + return amounts.end(); +} + optional balance_t::commodity_amount(const optional& commodity) const { @@ -222,6 +250,8 @@ balance_t::commodity_amount(const optional& commodity) const } else if (amounts.size() > 0) { amounts_map::const_iterator i = + commodity->has_annotation() ? + find_by_name(*commodity) : amounts.find(const_cast(&*commodity)); if (i != amounts.end()) return i->second; -- cgit v1.2.3