Removed the balance_pair_t type, since it's now an unneeded abstraction.

This type was a holdback from the days before the amount_expr was used
everywhere to determine a transaction's value.
This commit is contained in:
John Wiegley 2009-02-09 14:54:42 -04:00
parent 02110394b4
commit 3f7104e9be
9 changed files with 11 additions and 767 deletions

View file

@ -24,7 +24,6 @@ libledger_util_la_LDFLAGS = -release $(VERSION).0
libledger_math_la_SOURCES = \
src/value.cc \
src/balpair.cc \
src/balance.cc \
src/commodity.cc \
src/amount.cc
@ -89,7 +88,6 @@ pkginclude_HEADERS = \
src/amount.h \
src/commodity.h \
src/balance.h \
src/balpair.h \
src/value.h \
\
src/token.h \

View file

@ -174,9 +174,6 @@ void export_value()
.def(self / double())
.def(double() / self)
.def("add", &value_t::add,
return_value_policy<reference_existing_object>())
.def("negate", &value_t::negate)
.def("in_place_negate", &value_t::in_place_negate)
.def("in_place_not", &value_t::in_place_not)
@ -223,11 +220,8 @@ void export_value()
.def("set_long", &value_t::set_long)
.def("is_amount", &value_t::is_amount)
.def("is_balance", &value_t::is_balance)
.def("is_balance_pair", &value_t::is_balance_pair)
.def("is_string", &value_t::is_string)
.def("set_string", py_set_string)
@ -286,7 +280,6 @@ void export_value()
.value("INTEGER", value_t::INTEGER)
.value("AMOUNT", value_t::AMOUNT)
.value("BALANCE", value_t::BALANCE)
.value("BALANCE_PAIR", value_t::BALANCE_PAIR)
.value("STRING", value_t::STRING)
.value("SEQUENCE", value_t::SEQUENCE)
.value("POINTER", value_t::POINTER)

View file

