summaryrefslogtreecommitdiff
path: root/src/pool.cc
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2012-03-04 03:35:06 -0600
committerJohn Wiegley <johnw@newartisans.com>2012-03-05 05:03:52 -0600
commit48ab6ad1dbab100bb8abd87029a0ca5bc501a3db (patch)
tree294112ea9df8ecac90c98c08327ce13fe5592682 /src/pool.cc
parent58d912827d2c6bd90c063537378baffff014c0f2 (diff)
downloadfork-ledger-48ab6ad1dbab100bb8abd87029a0ca5bc501a3db.tar.gz
fork-ledger-48ab6ad1dbab100bb8abd87029a0ca5bc501a3db.tar.bz2
fork-ledger-48ab6ad1dbab100bb8abd87029a0ca5bc501a3db.zip
Switched to using Boost.Graph for commodity pricing
Diffstat (limited to 'src/pool.cc')
-rw-r--r--src/pool.cc88
1 files changed, 13 insertions, 75 deletions
diff --git a/src/pool.cc b/src/pool.cc
index ba408fc5..67cfe3d1 100644
--- a/src/pool.cc
+++ b/src/pool.cc
@@ -35,6 +35,7 @@
#include "commodity.h"
#include "annotate.h"
#include "pool.h"
+#include "history.h"
#include "quotes.h"
namespace ledger {
@@ -74,15 +75,15 @@ commodity_t * commodity_pool_t::create(const string& symbol)
commodity.get()));
assert(result.second);
+ commodity_price_history.add_commodity(*commodity.get());
+
return commodity.release();
}
commodity_t * commodity_pool_t::find_or_create(const string& symbol)
{
DEBUG("pool.commodities", "Find-or-create commodity " << symbol);
-
- commodity_t * commodity = find(symbol);
- if (commodity)
+ if (commodity_t * commodity = find(symbol))
return commodity;
return create(symbol);
}
@@ -222,6 +223,15 @@ commodity_t * commodity_pool_t::find_or_create(commodity_t& comm,
return create(comm, details, name);
}
+optional<price_point_t>
+commodity_pool_t::find_price(const commodity_t& source,
+ const optional<commodity_t&>& commodity,
+ const optional<datetime_t>& moment,
+ const optional<datetime_t>& oldest) const
+{
+ return commodity_price_history.find_price(source, commodity, moment, oldest);
+}
+
void commodity_pool_t::exchange(commodity_t& commodity,
const amount_t& per_unit_cost,
const datetime_t& moment)
@@ -382,76 +392,4 @@ commodity_pool_t::parse_price_expression(const std::string& str,
return NULL;
}
-void commodity_pool_t::print_pricemap(std::ostream& out,
- const keep_details_t& keep,
- const optional<datetime_t>& moment)
-{
- typedef std::map<commodity_t *, commodity_t *> comm_map_t;
-
- comm_map_t comm_map;
-
- foreach (const commodities_map::value_type& comm_pair, commodities) {
- commodity_t * comm(&comm_pair.second->strip_annotations(keep));
- comm_map.insert(comm_map_t::value_type(comm, NULL));
- }
-
- out << "digraph commodities {\n";
-
- foreach (const comm_map_t::value_type& comm_pair, comm_map) {
- commodity_t * comm(comm_pair.first);
- if (comm->has_flags(COMMODITY_BUILTIN))
- continue;
-
- out << " ";
- if (commodity_t::symbol_needs_quotes(comm->symbol()))
- out << comm->symbol() << ";\n";
- else
- out << "\"" << comm->symbol() << "\";\n";
-
- if (! comm->has_flags(COMMODITY_NOMARKET) &&
- (! commodity_pool_t::current_pool->default_commodity ||
- comm != commodity_pool_t::current_pool->default_commodity)) {
- if (optional<commodity_t::varied_history_t&> vhist =
- comm->varied_history()) {
- foreach (const commodity_t::history_by_commodity_map::value_type& pair,
- vhist->histories) {
- datetime_t most_recent;
- amount_t most_recent_amt;
- foreach (const commodity_t::history_map::value_type& inner_pair,
- pair.second.prices) {
- if ((most_recent.is_not_a_date_time() ||
- inner_pair.first > most_recent) &&
- (! moment || inner_pair.first <= moment)) {
- most_recent = inner_pair.first;
- most_recent_amt = inner_pair.second;
- }
- }
-
- if (! most_recent.is_not_a_date_time()) {
- out << " ";
- if (commodity_t::symbol_needs_quotes(comm->symbol()))
- out << comm->symbol();
- else
- out << "\"" << comm->symbol() << "\"";
-
- out << " -> ";
-
- if (commodity_t::symbol_needs_quotes(pair.first->symbol()))
- out << pair.first->symbol();
- else
- out << "\"" << pair.first->symbol() << "\"";
-
- out << " [label=\""
- << most_recent_amt.number() << "\\n"
- << format_date(most_recent.date(), FMT_WRITTEN)
- << "\" fontcolor=\"#008e28\"];\n";
- }
- }
- }
- }
- }
-
- out << "}\n";
-}
-
} // namespace ledger