summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2012-03-30 00:38:29 -0500
committerJohn Wiegley <johnw@newartisans.com>2012-03-30 00:51:05 -0500
commit8e8c2904f55eb9a43b3eb8057e9f11767a624dff (patch)
treebd7473e6ca867ab8a2fe10cc657484810e8daac7 /src
parent4b2b9dc009e5f44029cc23ea17e0a5c98266baff (diff)
downloadfork-ledger-8e8c2904f55eb9a43b3eb8057e9f11767a624dff.tar.gz
fork-ledger-8e8c2904f55eb9a43b3eb8057e9f11767a624dff.tar.bz2
fork-ledger-8e8c2904f55eb9a43b3eb8057e9f11767a624dff.zip
Never price commodities using annotated commodities
Diffstat (limited to 'src')
-rw-r--r--src/amount.cc6
-rw-r--r--src/amount.h9
-rw-r--r--src/annotate.cc2
-rw-r--r--src/commodity.cc17
-rw-r--r--src/commodity.h3
5 files changed, 24 insertions, 13 deletions
diff --git a/src/amount.cc b/src/amount.cc
index 2f7b434e..50496f2d 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -757,10 +757,10 @@ amount_t::value(const datetime_t& moment,
}
}
- if (! point) {
- if (comm && commodity().referent() == comm->referent())
- return *this;
+ if (comm && commodity().referent() == comm->referent())
+ return with_commodity(comm->referent());
+ if (! point) {
point = commodity().find_price(comm, moment);
// Whether a price was found or not, check whether we should attempt
diff --git a/src/amount.h b/src/amount.h
index 10e83552..cd77a79a 100644
--- a/src/amount.h
+++ b/src/amount.h
@@ -544,6 +544,15 @@ public:
*this = 0L;
commodity_ = &comm;
}
+ amount_t with_commodity(const commodity_t& comm) const {
+ if (commodity_ == &comm) {
+ return *this;
+ } else {
+ amount_t tmp(*this);
+ tmp.set_commodity(const_cast<commodity_t&>(comm));
+ return tmp;
+ }
+ }
void clear_commodity() {
commodity_ = NULL;
}
diff --git a/src/annotate.cc b/src/annotate.cc
index c9fd6d3f..41e7a752 100644
--- a/src/annotate.cc
+++ b/src/annotate.cc
@@ -288,7 +288,7 @@ annotated_commodity_t::find_price(const commodity_t * commodity,
return find_price_from_expr(const_cast<expr_t&>(*details.value_expr),
commodity, when);
- return commodity_t::find_price(target, moment, oldest);
+ return commodity_t::find_price(target, when, oldest);
}
commodity_t&
diff --git a/src/commodity.cc b/src/commodity.cc
index bc04c3db..a72d85c8 100644
--- a/src/commodity.cc
+++ b/src/commodity.cc
@@ -56,14 +56,14 @@ void commodity_t::add_price(const datetime_t& date, const amount_t& price,
DEBUG("history.find", "Adding price: " << symbol()
<< " for " << price << " on " << date);
- pool().commodity_price_history.add_price(*this, date, price);
+ pool().commodity_price_history.add_price(referent(), date, price);
base->price_map.clear(); // a price was added, invalid the map
}
void commodity_t::remove_price(const datetime_t& date, commodity_t& commodity)
{
- pool().commodity_price_history.remove_price(*this, commodity, date);
+ pool().commodity_price_history.remove_price(referent(), commodity, date);
DEBUG("history.find", "Removing price: " << symbol() << " on " << date);
@@ -83,7 +83,7 @@ void commodity_t::map_prices(function<void(datetime_t, const amount_t&)> fn,
else
when = CURRENT_TIME();
- pool().commodity_price_history.map_prices(fn, *this, when, _oldest,
+ pool().commodity_price_history.map_prices(fn, referent(), when, _oldest,
bidirectionally);
}
@@ -159,9 +159,9 @@ commodity_t::find_price(const commodity_t * commodity,
optional<price_point_t>
point(target ?
- pool().commodity_price_history.find_price(*this, *target,
+ pool().commodity_price_history.find_price(referent(), *target,
when, oldest) :
- pool().commodity_price_history.find_price(*this, when, oldest));
+ pool().commodity_price_history.find_price(referent(), when, oldest));
// Record this price point in the memoization map
if (base->price_map.size() > base_t::max_price_map_size) {
@@ -206,7 +206,7 @@ commodity_t::check_for_updated_price(const optional<price_point_t>& point,
DEBUG("commodity.download",
"attempting to download a more current quote...");
if (optional<price_point_t> quote =
- pool().get_commodity_quote(*this, in_terms_of)) {
+ pool().get_commodity_quote(referent(), in_terms_of)) {
if (! in_terms_of ||
(quote->price.has_commodity() &&
quote->price.commodity_ptr() == in_terms_of))
@@ -220,12 +220,11 @@ commodity_t::check_for_updated_price(const optional<price_point_t>& point,
commodity_t& commodity_t::nail_down(const expr_t& expr)
{
annotation_t new_details;
+
new_details.value_expr = expr;
new_details.add_flags(ANNOTATION_VALUE_EXPR_CALCULATED);
- commodity_t * new_comm =
- commodity_pool_t::current_pool->find_or_create(symbol(), new_details);
- return *new_comm;
+ return *pool().find_or_create(symbol(), new_details);
}
commodity_t::operator bool() const
diff --git a/src/commodity.h b/src/commodity.h
index d6885ee9..bfbabe6b 100644
--- a/src/commodity.h
+++ b/src/commodity.h
@@ -188,6 +188,9 @@ public:
return comm == *this;
return base.get() == comm.base.get();
}
+ bool operator==(const string& name) const {
+ return base_symbol() == name;
+ }
static bool symbol_needs_quotes(const string& symbol);