diff options
author | John Wiegley <johnw@newartisans.com> | 2004-08-21 23:18:45 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2004-08-21 23:18:45 -0400 |
commit | 6d5333b89618734e59853ba7da75b920ac40dae0 (patch) | |
tree | 3ddcc232a2c229a2ace5ca739ba7de3889fb6edc /value.cc | |
parent | 3ff84b7363575c63129e5b7a5b142896b76dcf06 (diff) | |
download | fork-ledger-6d5333b89618734e59853ba7da75b920ac40dae0.tar.gz fork-ledger-6d5333b89618734e59853ba7da75b920ac40dae0.tar.bz2 fork-ledger-6d5333b89618734e59853ba7da75b920ac40dae0.zip |
use value_t instead of balance_pair_t; gains for 10% binary, 2% textual
Diffstat (limited to 'value.cc')
-rw-r--r-- | value.cc | 1026 |
1 files changed, 355 insertions, 671 deletions
@@ -1,4 +1,5 @@ #include "value.h" +#include "ledger.h" namespace ledger { @@ -11,733 +12,333 @@ void value_t::destroy() case BALANCE: ((balance_t *)data)->~balance_t(); break; - default: + case BALANCE_PAIR: + ((balance_pair_t *)data)->~balance_pair_t(); break; - } -} - -value_t& value_t::operator+=(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: - case INTEGER: - switch (type) { - case BOOLEAN: - cast(INTEGER); - case INTEGER: - *((unsigned int *) data) += *((unsigned int *) value.data); - break; - - case AMOUNT: - *((amount_t *) data) += *((unsigned int *) value.data); - break; - - case BALANCE: - *((balance_t *) data) += amount_t(*((unsigned int *) value.data)); - break; - - default: - assert(0); - break; - } - break; - - case AMOUNT: - switch (type) { - case BOOLEAN: - case INTEGER: - cast(AMOUNT); - case AMOUNT: - *((amount_t *) data) += *((amount_t *) value.data); - break; - - case BALANCE: - *((balance_t *) data) += *((amount_t *) value.data); - break; - - default: - assert(0); - break; - } - break; - - case BALANCE: - switch (type) { - case BOOLEAN: - case INTEGER: - case AMOUNT: - cast(BALANCE); - case BALANCE: - *((balance_t *) data) += *((balance_t *) value.data); - break; - - default: - assert(0); - break; - } - break; - default: - assert(0); break; } - return *this; } -value_t& value_t::operator-=(const value_t& value) +value_t& value_t::operator=(const value_t& value) { + destroy(); switch (value.type) { case BOOLEAN: - case INTEGER: - switch (type) { - case BOOLEAN: - cast(INTEGER); - case INTEGER: - *((unsigned int *) data) -= *((unsigned int *) value.data); - break; - - case AMOUNT: - *((amount_t *) data) -= *((unsigned int *) value.data); - break; - - case BALANCE: - *((balance_t *) data) -= amount_t(*((unsigned int *) value.data)); - break; - - default: - assert(0); - break; - } + *((bool *) data) = *((bool *) value.data); break; - case AMOUNT: - switch (type) { - case BOOLEAN: - case INTEGER: - cast(AMOUNT); - case AMOUNT: - *((amount_t *) data) -= *((amount_t *) value.data); - break; - - case BALANCE: - *((balance_t *) data) -= *((amount_t *) value.data); - break; - - default: - assert(0); - break; - } - break; - - case BALANCE: - switch (type) { - case BOOLEAN: - case INTEGER: - case AMOUNT: - cast(BALANCE); - case BALANCE: - *((balance_t *) data) -= *((balance_t *) value.data); - break; - - default: - assert(0); - break; - } - break; - - default: - assert(0); - break; - } - return *this; -} - -value_t& value_t::operator*=(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: case INTEGER: - switch (type) { - case BOOLEAN: - cast(INTEGER); - case INTEGER: - *((unsigned int *) data) *= *((unsigned int *) value.data); - break; - - case AMOUNT: - *((amount_t *) data) *= *((unsigned int *) value.data); - break; - - case BALANCE: - *((balance_t *) data) *= amount_t(*((unsigned int *) value.data)); - break; - - default: - assert(0); - break; - } + *((unsigned int *) data) = *((unsigned int *) value.data); break; case AMOUNT: - switch (type) { - case BOOLEAN: - case INTEGER: - cast(AMOUNT); - case AMOUNT: - *((amount_t *) data) *= *((amount_t *) value.data); - break; - - case BALANCE: - *((balance_t *) data) *= *((amount_t *) value.data); - break; - - default: - assert(0); - break; - } + new((amount_t *)data) amount_t(*((amount_t *) value.data)); break; case BALANCE: - switch (type) { - case BOOLEAN: - case INTEGER: - case AMOUNT: - cast(BALANCE); - case BALANCE: - *((balance_t *) data) *= *((balance_t *) value.data); - break; - - default: - assert(0); - break; - } - break; - - default: - assert(0); - break; - } - return *this; -} - -value_t& value_t::operator/=(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: - case INTEGER: - switch (type) { - case BOOLEAN: - cast(INTEGER); - case INTEGER: - *((unsigned int *) data) /= *((unsigned int *) value.data); - break; - - case AMOUNT: - *((amount_t *) data) /= *((unsigned int *) value.data); - break; - - case BALANCE: - *((balance_t *) data) /= amount_t(*((unsigned int *) value.data)); - break; - - default: - assert(0); - break; - } - break; - - case AMOUNT: - switch (type) { - case BOOLEAN: - case INTEGER: - cast(AMOUNT); - case AMOUNT: - *((amount_t *) data) /= *((amount_t *) value.data); - break; - - case BALANCE: - *((balance_t *) data) /= *((amount_t *) value.data); - break; - - default: - assert(0); - break; - } + new((balance_t *)data) balance_t(*((balance_t *) value.data)); break; - case BALANCE: - switch (type) { - case BOOLEAN: - case INTEGER: - case AMOUNT: - cast(BALANCE); - case BALANCE: - *((balance_t *) data) /= *((balance_t *) value.data); - break; - - default: - assert(0); - break; - } + case BALANCE_PAIR: + new((balance_pair_t *)data) balance_pair_t(*((balance_pair_t *) value.data)); break; default: assert(0); break; } - return *this; -} - -bool value_t::operator==(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: - switch (type) { - case BOOLEAN: - return *((bool *) data) == *((bool *) value.data); - - case INTEGER: - return bool(*((unsigned int *) data)) == *((bool *) value.data); - - case AMOUNT: - return bool(*((amount_t *) data)) == *((bool *) value.data); - - case BALANCE: - return bool(*((balance_t *) data)) == *((bool *) value.data); - - default: - assert(0); - break; - } - break; - - case INTEGER: - switch (type) { - case BOOLEAN: - return ((unsigned int) *((bool *) data)) == *((unsigned int *) value.data); - - case INTEGER: - return *((unsigned int *) data) == *((unsigned int *) value.data); - - case AMOUNT: - return ((unsigned int) *((amount_t *) data)) == *((unsigned int *) value.data); - - case BALANCE: - return ((unsigned int) *((balance_t *) data)) == *((unsigned int *) value.data); - - default: - assert(0); - break; - } - break; - - case AMOUNT: - switch (type) { - case BOOLEAN: - return amount_t(*((bool *) data)) == *((amount_t *) value.data); - - case INTEGER: - return amount_t(*((unsigned int *) data)) == *((amount_t *) value.data); - - case AMOUNT: - return *((amount_t *) data) == *((amount_t *) value.data); - - case BALANCE: - return ((balance_t *) data)->amount() == *((amount_t *) value.data); - - default: - assert(0); - break; - } - break; - - case BALANCE: - switch (type) { - case BOOLEAN: - return balance_t(*((bool *) data)) == *((balance_t *) value.data); - - case INTEGER: - return balance_t(*((unsigned int *) data)) == *((balance_t *) value.data); - - case AMOUNT: - return balance_t(*((amount_t *) data)) == *((balance_t *) value.data); - - case BALANCE: - return *((balance_t *) data) == *((balance_t *) value.data); - default: - assert(0); - break; - } - break; + type = value.type; - default: - assert(0); - break; - } return *this; } -bool value_t::operator<(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: - switch (type) { - case BOOLEAN: - return *((bool *) data) < *((bool *) value.data); - - case INTEGER: - return bool(*((unsigned int *) data)) < *((bool *) value.data); - - case AMOUNT: - return bool(*((amount_t *) data)) < *((bool *) value.data); - - case BALANCE: - return bool(*((balance_t *) data)) < *((bool *) value.data); - - default: - assert(0); - break; - } - break; - - case INTEGER: - switch (type) { - case BOOLEAN: - return ((unsigned int) *((bool *) data)) < *((unsigned int *) value.data); - - case INTEGER: - return *((unsigned int *) data) < *((unsigned int *) value.data); - - case AMOUNT: - return *((amount_t *) data) < *((unsigned int *) value.data); - - case BALANCE: - return *((balance_t *) data) < *((unsigned int *) value.data); - - default: - assert(0); - break; - } - break; - - case AMOUNT: - switch (type) { - case BOOLEAN: - return amount_t(*((bool *) data)) < *((amount_t *) value.data); - - case INTEGER: - return amount_t(*((unsigned int *) data)) < *((amount_t *) value.data); - - case AMOUNT: - return *((amount_t *) data) < *((amount_t *) value.data); - - case BALANCE: - return ((balance_t *) data)->amount(((amount_t *) value.data)->commodity) < *((amount_t *) value.data); - - default: - assert(0); - break; - } - break; - - case BALANCE: - switch (type) { - case BOOLEAN: - return balance_t(*((bool *) data)) < *((balance_t *) value.data); - - case INTEGER: - return balance_t(*((unsigned int *) data)) < *((balance_t *) value.data); - - case AMOUNT: - return balance_t(*((amount_t *) data)) < *((balance_t *) value.data); - - case BALANCE: - return *((balance_t *) data) < *((balance_t *) value.data); - - default: - assert(0); - break; - } - break; - - default: - assert(0); - break; - } - return *this; +#define DEF_VALUE_OP(OP) \ +value_t& value_t::operator OP(const value_t& value) \ +{ \ + switch (value.type) { \ + case BOOLEAN: \ + case INTEGER: \ + switch (type) { \ + case BOOLEAN: \ + cast(INTEGER); \ + \ + case INTEGER: \ + *((unsigned int *) data) OP *((unsigned int *) value.data); \ + break; \ + \ + case AMOUNT: \ + *((amount_t *) data) OP *((unsigned int *) value.data); \ + break; \ + \ + case BALANCE: \ + *((balance_t *) data) OP amount_t(*((unsigned int *) value.data)); \ + break; \ + \ + case BALANCE_PAIR: \ + *((balance_pair_t *) data) OP amount_t(*((unsigned int *) value.data)); \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case AMOUNT: \ + switch (type) { \ + case BOOLEAN: \ + case INTEGER: \ + cast(AMOUNT); \ + \ + case AMOUNT: \ + if (((amount_t *) data)->commodity && \ + ((amount_t *) data)->commodity != \ + ((amount_t *) value.data)->commodity) { \ + cast(BALANCE); \ + return *this OP value; \ + } \ + *((amount_t *) data) OP *((amount_t *) value.data); \ + break; \ + \ + case BALANCE: \ + *((balance_t *) data) OP *((amount_t *) value.data); \ + break; \ + \ + case BALANCE_PAIR: \ + *((balance_pair_t *) data) OP *((amount_t *) value.data); \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE: \ + switch (type) { \ + case BOOLEAN: \ + case INTEGER: \ + case AMOUNT: \ + cast(BALANCE); \ + \ + case BALANCE: \ + *((balance_t *) data) OP *((balance_t *) value.data); \ + break; \ + \ + case BALANCE_PAIR: \ + *((balance_pair_t *) data) OP *((balance_t *) value.data); \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE_PAIR: \ + switch (type) { \ + case BOOLEAN: \ + case INTEGER: \ + case AMOUNT: \ + case BALANCE: \ + cast(BALANCE_PAIR); \ + \ + case BALANCE_PAIR: \ + *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + return *this; \ } -bool value_t::operator<=(const value_t& value) +DEF_VALUE_OP(+=) +DEF_VALUE_OP(-=) +DEF_VALUE_OP(*=) +DEF_VALUE_OP(/=) + +value_t& value_t::operator +=(const transaction_t& xact) { - switch (value.type) { + switch (type) { case BOOLEAN: - switch (type) { - case BOOLEAN: - return *((bool *) data) <= *((bool *) value.data); - - case INTEGER: - return bool(*((unsigned int *) data)) <= *((bool *) value.data); - - case AMOUNT: - return bool(*((amount_t *) data)) <= *((bool *) value.data); - - case BALANCE: - return bool(*((balance_t *) data)) <= *((bool *) value.data); - - default: - assert(0); - break; - } - break; - case INTEGER: - switch (type) { - case BOOLEAN: - return ((unsigned int) *((bool *) data)) <= *((unsigned int *) value.data); - - case INTEGER: - return *((unsigned int *) data) <= *((unsigned int *) value.data); - - case AMOUNT: - return ((unsigned int) *((amount_t *) data)) <= *((unsigned int *) value.data); - - case BALANCE: - return ((unsigned int) *((balance_t *) data)) <= *((unsigned int *) value.data); - - default: - assert(0); - break; - } - break; + cast(AMOUNT); case AMOUNT: - switch (type) { - case BOOLEAN: - return amount_t(*((bool *) data)) <= *((amount_t *) value.data); - - case INTEGER: - return amount_t(*((unsigned int *) data)) <= *((amount_t *) value.data); - - case AMOUNT: - return *((amount_t *) data) <= *((amount_t *) value.data); - - case BALANCE: - return ((balance_t *) data)->amount() <= *((amount_t *) value.data); - - default: - assert(0); - break; - } - break; - - case BALANCE: - switch (type) { - case BOOLEAN: - return balance_t(*((bool *) data)) <= *((balance_t *) value.data); - - case INTEGER: - return balance_t(*((unsigned int *) data)) <= *((balance_t *) value.data); - - case AMOUNT: - return balance_t(*((amount_t *) data)) <= *((balance_t *) value.data); - - case BALANCE: - return *((balance_t *) data) <= *((balance_t *) value.data); - - default: - assert(0); - break; - } - break; - - default: - assert(0); - break; - } - return *this; -} - -bool value_t::operator>(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: - switch (type) { - case BOOLEAN: - return *((bool *) data) > *((bool *) value.data); - - case INTEGER: - return bool(*((unsigned int *) data)) > *((bool *) value.data); - - case AMOUNT: - return bool(*((amount_t *) data)) > *((bool *) value.data); - - case BALANCE: - return bool(*((balance_t *) data)) > *((bool *) value.data); - - default: - assert(0); - break; + if (xact.cost) { + cast(BALANCE_PAIR); + return *this += xact; } - break; - - case INTEGER: - switch (type) { - case BOOLEAN: - return ((unsigned int) *((bool *) data)) > *((unsigned int *) value.data); - - case INTEGER: - return *((unsigned int *) data) > *((unsigned int *) value.data); - - case AMOUNT: - return ((unsigned int) *((amount_t *) data)) > *((unsigned int *) value.data); - - case BALANCE: - return ((unsigned int) *((balance_t *) data)) > *((unsigned int *) value.data); - - default: - assert(0); - break; + else if (((amount_t *) data)->commodity && + ((amount_t *) data)->commodity != xact.amount.commodity) { + cast(BALANCE); + return *this += xact; } + *((amount_t *) data) += xact.amount; break; - case AMOUNT: - switch (type) { - case BOOLEAN: - return amount_t(*((bool *) data)) > *((amount_t *) value.data); - - case INTEGER: - return amount_t(*((unsigned int *) data)) > *((amount_t *) value.data); - - case AMOUNT: - return *((amount_t *) data) > *((amount_t *) value.data); - - case BALANCE: - return ((balance_t *) data)->amount(((amount_t *) value.data)->commodity) > *((amount_t *) value.data); - - default: - assert(0); - break; + case BALANCE: + if (xact.cost) { + cast(BALANCE_PAIR); + return *this += xact; } + *((balance_t *) data) += xact.amount; break; - case BALANCE: - switch (type) { - case BOOLEAN: - return balance_t(*((bool *) data)) > *((balance_t *) value.data); - - case INTEGER: - return balance_t(*((unsigned int *) data)) > *((balance_t *) value.data); - - case AMOUNT: - return balance_t(*((amount_t *) data)) > *((balance_t *) value.data); - - case BALANCE: - return *((balance_t *) data) > *((balance_t *) value.data); - - default: - assert(0); - break; - } + case BALANCE_PAIR: + *((balance_pair_t *) data) += xact; break; default: assert(0); break; } + return *this; } -bool value_t::operator>=(const value_t& value) -{ - switch (value.type) { - case BOOLEAN: - switch (type) { - case BOOLEAN: - return *((bool *) data) >= *((bool *) value.data); - - case INTEGER: - return bool(*((unsigned int *) data)) >= *((bool *) value.data); - - case AMOUNT: - return bool(*((amount_t *) data)) >= *((bool *) value.data); - - case BALANCE: - return bool(*((balance_t *) data)) >= *((bool *) value.data); - - default: - assert(0); - break; - } - break; - - case INTEGER: - switch (type) { - case BOOLEAN: - return ((unsigned int) *((bool *) data)) >= *((unsigned int *) value.data); - - case INTEGER: - return *((unsigned int *) data) >= *((unsigned int *) value.data); - - case AMOUNT: - return ((unsigned int) *((amount_t *) data)) >= *((unsigned int *) value.data); - - case BALANCE: - return ((unsigned int) *((balance_t *) data)) >= *((unsigned int *) value.data); - - default: - assert(0); - break; - } - break; - - case AMOUNT: - switch (type) { - case BOOLEAN: - return amount_t(*((bool *) data)) >= *((amount_t *) value.data); - - case INTEGER: - return amount_t(*((unsigned int *) data)) >= *((amount_t *) value.data); - - case AMOUNT: - return *((amount_t *) data) >= *((amount_t *) value.data); - - case BALANCE: - return ((balance_t *) data)->amount() >= *((amount_t *) value.data); - - default: - assert(0); - break; - } - break; - - case BALANCE: - switch (type) { - case BOOLEAN: - return balance_t(*((bool *) data)) >= *((balance_t *) value.data); - - case INTEGER: - return balance_t(*((unsigned int *) data)) >= *((balance_t *) value.data); - - case AMOUNT: - return balance_t(*((amount_t *) data)) >= *((balance_t *) value.data); - - case BALANCE: - return *((balance_t *) data) >= *((balance_t *) value.data); - - default: - assert(0); - break; - } - break; - - default: - assert(0); - break; - } - return *this; +#define DEF_VALUE_CMP_OP(OP) \ +bool value_t::operator OP(const value_t& value) \ +{ \ + switch (value.type) { \ + case BOOLEAN: \ + switch (type) { \ + case BOOLEAN: \ + return *((bool *) data) OP *((bool *) value.data); \ + \ + case INTEGER: \ + return bool(*((unsigned int *) data)) OP *((bool *) value.data); \ + \ + case AMOUNT: \ + return bool(*((amount_t *) data)) OP *((bool *) value.data); \ + \ + case BALANCE: \ + return bool(*((balance_t *) data)) OP *((bool *) value.data); \ + \ + case BALANCE_PAIR: \ + return bool(*((balance_pair_t *) data)) OP *((bool *) value.data); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case INTEGER: \ + switch (type) { \ + case BOOLEAN: \ + return ((unsigned int) *((bool *) data)) OP *((unsigned int *) value.data); \ + \ + case INTEGER: \ + return *((unsigned int *) data) OP *((unsigned int *) value.data); \ + \ + case AMOUNT: \ + return ((unsigned int) *((amount_t *) data)) OP *((unsigned int *) value.data); \ + \ + case BALANCE: \ + return ((unsigned int) *((balance_t *) data)) OP *((unsigned int *) value.data); \ + \ + case BALANCE_PAIR: \ + return ((unsigned int) *((balance_pair_t *) data)) OP *((unsigned int *) value.data); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case AMOUNT: \ + switch (type) { \ + case BOOLEAN: \ + return amount_t(*((bool *) data)) OP *((amount_t *) value.data); \ + \ + case INTEGER: \ + return amount_t(*((unsigned int *) data)) OP *((amount_t *) value.data); \ + \ + case AMOUNT: \ + return *((amount_t *) data) OP *((amount_t *) value.data); \ + \ + case BALANCE: \ + return ((balance_t *) data)->amount(((amount_t *) value.data)->commodity) OP *((amount_t *) value.data); \ + \ + case BALANCE_PAIR: \ + return ((balance_pair_t *) data)->quantity.amount(((amount_t *) value.data)->commodity) OP *((amount_t *) value.data); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE: \ + switch (type) { \ + case BOOLEAN: \ + return balance_t(*((bool *) data)) OP *((balance_t *) value.data); \ + \ + case INTEGER: \ + return balance_t(*((unsigned int *) data)) OP *((balance_t *) value.data); \ + \ + case AMOUNT: \ + return balance_t(*((amount_t *) data)) OP *((balance_t *) value.data); \ + \ + case BALANCE: \ + return *((balance_t *) data) OP *((balance_t *) value.data); \ + \ + case BALANCE_PAIR: \ + return *((balance_pair_t *) data) OP *((balance_t *) value.data); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + case BALANCE_PAIR: \ + switch (type) { \ + case BOOLEAN: \ + return balance_pair_t(*((bool *) data)) OP *((balance_pair_t *) value.data); \ + \ + case INTEGER: \ + return balance_pair_t(*((unsigned int *) data)) OP *((balance_pair_t *) value.data); \ + \ + case AMOUNT: \ + return balance_pair_t(*((amount_t *) data)) OP *((balance_pair_t *) value.data); \ + \ + case BALANCE: \ + return balance_pair_t(*((balance_t *) data)) OP *((balance_pair_t *) value.data); \ + \ + case BALANCE_PAIR: \ + return *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ + \ + default: \ + assert(0); \ + break; \ + } \ + break; \ + \ + default: \ + assert(0); \ + break; \ + } \ + return *this; \ } +DEF_VALUE_CMP_OP(==) +DEF_VALUE_CMP_OP(<) +DEF_VALUE_CMP_OP(<=) +DEF_VALUE_CMP_OP(>) +DEF_VALUE_CMP_OP(>=) + void value_t::cast(type_t cast_type) { switch (type) { @@ -754,6 +355,9 @@ void value_t::cast(type_t cast_type) case BALANCE: new((balance_t *)data) balance_t(*((bool *) data)); break; + case BALANCE_PAIR: + new((balance_pair_t *)data) balance_pair_t(*((bool *) data)); + break; default: assert(0); @@ -774,6 +378,9 @@ void value_t::cast(type_t cast_type) case BALANCE: new((balance_t *)data) balance_t(*((unsigned int *) data)); break; + case BALANCE_PAIR: + new((balance_pair_t *)data) balance_pair_t(*((unsigned int *) data)); + break; default: assert(0); @@ -803,6 +410,12 @@ void value_t::cast(type_t cast_type) new((balance_t *)data) balance_t(temp); break; } + case BALANCE_PAIR: { + amount_t temp = *((amount_t *) data); + destroy(); + new((balance_pair_t *)data) balance_pair_t(temp); + break; + } default: assert(0); @@ -832,6 +445,47 @@ void value_t::cast(type_t cast_type) } case BALANCE: break; + case BALANCE_PAIR: { + balance_t temp = *((balance_t *) data); + destroy(); + new((balance_pair_t *)data) balance_pair_t(temp); + break; + } + + default: + assert(0); + break; + } + break; + + case BALANCE_PAIR: + switch (cast_type) { + case BOOLEAN: { + bool temp = *((balance_pair_t *) data); + destroy(); + *((bool *)data) = temp; + break; + } + case INTEGER: { + unsigned int temp = ((balance_pair_t *) data)->quantity.amount(); + destroy(); + *((unsigned int *)data) = temp; + break; + } + case AMOUNT: { + amount_t temp = ((balance_pair_t *) data)->quantity.amount(); + destroy(); + new((amount_t *)data) amount_t(temp); + break; + } + case BALANCE: { + balance_t temp = ((balance_pair_t *) data)->quantity; + destroy(); + new((balance_t *)data) balance_t(temp); + break; + } + case BALANCE_PAIR: + break; default: assert(0); @@ -861,6 +515,9 @@ void value_t::negate() case BALANCE: ((balance_t *) data)->negate(); break; + case BALANCE_PAIR: + ((balance_pair_t *) data)->negate(); + break; default: assert(0); @@ -883,11 +540,38 @@ void value_t::abs() case BALANCE: ((balance_t *) data)->abs(); break; + case BALANCE_PAIR: + ((balance_pair_t *) data)->abs(); + break; + + default: + assert(0); + break; + } +} + +value_t value_t::cost() const +{ + switch (type) { + case BOOLEAN: + case INTEGER: + case AMOUNT: + case BALANCE: + return *this; + + case BALANCE_PAIR: + assert(((balance_pair_t *) data)->cost); + if (((balance_pair_t *) data)->cost) + return *(((balance_pair_t *) data)->cost); + else + return ((balance_pair_t *) data)->quantity; default: assert(0); break; } + assert(0); + return value_t(); } } // namespace ledger |