diff options
Diffstat (limited to 'src/amount.cc')
-rw-r--r-- | src/amount.cc | 101 |
1 files changed, 87 insertions, 14 deletions
diff --git a/src/amount.cc b/src/amount.cc index a6788af9..3ac47c59 100644 --- a/src/amount.cc +++ b/src/amount.cc @@ -56,25 +56,25 @@ struct amount_t::bigint_t : public supports_flags<> mpq_t val; precision_t prec; - uint_least16_t ref; + uint_least16_t refc; #define MP(bigint) ((bigint)->val) - bigint_t() : prec(0), ref(1) { + bigint_t() : prec(0), refc(1) { TRACE_CTOR(bigint_t, ""); mpq_init(val); } bigint_t(const bigint_t& other) : supports_flags<>(static_cast<uint_least8_t> (other.flags() & ~BIGINT_BULK_ALLOC)), - prec(other.prec), ref(1) { + prec(other.prec), refc(1) { TRACE_CTOR(bigint_t, "copy"); mpq_init(val); mpq_set(val, other.val); } ~bigint_t() { TRACE_DTOR(bigint_t); - assert(ref == 0); + assert(refc == 0); mpq_clear(val); } @@ -83,8 +83,8 @@ struct amount_t::bigint_t : public supports_flags<> DEBUG("ledger.validate", "amount_t::bigint_t: prec > 128"); return false; } - if (ref > 16535) { - DEBUG("ledger.validate", "amount_t::bigint_t: ref > 16535"); + if (refc > 16535) { + DEBUG("ledger.validate", "amount_t::bigint_t: refc > 16535"); return false; } if (flags() & ~(BIGINT_BULK_ALLOC | BIGINT_KEEP_PREC)) { @@ -94,6 +94,20 @@ struct amount_t::bigint_t : public supports_flags<> } return true; } + +#if defined(HAVE_BOOST_SERIALIZATION) +private: + friend class boost::serialization::access; + + template<class Archive> + void serialize(Archive& ar, const unsigned int /* version */) + { + ar & boost::serialization::base_object<supports_flags<> >(*this); + ar & val; + ar & prec; + ar & refc; + } +#endif // HAVE_BOOST_SERIALIZATION }; shared_ptr<commodity_pool_t> amount_t::current_pool; @@ -147,8 +161,8 @@ void amount_t::_copy(const amount_t& amt) } else { quantity = amt.quantity; DEBUG("amounts.refs", - quantity << " ref++, now " << (quantity->ref + 1)); - quantity->ref++; + quantity << " refc++, now " << (quantity->refc + 1)); + quantity->refc++; } } commodity_ = amt.commodity_; @@ -160,7 +174,7 @@ void amount_t::_dup() { VERIFY(valid()); - if (quantity->ref > 1) { + if (quantity->refc > 1) { bigint_t * q = new bigint_t(*quantity); _release(); quantity = q; @@ -184,9 +198,9 @@ void amount_t::_release() { VERIFY(valid()); - DEBUG("amounts.refs", quantity << " ref--, now " << (quantity->ref - 1)); + DEBUG("amounts.refs", quantity << " refc--, now " << (quantity->refc - 1)); - if (--quantity->ref == 0) { + if (--quantity->refc == 0) { if (quantity->has_flags(BIGINT_BULK_ALLOC)) quantity->~bigint_t(); else @@ -920,7 +934,7 @@ bool amount_t::parse(std::istream& in, const parse_flags_t& flags) quantity = new bigint_t; safe_holder.reset(quantity); } - else if (quantity->ref > 1) { + else if (quantity->refc > 1) { _release(); quantity = new bigint_t; safe_holder.reset(quantity); @@ -1095,8 +1109,8 @@ bool amount_t::valid() const return false; } - if (quantity->ref == 0) { - DEBUG("ledger.validate", "amount_t: quantity->ref == 0"); + if (quantity->refc == 0) { + DEBUG("ledger.validate", "amount_t: quantity->refc == 0"); return false; } } @@ -1107,4 +1121,63 @@ bool amount_t::valid() const return true; } +#if defined(HAVE_BOOST_SERIALIZATION) + +template<class Archive> +void amount_t::serialize(Archive& ar, const unsigned int /* version */) +{ + ar & current_pool; + ar & is_initialized; + ar & quantity; + ar & commodity_; +} + +#endif // HAVE_BOOST_SERIALIZATION + } // namespace ledger + +#if defined(HAVE_BOOST_SERIALIZATION) +namespace boost { +namespace serialization { + +template <class Archive> +void serialize(Archive& ar, MP_INT& mpz, const unsigned int /* version */) +{ + ar & mpz._mp_alloc; + ar & mpz._mp_size; + ar & mpz._mp_d; +} + +template <class Archive> +void serialize(Archive& ar, MP_RAT& mpq, const unsigned int /* version */) +{ + ar & mpq._mp_num; + ar & mpq._mp_den; +} + +template <class Archive> +void serialize(Archive& ar, long unsigned int& integer, + const unsigned int /* version */) +{ + ar & make_binary_object(&integer, sizeof(long unsigned int)); +} + +} // namespace serialization +} // namespace boost + +BOOST_CLASS_EXPORT(ledger::annotated_commodity_t) + +template void boost::serialization::serialize(boost::archive::binary_oarchive&, + MP_INT&, const unsigned int); +template void boost::serialization::serialize(boost::archive::binary_iarchive&, + MP_RAT&, const unsigned int); +template void boost::serialization::serialize(boost::archive::binary_iarchive&, + long unsigned int&, + const unsigned int); + +template void ledger::amount_t::serialize(boost::archive::binary_oarchive&, + const unsigned int); +template void ledger::amount_t::serialize(boost::archive::binary_iarchive&, + const unsigned int); + +#endif // HAVE_BOOST_SERIALIZATION |