diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/data/journal.cc | 2 | ||||
-rw-r--r-- | src/driver/session.cc | 2 | ||||
-rw-r--r-- | src/ledger.h | 1 | ||||
-rw-r--r-- | src/numerics/amount.cc | 43 | ||||
-rw-r--r-- | src/numerics/amount.h | 7 | ||||
-rw-r--r-- | src/numerics/balpair.h | 22 | ||||
-rw-r--r-- | src/numerics/value.cc | 12 |
7 files changed, 47 insertions, 42 deletions
diff --git a/src/data/journal.cc b/src/data/journal.cc index 32e45697..17294820 100644 --- a/src/data/journal.cc +++ b/src/data/journal.cc @@ -229,7 +229,7 @@ bool entry_base_t::finalize() const balance_t * bal = NULL; switch (balance.type()) { case value_t::BALANCE_PAIR: - bal = &balance.as_balance_pair().quantity; + bal = &balance.as_balance_pair().quantity(); // fall through... case value_t::BALANCE: diff --git a/src/driver/session.cc b/src/driver/session.cc index 71f5f349..98ae9613 100644 --- a/src/driver/session.cc +++ b/src/driver/session.cc @@ -178,7 +178,7 @@ std::size_t session_t::read_data(xml::builder_t& builder, DEBUG("ledger.cache", "using_cache " << cache_file->string()); cache_dirty = true; if (exists(*cache_file)) { - scoped_variable<optional<path> > + push_variable<optional<path> > save_price_db(journal->price_db, price_db); entry_count += read_journal(*cache_file, builder); diff --git a/src/ledger.h b/src/ledger.h index c1d0ef1d..76e10d7b 100644 --- a/src/ledger.h +++ b/src/ledger.h @@ -45,7 +45,6 @@ #include <balance.h> #include <value.h> #include <xpath.h> -#include <format.h> #include <session.h> #include <journal.h> #include <parser.h> diff --git a/src/numerics/amount.cc b/src/numerics/amount.cc index 79dd663c..7bc2841c 100644 --- a/src/numerics/amount.cc +++ b/src/numerics/amount.cc @@ -586,35 +586,40 @@ amount_t& amount_t::in_place_negate() return *this; } -amount_t amount_t::round(const optional<precision_t>& prec) const +amount_t amount_t::round() const { if (! quantity) throw_(amount_error, "Cannot round an uninitialized amount"); - if (! prec) { - if (! has_commodity()) - return *this; - return round(commodity().precision()); - } else { - amount_t t(*this); - - if (quantity->prec <= *prec) { - if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) { - t._dup(); - t.quantity->drop_flags(BIGINT_KEEP_PREC); - } - return t; - } + if (! has_commodity()) + return *this; - t._dup(); + return round(commodity().precision()); +} - mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, *prec); +amount_t amount_t::round(precision_t prec) const +{ + if (! quantity) + throw_(amount_error, "Cannot round an uninitialized amount"); - t.quantity->prec = *prec; - t.quantity->drop_flags(BIGINT_KEEP_PREC); + amount_t t(*this); + if (quantity->prec <= prec) { + if (quantity && quantity->has_flags(BIGINT_KEEP_PREC)) { + t._dup(); + t.quantity->drop_flags(BIGINT_KEEP_PREC); + } return t; } + + t._dup(); + + mpz_round(MPZ(t.quantity), MPZ(t.quantity), t.quantity->prec, prec); + + t.quantity->prec = prec; + t.quantity->drop_flags(BIGINT_KEEP_PREC); + + return t; } amount_t amount_t::unround() const diff --git a/src/numerics/amount.h b/src/numerics/amount.h index 1c8ef6d0..158b8b8a 100644 --- a/src/numerics/amount.h +++ b/src/numerics/amount.h @@ -294,8 +294,8 @@ public: * abs() returns the absolute value of an amount. It is equivalent * to: `(x < 0) ? - x : x'. * - * round(optional<precision_t>) rounds an amount's internal value to - * the given precision, or to the commodity's current display + * round(precision_t) and round() round an amount's internal value + * to the given precision, or to the commodity's current display * precision if no precision value is given. This method changes * the internal value of the amount, if it's internal precision was * greater than the rounding precision. @@ -347,7 +347,8 @@ public: return *this; } - amount_t round(const optional<precision_t>& prec = none) const; + amount_t round() const; + amount_t round(precision_t prec) const; amount_t unround() const; amount_t reduce() const { diff --git a/src/numerics/balpair.h b/src/numerics/balpair.h index af1a25ab..4aa682fc 100644 --- a/src/numerics/balpair.h +++ b/src/numerics/balpair.h @@ -83,17 +83,6 @@ class balance_pair_t */ optional<balance_t> cost; - /** - * The `quantity' method provides direct access to the balance_t - * base-class part of the balance pair. - */ - balance_t& quantity() { - return *this; - } - const balance_t& quantity() const { - return *this; - } - friend class value_t; friend class entry_base_t; @@ -225,6 +214,17 @@ public: return quantity() == amt; } + /** + * The `quantity' method provides direct access to the balance_t + * base-class part of the balance pair. + */ + balance_t& quantity() { + return *this; + } + const balance_t& quantity() const { + return *this; + } + // unary negation void in_place_negate() { #if 0 diff --git a/src/numerics/value.cc b/src/numerics/value.cc index a938b1bc..bdb102c5 100644 --- a/src/numerics/value.cc +++ b/src/numerics/value.cc @@ -1053,7 +1053,7 @@ void value_t::in_place_cast(type_t cast_type) case BALANCE_PAIR: switch (cast_type) { case AMOUNT: { - const balance_t& temp(as_balance_pair().quantity); + const balance_t& temp(as_balance_pair().quantity()); if (temp.amounts.size() == 1) { set_amount((*temp.amounts.begin()).second); return; @@ -1069,7 +1069,7 @@ void value_t::in_place_cast(type_t cast_type) break; } case BALANCE: - set_balance(as_balance_pair().quantity); + set_balance(as_balance_pair().quantity()); return; default: break; @@ -1184,7 +1184,7 @@ value_t value_t::value(const optional<moment_t>& moment) const } case BALANCE_PAIR: { if (optional<balance_t> bal_pair = - as_balance_pair().quantity.value(moment)) + as_balance_pair().quantity().value(moment)) return *bal_pair; return false; } @@ -1353,8 +1353,8 @@ value_t value_t::strip_annotations(const bool keep_price, case BALANCE: return as_balance().strip_annotations(keep_price, keep_date, keep_tag); case BALANCE_PAIR: - return as_balance_pair().quantity.strip_annotations(keep_price, - keep_date, keep_tag); + return as_balance_pair().quantity().strip_annotations(keep_price, keep_date, + keep_tag); default: assert(false); @@ -1377,7 +1377,7 @@ value_t value_t::cost() const if (as_balance_pair().cost) return *(as_balance_pair().cost); else - return as_balance_pair().quantity; + return as_balance_pair().quantity(); case XML_NODE: return as_xml_node()->to_value().cost(); |