From e5048ec71bf114c351c62844b7603893195df4d4 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 31 Jul 2008 17:48:29 -0400 Subject: [PATCH] Change many uses of for+iterator to use Boost.Foreach. --- account.cc | 14 ++--- balance.cc | 50 ++++++------------ balance.h | 72 +++++++++---------------- balpair.h | 36 +++++-------- cache.cc | 105 +++++++++++++++--------------------- commodity.cc | 4 +- csv.cc | 6 +-- derive.cc | 17 +++--- emacs.cc | 6 +-- entry.cc | 146 +++++++++++++++++++++------------------------------ entry.h | 4 -- format.cc | 56 ++++++++------------ hooks.h | 25 ++++----- journal.cc | 48 +++++++---------- journal.h | 6 ++- op.cc | 6 +-- option.cc | 6 +-- reconcile.cc | 16 +++--- session.cc | 6 +-- textual.cc | 49 +++++++---------- utils.cc | 42 ++++++--------- value.cc | 6 +-- walk.cc | 142 +++++++++++++++++++------------------------------ walk.h | 21 +++----- xact.cc | 6 +-- xml.cc | 52 +++++++++--------- 26 files changed, 369 insertions(+), 578 deletions(-) diff --git a/account.cc b/account.cc index 4a03e784..241ac097 100644 --- a/account.cc +++ b/account.cc @@ -37,10 +37,8 @@ account_t::~account_t() { TRACE_DTOR(account_t); - for (accounts_map::iterator i = accounts.begin(); - i != accounts.end(); - i++) - checked_delete((*i).second); + foreach (accounts_map::value_type& pair, accounts) + checked_delete(pair.second); } account_t * account_t::find_account(const string& name, @@ -130,15 +128,13 @@ bool account_t::valid() const return false; } - for (accounts_map::const_iterator i = accounts.begin(); - i != accounts.end(); - i++) { - if (this == (*i).second) { + foreach (const accounts_map::value_type& pair, accounts) { + if (this == pair.second) { DEBUG("ledger.validate", "account_t: parent refers to itself!"); return false; } - if (! (*i).second->valid()) { + if (! pair.second->valid()) { DEBUG("ledger.validate", "account_t: child not valid"); return false; } diff --git a/balance.cc b/balance.cc index c76212c1..bac8d40c 100644 --- a/balance.cc +++ b/balance.cc @@ -35,10 +35,8 @@ namespace ledger { balance_t& balance_t::operator+=(const balance_t& bal) { - for (amounts_map::const_iterator i = bal.amounts.begin(); - i != bal.amounts.end(); - i++) - *this += i->second; + foreach (const amounts_map::value_type& pair, bal.amounts) + *this += pair.second; return *this; } @@ -62,10 +60,8 @@ balance_t& balance_t::operator+=(const amount_t& amt) balance_t& balance_t::operator-=(const balance_t& bal) { - for (amounts_map::const_iterator i = bal.amounts.begin(); - i != bal.amounts.end(); - i++) - *this -= i->second; + foreach (const amounts_map::value_type& pair, bal.amounts) + *this -= pair.second; return *this; } @@ -104,10 +100,8 @@ balance_t& balance_t::operator*=(const amount_t& amt) else if (! amt.commodity()) { // Multiplying by an amount with no commodity causes all the // component amounts to be increased by the same factor. - for (amounts_map::iterator i = amounts.begin(); - i != amounts.end(); - i++) - i->second *= amt; + foreach (amounts_map::value_type& pair, amounts) + pair.second *= amt; } else if (amounts.size() == 1) { // Multiplying by a commoditized amount is only valid if the sole @@ -142,10 +136,8 @@ balance_t& balance_t::operator/=(const amount_t& amt) else if (! amt.commodity()) { // Dividing by an amount with no commodity causes all the // component amounts to be divided by the same factor. - for (amounts_map::iterator i = amounts.begin(); - i != amounts.end(); - i++) - i->second /= amt; + foreach (amounts_map::value_type& pair, amounts) + pair.second /= amt; } else if (amounts.size() == 1) { // Dividing by a commoditized amount is only valid if the sole @@ -170,10 +162,8 @@ balance_t::value(const optional& moment) const { optional temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if (optional val = i->second.value(moment)) { + foreach (const amounts_map::value_type& pair, amounts) + if (optional val = pair.second.value(moment)) { if (! temp) temp = balance_t(); *temp += *val; @@ -215,10 +205,8 @@ balance_t balance_t::strip_annotations(const bool keep_price, { balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.strip_annotations(keep_price, keep_date, keep_tag); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.strip_annotations(keep_price, keep_date, keep_tag); return temp; } @@ -236,17 +224,13 @@ void balance_t::print(std::ostream& out, typedef std::vector amounts_array; amounts_array sorted; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if (i->second) - sorted.push_back(&i->second); + foreach (const amounts_map::value_type& pair, amounts) + if (pair.second) + sorted.push_back(&pair.second); std::stable_sort(sorted.begin(), sorted.end(), compare_amount_commodities()); - for (amounts_array::const_iterator i = sorted.begin(); - i != sorted.end(); - i++) { + foreach (const amount_t * amount, sorted) { int width; if (! first) { out << std::endl; @@ -258,7 +242,7 @@ void balance_t::print(std::ostream& out, out.width(width); out.fill(' '); - out << std::right << **i; + out << std::right << *amount; } if (first) { diff --git a/balance.h b/balance.h index 0326ef13..5146937c 100644 --- a/balance.h +++ b/balance.h @@ -301,10 +301,8 @@ public: return temp; } virtual balance_t& in_place_negate() { - for (amounts_map::iterator i = amounts.begin(); - i != amounts.end(); - i++) - i->second.in_place_negate(); + foreach (amounts_map::value_type& pair, amounts) + pair.second.in_place_negate(); return *this; } balance_t operator-() const { @@ -313,35 +311,27 @@ public: balance_t abs() const { balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.abs(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.abs(); return temp; } balance_t round() const { balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.round(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.round(); return temp; } balance_t round(amount_t::precision_t prec) const { balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.round(prec); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.round(prec); return temp; } balance_t unround() const { balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.unround(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.unround(); return temp; } @@ -354,10 +344,8 @@ public: // A temporary must be used here because reduction may cause // multiple component amounts to collapse to the same commodity. balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.reduce(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.reduce(); return *this = temp; } @@ -370,10 +358,8 @@ public: // A temporary must be used here because unreduction may cause // multiple component amounts to collapse to the same commodity. balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.unreduce(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.unreduce(); return *this = temp; } @@ -398,10 +384,8 @@ public: * it. */ operator bool() const { - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if (i->second.is_nonzero()) + foreach (const amounts_map::value_type& pair, amounts) + if (pair.second.is_nonzero()) return true; return false; } @@ -410,10 +394,8 @@ public: if (is_empty()) return true; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if (! i->second.is_zero()) + foreach (const amounts_map::value_type& pair, amounts) + if (! pair.second.is_zero()) return false; return true; } @@ -422,10 +404,8 @@ public: if (is_empty()) return true; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if (! i->second.is_realzero()) + foreach (const amounts_map::value_type& pair, amounts) + if (! pair.second.is_realzero()) return false; return true; } @@ -518,23 +498,19 @@ public: void dump(std::ostream& out) const { out << "BALANCE("; bool first = true; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) { + foreach (const amounts_map::value_type& pair, amounts) { if (first) first = false; else out << ", "; - i->second.print(out); + pair.second.print(out); } out << ")"; } virtual bool valid() const { - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - if (! i->second.valid()) + foreach (const amounts_map::value_type& pair, amounts) + if (! pair.second.valid()) return false; return true; } diff --git a/balpair.h b/balpair.h index be6669dc..62da0810 100644 --- a/balpair.h +++ b/balpair.h @@ -246,17 +246,13 @@ public: */ balance_pair_t abs() const { balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.abs(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.abs(); if (cost) { balance_t cost_temp; - for (amounts_map::const_iterator i = cost->amounts.begin(); - i != cost->amounts.end(); - i++) - cost_temp += i->second.abs(); + foreach (const amounts_map::value_type& pair, amounts) + cost_temp += pair.second.abs(); return balance_pair_t(temp, cost_temp); } return temp; @@ -273,17 +269,13 @@ public: // A temporary must be used here because reduction may cause // multiple component amounts to collapse to the same commodity. balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.reduce(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.reduce(); if (cost) { balance_t cost_temp; - for (amounts_map::const_iterator i = cost->amounts.begin(); - i != cost->amounts.end(); - i++) - cost_temp += i->second.reduce(); + foreach (const amounts_map::value_type& pair, amounts) + cost_temp += pair.second.reduce(); return *this = balance_pair_t(temp, cost_temp); } return *this = temp; @@ -293,17 +285,13 @@ public: // A temporary must be used here because unreduction may cause // multiple component amounts to collapse to the same commodity. balance_t temp; - for (amounts_map::const_iterator i = amounts.begin(); - i != amounts.end(); - i++) - temp += i->second.unreduce(); + foreach (const amounts_map::value_type& pair, amounts) + temp += pair.second.unreduce(); if (cost) { balance_t cost_temp; - for (amounts_map::const_iterator i = cost->amounts.begin(); - i != cost->amounts.end(); - i++) - cost_temp += i->second.unreduce(); + foreach (const amounts_map::value_type& pair, amounts) + cost_temp += pair.second.unreduce(); return *this = balance_pair_t(temp, cost_temp); } return *this = temp; diff --git a/cache.cc b/cache.cc index dd29fbd7..d9ee7549 100644 --- a/cache.cc +++ b/cache.cc @@ -133,6 +133,7 @@ void read_entry_base(const char *& data, entry_base_t * entry, xact_t *& xact_pool, bool& finalize) { read_long(data, entry->src_idx); + // jww (2008-07-31): Use istream_pos_type entry->beg_pos = read_long(data); read_long(data, entry->beg_line); entry->end_pos = read_long(data); @@ -140,7 +141,7 @@ void read_entry_base(const char *& data, entry_base_t * entry, bool ignore_calculated = read_bool(data); - for (unsigned long i = 0, count = read_long(data); + for (std::size_t i = 0, count = read_long(data); i < count; i++) { new(xact_pool) xact_t; @@ -160,10 +161,8 @@ void write_entry_base(std::ostream& out, entry_base_t * entry) write_long(out, entry->end_line); bool ignore_calculated = false; - for (xacts_list::const_iterator i = entry->xacts.begin(); - i != entry->xacts.end(); - i++) - if ((*i)->amount_expr) { + foreach (transaction_t * xact, entry->xacts) + if (xact->amount_expr) { ignore_calculated = true; break; } @@ -171,10 +170,8 @@ void write_entry_base(std::ostream& out, entry_base_t * entry) write_bool(out, ignore_calculated); write_long(out, entry->xacts.size()); - for (xacts_list::const_iterator i = entry->xacts.begin(); - i != entry->xacts.end(); - i++) - write_xact(out, *i, ignore_calculated); + foreach (transaction_t * xact, entry->xacts) + write_xact(out, xact, ignore_calculated); } void read_entry(const char *& data, entry_t * entry, @@ -271,7 +268,10 @@ void read_commodity_base_extra(const char *& data, commodity_t::base_t * commodity = base_commodities[ident]; bool read_history = false; - for (unsigned long i = 0, count = read_long(data); + // jww (2008-07-31): create a function read_size which does + // read_long. Don't use read_number, but it + // wastes too much space. + for (std::size_t i = 0, count = read_long(data); i < count; i++) { datetime_t when; @@ -316,12 +316,10 @@ void write_commodity_base_extra(std::ostream& out, write_long(out, 0); } else { write_long(out, commodity->history->prices.size()); - for (commodity_t::history_map::const_iterator - i = commodity->history->prices.begin(); - i != commodity->history->prices.end(); - i++) { - write_number(out, (*i).first); - (*i).second.write(out); + foreach (commodity_t::history_map::value_type& pair, + commodity->history->prices) { + write_number(out, pair.first); + pair.second.write(out); } write_number(out, commodity->history->last_lookup); } @@ -452,8 +450,7 @@ account_t * read_account(const char *& data, account_t * master = NULL) acct = master; } - for (account_t::ident_t i = 0, - count = read_long(data); + for (std::size_t i = 0, count = read_long(data); i < count; i++) { account_t * child = read_account(data); @@ -470,10 +467,8 @@ namespace { { account_t::ident_t count = 1; - for (accounts_map::iterator i = account->accounts.begin(); - i != account->accounts.end(); - i++) - count += count_accounts((*i).second); + foreach (accounts_map::value_type& pair, account->accounts) + count += count_accounts(pair.second); return count; } @@ -494,10 +489,8 @@ void write_account(std::ostream& out, account_t * account) write_number(out, account->accounts.size()); - for (accounts_map::iterator i = account->accounts.begin(); - i != account->accounts.end(); - i++) - write_account(out, (*i).second); + foreach (accounts_map::value_type& pair, account->accounts) + write_account(out, pair.second); } unsigned int read_journal(std::istream& in, @@ -511,8 +504,7 @@ unsigned int read_journal(std::istream& in, // can be checked for changes on reading. if (! file.empty()) { - for (unsigned short i = 0, - count = read_number(in); + for (unsigned short i = 0, count = read_number(in); i < count; i++) { path pathname = read_string(in); @@ -590,12 +582,10 @@ write_journal(std::ostream& out, const journal_t& journal) write_number(out, 0); } else { write_number(out, sources.size()); - for (paths_list::const_iterator i = sources.begin(); - i != sources.end(); - i++) { - write_string(out, (*i).string()); + foreach (const path& path, sources) { + write_string(out, path.string()); struct stat info; - stat((*i).string().c_str(), &info); + stat(path.string().c_str(), &info); write_number(out, std::time_t(info.st_mtime)); } @@ -623,31 +613,25 @@ write_journal(std::ostream& out, const journal_t& journal) std::size_t this_entry_count = 0; std::size_t this_xact_count = 0; - for (entries_list::const_iterator i = entries.begin(); - i != entries.end(); - i++) { - write_entry(out, *i); + foreach (entry_t * entry, entries) { + write_entry(out, entry); this_entry_count++; - this_xact_count += (*i)->xacts.size(); + this_xact_count += entry->xacts.size(); } - for (auto_entries_list::const_iterator i = auto_entries.begin(); - i != auto_entries.end(); - i++) { - write_auto_entry(out, *i); + foreach (auto_entry_t * entry, auto_entries) { + write_auto_entry(out, entry); this_entry_count++; - this_xact_count += (*i)->xacts.size(); + this_xact_count += entry->xacts.size(); } - for (period_entries_list::const_iterator i = period_entries.begin(); - i != period_entries.end(); - i++) { - write_period_entry(out, *i); + foreach (period_entry_t * entry, period_entries) { + write_period_entry(out, entry); this_entry_count++; - this_xact_count += (*i)->xacts.size(); + this_xact_count += entry->xacts.size(); } return std::pair(this_entry_count, @@ -834,31 +818,24 @@ void write_session(std::ostream& out, session_t& session) write_number(out, session.commodity_pool->commodities.size()); write_number(out, session.commodity_pool->commodities.size()); - for (base_commodities_map::const_iterator i = - commodity_t::base_t::commodities.begin(); - i != commodity_t::base_t::commodities.end(); - i++) - write_commodity_base(out, (*i).second); + for (base_commodities_map::value_type pair, commodity_t::base_t::commodities) + write_commodity_base(out, pair.second); write_number (out, commodity_t::commodities.size()); - for (commodities_map::const_iterator i = commodity_t::commodities.begin(); - i != commodity_t::commodities.end(); - i++) { - if (! (*i).second->annotated) { + for (commodities_map::value_type pair, commodity_t::commodities) { + if (! pair.second->annotated) { write_bool(out, false); - write_commodity(out, (*i).second); + write_commodity(out, pair.second); } } - for (commodities_map::const_iterator i = commodity_t::commodities.begin(); - i != commodity_t::commodities.end(); - i++) { - if ((*i).second->annotated) { + for (commodities_map::value_type pair, commodity_t::commodities) { + if (pair.second->annotated) { write_bool(out, true); - write_string(out, (*i).first); // the mapping key - write_commodity_annotated(out, (*i).second); + write_string(out, pair.first); // the mapping key + write_commodity_annotated(out, pair.second); } } diff --git a/commodity.cc b/commodity.cc index 1850dc14..81259c8b 100644 --- a/commodity.cc +++ b/commodity.cc @@ -193,8 +193,8 @@ commodity_t::operator bool() const bool commodity_t::symbol_needs_quotes(const string& symbol) { - for (const char * p = symbol.c_str(); *p; p++) - if (std::isspace(*p) || std::isdigit(*p) || *p == '-' || *p == '.') + foreach (char ch, symbol) + if (std::isspace(ch) || std::isdigit(ch) || ch == '-' || ch == '.') return true; return false; diff --git a/csv.cc b/csv.cc index 58ffab7d..7c8a92e0 100644 --- a/csv.cc +++ b/csv.cc @@ -6,12 +6,12 @@ namespace { inline void write_escaped_string(std::ostream& out, const string& xact) { out << "\""; - for (string::const_iterator i = xact.begin(); i != xact.end(); i++) - if (*i == '"') { + foreach (char ch, xact) + if (ch == '"') { out << "\\"; out << "\""; } else { - out << *i; + out << ch; } out << "\""; } diff --git a/derive.cc b/derive.cc index bcf8f8f3..03b05619 100644 --- a/derive.cc +++ b/derive.cc @@ -96,10 +96,8 @@ entry_t * derive_new_entry(report_t& report, // to see the same xact as last time. added->code = matching->code; - for (xacts_list::iterator k = matching->xacts.begin(); - k != matching->xacts.end(); - k++) - added->add_xact(new xact_t(**k)); + foreach (xact_t * xact, matching->xacts) + added->add_xact(new xact_t(*xact)); } else if ((*i)[0] == '-' || std::isdigit((*i)[0])) { xact_t * m_xact, * xact, * first; @@ -137,13 +135,10 @@ entry_t * derive_new_entry(report_t& report, for (; j != matching->journal->entries.rend(); j++) if (regexp.match((*j)->payee)) { entry_t * entry = *j; - for (xacts_list::const_iterator x = - entry->xacts.begin(); - x != entry->xacts.end(); - x++) - if (acct_regex.match((*x)->account->fullname())) { - acct = (*x)->account; - amt = &(*x)->amount; + foreach (xact_t * xact, entry->xacts) + if (acct_regex.match(xact->account->fullname())) { + acct = xact->account; + amt = &xact->amount; matching = entry; goto found; } diff --git a/emacs.cc b/emacs.cc index afce595a..bc039290 100644 --- a/emacs.cc +++ b/emacs.cc @@ -5,11 +5,9 @@ namespace ledger { void format_emacs_xacts::write_entry(entry_t& entry) { int idx = entry.src_idx; - for (paths_list::const_iterator i = entry.journal->sources.begin(); - i != entry.journal->sources.end(); - i++) + foreach (const path& path, entry.journal->sources) if (! idx--) { - out << "\"" << *i << "\" "; + out << "\"" << path << "\" "; break; } diff --git a/entry.cc b/entry.cc index ab9ea9f0..562c6fa7 100644 --- a/entry.cc +++ b/entry.cc @@ -40,29 +40,24 @@ entry_base_t::entry_base_t(const entry_base_t& e) beg_pos(0), beg_line(0), end_pos(0), end_line(0) { TRACE_CTOR(entry_base_t, "copy"); - - for (xacts_list::const_iterator i = e.xacts.begin(); - i != e.xacts.end(); - i++) - xacts.push_back(new xact_t(**i)); + xacts.insert(xacts.end(), e.xacts.begin(), e.xacts.end()); } entry_base_t::~entry_base_t() { TRACE_DTOR(entry_base_t); - for (xacts_list::iterator i = xacts.begin(); - i != xacts.end(); - i++) + foreach (xact_t * xact, xacts) { // If the transaction is a temporary, it will be destructed when the // temporary is. If it's from a binary cache, we can safely destruct it // but its memory will be deallocated with the cache. - if (! (*i)->has_flags(XACT_TEMP)) { - if (! (*i)->has_flags(XACT_IN_CACHE)) - checked_delete(*i); + if (! xact->has_flags(XACT_TEMP)) { + if (! xact->has_flags(XACT_IN_CACHE)) + checked_delete(xact); else - (*i)->~xact_t(); + xact->~xact_t(); } + } } void entry_base_t::add_xact(xact_t * xact) @@ -85,7 +80,7 @@ bool entry_base_t::finalize() // (let ((balance 0) // null-xact) - value_t balance; + value_t balance; xact_t * null_xact = NULL; // (do-xacts (xact entry) @@ -101,11 +96,9 @@ bool entry_base_t::finalize() // (setf null-xact xact)))))) // - for (xacts_list::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->must_balance()) { - amount_t& p((*x)->cost ? *(*x)->cost : (*x)->amount); + foreach (xact_t * xact, xacts) { + if (xact->must_balance()) { + amount_t& p(xact->cost ? *xact->cost : xact->amount); if (! p.is_null()) { if (balance.is_null()) balance = p; @@ -116,7 +109,7 @@ bool entry_base_t::finalize() throw_(std::logic_error, "Only one xact with null amount allowed per entry"); else - null_xact = *x; + null_xact = xact; } } } @@ -172,16 +165,13 @@ bool entry_base_t::finalize() if (balance.is_balance()) { bool first = true; const balance_t& bal(balance.as_balance()); - for (balance_t::amounts_map::const_iterator i = bal.amounts.begin(); - i != bal.amounts.end(); - i++) { + foreach (const balance_t::amounts_map::value_type& pair, bal.amounts) { if (first) { - null_xact->amount = (*i).second.negate(); + null_xact->amount = pair.second.negate(); first = false; } else { - add_xact(new xact_t(null_xact->account, - (*i).second.negate(), - XACT_GENERATED)); + add_xact(new xact_t(null_xact->account, pair.second.negate(), + XACT_GENERATED)); } } } else { @@ -226,13 +216,11 @@ bool entry_base_t::finalize() commodity_t& comm(x.commodity()); - for (xacts_list::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) { - const amount_t& x_amt((*x)->amount); + foreach (xact_t * xact, xacts) { + const amount_t& x_amt(xact->amount); - if (! ((*x)->cost || - ! (*x)->must_balance() || + if (! (xact->cost || + ! xact->must_balance() || x_amt.commodity() == comm)) { DEBUG("ledger.journal.finalize", "before operation 1 = " << balance); balance -= x_amt; @@ -240,10 +228,10 @@ bool entry_base_t::finalize() DEBUG("ledger.journal.finalize", "x_amt = " << x_amt); DEBUG("ledger.journal.finalize", "per_unit_cost = " << per_unit_cost); - (*x)->cost = per_unit_cost * x_amt; - DEBUG("ledger.journal.finalize", "*(*x)->cost = " << *(*x)->cost); + xact->cost = per_unit_cost * x_amt; + DEBUG("ledger.journal.finalize", "*xact->cost = " << *xact->cost); - balance += *(*x)->cost; + balance += *xact->cost; DEBUG("ledger.journal.finalize", "after operation 2 = " << balance); } @@ -274,13 +262,11 @@ bool entry_base_t::finalize() // (add balance (subtract basis-cost total-cost)))) // (setf (xact-amount* xact) annotated-amount)))))) - for (xacts_list::const_iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if ((*x)->cost) { - const amount_t& x_amt((*x)->amount); + foreach (xact_t * xact, xacts) { + if (xact->cost) { + const amount_t& x_amt(xact->amount); - assert(x_amt.commodity() != (*x)->cost->commodity()); + assert(x_amt.commodity() != xact->cost->commodity()); entry_t * entry = dynamic_cast(this); @@ -290,10 +276,10 @@ bool entry_base_t::finalize() amount_t basis_cost; amount_t ann_amount = commodity_t::exchange(x_amt, final_cost, basis_cost, - (*x)->cost, none, (*x)->actual_date(), + xact->cost, none, xact->actual_date(), entry ? entry->code : optional()); - if ((*x)->amount.annotated()) { + if (xact->amount.annotated()) { if (ann_amount.annotation().price) { if (balance.is_null()) balance = basis_cost - final_cost; @@ -301,7 +287,7 @@ bool entry_base_t::finalize() balance += basis_cost - final_cost; } } else { - (*x)->amount = ann_amount; + xact->amount = ann_amount; } } } @@ -340,10 +326,8 @@ entry_t::entry_t(const entry_t& e) { TRACE_CTOR(entry_t, "copy"); - for (xacts_list::const_iterator i = xacts.begin(); - i != xacts.end(); - i++) - (*i)->entry = this; + foreach (xact_t * xact, xacts) + xact->entry = this; } bool entry_t::get_state(xact_t::state_t * state) const @@ -351,14 +335,12 @@ bool entry_t::get_state(xact_t::state_t * state) const bool first = true; bool hetero = false; - for (xacts_list::const_iterator i = xacts.begin(); - i != xacts.end(); - i++) { + foreach (xact_t * xact, xacts) { if (first) { - *state = (*i)->state; + *state = xact->state; first = false; } - else if (*state != (*i)->state) { + else if (*state != xact->state) { hetero = true; break; } @@ -416,10 +398,8 @@ bool entry_t::valid() const return false; } - for (xacts_list::const_iterator i = xacts.begin(); - i != xacts.end(); - i++) - if ((*i)->entry != this || ! (*i)->valid()) { + foreach (xact_t * xact, xacts) + if (xact->entry != this || ! xact->valid()) { DEBUG("ledger.validate", "entry_t: xact not valid"); return false; } @@ -442,47 +422,43 @@ void auto_entry_t::extend_entry(entry_base_t& entry, bool post) xacts_list initial_xacts(entry.xacts.begin(), entry.xacts.end()); - for (xacts_list::iterator i = initial_xacts.begin(); - i != initial_xacts.end(); - i++) { - if (predicate(**i)) { - for (xacts_list::iterator t = xacts.begin(); - t != xacts.end(); - t++) { + foreach (xact_t * initial_xact, initial_xacts) { + if (predicate(*initial_xact)) { + foreach (xact_t * xact, xacts) { amount_t amt; - assert((*t)->amount); - if (! (*t)->amount.commodity()) { + assert(xact->amount); + if (! xact->amount.commodity()) { if (! post) continue; - assert((*i)->amount); - amt = (*i)->amount * (*t)->amount; + assert(initial_xact->amount); + amt = initial_xact->amount * xact->amount; } else { if (post) continue; - amt = (*t)->amount; + amt = xact->amount; } - account_t * account = (*t)->account; + account_t * account = xact->account; string fullname = account->fullname(); assert(! fullname.empty()); if (fullname == "$account" || fullname == "@account") - account = (*i)->account; + account = initial_xact->account; - xact_t * xact - = new xact_t(account, amt, (*t)->flags() | XACT_AUTO); + xact_t * new_xact + = new xact_t(account, amt, xact->flags() | XACT_AUTO); // Copy over details so that the resulting xact is a mirror of // the automated entry's one. - xact->state = (*t)->state; - xact->_date = (*t)->_date; - xact->_date_eff = (*t)->_date_eff; - xact->note = (*t)->note; - xact->beg_pos = (*t)->beg_pos; - xact->beg_line = (*t)->beg_line; - xact->end_pos = (*t)->end_pos; - xact->end_line = (*t)->end_line; + new_xact->state = xact->state; + new_xact->_date = xact->_date; + new_xact->_date_eff = xact->_date_eff; + new_xact->note = xact->note; + new_xact->beg_pos = xact->beg_pos; + new_xact->beg_line = xact->beg_line; + new_xact->end_pos = xact->end_pos; + new_xact->end_line = xact->end_line; - entry.add_xact(xact); + entry.add_xact(new_xact); } } } @@ -490,10 +466,8 @@ void auto_entry_t::extend_entry(entry_base_t& entry, bool post) void extend_entry_base(journal_t * journal, entry_base_t& entry, bool post) { - for (auto_entries_list::iterator i = journal->auto_entries.begin(); - i != journal->auto_entries.end(); - i++) - (*i)->extend_entry(entry, post); + foreach (auto_entry_t * entry, journal->auto_entries) + entry->extend_entry(*entry, post); } } // namespace ledger diff --git a/entry.h b/entry.h index 16fc3bf3..a5df0ba4 100644 --- a/entry.h +++ b/entry.h @@ -225,10 +225,6 @@ public: } }; -typedef std::list entries_list; -typedef std::list auto_entries_list; -typedef std::list period_entries_list; - void extend_entry_base(journal_t * journal, entry_base_t& entry, bool post); inline bool auto_entry_finalizer_t::operator()(entry_t& entry, bool post) { diff --git a/format.cc b/format.cc index db639edd..2f457ea7 100644 --- a/format.cc +++ b/format.cc @@ -504,17 +504,15 @@ void format_t::format(std::ostream& out_str, scope_t& scope) const xact_t * first = NULL; xact_t * last = NULL; - for (xacts_list::const_iterator i - = details.entry->xacts.begin(); - i != details.entry->xacts.end(); - i++) - if (xact_has_xdata(**i) && - xact_xdata_(**i).dflags & XACT_TO_DISPLAY) { + foreach (const transaction_t * xact, details.entry->xacts) { + if (xact_has_xdata(*xact) && + xact_xdata_(*xact).dflags & XACT_TO_DISPLAY) { xacts_count++; if (! first) - first = *i; - last = *i; + first = xact; + last = xact; } + } use_disp = (xacts_count == 2 && details.xact == last && first->amount == - last->amount); @@ -534,11 +532,9 @@ void format_t::format(std::ostream& out_str, scope_t& scope) const case element_t::SOURCE: if (details.entry && details.entry->journal) { int idx = details.entry->src_idx; - for (paths_list::const_iterator i = details.entry->journal->sources.begin(); - i != details.entry->journal->sources.end(); - i++) + foreach (const path& path, details.entry->journal->sources) if (! idx--) { - out << *i; + out << path; break; } } @@ -808,18 +804,16 @@ void format_entries::format_last_entry() { #if 0 bool first = true; - for (xacts_list::const_iterator i = last_entry->xacts.begin(); - i != last_entry->xacts.end(); - i++) { - if (xact_has_xdata(**i) && - xact_xdata_(**i).dflags & XACT_TO_DISPLAY) { + foreach (const transaction_t * xact, last_entry->xacts) { + if (xact_has_xdata(*xact) && + xact_xdata_(*xact).dflags & XACT_TO_DISPLAY) { if (first) { - first_line_format.format(output_stream, details_t(**i)); + first_line_format.format(output_stream, details_t(*xact)); first = false; } else { - next_lines_format.format(output_stream, details_t(**i)); + next_lines_format.format(output_stream, details_t(*xact)); } - xact_xdata_(**i).dflags |= XACT_DISPLAYED; + xact_xdata_(*xact).dflags |= XACT_DISPLAYED; } } #endif @@ -885,13 +879,11 @@ bool disp_subaccounts_p(const account_t& account, to_show = NULL; #if 0 - for (accounts_map::const_iterator i = account.accounts.begin(); - i != account.accounts.end(); - i++) { - if (disp_pred && ! (*disp_pred)(*(*i).second)) + for (accounts_map::value_type pair, account.accounts) { + if (disp_pred && ! (*disp_pred)(*pair.second)) continue; - compute_total(result, details_t(*(*i).second)); + compute_total(result, details_t(*pair.second)); if (! computed) { compute_total(acct_total, details_t(account)); computed = true; @@ -901,7 +893,7 @@ bool disp_subaccounts_p(const account_t& account, display = matches; break; } - to_show = (*i).second; + to_show = pair.second; counted++; } #endif @@ -986,10 +978,8 @@ void format_equity::flush() else assert(false); - for (balance_t::amounts_map::const_iterator i = bal->amounts.begin(); - i != bal->amounts.end(); - i++) { - xdata.value = (*i).second; + for (balance_t::amounts_map::value_type pair, bal->amounts) { + xdata.value = pair.second; xdata.value.negate(); next_lines_format.format(output_stream, details_t(summary)); } @@ -1016,10 +1006,8 @@ void format_equity::operator()(account_t& account) else assert(false); - for (balance_t::amounts_map::const_iterator i = bal->amounts.begin(); - i != bal->amounts.end(); - i++) { - account_xdata_(account).value = (*i).second; + for (balance_t::amounts_map::value_type pair, bal->amounts) { + account_xdata_(account).value = pair.second; next_lines_format.format(output_stream, details_t(account)); } account_xdata_(account).value = val; diff --git a/hooks.h b/hooks.h index f9453be9..da197cdd 100644 --- a/hooks.h +++ b/hooks.h @@ -32,10 +32,14 @@ #ifndef _HOOKS_H #define _HOOKS_H -template +template class hooks_t : public boost::noncopyable { - std::list list; +public: + typedef boost::function function_t; + +protected: + std::list list; public: hooks_t() { @@ -45,23 +49,20 @@ public: TRACE_DTOR(hooks_t); } - void add_hook(T obj, const bool prepend = false) { + void add_hook(T * func, const bool prepend = false) { if (prepend) - list.push_front(obj); + list.push_front(func); else - list.push_back(obj); + list.push_back(func); } - void remove_hook(T obj) { - list.remove(obj); + void remove_hook(T * func) { + list.remove(func); } - template bool run_hooks(Data& item, bool post) { - for (typename std::list::const_iterator i = list.begin(); - i != list.end(); - i++) - if (! (*(*i))(item, post)) + foreach (T * func, list) + if (! (*func)(item, post)) return false; return true; } diff --git a/journal.cc b/journal.cc index 5ad9dc08..c648baf8 100644 --- a/journal.cc +++ b/journal.cc @@ -50,29 +50,23 @@ journal_t::~journal_t() // Don't bother unhooking each entry's xacts from the // accounts they refer to, because all accounts are about to // be deleted. - for (entries_list::iterator i = entries.begin(); - i != entries.end(); - i++) - if (! (*i)->has_flags(ENTRY_IN_CACHE)) - checked_delete(*i); + foreach (entry_t * entry, entries) + if (! entry->has_flags(ENTRY_IN_CACHE)) + checked_delete(entry); else - (*i)->~entry_t(); + entry->~entry_t(); - for (auto_entries_list::iterator i = auto_entries.begin(); - i != auto_entries.end(); - i++) - if (! (*i)->has_flags(ENTRY_IN_CACHE)) - checked_delete(*i); + foreach (auto_entry_t * entry, auto_entries) + if (! entry->has_flags(ENTRY_IN_CACHE)) + checked_delete(entry); else - (*i)->~auto_entry_t(); + entry->~auto_entry_t(); - for (period_entries_list::iterator i = period_entries.begin(); - i != period_entries.end(); - i++) - if (! (*i)->has_flags(ENTRY_IN_CACHE)) - checked_delete(*i); + foreach (period_entry_t * entry, period_entries) + if (! entry->has_flags(ENTRY_IN_CACHE)) + checked_delete(entry); else - (*i)->~period_entry_t(); + entry->~period_entry_t(); } void journal_t::add_account(account_t * acct) @@ -108,13 +102,11 @@ bool journal_t::add_entry(entry_t * entry) entries.push_back(entry); - for (xacts_list::const_iterator i = entry->xacts.begin(); - i != entry->xacts.end(); - i++) - if ((*i)->cost) { - assert((*i)->amount); - (*i)->amount.commodity().add_price(entry->date(), - *(*i)->cost / (*i)->amount.number()); + foreach (const xact_t * xact, entry->xacts) + if (xact->cost) { + assert(xact->amount); + xact->amount.commodity().add_price(entry->date(), + *xact->cost / xact->amount.number()); } return true; @@ -145,10 +137,8 @@ bool journal_t::valid() const return false; } - for (entries_list::const_iterator i = entries.begin(); - i != entries.end(); - i++) - if (! (*i)->valid()) { + foreach (const entry_t * entry, entries) + if (! entry->valid()) { DEBUG("ledger.validate", "journal_t: entry not valid"); return false; } diff --git a/journal.h b/journal.h index d24e4fc1..df17764d 100644 --- a/journal.h +++ b/journal.h @@ -43,6 +43,10 @@ typedef std::list paths_list; class session_t; class account_t; +typedef std::list entries_list; +typedef std::list auto_entries_list; +typedef std::list period_entries_list; + class journal_t : public noncopyable { public: @@ -56,7 +60,7 @@ public: auto_entries_list auto_entries; period_entries_list period_entries; - hooks_t entry_finalize_hooks; + hooks_t entry_finalize_hooks; journal_t(session_t * _owner); ~journal_t(); diff --git a/op.cc b/op.cc index dd808eb2..5a8c0ea9 100644 --- a/op.cc +++ b/op.cc @@ -408,10 +408,8 @@ void expr_t::op_t::compute(value_t& result, result.cast(value_t::AMOUNT); } else { value_t temp; - for (balance_t::amounts_map::const_iterator i = bal->amounts.begin(); - i != bal->amounts.end(); - i++) { - amount_t x = (*i).second; + for (balance_t::amounts_map::value_type pair, bal->amounts) { + amount_t x = pair.second; x.clear_commodity(); temp += x; } diff --git a/option.cc b/option.cc index b3673633..0581dab5 100644 --- a/option.cc +++ b/option.cc @@ -41,11 +41,11 @@ namespace { char buf[128]; std::strcpy(buf, "option_"); char * p = &buf[7]; - for (const char * q = name.c_str(); *q; q++) { - if (*q == '-') + foreach (char ch, name) { + if (ch == '-') *p++ = '_'; else - *p++ = *q; + *p++ = ch; } *p = '\0'; diff --git a/reconcile.cc b/reconcile.cc index b3f53483..12b66c8c 100644 --- a/reconcile.cc +++ b/reconcile.cc @@ -40,19 +40,17 @@ void reconcile_xacts::flush() xact_t * first = NULL; xact_t ** last_ptr = &first; - for (xacts_list::iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if (! is_valid(cutoff) || (*x)->date() < cutoff) { - switch ((*x)->state) { + foreach (xact_t * xact, xacts) { + if (! is_valid(cutoff) || xact->date() < cutoff) { + switch (xact->state) { case xact_t::CLEARED: - cleared_balance += (*x)->amount; + cleared_balance += xact->amount; break; case xact_t::UNCLEARED: case xact_t::PENDING: - pending_balance += (*x)->amount; - *last_ptr = *x; - last_ptr = xact_next_ptr(*x); + pending_balance += xact->amount; + *last_ptr = xact; + last_ptr = xact_next_ptr(xact); break; } } diff --git a/session.cc b/session.cc index 6f44cee2..407f6176 100644 --- a/session.cc +++ b/session.cc @@ -222,10 +222,8 @@ namespace { if (regexp.match(account->fullname())) return account; - for (accounts_map::iterator i = account->accounts.begin(); - i != account->accounts.end(); - i++) - if (account_t * a = find_account_re_((*i).second, regexp)) + foreach (accounts_map::value_type& pair, account->accounts) + if (account_t * a = find_account_re_(pair.second, regexp)) return a; return NULL; diff --git a/textual.cc b/textual.cc index 733a6c19..aaa96007 100644 --- a/textual.cc +++ b/textual.cc @@ -718,12 +718,9 @@ unsigned int textual_parser_t::parse(std::istream& in, account_stack.front()->find_account(p), n ? n : ""); if (! time_entries.empty()) - for (std::list::iterator i = time_entries.begin(); - i != time_entries.end(); - i++) - if (event.account == (*i).account) - throw parse_error - ("Cannot double check-in to the same account"); + foreach (time_entry_t& time_entry, time_entries) + if (event.account == time_entry.account) + throw parse_error("Cannot double check-in to the same account"); time_entries.push_back(event); break; @@ -992,15 +989,12 @@ unsigned int textual_parser_t::parse(std::istream& in, if (! time_entries.empty()) { std::list accounts; - for (std::list::iterator i = time_entries.begin(); - i != time_entries.end(); - i++) - accounts.push_back((*i).account); + foreach (time_entry_t& time_entry, time_entries) + accounts.push_back(time_entry.account); - for (std::list::iterator i = accounts.begin(); - i != accounts.end(); - i++) - clock_out_from_timelog(time_entries, current_moment, *i, NULL, journal); + foreach (account_t * account, accounts) + clock_out_from_timelog(time_entries, current_moment, account, + NULL, journal); assert(time_entries.empty()); } @@ -1035,22 +1029,18 @@ void write_textual_journal(journal_t& journal, ::realpath(pathname.string().c_str(), buf1); - for (paths_list::iterator i = journal.sources.begin(); - i != journal.sources.end(); - i++) { - ::realpath((*i).string().c_str(), buf2); + foreach (const path& path, journal.sources) { + ::realpath(path.string().c_str(), buf2); if (std::strcmp(buf1, buf2) == 0) { - found = *i; + found = path; break; } index++; } #else - for (paths_list::iterator i = journal.sources.begin(); - i != journal.sources.end(); - i++) { - if (pathname == *i) { - found = *i; + foreach (const path& path, journal.sources) { + if (pathname == path) { + found = path; break; } index++; @@ -1090,13 +1080,12 @@ void write_textual_journal(journal_t& journal, char c; if (base) { - for (xacts_list::iterator x = base->xacts.begin(); - x != base->xacts.end(); - x++) - if (! (*x)->has_flags(XACT_AUTO)) { - xact_xdata(**x).dflags |= XACT_TO_DISPLAY; - (*formatter)(**x); + foreach (xact_t * xact, base->xacts) { + if (! xact->has_flags(XACT_AUTO)) { + xact_xdata(*xact).dflags |= XACT_TO_DISPLAY; + (*formatter)(*xact); } + } formatter->flush(); while (pos < base->end_pos) { diff --git a/utils.cc b/utils.cc index 9be92c7b..9a6eb7d0 100644 --- a/utils.cc +++ b/utils.cc @@ -143,10 +143,8 @@ std::size_t current_memory_size() { std::size_t memory_size = 0; - for (object_count_map::const_iterator i = live_memory_count->begin(); - i != live_memory_count->end(); - i++) - memory_size += (*i).second.second; + foreach (const object_count_map::value_type& pair, *live_memory_count) + memory_size += pair.second.second; return memory_size; } @@ -249,12 +247,10 @@ namespace ledger { inline void report_count_map(std::ostream& out, object_count_map& the_map) { - for (object_count_map::iterator i = the_map.begin(); - i != the_map.end(); - i++) - out << " " << std::right << std::setw(12) << (*i).second.first - << " " << std::right << std::setw(7) << (*i).second.second - << " " << std::left << (*i).first + foreach (object_count_map::value_type& pair, the_map) + out << " " << std::right << std::setw(12) << pair.second.first + << " " << std::right << std::setw(7) << pair.second.second + << " " << std::left << pair.first << std::endl; } @@ -262,10 +258,8 @@ std::size_t current_objects_size() { std::size_t objects_size = 0; - for (object_count_map::const_iterator i = live_object_count->begin(); - i != live_object_count->end(); - i++) - objects_size += (*i).second.second; + foreach (const object_count_map::value_type& pair, *live_object_count) + objects_size += pair.second.second; return objects_size; } @@ -343,12 +337,10 @@ void report_memory(std::ostream& out, bool report_all) if (live_memory->size() > 0) { out << "Live memory:" << std::endl; - for (live_memory_map::const_iterator i = live_memory->begin(); - i != live_memory->end(); - i++) - out << " " << std::right << std::setw(12) << (*i).first - << " " << std::right << std::setw(7) << (*i).second.second - << " " << std::left << (*i).second.first + foreach (const live_memory_map::value_type& pair, *live_memory) + out << " " << std::right << std::setw(12) << pair.first + << " " << std::right << std::setw(7) << pair.second.second + << " " << std::left << pair.second.first << std::endl; } @@ -365,12 +357,10 @@ void report_memory(std::ostream& out, bool report_all) if (live_objects->size() > 0) { out << "Live objects:" << std::endl; - for (live_objects_map::const_iterator i = live_objects->begin(); - i != live_objects->end(); - i++) - out << " " << std::right << std::setw(12) << (*i).first - << " " << std::right << std::setw(7) << (*i).second.second - << " " << std::left << (*i).second.first + foreach (const live_objects_map::value_type& pair, *live_objects) + out << " " << std::right << std::setw(12) << pair.first + << " " << std::right << std::setw(7) << pair.second.second + << " " << std::left << pair.second.first << std::endl; } diff --git a/value.cc b/value.cc index 8edc5df1..1f8d4823 100644 --- a/value.cc +++ b/value.cc @@ -467,10 +467,8 @@ value_t& value_t::operator-=(const value_t& val) sequence_t& seq(as_sequence_lval()); if (val.is_sequence()) { - for (sequence_t::const_iterator i = val.as_sequence().begin(); - i != val.as_sequence().end(); - i++) { - sequence_t::iterator j = std::find(seq.begin(), seq.end(), *i); + foreach (const value_t& v, val.as_sequence()) { + sequence_t::iterator j = std::find(seq.begin(), seq.end(), v); if (j != seq.end()) seq.erase(j); } diff --git a/walk.cc b/walk.cc index 61681808..44d5ca3b 100644 --- a/walk.cc +++ b/walk.cc @@ -120,23 +120,19 @@ void truncate_entries::flush() entry_t * last_entry = (*xacts.begin())->entry; int l = 0; - for (xacts_list::iterator x = xacts.begin(); - x != xacts.end(); - x++) - if (last_entry != (*x)->entry) { + foreach (xact_t * xact, xacts) + if (last_entry != xact->entry) { l++; - last_entry = (*x)->entry; + last_entry = xact->entry; } l++; last_entry = (*xacts.begin())->entry; int i = 0; - for (xacts_list::iterator x = xacts.begin(); - x != xacts.end(); - x++) { - if (last_entry != (*x)->entry) { - last_entry = (*x)->entry; + foreach (xact_t * xact, xacts) { + if (last_entry != xact->entry) { + last_entry = xact->entry; i++; } @@ -156,7 +152,7 @@ void truncate_entries::flush() } if (print) - item_handler::operator()(**x); + item_handler::operator()(*xact); } xacts.clear(); @@ -183,11 +179,9 @@ void sort_xacts::post_accumulated_xacts() std::stable_sort(xacts.begin(), xacts.end(), compare_items(sort_order)); - for (xacts_deque::iterator i = xacts.begin(); - i != xacts.end(); - i++) { - xact_xdata(**i).dflags &= ~XACT_SORT_CALC; - item_handler::operator()(**i); + foreach (xact_t * xact, xacts) { + xact_xdata(*xact).dflags &= ~XACT_SORT_CALC; + item_handler::operator()(*xact); } xacts.clear(); @@ -349,31 +343,27 @@ void collapse_xacts::operator()(xact_t& xact) void related_xacts::flush() { if (xacts.size() > 0) { - for (xacts_list::iterator i = xacts.begin(); - i != xacts.end(); - i++) { - if ((*i)->entry) { - for (xacts_list::iterator j = (*i)->entry->xacts.begin(); - j != (*i)->entry->xacts.end(); - j++) { - xact_xdata_t& xdata = xact_xdata(**j); + foreach (xact_t * xact, xacts) { + if (xact->entry) { + foreach (xact_t * r_xact, xact->entry->xacts) { + xact_xdata_t& xdata = xact_xdata(*r_xact); if (! (xdata.dflags & XACT_HANDLED) && (! (xdata.dflags & XACT_RECEIVED) ? - ! (*j)->has_flags(XACT_AUTO | XACT_VIRTUAL) : + ! r_xact->has_flags(XACT_AUTO | XACT_VIRTUAL) : also_matching)) { xdata.dflags |= XACT_HANDLED; - item_handler::operator()(**j); + item_handler::operator()(*r_xact); } } } else { // This code should only be reachable from the "output" // command, since that is the only command which attempts to // output auto or period entries. - xact_xdata_t& xdata = xact_xdata(**i); + xact_xdata_t& xdata = xact_xdata(*xact); if (! (xdata.dflags & XACT_HANDLED) && - ! (*i)->has_flags(XACT_AUTO)) { + ! xact->has_flags(XACT_AUTO)) { xdata.dflags |= XACT_HANDLED; - item_handler::operator()(**i); + item_handler::operator()(*xact); } } } @@ -463,11 +453,9 @@ void subtotal_xacts::report_subtotal(const char * spec_fmt) entry.payee = out_date.str(); entry._date = start; - for (values_map::iterator i = values.begin(); - i != values.end(); - i++) - handle_value((*i).second.value, (*i).second.account, &entry, 0, - xact_temps, *handler, finish, &(*i).second.components); + foreach (values_map::value_type& pair, values) + handle_value(pair.second.value, pair.second.account, &entry, 0, + xact_temps, *handler, finish, &pair.second.components); values.clear(); } @@ -571,18 +559,14 @@ by_payee_xacts::~by_payee_xacts() { TRACE_DTOR(by_payee_xacts); - for (payee_subtotals_map::iterator i = payee_subtotals.begin(); - i != payee_subtotals.end(); - i++) - checked_delete((*i).second); + foreach (payee_subtotals_map::value_type& pair, payee_subtotals) + checked_delete(pair.second); } void by_payee_xacts::flush() { - for (payee_subtotals_map::iterator i = payee_subtotals.begin(); - i != payee_subtotals.end(); - i++) - (*i).second->report_subtotal((*i).first.c_str()); + foreach (payee_subtotals_map::value_type& pair, payee_subtotals) + pair.second->report_subtotal(pair.first.c_str()); item_handler::flush(); @@ -663,10 +647,8 @@ void dow_xacts::flush() #if 0 start = finish = 0; #endif - for (xacts_list::iterator d = days_of_the_week[i].begin(); - d != days_of_the_week[i].end(); - d++) - subtotal_xacts::operator()(**d); + foreach (xact_t * xact, days_of_the_week[i]) + subtotal_xacts::operator()(*xact); subtotal_xacts::report_subtotal("%As"); days_of_the_week[i].clear(); } @@ -677,13 +659,9 @@ void dow_xacts::flush() void generate_xacts::add_period_entries (period_entries_list& period_entries) { - for (period_entries_list::iterator i = period_entries.begin(); - i != period_entries.end(); - i++) - for (xacts_list::iterator j = (*i)->xacts.begin(); - j != (*i)->xacts.end(); - j++) - add_xact((*i)->period, **j); + foreach (period_entry_t * entry, period_entries) + foreach (xact_t * xact, entry->xacts) + add_xact(entry->period, *xact); } void generate_xacts::add_xact(const interval_t& period, @@ -700,18 +678,16 @@ void budget_xacts::report_budget_items(const datetime_t& moment) bool reported; do { reported = false; - for (pending_xacts_list::iterator i = pending_xacts.begin(); - i != pending_xacts.end(); - i++) { - datetime_t& begin = (*i).first.begin; + foreach (pending_xacts_list::value_type& pair, pending_xacts) { + datetime_t& begin = pair.first.begin; if (! is_valid(begin)) { - (*i).first.start(moment); - begin = (*i).first.begin; + pair.first.start(moment); + begin = pair.first.begin; } if (begin < moment && - (! is_valid((*i).first.end) || begin < (*i).first.end)) { - xact_t& xact = *(*i).second; + (! is_valid(pair.first.end) || begin < pair.first.end)) { + xact_t& xact = *pair.second; DEBUG("ledger.walk.budget", "Reporting budget for " << xact_account(xact)->fullname()); @@ -733,7 +709,7 @@ void budget_xacts::report_budget_items(const datetime_t& moment) temp.amount.negate(); entry.add_xact(&temp); - begin = (*i).first.increment(begin); + begin = pair.first.increment(begin); item_handler::operator()(temp); @@ -747,13 +723,11 @@ void budget_xacts::operator()(xact_t& xact) { bool xact_in_budget = false; - for (pending_xacts_list::iterator i = pending_xacts.begin(); - i != pending_xacts.end(); - i++) + foreach (pending_xacts_list::value_type& pair, pending_xacts) for (account_t * acct = xact_account(xact); acct; acct = acct->parent) { - if (acct == xact_account(*(*i).second)) { + if (acct == xact_account(*pair.second)) { xact_in_budget = true; // Report the xact as if it had occurred in the parent // account. @@ -773,8 +747,7 @@ void budget_xacts::operator()(xact_t& xact) } } -void forecast_xacts::add_xact(const interval_t& period, - xact_t& xact) +void forecast_xacts::add_xact(const interval_t& period, xact_t& xact) { generate_xacts::add_xact(period, xact); @@ -839,10 +812,8 @@ void forecast_xacts::flush() passed.clear(); } else { bool found = false; - for (xacts_list::iterator i = passed.begin(); - i != passed.end(); - i++) - if (*i == &xact) { + foreach (xact_t * x, passed) + if (x == &xact) { found = true; break; } @@ -896,14 +867,12 @@ void sum_accounts(account_t& account) { account_xdata_t& xdata(account_xdata(account)); - for (accounts_map::iterator i = account.accounts.begin(); - i != account.accounts.end(); - i++) { - sum_accounts(*(*i).second); + foreach (accounts_map::value_type& pair, account.accounts) { + sum_accounts(*pair.second); - xdata.total += account_xdata_(*(*i).second).total; - xdata.total_count += (account_xdata_(*(*i).second).total_count + - account_xdata_(*(*i).second).count); + xdata.total += account_xdata_(*pair.second).total; + xdata.total_count += (account_xdata_(*pair.second).total_count + + account_xdata_(*pair.second).count); } value_t result; @@ -938,10 +907,8 @@ account_t * accounts_iterator::operator()() void sorted_accounts_iterator::sort_accounts(account_t& account, accounts_deque_t& deque) { - for (accounts_map::iterator i = account.accounts.begin(); - i != account.accounts.end(); - i++) - deque.push_back((*i).second); + foreach (accounts_map::value_type& pair, account.accounts) + deque.push_back(pair.second); std::stable_sort(deque.begin(), deque.end(), compare_items(sort_cmp)); @@ -988,15 +955,14 @@ void walk_commodities(commodity_pool_t::commodities_by_ident& commodities, acct_temps.push_back(account_t(NULL, (*i)->symbol())); if ((*i)->history()) - for (commodity_t::history_map::iterator j = (*i)->history()->prices.begin(); - j != (*i)->history()->prices.end(); - j++) { - entry_temps.back()._date = (*j).first; + foreach (const commodity_t::history_map::value_type& pair, + (*i)->history()->prices) { + entry_temps.back()._date = pair.first; xact_temps.push_back(xact_t(&acct_temps.back())); xact_t& temp = xact_temps.back(); temp.entry = &entry_temps.back(); - temp.amount = (*j).second; + temp.amount = pair.second; temp.add_flags(XACT_TEMP); entry_temps.back().add_xact(&temp); diff --git a/walk.h b/walk.h index c92366e0..7984e62c 100644 --- a/walk.h +++ b/walk.h @@ -101,8 +101,7 @@ struct xact_xdata_t : public noncopyable datetime_t date; account_t * account; void * ptr; - - xacts_list * component_xacts; + xacts_list * component_xacts; xact_xdata_t() : index(0), dflags(0), @@ -126,17 +125,13 @@ struct xact_xdata_t : public noncopyable } void copy_component_xacts(xacts_list& xacts) { - for (xacts_list::const_iterator i = xacts.begin(); - i != xacts.end(); - i++) - remember_xact(**i); + foreach (xact_t * xact, xacts) + remember_xact(*xact); } void walk_component_xacts(item_handler& handler) const { - for (xacts_list::const_iterator i = component_xacts->begin(); - i != component_xacts->end(); - i++) - handler(**i); + foreach (xact_t * xact, *component_xacts) + handler(*xact); } }; @@ -493,10 +488,8 @@ public: }; inline void clear_entries_xacts(std::list& entries_list) { - for (std::list::iterator i = entries_list.begin(); - i != entries_list.end(); - i++) - (*i).xacts.clear(); + foreach (entry_t& entry, entries_list) + entry.xacts.clear(); } class collapse_xacts : public item_handler diff --git a/xact.cc b/xact.cc index 211553a9..d0360f7f 100644 --- a/xact.cc +++ b/xact.cc @@ -169,11 +169,9 @@ xact_context::xact_context(const xact_t& _xact, const string& desc) throw() { const paths_list& sources(xact.entry->journal->sources); unsigned int x = 0; - for (paths_list::const_iterator i = sources.begin(); - i != sources.end(); - i++, x++) + foreach (const path& path, sources) if (x == xact.entry->src_idx) { - file = *i; + file = path; break; } line = xact.beg_line; diff --git a/xml.cc b/xml.cc index 297b710a..c65fa190 100644 --- a/xml.cc +++ b/xml.cc @@ -318,10 +318,8 @@ void xml_write_value(std::ostream& out, const value_t& value, for (int i = 0; i < depth + 2; i++) out << ' '; out << "\n"; - for (balance_t::amounts_map::const_iterator i = bal->amounts.begin(); - i != bal->amounts.end(); - i++) - xml_write_amount(out, (*i).second, depth + 4); + foreach (const balance_t::amounts_map::value_type& pair, bal->amounts) + xml_write_amount(out, pair.second, depth + 4); for (int i = 0; i < depth + 2; i++) out << ' '; out << "\n"; @@ -383,11 +381,9 @@ void format_xml_entries::format_last_entry() } bool first = true; - for (xacts_list::const_iterator i = last_entry->xacts.begin(); - i != last_entry->xacts.end(); - i++) { - if (xact_has_xdata(**i) && - xact_xdata_(**i).dflags & XACT_TO_DISPLAY) { + foreach (const xact_t * xact, last_entry->xacts) { + if (xact_has_xdata(*xact) && + xact_xdata_(*xact).dflags & XACT_TO_DISPLAY) { if (first) { output_stream << " \n"; first = false; @@ -397,29 +393,29 @@ void format_xml_entries::format_last_entry() #if 0 // jww (2008-05-08): Need to format these - if ((*i)->_date) + if (xact->_date) output_stream << " " - << (*i)->_date.to_string("%Y/%m/%d") + << xact->_date.to_string("%Y/%m/%d") << "\n"; - if (is_valid((*i)->_date_eff)) + if (is_valid(xact->_date_eff)) output_stream << " " - << (*i)->_date_eff.to_string("%Y/%m/%d") + << xact->_date_eff.to_string("%Y/%m/%d") << "\n"; #endif - if ((*i)->state == xact_t::CLEARED) + if (xact->state == xact_t::CLEARED) output_stream << " \n"; - else if ((*i)->state == xact_t::PENDING) + else if (xact->state == xact_t::PENDING) output_stream << " \n"; - if ((*i)->has_flags(XACT_VIRTUAL)) + if (xact->has_flags(XACT_VIRTUAL)) output_stream << " \n"; - if ((*i)->has_flags(XACT_AUTO)) + if (xact->has_flags(XACT_AUTO)) output_stream << " \n"; - if ((*i)->account) { - string name = (*i)->account->fullname(); + if (xact->account) { + string name = xact->account->fullname(); if (name == "") name = "[TOTAL]"; else if (name == "") @@ -431,34 +427,34 @@ void format_xml_entries::format_last_entry() } output_stream << " \n"; - if (xact_xdata_(**i).dflags & XACT_COMPOUND) + if (xact_xdata_(*xact).dflags & XACT_COMPOUND) xml_write_value(output_stream, - xact_xdata_(**i).value, 10); + xact_xdata_(*xact).value, 10); else - xml_write_value(output_stream, value_t((*i)->amount), 10); + xml_write_value(output_stream, value_t(xact->amount), 10); output_stream << " \n"; - if ((*i)->cost) { + if (xact->cost) { output_stream << " \n"; - xml_write_value(output_stream, value_t(*(*i)->cost), 10); + xml_write_value(output_stream, value_t(*xact->cost), 10); output_stream << " \n"; } - if ((*i)->note) { + if (xact->note) { output_stream << " "; - output_xml_string(output_stream, *(*i)->note); + output_xml_string(output_stream, *xact->note); output_stream << "\n"; } if (show_totals) { output_stream << " \n"; - xml_write_value(output_stream, xact_xdata_(**i).total, 10); + xml_write_value(output_stream, xact_xdata_(*xact).total, 10); output_stream << " \n"; } output_stream << " \n"; - xact_xdata_(**i).dflags |= XACT_DISPLAYED; + xact_xdata_(*xact).dflags |= XACT_DISPLAYED; } }