summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2009-01-19 19:59:49 -0400
committerJohn Wiegley <johnw@newartisans.com>2009-01-19 19:59:49 -0400
commit52e140d14e36617f5fea13f62195820ea428fcb5 (patch)
tree7e5a61d41a0dac1035e4accb502b7d2e2d49d976
parent85be211f996c00b1304ed8b2d4c2cb602c1486d1 (diff)
downloadfork-ledger-52e140d14e36617f5fea13f62195820ea428fcb5.tar.gz
fork-ledger-52e140d14e36617f5fea13f62195820ea428fcb5.tar.bz2
fork-ledger-52e140d14e36617f5fea13f62195820ea428fcb5.zip
Lookup commodity prices recursively, searching for the newest data.
-rw-r--r--src/commodity.cc33
-rw-r--r--src/commodity.h37
2 files changed, 42 insertions, 28 deletions
diff --git a/src/commodity.cc b/src/commodity.cc
index ad0d6ec9..113c1538 100644
--- a/src/commodity.cc
+++ b/src/commodity.cc
@@ -43,9 +43,10 @@
namespace ledger {
-void commodity_t::base_t::history_t::add_price(const datetime_t& date,
- const amount_t& price,
- const bool reflexive)
+void commodity_t::base_t::history_t::add_price(const commodity_t& source,
+ const datetime_t& date,
+ const amount_t& price,
+ const bool reflexive)
{
DEBUG("commodity.prices", "add_price: " << date << ", " << price);
@@ -59,7 +60,9 @@ void commodity_t::base_t::history_t::add_price(const datetime_t& date,
}
if (reflexive) {
- price.commodity().add_price(date, *one / price, false);
+ amount_t inverse(*one / price);
+ inverse.set_commodity(const_cast<commodity_t&>(source));
+ price.commodity().add_price(date, inverse, false);
}
}
@@ -73,9 +76,11 @@ bool commodity_t::base_t::history_t::remove_price(const datetime_t& date)
return false;
}
-void commodity_t::base_t::varied_history_t::add_price(const datetime_t& date,
- const amount_t& price,
- const bool reflexive)
+void commodity_t::base_t::varied_history_t::
+ add_price(const commodity_t& source,
+ const datetime_t& date,
+ const amount_t& price,
+ const bool reflexive)
{
DEBUG("commodity.prices", "varied_add_price: " << date << ", " << price);
@@ -90,7 +95,7 @@ void commodity_t::base_t::varied_history_t::add_price(const datetime_t& date,
}
assert(hist);
- hist->add_price(date, price, reflexive);
+ hist->add_price(source, date, price, reflexive);
}
bool commodity_t::base_t::varied_history_t::remove_price(const datetime_t& date,
@@ -112,7 +117,7 @@ optional<price_point_t>
#if defined(DEBUG_ON)
, const int indent
#endif
- )
+ ) const
{
price_point_t point;
bool found = false;
@@ -219,7 +224,7 @@ optional<price_point_t>
#if defined(DEBUG_ON)
, const int indent
#endif
- )
+ ) const
{
optional<price_point_t> point;
optional<datetime_t> limit = oldest;
@@ -295,7 +300,8 @@ optional<price_point_t>
assert(! commodity || point->price.commodity() == *commodity);
DEBUG_INDENT("commodity.prices", indent + 1);
- DEBUG("commodity.prices", " saw a price there: " << point->price);
+ DEBUG("commodity.prices",
+ " saw a price there: " << point->price << " from " << point->when);
if (! limit || point->when > *limit) {
limit = point->when;
best = *point;
@@ -325,7 +331,7 @@ optional<price_point_t>
#if defined(DEBUG_ON)
, const int indent
#endif
- )
+ ) const
{
foreach (commodity_t * commodity, commodities) {
if (optional<price_point_t> point = find_price(source, *commodity,
@@ -753,8 +759,7 @@ commodity_pool_t::commodity_pool_t() : default_commodity(NULL)
{
TRACE_CTOR(commodity_pool_t, "");
null_commodity = create("");
- null_commodity->add_flags(COMMODITY_STYLE_NOMARKET |
- COMMODITY_STYLE_BUILTIN);
+ null_commodity->add_flags(COMMODITY_BUILTIN | COMMODITY_STYLE_NOMARKET);
}
commodity_t * commodity_pool_t::create(const string& symbol)
diff --git a/src/commodity.h b/src/commodity.h
index 204dec8d..c97ca569 100644
--- a/src/commodity.h
+++ b/src/commodity.h
@@ -77,8 +77,10 @@ public:
history_map prices;
ptime last_lookup;
- void add_price(const datetime_t& date, const amount_t& price,
- const bool reflexive = true);
+ void add_price(const commodity_t& source,
+ const datetime_t& date,
+ const amount_t& price,
+ const bool reflexive = true);
bool remove_price(const datetime_t& date);
optional<price_point_t>
@@ -89,7 +91,7 @@ public:
#if defined(DEBUG_ON)
, const int indent = 0
#endif
- );
+ ) const;
};
typedef std::map<commodity_t *, history_t> history_by_commodity_map;
@@ -98,8 +100,10 @@ public:
{
history_by_commodity_map histories;
- void add_price(const datetime_t& date, const amount_t& price,
- const bool reflexive = true);
+ void add_price(const commodity_t& source,
+ const datetime_t& date,
+ const amount_t& price,
+ const bool reflexive = true);
bool remove_price(const datetime_t& date, commodity_t& commodity);
optional<price_point_t>
@@ -110,7 +114,7 @@ public:
#if defined(DEBUG_ON)
, const int indent = 0
#endif
- );
+ ) const;
optional<price_point_t>
find_price(const commodity_t& source,
const std::vector<commodity_t *>& commodities,
@@ -119,7 +123,7 @@ public:
#if defined(DEBUG_ON)
, const int indent = 0
#endif
- );
+ ) const;
optional<history_t&>
history(const optional<commodity_t&>& commodity = none);
@@ -270,7 +274,7 @@ public:
if (! base->varied_history)
base->varied_history = varied_history_t();
- base->varied_history->add_price(date, price, reflexive);
+ base->varied_history->add_price(*this, date, price, reflexive);
}
bool remove_price(const datetime_t& date, commodity_t& commodity) {
if (base->varied_history)
@@ -285,13 +289,18 @@ public:
#if defined(DEBUG_ON)
, const int indent = 0
#endif
- ) {
- if (base->varied_history)
- return base->varied_history->find_price(*this, commodity, moment, oldest
+ ) const {
+ if (base->varied_history && ! has_flags(COMMODITY_WALKED)) {
+ const_cast<commodity_t&>(*this).add_flags(COMMODITY_WALKED);
+ optional<price_point_t> point =
+ base->varied_history->find_price(*this, commodity, moment, oldest
#if defined(DEBUG_ON)
- , indent
+ , indent
#endif
- );
+ );
+ const_cast<commodity_t&>(*this).drop_flags(COMMODITY_WALKED);
+ return point;
+ }
return none;
}
@@ -302,7 +311,7 @@ public:
#if defined(DEBUG_ON)
, const int indent = 0
#endif
- ) {
+ ) const {
if (base->varied_history)
return base->varied_history->find_price(*this, commodities, moment, oldest
#if defined(DEBUG_ON)