@ -1,56 +0,0 @@
/*
* Copyright (c) 2003-2009, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "balpair.h"
namespace ledger {
optional<balance_pair_t>
balance_pair_t::value(const optional<datetime_t>& moment,
const optional<commodity_t&>& in_terms_of) const
{
optional<balance_t> temp = quantity().value(moment, in_terms_of);
optional<balance_t> cost_temp;
if (cost)
cost_temp = cost->value(moment, in_terms_of);
if (temp) {
if (cost_temp)
return balance_pair_t(*temp, *cost_temp);
else if (cost)
return balance_pair_t(*temp, *cost);
else
return balance_pair_t(*temp);
}
return none;
}
} // namespace ledger

View file

@ -1,366 +0,0 @@
/*
* Copyright (c) 2003-2009, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @addtogroup math
*/
/**
* @file balpair.h
* @author John Wiegley
*
* @ingroup math
*
* @brief Provides an abstraction around balance_t for tracking costs
*
* When a transaction's amount is added to a balance, only the "value"
* of the amount is added -- not the associated cost of the
* transaction. To provide for this, the balance_pair_t type allows
* for adding amounts and costs simultaneously to a single balance.
* Both are tracked, and any time either the total amount balance or
* the total cost balance may be extracted.
*
* Note: By default, all balance-like operations operate on the amount
* balance, and not the cost. Also, the cost is entirely optional, in
* which case a balance_pair_t may be used as if it were a balance_t,
* from which is it derived.
*/
#ifndef _BALPAIR_H
#define _BALPAIR_H
#include "balance.h"
namespace ledger {
/**
* @brief Brief
*
* Long.
*/
class balance_pair_t
: public balance_t,
public equality_comparable<balance_pair_t,
equality_comparable<balance_pair_t, balance_t,
equality_comparable<balance_pair_t, amount_t,
equality_comparable<balance_pair_t, double,
equality_comparable<balance_pair_t, unsigned long,
equality_comparable<balance_pair_t, long,
additive<balance_pair_t,
additive<balance_pair_t, balance_t,
additive<balance_pair_t, amount_t,
additive<balance_pair_t, double,
additive<balance_pair_t, unsigned long,
additive<balance_pair_t, long,
multiplicative<balance_pair_t, amount_t,
multiplicative<balance_pair_t, balance_t,
multiplicative<balance_pair_t, double,
multiplicative<balance_pair_t, unsigned long,
multiplicative<balance_pair_t, long> > > > > > > > > > > > > > > > >
{
/**
* The `cost' member of a balance pair tracks the cost associated
* with each transaction amount that is added. This member is
* optional, and if not cost-bearing transactions are added, it will
* remain uninitialized.
*/
optional<balance_t> cost;
friend class value_t;
friend class entry_base_t;
public:
/**
* Constructors. balance_pair_t supports identical forms of construction
* to balance_t. See balance_t for more information.
*/
balance_pair_t() {
TRACE_CTOR(balance_pair_t, "");
}
balance_pair_t(const balance_t& bal) : balance_t(bal) {
TRACE_CTOR(balance_pair_t, "const balance_t&");
}
balance_pair_t(const balance_t& bal, const balance_t& cost_bal)
: balance_t(bal), cost(cost_bal) {
TRACE_CTOR(balance_pair_t, "const balance_t&, const balance_t&");
}
balance_pair_t(const amount_t& amt) : balance_t(amt) {
TRACE_CTOR(balance_pair_t, "const amount_t&");
}
balance_pair_t(const amount_t& amt, const amount_t& cost_amt)
: balance_t(amt), cost(cost_amt) {
TRACE_CTOR(balance_pair_t, "const amount_t&, const amount_t&");
}
balance_pair_t(const double val) : balance_t(val) {
TRACE_CTOR(balance_pair_t, "const double");
}
balance_pair_t(const unsigned long val) : balance_t(val) {
TRACE_CTOR(balance_pair_t, "const unsigned long");
}
balance_pair_t(const long val) : balance_t(val) {
TRACE_CTOR(balance_pair_t, "const long");
}
explicit balance_pair_t(const string& val) : balance_t(val) {
TRACE_CTOR(balance_pair_t, "const string&");
}
explicit balance_pair_t(const char * val) : balance_t(val) {
TRACE_CTOR(balance_pair_t, "const char *");
}
/**
* Destructor.
*/
virtual ~balance_pair_t() {
TRACE_DTOR(balance_pair_t);
}
/**
* Assignment and copy operators. A balance pair may be assigned or
* copied, and assigned or copied from a balance.
*/
balance_pair_t(const balance_pair_t& bal_pair)
: balance_t(bal_pair), cost(bal_pair.cost) {
TRACE_CTOR(balance_pair_t, "copy");
}
balance_pair_t& operator=(const balance_pair_t& bal_pair) {
if (this != &bal_pair) {
balance_t::operator=(bal_pair.quantity());
cost = bal_pair.cost;
}
return *this;
}
balance_pair_t& operator=(const balance_t& bal) {
balance_t::operator=(bal);
return *this;
}
balance_pair_t& operator=(const amount_t& amt) {
balance_t::operator=(amt);
return *this;
}
balance_t& operator=(const string& str) {
return *this = balance_t(str);
}
balance_t& operator=(const char * str) {
return *this = balance_t(str);
}
/**
* Binary arithmetic operators. Balances support addition and
* subtraction of other balance pairs, balances or amounts, but
* multiplication and division are restricted to uncommoditized
* amounts only.
*
* There is also an additional additive method called `add' which
* allows for adding an amount and an associated cost
* simultaneously. The signature is:
* add(amount_t amount, optional<amount_t> cost)
*/
balance_pair_t& operator+=(const balance_pair_t& bal_pair) {
balance_t::operator+=(bal_pair);
if (bal_pair.cost) {
if (! cost)
cost = quantity();
*cost += *bal_pair.cost;
}
return *this;
}
balance_pair_t& operator-=(const balance_pair_t& bal_pair) {
balance_t::operator+=(bal_pair);
if (bal_pair.cost) {
if (! cost)
cost = quantity();
*cost += *bal_pair.cost;
}
return *this;
}
virtual balance_pair_t& operator*=(const amount_t& amt) {
balance_t::operator*=(amt);
if (cost)
*cost *= amt;
return *this;
}
virtual balance_pair_t& operator/=(const amount_t& amt) {
balance_t::operator/=(amt);
if (cost)
*cost /= amt;
return *this;
}
balance_pair_t& add(const amount_t& amt,
const optional<amount_t>& a_cost = none)
{
if (a_cost && ! cost)
cost = quantity();
*this += amt;
if (cost)
*cost += a_cost ? *a_cost : amt;
return *this;
}
/**
* Unary arithmetic operators. There are only a few unary methods
* supported for balance pairs (otherwise, the operators inherited
* from balance_t are used):
*
* abs() returns the absolute value of both the quantity and the
* cost of a balance pair.
*
* in_place_negate() negates all the amounts in both the quantity
* and the cost.
*
* in_place_reduce() reduces all the amounts in both the quantity
* and the cost.
*
* in_place_unreduce() unreduces all the amounts in both the
* quantity and the cost.
*
* quantity() returns the balance part of a balance. It is the same
* as doing a downcast<balance_t>(balance_pair).
*/
balance_pair_t abs() const {
balance_t temp;
foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.abs();
if (cost) {
balance_t cost_temp;
foreach (const amounts_map::value_type& pair, amounts)
cost_temp += pair.second.abs();
return balance_pair_t(temp, cost_temp);
}
return temp;
}
virtual balance_t& in_place_negate() {
balance_t::in_place_negate();
if (cost)
cost->in_place_negate();
return *this;
}
virtual balance_t& in_place_reduce() {
// A temporary must be used here because reduction may cause
// multiple component amounts to collapse to the same commodity.
balance_t temp;
foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.reduce();
if (cost) {
balance_t cost_temp;
foreach (const amounts_map::value_type& pair, amounts)
cost_temp += pair.second.reduce();
return *this = balance_pair_t(temp, cost_temp);
}
return *this = temp;
}
virtual balance_t& in_place_unreduce() {
// A temporary must be used here because unreduction may cause
// multiple component amounts to collapse to the same commodity.
balance_t temp;
foreach (const amounts_map::value_type& pair, amounts)
temp += pair.second.unreduce();
if (cost) {
balance_t cost_temp;
foreach (const amounts_map::value_type& pair, amounts)
cost_temp += pair.second.unreduce();
return *this = balance_pair_t(temp, cost_temp);
}
return *this = temp;
}
balance_t& quantity() {
return *this;
}
const balance_t& quantity() const {
return *this;
}
optional<balance_pair_t>
value(const optional<datetime_t>& moment = none,
const optional<commodity_t&>& in_terms_of = none) const;
/**
* Truth tests. An balance pair may be truth tested by comparison
* to another balance pair, or by using one of the inherited
* operators from balance_t.
*/
bool operator==(const balance_pair_t& bal_pair) const {
if (quantity() != bal_pair.quantity())
return false;
if ((cost && ! bal_pair.cost) ||
(! cost && bal_pair.cost))
return false;
if (*cost != *bal_pair.cost)
return false;
return true;
}
bool operator==(const balance_t& bal) const {
return balance_t::operator==(bal);
}
bool operator==(const amount_t& amt) const {
return balance_t::operator==(amt);
}
template <typename T>
bool operator==(const T& val) const {
return balance_t::operator==(val);
}
/**
* Debugging methods. There is only one method specifically for
* balance pairs to help with debugging:
*
* valid() returns true if the balances within the balance pair are
* valid.
*/
bool valid() const {
if (! balance_t::valid())
return false;
if (cost && ! cost->valid())
return false;
return true;
}
};
} // namespace ledger
#endif // _BALPAIR_H

