summaryrefslogtreecommitdiff
path: root/src/iterators.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/iterators.cc')
-rw-r--r--src/iterators.cc115
1 files changed, 65 insertions, 50 deletions
diff --git a/src/iterators.cc b/src/iterators.cc
index b398646e..7cc1291a 100644
--- a/src/iterators.cc
+++ b/src/iterators.cc
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003-2010, John Wiegley. All rights reserved.
+ * Copyright (c) 2003-2012, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
@@ -75,6 +75,65 @@ void journal_posts_iterator::increment()
}
}
+namespace {
+ struct create_price_xact
+ {
+ journal_t& journal;
+ account_t * account;
+ temporaries_t& temps;
+ xacts_list& xact_temps;
+
+ std::map<string, xact_t *> xacts_by_commodity;
+
+ create_price_xact(journal_t& _journal, account_t * _account,
+ temporaries_t& _temps, xacts_list& _xact_temps)
+ : journal(_journal), account(_account), temps(_temps),
+ xact_temps(_xact_temps) {
+ TRACE_CTOR(create_price_xact,
+ "journal_t&, account_t *, temporaries_t&, xacts_list&");
+ }
+ ~create_price_xact() throw() {
+ TRACE_DTOR(create_price_xact);
+ }
+
+ void operator()(datetime_t& date, const amount_t& price) {
+ xact_t * xact;
+ string symbol = price.commodity().symbol();
+
+ std::map<string, xact_t *>::iterator i =
+ xacts_by_commodity.find(symbol);
+ if (i != xacts_by_commodity.end()) {
+ xact = (*i).second;
+ } else {
+ xact = &temps.create_xact();
+ xact_temps.push_back(xact);
+ xact->payee = symbol;
+ xact->_date = date.date();
+ xacts_by_commodity.insert
+ (std::pair<string, xact_t *>(symbol, xact));
+ xact->journal = &journal;
+ }
+
+ bool post_already_exists = false;
+
+ foreach (post_t * post, xact->posts) {
+ if (post->date() == date.date() && post->amount == price) {
+ post_already_exists = true;
+ break;
+ }
+ }
+
+ if (! post_already_exists) {
+ post_t& temp = temps.create_post(*xact, account);
+ temp._date = date.date();
+ temp.amount = price;
+
+ temp.xdata().datetime = date;
+ }
+ }
+ };
+}
+
void posts_commodities_iterator::reset(journal_t& journal)
{
journal_posts.reset(journal);
@@ -85,57 +144,13 @@ void posts_commodities_iterator::reset(journal_t& journal)
commodity_t& comm(post->amount.commodity());
if (comm.flags() & COMMODITY_NOMARKET)
continue;
- commodities.insert(&comm);
+ commodities.insert(&comm.referent());
}
- std::map<string, xact_t *> xacts_by_commodity;
-
- foreach (commodity_t * comm, commodities) {
- if (optional<commodity_t::varied_history_t&> history =
- comm->varied_history()) {
- account_t * account = journal.master->find_account(comm->symbol());
-
- foreach (commodity_t::history_by_commodity_map::value_type& pair,
- history->histories) {
- foreach (commodity_t::history_map::value_type& hpair,
- pair.second.prices) {
- xact_t * xact;
- string symbol = hpair.second.commodity().symbol();
-
- std::map<string, xact_t *>::iterator i =
- xacts_by_commodity.find(symbol);
- if (i != xacts_by_commodity.end()) {
- xact = (*i).second;
- } else {
- xact = &temps.create_xact();
- xact_temps.push_back(xact);
- xact->payee = symbol;
- xact->_date = hpair.first.date();
- xacts_by_commodity.insert
- (std::pair<string, xact_t *>(symbol, xact));
- }
-
- bool post_already_exists = false;
-
- foreach (post_t * post, xact->posts) {
- if (post->_date == hpair.first.date() &&
- post->amount == hpair.second) {
- post_already_exists = true;
- break;
- }
- }
-
- if (! post_already_exists) {
- post_t& temp = temps.create_post(*xact, account);
- temp._date = hpair.first.date();
- temp.amount = hpair.second;
-
- temp.xdata().datetime = hpair.first;
- }
- }
- }
- }
- }
+ foreach (commodity_t * comm, commodities)
+ comm->map_prices
+ (create_price_xact(journal, journal.master->find_account(comm->symbol()),
+ temps, xact_temps));
xacts.reset(xact_temps.begin(), xact_temps.end());