Removed all pending todos from the amount_t code.

This commit is contained in:
John Wiegley 2008-08-17 03:40:21 -04:00
parent 891d7b87c8
commit 752eb99445
3 changed files with 52 additions and 59 deletions

View file

@ -106,13 +106,6 @@ struct amount_t::bigint_t : public supports_flags<>
DEBUG("ledger.validate", "amount_t::bigint_t: ref > 16535"); DEBUG("ledger.validate", "amount_t::bigint_t: ref > 16535");
return false; 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; return true;
} }
}; };
@ -127,7 +120,6 @@ void amount_t::initialize()
mpz_init(temp); mpz_init(temp);
mpz_init(divisor); mpz_init(divisor);
// jww (2007-05-02): Be very careful here!
if (! current_pool) if (! current_pool)
current_pool = new commodity_pool_t; current_pool = new commodity_pool_t;
@ -148,7 +140,6 @@ void amount_t::shutdown()
mpz_clear(temp); mpz_clear(temp);
mpz_clear(divisor); mpz_clear(divisor);
// jww (2007-05-02): Be very careful here!
if (current_pool) { if (current_pool) {
checked_delete(current_pool); checked_delete(current_pool);
current_pool = NULL; current_pool = NULL;
@ -493,16 +484,6 @@ amount_t& amount_t::operator*=(const amount_t& amt)
throw_(amount_error, "Cannot multiply two uninitialized amounts"); 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(); _dup();
mpz_mul(MPZ(quantity), MPZ(quantity), MPZ(amt.quantity)); 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"); 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) if (! amt)
throw_(amount_error, "Divide by zero"); 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; using namespace ledger::binary;
@ -1315,10 +1288,8 @@ void amount_t::read(const char *& data)
if (byte < 3) { if (byte < 3) {
if (byte == 2) { if (byte == 2) {
#if 0 quantity = new(reinterpret_cast<bigint_t *>(*pool_next)) bigint_t;
quantity = new(reinterpret_cast<bigint_t *>(bigints_next)) bigint_t; *pool_next += sizeof(bigint_t);
bigints_next += sizeof(bigint_t);
#endif
} else { } else {
quantity = new bigint_t; quantity = new bigint_t;
} }
@ -1342,19 +1313,18 @@ void amount_t::read(const char *& data)
if (byte == 2) if (byte == 2)
quantity->add_flags(BIGINT_BULK_ALLOC); quantity->add_flags(BIGINT_BULK_ALLOC);
} else { } else {
#if 0
uint_fast32_t index = *reinterpret_cast<uint_fast32_t *>(const_cast<char *>(data)); uint_fast32_t index = *reinterpret_cast<uint_fast32_t *>(const_cast<char *>(data));
data += sizeof(uint_fast32_t); data += sizeof(uint_fast32_t);
quantity = reinterpret_cast<bigint_t *>(bigints + (index - 1) * sizeof(bigint_t)); quantity = reinterpret_cast<bigint_t *>(*pool + (index - 1) * sizeof(bigint_t));
#endif
DEBUG("amounts.refs", DEBUG("amounts.refs",
quantity << " ref++, now " << (quantity->ref + 1)); quantity << " ref++, now " << (quantity->ref + 1));
quantity->ref++; 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; using namespace ledger::binary;
@ -1372,13 +1342,10 @@ void amount_t::write(std::ostream& out, bool optimized) const
char byte; char byte;
if (! optimized || quantity->index == 0) { if (index == 0 || quantity->index == 0) {
if (optimized) { if (index != 0) {
#if 0 quantity->index = index; // if !optimized, this is garbage
quantity->index = ++bigints_index; // if !optimized, this is garbage
bigints_count++;
byte = 2; byte = 2;
#endif
} else { } else {
byte = 1; byte = 1;
} }

View file

@ -59,6 +59,7 @@ namespace ledger {
class commodity_t; class commodity_t;
class annotation_t; class annotation_t;
class commodity_pool_t; class commodity_pool_t;
class session_t;
DECLARE_EXCEPTION(amount_error, std::runtime_error); DECLARE_EXCEPTION(amount_error, std::runtime_error);
@ -86,9 +87,6 @@ class amount_t
> >
#endif #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 * The initialize and shutdown methods ready the amount subsystem
* for use. Normally they are called by `ledger::initialize' and * for use. Normally they are called by `ledger::initialize' and
@ -97,6 +95,8 @@ public:
static void initialize(); static void initialize();
static void shutdown(); static void shutdown();
friend class session_t;
public: public:
typedef uint_least16_t precision_t; typedef uint_least16_t precision_t;
@ -671,8 +671,10 @@ public:
* knows about. * knows about.
*/ */
void read(std::istream& in); void read(std::istream& in);
void read(const char *& data); void read(const char *& data,
void write(std::ostream& out, bool optimize = false) const; 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 * Debugging methods. There are two methods defined to help with

View file

@ -83,6 +83,12 @@ void read_xact(const char *& data, xact_t * xact)
expr_t::compute_amount(xact->amount_expr.get(), xact->amount, 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, void write_xact(std::ostream& out, xact_t * xact,
bool ignore_calculated) bool ignore_calculated)
{ {
@ -92,7 +98,8 @@ void write_xact(std::ostream& out, xact_t * xact,
if (ignore_calculated && xact->has_flags(XACT_CALCULATED)) { if (ignore_calculated && xact->has_flags(XACT_CALCULATED)) {
write_number<unsigned char>(out, 0); write_number<unsigned char>(out, 0);
amount_t().write(out); amount_t temp;
write_amount(out, temp);
} }
else if (xact->amount_expr) { else if (xact->amount_expr) {
write_number<unsigned char>(out, 2); 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()) { else if (! xact->amount_expr->text().empty()) {
write_number<unsigned char>(out, 1); write_number<unsigned char>(out, 1);
xact->amount.write(out); write_amount(out, xact->amount);
write_string(out, xact->amount_expr->text()); write_string(out, xact->amount_expr->text());
} }
else { else {
write_number<unsigned char>(out, 0); write_number<unsigned char>(out, 0);
xact->amount.write(out); write_amount(out, xact->amount);
} }
if (xact->cost && if (xact->cost &&
(! (ignore_calculated && xact->has_flags(XACT_CALCULATED)))) { (! (ignore_calculated && xact->has_flags(XACT_CALCULATED)))) {
write_bool(out, true); write_bool(out, true);
xact->cost->write(out); write_amount(out, *xact->cost);
// jww (2008-07-30): What if there is no cost expression? // jww (2008-07-30): What if there is no cost expression?
xact->cost_expr->write(out); xact->cost_expr->write(out);
} else { } else {
@ -319,21 +326,21 @@ void write_commodity_base_extra(std::ostream& out,
foreach (commodity_t::history_map::value_type& pair, foreach (commodity_t::history_map::value_type& pair,
commodity->history->prices) { commodity->history->prices) {
write_number(out, pair.first); write_number(out, pair.first);
pair.second.write(out); write_amount(out, pair.second);
} }
write_number(out, commodity->history->last_lookup); write_number(out, commodity->history->last_lookup);
} }
if (commodity->smaller) { if (commodity->smaller) {
write_bool(out, true); write_bool(out, true);
commodity->smaller->write(out); write_amount(out, *commodity->smaller);
} else { } else {
write_bool(out, false); write_bool(out, false);
} }
if (commodity->larger) { if (commodity->larger) {
write_bool(out, true); write_bool(out, true);
commodity->larger->write(out); write_amount(out, *commodity->larger);
} else { } else {
write_bool(out, false); write_bool(out, false);
} }
@ -418,9 +425,26 @@ void write_commodity_annotated(std::ostream& out,
// jww (2008-04-22): No longer needed? // jww (2008-04-22): No longer needed?
//write_long(out, ann_comm->base->ident); //write_long(out, ann_comm->base->ident);
// jww (2008-04-22): Make a write_annotation_details function; and optional! // jww (2008-04-22): Make a write_annotation_details function; and optional!
ann_comm->details.price->write(out); if (ann_comm->details.price) {
ann_comm->details.date->write(out); write_bool(out, true);
ann_comm->details.tag->write(out); 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 inline