View file

@ -262,7 +262,6 @@ namespace {
break;
case value_t::BALANCE:
case value_t::BALANCE_PAIR:
xdata.value = temp;
flags |= XACT_EXT_COMPOUND;
break;

View file

@ -274,8 +274,6 @@ void format_equity::flush()
const balance_t * bal;
if (total.is_type(value_t::BALANCE))
bal = &(total.as_balance());
else if (total.is_type(value_t::BALANCE_PAIR))
bal = &(total.as_balance_pair().quantity());
else
assert(false);
@ -304,8 +302,6 @@ void format_equity::operator()(account_t& account)
const balance_t * bal;
if (val.is_type(value_t::BALANCE))
bal = &(val.as_balance());
else if (val.is_type(value_t::BALANCE_PAIR))
bal = &(val.as_balance_pair().quantity());
else
assert(false);

View file

@ -927,14 +927,6 @@ xact_t * instance_t::parse_xact(char * line,
diff = amt;
break;
case value_t::BALANCE_PAIR:
if (optional<amount_t> comm_bal =
xdata.value.as_balance_pair().commodity_amount(amt.commodity()))
diff = amt - *comm_bal;
else
diff = amt;
break;
default:
diff = amt;
break;

View file

@ -64,12 +64,6 @@ value_t::storage_t& value_t::storage_t::operator=(const value_t::storage_t& rhs)
(const_cast<char *>(rhs.data)));
break;
case BALANCE_PAIR:
*reinterpret_cast<balance_pair_t **>(data) =
new balance_pair_t(**reinterpret_cast<balance_pair_t **>
(const_cast<char *>(rhs.data)));
break;
case STRING:
new(reinterpret_cast<string *>(data))
string(*reinterpret_cast<string *>(const_cast<char *>(rhs.data)));
@ -105,9 +99,6 @@ void value_t::storage_t::destroy()
case BALANCE:
checked_delete(*reinterpret_cast<balance_t **>(data));
break;
case BALANCE_PAIR:
checked_delete(*reinterpret_cast<balance_pair_t **>(data));
break;
case STRING:
reinterpret_cast<string *>(data)->~string();
break;
@ -147,7 +138,6 @@ void value_t::initialize()
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(long));
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(amount_t));
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(balance_t *));
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(balance_pair_t *));
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(string));
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(mask_t));
BOOST_STATIC_ASSERT(sizeof(amount_t) >= sizeof(sequence_t *));
@ -165,8 +155,6 @@ void value_t::initialize()
<< " sizeof(amount_t)");
DEBUG_(std::setw(3) << std::right << sizeof(balance_t *)
<< " sizeof(balance_t *)");
DEBUG_(std::setw(3) << std::right << sizeof(balance_pair_t *)
<< " sizeof(balance_pair_t *)");
DEBUG_(std::setw(3) << std::right << sizeof(string)
<< " sizeof(string)");
DEBUG_(std::setw(3) << std::right << sizeof(mask_t)
@ -207,8 +195,6 @@ value_t::operator bool() const
return as_amount();
case BALANCE:
return as_balance();
case BALANCE_PAIR:
return as_balance_pair();
case STRING:
return ! as_string().empty();
case SEQUENCE:
@ -289,17 +275,6 @@ balance_t value_t::to_balance() const
}
}
balance_pair_t value_t::to_balance_pair() const
{
if (is_balance_pair()) {
return as_balance_pair();
} else {
value_t temp(*this);
temp.in_place_cast(BALANCE_PAIR);
return temp.as_balance_pair();
}
}
string value_t::to_string() const
{
if (is_string()) {
@ -346,12 +321,6 @@ void value_t::in_place_simplify()
return;
}
if (is_balance_pair() &&
(! as_balance_pair().cost || as_balance_pair().cost->is_realzero())) {
DEBUG_("Reducing balance pair to balance");
in_place_cast(BALANCE);
}
if (is_balance() && as_balance().amounts.size() == 1) {
DEBUG_("Reducing balance to amount");
DEBUG("ledger.value.reduce", "as a balance it looks like: " << *this);
@ -428,10 +397,6 @@ value_t& value_t::operator+=(const value_t& val)
in_place_cast(BALANCE);
as_balance_lval() += val.as_balance();
return *this;
case BALANCE_PAIR:
in_place_cast(BALANCE_PAIR);
as_balance_pair_lval() += val.as_balance_pair();
return *this;
default:
break;
}
@ -464,10 +429,6 @@ value_t& value_t::operator+=(const value_t& val)
as_balance_lval() += val.as_balance();
return *this;
case BALANCE_PAIR:
in_place_cast(BALANCE_PAIR);
as_balance_pair_lval() += val.as_balance_pair();
return *this;
default:
break;
}
@ -484,29 +445,6 @@ value_t& value_t::operator+=(const value_t& val)
case BALANCE:
as_balance_lval() += val.as_balance();
return *this;
case BALANCE_PAIR:
in_place_cast(BALANCE_PAIR);
as_balance_pair_lval() += val.as_balance_pair();
return *this;
default:
break;
}
break;
case BALANCE_PAIR:
switch (val.type()) {
case INTEGER:
as_balance_pair_lval() += val.to_amount();
return *this;
case AMOUNT:
as_balance_pair_lval() += val.as_amount();
return *this;
case BALANCE:
as_balance_pair_lval() += val.as_balance();
return *this;
case BALANCE_PAIR:
as_balance_pair_lval() += val.as_balance_pair();
return *this;
default:
break;
}
@ -581,11 +519,6 @@ value_t& value_t::operator-=(const value_t& val)
as_balance_lval() -= val.as_balance();
in_place_simplify();
return *this;
case BALANCE_PAIR:
in_place_cast(BALANCE_PAIR);
as_balance_pair_lval() -= val.as_balance_pair();
in_place_simplify();
return *this;
default:
break;
}
@ -625,11 +558,6 @@ value_t& value_t::operator-=(const value_t& val)
in_place_simplify();
return *this;
case BALANCE_PAIR:
in_place_cast(BALANCE_PAIR);
as_balance_pair_lval() -= val.as_balance_pair();
in_place_simplify();
return *this;
default:
break;
}
@ -649,34 +577,6 @@ value_t& value_t::operator-=(const value_t& val)
as_balance_lval() -= val.as_balance();
in_place_simplify();
return *this;
case BALANCE_PAIR:
in_place_cast(BALANCE_PAIR);
as_balance_pair_lval() -= val.as_balance_pair();
in_place_simplify();
return *this;
default:
break;
}
break;
case BALANCE_PAIR:
switch (val.type()) {
case INTEGER:
as_balance_pair_lval() -= val.to_amount();
in_place_simplify();
return *this;
case AMOUNT:
as_balance_pair_lval() -= val.as_amount();
in_place_simplify();
return *this;
case BALANCE:
as_balance_pair_lval() -= val.as_balance();
in_place_simplify();
return *this;
case BALANCE_PAIR:
as_balance_pair_lval() -= val.as_balance_pair();
in_place_simplify();
return *this;
default:
break;
}
@ -756,22 +656,6 @@ value_t& value_t::operator*=(const value_t& val)
}
break;
case BALANCE_PAIR:
switch (val.type()) {
case INTEGER:
as_balance_pair_lval() *= val.as_long();
return *this;
case AMOUNT:
if (! val.as_amount().has_commodity()) {
as_balance_pair_lval() *= val.as_amount();
return *this;
}
break;
default:
break;
}
break;
default:
break;
}
@ -831,22 +715,6 @@ value_t& value_t::operator/=(const value_t& val)
}
break;
case BALANCE_PAIR:
switch (val.type()) {
case INTEGER:
as_balance_pair_lval() /= val.as_long();
return *this;
case AMOUNT:
if (! val.as_amount().has_commodity()) {
as_balance_pair_lval() /= val.as_amount();
return *this;
}
break;
default:
break;
}
break;
default:
break;
}
@ -886,8 +754,6 @@ bool value_t::is_equal_to(const value_t& val) const
return val.as_amount() == to_amount();
case BALANCE:
return val.as_balance() == to_amount();
case BALANCE_PAIR:
return val.as_balance_pair() == to_amount();
default:
break;
}
@ -901,8 +767,6 @@ bool value_t::is_equal_to(const value_t& val) const
return as_amount() == val.as_amount();
case BALANCE:
return val.as_balance() == as_amount();
case BALANCE_PAIR:
return val.as_balance_pair() == as_amount();
default:
break;
}
@ -916,23 +780,6 @@ bool value_t::is_equal_to(const value_t& val) const
return as_balance() == val.as_amount();
case BALANCE:
return as_balance() == val.as_balance();
case BALANCE_PAIR:
return val.as_balance_pair() == as_balance();
default:
break;
}
break;
case BALANCE_PAIR:
switch (val.type()) {
case INTEGER:
return as_balance_pair() == val.to_amount();
case AMOUNT:
return as_balance_pair() == val.as_amount();
case BALANCE:
return as_balance_pair() == val.as_balance();
case BALANCE_PAIR:
return as_balance_pair() == val.as_balance_pair();
default:
break;
}
@ -1101,9 +948,6 @@ void value_t::in_place_cast(type_t cast_type)
case BALANCE:
set_balance(to_amount());
return;
case BALANCE_PAIR:
set_balance_pair(to_amount());
return;
case STRING:
set_string(lexical_cast<string>(as_long()));
return;
@ -1127,12 +971,6 @@ void value_t::in_place_cast(type_t cast_type)
else
set_balance(as_amount()); // creates temporary
return;
case BALANCE_PAIR:
if (amt.is_null())
set_balance_pair(balance_pair_t());
else
set_balance_pair(as_amount()); // creates temporary
return;
case STRING:
if (amt.is_null())
set_string("");
@ -1166,37 +1004,6 @@ void value_t::in_place_cast(type_t cast_type)
}
break;
}
case BALANCE_PAIR:
set_balance_pair(as_balance());
return;
default:
break;
}
break;
case BALANCE_PAIR:
switch (cast_type) {
case AMOUNT: {
const balance_t& temp(as_balance_pair().quantity());
if (temp.amounts.size() == 1) {
set_amount(amount_t((*temp.amounts.begin()).second));
return;
}
else if (temp.amounts.size() == 0) {
set_amount(0L);
return;
}
else {
throw_(value_error, "Cannot convert " << label() <<
" with multiple commodities to " << label(cast_type));
}
break;
}
case BALANCE:
// A temporary is required, becaues set_balance is going to wipe us out
// before assigned the value passed in.
set_balance(balance_t(as_balance_pair().quantity()));
return;
default:
break;
}
@ -1249,9 +1056,6 @@ void value_t::in_place_negate()
case BALANCE:
as_balance_lval().in_place_negate();
return;
case BALANCE_PAIR:
as_balance_pair_lval().in_place_negate();
return;
default:
break;
}
@ -1278,9 +1082,6 @@ void value_t::in_place_not()
case BALANCE:
set_boolean(! as_balance());
return;
case BALANCE_PAIR:
set_boolean(! as_balance_pair());
return;
case STRING:
set_boolean(as_string().empty());
return;
@ -1306,8 +1107,6 @@ bool value_t::is_realzero() const
return as_amount().is_realzero();
case BALANCE:
return as_balance().is_realzero();
case BALANCE_PAIR:
return as_balance_pair().is_realzero();
case STRING:
return as_string().empty();
case SEQUENCE:
@ -1337,8 +1136,6 @@ bool value_t::is_zero() const
return as_amount().is_zero();
case BALANCE:
return as_balance().is_zero();
case BALANCE_PAIR:
return as_balance_pair().is_zero();
case STRING:
return as_string().empty();
case SEQUENCE:
@ -1370,12 +1167,6 @@ value_t value_t::value(const optional<datetime_t>& moment,
return *bal;
return false;
}
case BALANCE_PAIR: {
if (optional<balance_pair_t> bal_pair =
as_balance_pair().value(moment, in_terms_of))
return *bal_pair;
return false;
}
default:
break;
@ -1394,9 +1185,6 @@ void value_t::in_place_reduce()
case BALANCE:
as_balance_lval().in_place_reduce();
return;
case BALANCE_PAIR:
as_balance_pair_lval().in_place_reduce();
return;
default:
return;
}
@ -1413,9 +1201,6 @@ void value_t::in_place_unreduce()
case BALANCE:
as_balance_lval().in_place_unreduce();
return;
case BALANCE_PAIR:
as_balance_pair_lval().in_place_unreduce();
return;
default:
return;
}
@ -1436,8 +1221,6 @@ value_t value_t::abs() const
return as_amount().abs();
case BALANCE:
return as_balance().abs();
case BALANCE_PAIR:
return as_balance_pair().abs();
default:
break;
}
@ -1567,8 +1350,6 @@ value_t value_t::strip_annotations(const keep_details_t& what_to_keep) const
return as_amount().strip_annotations(what_to_keep);
case BALANCE:
return as_balance().strip_annotations(what_to_keep);
case BALANCE_PAIR:
return as_balance_pair().quantity().strip_annotations(what_to_keep);
default:
assert(false);
@ -1586,13 +1367,6 @@ value_t value_t::cost() const
case BALANCE:
return *this;
case BALANCE_PAIR:
assert(as_balance_pair().cost);
if (as_balance_pair().cost)
return *(as_balance_pair().cost);
else
return as_balance_pair().quantity();
default:
break;
}
@ -1601,45 +1375,6 @@ value_t value_t::cost() const
return NULL_VALUE;
}
value_t& value_t::add(const amount_t& amount, const optional<amount_t>& tcost)
{
switch (type()) {
case INTEGER:
case AMOUNT:
if (tcost) {
in_place_cast(BALANCE_PAIR);
return add(amount, tcost);
}
else if ((is_amount() &&
as_amount().commodity() != amount.commodity()) ||
(! is_amount() && amount.commodity())) {
in_place_cast(BALANCE);
return add(amount, tcost);
}
else if (! is_amount()) {
in_place_cast(AMOUNT);
}
return *this += amount;
case BALANCE:
if (tcost) {
in_place_cast(BALANCE_PAIR);
return add(amount, tcost);
}
return *this += amount;
case BALANCE_PAIR:
as_balance_pair_lval().add(amount, tcost);
return *this;
default:
break;
}
throw_(value_error, "Cannot add an amount to " << label());
return *this;
}
void value_t::print(std::ostream& out,
const int first_width,
const int latter_width,
@ -1702,9 +1437,6 @@ void value_t::print(std::ostream& out,
case BALANCE:
as_balance().print(out, first_width, latter_width);
break;
case BALANCE_PAIR:
as_balance_pair().print(out, first_width, latter_width);
break;
default:
throw_(value_error, "Cannot print " << label());
@ -1747,9 +1479,6 @@ void value_t::dump(std::ostream& out, const bool relaxed) const
case BALANCE:
out << as_balance();
break;
case BALANCE_PAIR:
out << as_balance_pair();
break;
case STRING:
out << '"' << as_string() << '"';
@ -1791,8 +1520,6 @@ bool value_t::valid() const
return as_amount().valid();
case BALANCE:
return as_balance().valid();
case BALANCE_PAIR:
return as_balance_pair().valid();
default:
break;
}

View file

@ -49,7 +49,7 @@
#ifndef _VALUE_H
#define _VALUE_H
#include "balpair.h" // pulls in balance.h and amount.h
#include "balance.h"
#include "mask.h"
namespace ledger {
@ -72,16 +72,13 @@ DECLARE_EXCEPTION(value_error, std::runtime_error);
*/
class value_t
: public ordered_field_operators<value_t,
equality_comparable<value_t, balance_pair_t,
equality_comparable<value_t, balance_t,
additive<value_t, balance_pair_t,
additive<value_t, balance_t,
multiplicative<value_t, balance_pair_t,
multiplicative<value_t, balance_t,
ordered_field_operators<value_t, amount_t,
ordered_field_operators<value_t, double,
ordered_field_operators<value_t, unsigned long,
ordered_field_operators<value_t, long> > > > > > > > > > >
ordered_field_operators<value_t, long> > > > > > > >
{
public:
/**
@ -107,7 +104,6 @@ public:
INTEGER, // a signed integer value
AMOUNT, // a ledger::amount_t
BALANCE, // a ledger::balance_t
BALANCE_PAIR, // a ledger::balance_pair_t
STRING, // a string object
MASK, // a regular expression mask
SEQUENCE, // a vector of value_t objects
@ -297,10 +293,6 @@ public:
TRACE_CTOR(value_t, "const balance_t&");
set_balance(val);
}
value_t(const balance_pair_t& val) {
TRACE_CTOR(value_t, "const balance_pair_t&");
set_balance_pair(val);
}
value_t(const mask_t& val) {
TRACE_CTOR(value_t, "const mask_t&");
set_mask(val);
@ -389,11 +381,6 @@ public:
value_t& operator*=(const value_t& val);
value_t& operator/=(const value_t& val);
// This special form of add is use to produce a balance pair by
// simultaneously adding both an amount and its cost.
value_t& add(const amount_t& amount,
const optional<amount_t>& cost = none);
/**
* Unary arithmetic operators.
*/
@ -488,7 +475,6 @@ public:
* is_date()
* is_amount()
* is_balance()
* is_balance_pair()
* is_string()
* is_mask()
* is_sequence()
@ -620,28 +606,6 @@ public:
*reinterpret_cast<balance_t **>(storage->data) = new balance_t(val);
}
bool is_balance_pair() const {
return is_type(BALANCE_PAIR);
}
balance_pair_t& as_balance_pair_lval() {
assert(is_balance_pair());
_dup();
balance_pair_t& bal_pair(**reinterpret_cast<balance_pair_t **>(storage->data));
assert(bal_pair.valid());
return bal_pair;
}
const balance_pair_t& as_balance_pair() const {
assert(is_balance_pair());
balance_pair_t& bal_pair(**reinterpret_cast<balance_pair_t **>(storage->data));
assert(bal_pair.valid());
return bal_pair;
}
void set_balance_pair(const balance_pair_t& val) {
assert(val.valid());
set_type(BALANCE_PAIR);
*reinterpret_cast<balance_pair_t **>(storage->data) = new balance_pair_t(val);
}
bool is_string() const {
return is_type(STRING);
}
@ -758,16 +722,15 @@ public:
* its underlying type, where possible. If not possible, an
* exception is thrown.
*/
bool to_boolean() const;
long to_long() const;
datetime_t to_datetime() const;
date_t to_date() const;
amount_t to_amount() const;
balance_t to_balance() const;
balance_pair_t to_balance_pair() const;
string to_string() const;
mask_t to_mask() const;
sequence_t to_sequence() const;
bool to_boolean() const;
long to_long() const;
datetime_t to_datetime() const;
date_t to_date() const;
amount_t to_amount() const;
balance_t to_balance() const;
string to_string() const;
mask_t to_mask() const;
sequence_t to_sequence() const;
/**
* Dynamic typing conversion methods.
@ -909,8 +872,6 @@ public:
return "an amount";
case BALANCE:
return "a balance";
case BALANCE_PAIR:
return "a balance pair";
case STRING:
return "a string";
case MASK: