diff options
-rw-r--r-- | src/amount.cc | 55 | ||||
-rw-r--r-- | src/amount.h | 12 | ||||
-rw-r--r-- | src/cache.cc | 44 |
3 files changed, 52 insertions, 59 deletions
diff --git a/src/amount.cc b/src/amount.cc index 502f1916..d3265bc0 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -106,13 +106,6 @@ struct amount_t::bigint_t : public supports_flags<> DEBUG("ledger.validate", "amount_t::bigint_t: ref > 16535"); return false; } -#if 0 - // jww (2008-07-24): How does one check the validity of an mpz_t? - if (val[0]._mp_size < 0 || val[0]._mp_size > 100) { - DEBUG("ledger.validate", "amount_t::bigint_t: val._mp_size is bad"); - return false; - } -#endif return true; } }; @@ -127,7 +120,6 @@ void amount_t::initialize() mpz_init(temp); mpz_init(divisor); - // jww (2007-05-02): Be very careful here! if (! current_pool) current_pool = new commodity_pool_t; @@ -148,7 +140,6 @@ void amount_t::shutdown() mpz_clear(temp); mpz_clear(divisor); - // jww (2007-05-02): Be very careful here! if (current_pool) { checked_delete(current_pool); current_pool = NULL; @@ -493,16 +484,6 @@ amount_t& amount_t::operator*=(const amount_t& amt) throw_(amount_error, "Cannot multiply two uninitialized amounts"); } -#if 0 - if (has_commodity() && amt.has_commodity() && - commodity() != amt.commodity()) - throw_(amount_error, - "Multiplying amounts with different commodities: " << - (has_commodity() ? commodity().symbol() : "NONE") << - " != " << - (amt.has_commodity() ? amt.commodity().symbol() : "NONE")); -#endif - _dup(); mpz_mul(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); @@ -535,16 +516,6 @@ amount_t& amount_t::operator/=(const amount_t& amt) throw_(amount_error, "Cannot divide two uninitialized amounts"); } -#if 0 - if (has_commodity() && amt.has_commodity() && - commodity() != amt.commodity()) - throw_(amount_error, - "Dividing amounts with different commodities: " << - (has_commodity() ? commodity().symbol() : "NONE") << - " != " << - (amt.has_commodity() ? amt.commodity().symbol() : "NONE")); -#endif - if (! amt) throw_(amount_error, "Divide by zero"); @@ -1292,7 +1263,9 @@ void amount_t::read(std::istream& in) } } -void amount_t::read(const char *& data) +void amount_t::read(const char *& data, + char ** pool, + char ** pool_next) { using namespace ledger::binary; @@ -1315,10 +1288,8 @@ void amount_t::read(const char *& data) if (byte < 3) { if (byte == 2) { -#if 0 - quantity = new(reinterpret_cast<bigint_t *>(bigints_next)) bigint_t; - bigints_next += sizeof(bigint_t); -#endif + quantity = new(reinterpret_cast<bigint_t *>(*pool_next)) bigint_t; + *pool_next += sizeof(bigint_t); } else { quantity = new bigint_t; } @@ -1342,19 +1313,18 @@ void amount_t::read(const char *& data) if (byte == 2) quantity->add_flags(BIGINT_BULK_ALLOC); } else { -#if 0 uint_fast32_t index = *reinterpret_cast<uint_fast32_t *>(const_cast<char *>(data)); data += sizeof(uint_fast32_t); - quantity = reinterpret_cast<bigint_t *>(bigints + (index - 1) * sizeof(bigint_t)); -#endif + quantity = reinterpret_cast<bigint_t *>(*pool + (index - 1) * sizeof(bigint_t)); + DEBUG("amounts.refs", quantity << " ref++, now " << (quantity->ref + 1)); quantity->ref++; } } -void amount_t::write(std::ostream& out, bool optimized) const +void amount_t::write(std::ostream& out, unsigned int index) const { using namespace ledger::binary; @@ -1372,13 +1342,10 @@ void amount_t::write(std::ostream& out, bool optimized) const char byte; - if (! optimized || quantity->index == 0) { - if (optimized) { -#if 0 - quantity->index = ++bigints_index; // if !optimized, this is garbage - bigints_count++; + if (index == 0 || quantity->index == 0) { + if (index != 0) { + quantity->index = index; // if !optimized, this is garbage byte = 2; -#endif } else { byte = 1; } diff --git a/src/amount.h b/src/amount.h index a5a05835..29634fa6 100644 --- a/src/amount.h +++ b/src/amount.h @@ -59,6 +59,7 @@ namespace ledger { class commodity_t; class annotation_t; class commodity_pool_t; +class session_t; DECLARE_EXCEPTION(amount_error, std::runtime_error); @@ -86,9 +87,6 @@ class amount_t > #endif { - // jww (2007-05-03): Make this private, and then make - // ledger::initialize into a member function of session_t. -public: /** * The initialize and shutdown methods ready the amount subsystem * for use. Normally they are called by `ledger::initialize' and @@ -97,6 +95,8 @@ public: static void initialize(); static void shutdown(); + friend class session_t; + public: typedef uint_least16_t precision_t; @@ -671,8 +671,10 @@ public: * knows about. */ void read(std::istream& in); - void read(const char *& data); - void write(std::ostream& out, bool optimize = false) const; + void read(const char *& data, + char ** pool = NULL, + char ** pool_next = NULL); + void write(std::ostream& out, unsigned int index = 0) const; /** * Debugging methods. There are two methods defined to help with diff --git a/src/cache.cc b/src/cache.cc index d9ee7549..0501cee6 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -83,6 +83,12 @@ void read_xact(const char *& data, xact_t * xact) expr_t::compute_amount(xact->amount_expr.get(), xact->amount, xact); } +void write_amount(std::ostream& out, amount_t& amt) +{ + amt.write(out, ++bigints_index); + bigints_count++; +} + void write_xact(std::ostream& out, xact_t * xact, bool ignore_calculated) { @@ -92,7 +98,8 @@ void write_xact(std::ostream& out, xact_t * xact, if (ignore_calculated && xact->has_flags(XACT_CALCULATED)) { write_number<unsigned char>(out, 0); - amount_t().write(out); + amount_t temp; + write_amount(out, temp); } else if (xact->amount_expr) { write_number<unsigned char>(out, 2); @@ -101,18 +108,18 @@ void write_xact(std::ostream& out, xact_t * xact, } else if (! xact->amount_expr->text().empty()) { write_number<unsigned char>(out, 1); - xact->amount.write(out); + write_amount(out, xact->amount); write_string(out, xact->amount_expr->text()); } else { write_number<unsigned char>(out, 0); - xact->amount.write(out); + write_amount(out, xact->amount); } if (xact->cost && (! (ignore_calculated && xact->has_flags(XACT_CALCULATED)))) { write_bool(out, true); - xact->cost->write(out); + write_amount(out, *xact->cost); // jww (2008-07-30): What if there is no cost expression? xact->cost_expr->write(out); } else { @@ -319,21 +326,21 @@ void write_commodity_base_extra(std::ostream& out, foreach (commodity_t::history_map::value_type& pair, commodity->history->prices) { write_number(out, pair.first); - pair.second.write(out); + write_amount(out, pair.second); } write_number(out, commodity->history->last_lookup); } if (commodity->smaller) { write_bool(out, true); - commodity->smaller->write(out); + write_amount(out, *commodity->smaller); } else { write_bool(out, false); } if (commodity->larger) { write_bool(out, true); - commodity->larger->write(out); + write_amount(out, *commodity->larger); } else { write_bool(out, false); } @@ -418,9 +425,26 @@ void write_commodity_annotated(std::ostream& out, // jww (2008-04-22): No longer needed? //write_long(out, ann_comm->base->ident); // jww (2008-04-22): Make a write_annotation_details function; and optional! - ann_comm->details.price->write(out); - ann_comm->details.date->write(out); - ann_comm->details.tag->write(out); + if (ann_comm->details.price) { + write_bool(out, true); + write_amount(out, *ann_comm->details.price); + } else { + write_bool(out, false); + } + + if (ann_comm->details.date) { + write_bool(out, true); + ann_comm->details.date->write(out); + } else { + write_bool(out, false); + } + + if (ann_comm->details.tag) { + write_bool(out, true); + ann_comm->details.tag->write(out); + } else { + write_bool(out, false); + } } inline |