fixed several issues in value_t's core

This commit is contained in:
John Wiegley 2004-09-21 03:41:40 -04:00
parent 565a128b1a
commit 9f8a32ab48
9 changed files with 406 additions and 216 deletions

6
NEWS
View file

@ -61,11 +61,11 @@
- To include entries from a file into a specific account, use:
@ ACCOUNT
!account ACCOUNT
!include FILE
@@
!end
Entries specified within "@ ACCOUNT/@@" will affect only that
Entries specified within an !account block will affect only that
account.
- Register reports now show only the account affected by default. Use

View file

@ -463,54 +463,11 @@ void amount_t::negate()
}
}
// integer comparisons
template <typename T>
static inline void parse_num(amount_t& amt, T num) {
std::string str;
{ std::ostringstream strstr(str);
strstr << num;
}
{ std::istringstream strstr(str);
amt.parse(strstr);
}
int amount_t::sign() const
{
return quantity ? mpz_sgn(MPZ(quantity)) : 0;
}
#define AMOUNT_CMP_INT(OP) \
bool amount_t::operator OP (const int num) const \
{ \
if (num == 0) { \
return quantity ? mpz_sgn(MPZ(quantity)) OP 0 : false; \
} else { \
amount_t amt; \
parse_num(amt, num); \
return *this OP amt; \
} \
}
AMOUNT_CMP_INT(<)
AMOUNT_CMP_INT(<=)
AMOUNT_CMP_INT(>)
AMOUNT_CMP_INT(>=)
AMOUNT_CMP_INT(==)
#define AMOUNT_CMP_UINT(OP) \
bool amount_t::operator OP (const unsigned int num) const \
{ \
if (num == 0) { \
return quantity ? mpz_sgn(MPZ(quantity)) OP 0 : false; \
} else { \
amount_t amt; \
parse_num(amt, num); \
return *this OP amt; \
} \
}
AMOUNT_CMP_UINT(<)
AMOUNT_CMP_UINT(<=)
AMOUNT_CMP_UINT(>)
AMOUNT_CMP_UINT(>=)
AMOUNT_CMP_UINT(==)
// comparisons between amounts
#define AMOUNT_CMP_AMOUNT(OP) \
bool amount_t::operator OP(const amount_t& amt) const \

View file

@ -91,16 +91,20 @@ class amount_t
amount_t& operator*=(const amount_t& amt);
amount_t& operator/=(const amount_t& amt);
amount_t& operator+=(const int value) {
template <typename T>
amount_t& operator+=(T value) {
return *this += amount_t(value);
}
amount_t& operator-=(const int value) {
template <typename T>
amount_t& operator-=(T value) {
return *this -= amount_t(value);
}
amount_t& operator*=(const int value) {
template <typename T>
amount_t& operator*=(T value) {
return *this *= amount_t(value);
}
amount_t& operator/=(const int value) {
template <typename T>
amount_t& operator/=(T value) {
return *this /= amount_t(value);
}
@ -126,22 +130,26 @@ class amount_t
return temp;
}
amount_t operator+(const int value) const {
template <typename T>
amount_t operator+(T value) const {
amount_t temp = *this;
temp += value;
return temp;
}
amount_t operator-(const int value) const {
template <typename T>
amount_t operator-(T value) const {
amount_t temp = *this;
temp -= value;
return temp;
}
amount_t operator*(const int value) const {
template <typename T>
amount_t operator*(T value) const {
amount_t temp = *this;
temp *= value;
return temp;
}
amount_t operator/(const int value) const {
template <typename T>
amount_t operator/(T value) const {
amount_t temp = *this;
temp /= value;
return temp;
@ -161,25 +169,6 @@ class amount_t
// test for non-zero (use ! for zero)
operator bool() const;
// integer comparisons
bool operator<(const int num) const;
bool operator<=(const int num) const;
bool operator>(const int num) const;
bool operator>=(const int num) const;
bool operator==(const int num) const;
bool operator!=(const int num) const {
return ! (*this == num);
}
bool operator<(const unsigned int num) const;
bool operator<=(const unsigned int num) const;
bool operator>(const unsigned int num) const;
bool operator>=(const unsigned int num) const;
bool operator==(const unsigned int num) const;
bool operator!=(const unsigned int num) const {
return ! (*this == num);
}
// comparisons between amounts
bool operator<(const amount_t& amt) const;
bool operator<=(const amount_t& amt) const;
@ -192,6 +181,43 @@ class amount_t
return ! (*this == amt);
}
template <typename T>
void parse_num(T num) {
std::string str;
{ std::ostringstream strstr(str);
strstr << num;
}
{ std::istringstream strstr(str);
parse(strstr);
}
}
int sign() const;
// POD comparisons
#define AMOUNT_CMP_INT(OP) \
template <typename T> \
bool operator OP (T num) const { \
if (num == 0) { \
return sign() OP 0; \
} else { \
amount_t amt; \
amt.parse_num(num); \
return *this OP amt; \
} \
}
AMOUNT_CMP_INT(<)
AMOUNT_CMP_INT(<=)
AMOUNT_CMP_INT(>)
AMOUNT_CMP_INT(>=)
AMOUNT_CMP_INT(==)
template <typename T>
bool operator!=(T num) const {
return ! (*this == num);
}
amount_t value(const std::time_t moment) const;
void abs() {

View file

@ -135,34 +135,48 @@ void export_balance()
.def(self += self)
.def(self += other<amount_t>())
.def(self += int())
.def(self + self)
.def(self + other<amount_t>())
.def(self + int())
.def(self -= self)
.def(self -= other<amount_t>())
.def(self -= int())
.def(self - self)
.def(self - other<amount_t>())
.def(self - int())
.def(self *= self)
.def(self *= other<amount_t>())
.def(self *= int())
.def(self * self)
.def(self * other<amount_t>())
.def(self * int())
.def(self /= self)
.def(self /= other<amount_t>())
.def(self /= int())
.def(self / self)
.def(self / other<amount_t>())
.def(self / int())
.def(- self)
.def(self < self)
.def(self < other<amount_t>())
.def(self < int())
.def(self <= self)
.def(self <= other<amount_t>())
.def(self <= int())
.def(self > self)
.def(self > other<amount_t>())
.def(self > int())
.def(self >= self)
.def(self >= other<amount_t>())
.def(self >= int())
.def(self == self)
.def(self == other<amount_t>())
.def(self == int())
.def(self != self)
.def(self != other<amount_t>())
.def(self != int())
.def(! self)
.def(abs(self))
@ -189,47 +203,61 @@ void export_balance()
.def(self += self)
.def(self += other<balance_t>())
.def(self += other<amount_t>())
.def(self += int())
.def(self + self)
.def(self + other<balance_t>())
.def(self + other<amount_t>())
.def(self + int())
.def(self -= self)
.def(self -= other<balance_t>())
.def(self -= other<amount_t>())
.def(self -= int())
.def(self - self)
.def(self - other<balance_t>())
.def(self - other<amount_t>())
.def(self - int())
.def(self *= self)
.def(self *= other<balance_t>())
.def(self *= other<amount_t>())
.def(self *= int())
.def(self * self)
.def(self * other<balance_t>())
.def(self * other<amount_t>())
.def(self * int())
.def(self /= self)
.def(self /= other<balance_t>())
.def(self /= other<amount_t>())
.def(self /= int())
.def(self / self)
.def(self / other<balance_t>())
.def(self / other<amount_t>())
.def(self / int())
.def(- self)
.def(self < self)
.def(self < other<balance_t>())
.def(self < other<amount_t>())
.def(self < int())
.def(self <= self)
.def(self <= other<balance_t>())
.def(self <= other<amount_t>())
.def(self <= int())
.def(self > self)
.def(self > other<balance_t>())
.def(self > other<amount_t>())
.def(self > int())
.def(self >= self)
.def(self >= other<balance_t>())
.def(self >= other<amount_t>())
.def(self >= int())
.def(self == self)
.def(self == other<balance_t>())
.def(self == other<amount_t>())
.def(self == int())
.def(self != self)
.def(self != other<balance_t>())
.def(self != other<amount_t>())
.def(self != int())
.def(! self)
.def(abs(self))

247
balance.h
View file

@ -38,17 +38,8 @@ class balance_t
if (amt)
amounts.insert(amounts_pair(&amt.commodity(), amt));
}
balance_t(const int value) {
amount_t amt(value);
if (amt)
amounts.insert(amounts_pair(&amt.commodity(), amt));
}
balance_t(const unsigned int value) {
amount_t amt(value);
if (amt)
amounts.insert(amounts_pair(&amt.commodity(), amt));
}
balance_t(const double value) {
template <typename T>
balance_t(T value) {
amount_t amt(value);
if (amt)
amounts.insert(amounts_pair(&amt.commodity(), amt));
@ -70,19 +61,10 @@ class balance_t
*this += amt;
return *this;
}
balance_t& operator=(const int value) {
template <typename T>
balance_t& operator=(T value) {
amounts.clear();
*this += amount_t(value);
return *this;
}
balance_t& operator=(const unsigned int value) {
amounts.clear();
*this += amount_t(value);
return *this;
}
balance_t& operator=(const double value) {
amounts.clear();
*this += amount_t(value);
*this += value;
return *this;
}
@ -102,6 +84,10 @@ class balance_t
amounts.insert(amounts_pair(&amt.commodity(), amt));
return *this;
}
template <typename T>
balance_t& operator+=(T val) {
return *this += amount_t(val);
}
balance_t& operator-=(const balance_t& bal) {
for (amounts_map::const_iterator i = bal.amounts.begin();
i != bal.amounts.end();
@ -117,6 +103,10 @@ class balance_t
amounts.insert(amounts_pair(&amt.commodity(), amt));
return *this;
}
template <typename T>
balance_t& operator-=(T val) {
return *this -= amount_t(val);
}
// simple arithmetic
balance_t operator+(const balance_t& bal) const {
@ -129,6 +119,12 @@ class balance_t
temp += amt;
return temp;
}
template <typename T>
balance_t operator+(T val) const {
balance_t temp = *this;
temp += val;
return temp;
}
balance_t operator-(const balance_t& bal) const {
balance_t temp = *this;
temp -= bal;
@ -139,6 +135,12 @@ class balance_t
temp -= amt;
return temp;
}
template <typename T>
balance_t operator-(T val) const {
balance_t temp = *this;
temp -= val;
return temp;
}
// multiplication and divide
balance_t& operator*=(const balance_t& bal) {
@ -163,6 +165,10 @@ class balance_t
}
return *this;
}
template <typename T>
balance_t& operator*=(T val) {
return *this *= amount_t(val);
}
balance_t& operator/=(const balance_t& bal) {
for (amounts_map::const_iterator i = bal.amounts.begin();
@ -186,6 +192,10 @@ class balance_t
}
return *this;
}
template <typename T>
balance_t& operator/=(T val) {
return *this /= amount_t(val);
}
// multiplication and divide
balance_t operator*(const balance_t& bal) const {
@ -198,6 +208,12 @@ class balance_t
temp *= amt;
return temp;
}
template <typename T>
balance_t operator*(T val) const {
balance_t temp = *this;
temp *= val;
return temp;
}
balance_t operator/(const balance_t& bal) const {
balance_t temp = *this;
temp /= bal;
@ -208,6 +224,12 @@ class balance_t
temp /= amt;
return temp;
}
template <typename T>
balance_t operator/(T val) const {
balance_t temp = *this;
temp /= val;
return temp;
}
// comparison
bool operator<(const balance_t& bal) const {
@ -228,6 +250,19 @@ class balance_t
return true;
}
bool operator<(const amount_t& amt) const {
return amount(amt.commodity()) < amt;
}
template <typename T>
bool operator<(T val) const {
for (amounts_map::const_iterator i = amounts.begin();
i != amounts.end();
i++)
if ((*i).second < val)
return true;
return false;
}
bool operator<=(const balance_t& bal) const {
for (amounts_map::const_iterator i = bal.amounts.begin();
i != bal.amounts.end();
@ -243,20 +278,18 @@ class balance_t
return true;
}
bool operator<(const amount_t& amt) const {
return amount(amt.commodity()) < amt;
}
bool operator<=(const amount_t& amt) const {
return amount(amt.commodity()) <= amt;
}
#if 0
bool operator<(const unsigned int val) const {
return amount() < val;
template <typename T>
bool operator<=(T val) const {
for (amounts_map::const_iterator i = amounts.begin();
i != amounts.end();
i++)
if ((*i).second <= val)
return true;
return false;
}
bool operator<=(const unsigned int val) const {
return amount() <= val;
}
#endif
bool operator>(const balance_t& bal) const {
for (amounts_map::const_iterator i = bal.amounts.begin();
@ -276,6 +309,19 @@ class balance_t
return true;
}
bool operator>(const amount_t& amt) const {
return amount(amt.commodity()) > amt;
}
template <typename T>
bool operator>(T val) const {
for (amounts_map::const_iterator i = amounts.begin();
i != amounts.end();
i++)
if ((*i).second > val)
return true;
return false;
}
bool operator>=(const balance_t& bal) const {
for (amounts_map::const_iterator i = bal.amounts.begin();
i != bal.amounts.end();
@ -291,20 +337,18 @@ class balance_t
return true;
}
bool operator>(const amount_t& amt) const {
return amount(amt.commodity()) > amt;
}
bool operator>=(const amount_t& amt) const {
return amount(amt.commodity()) >= amt;
}
#if 0
bool operator>(const unsigned int val) const {
return amount() > val;
template <typename T>
bool operator>=(T val) const {
for (amounts_map::const_iterator i = amounts.begin();
i != amounts.end();
i++)
if ((*i).second >= val)
return true;
return false;
}
bool operator>=(const unsigned int val) const {
return amount() >= val;
}
#endif
bool operator==(const balance_t& bal) const {
amounts_map::const_iterator i, j;
@ -317,23 +361,29 @@ class balance_t
}
return i == amounts.end() && j == bal.amounts.end();
}
bool operator!=(const balance_t& bal) const {
return ! (*this == bal);
}
bool operator==(const amount_t& amt) const {
return amounts.size() == 1 && (*amounts.begin()).second == amt;
}
template <typename T>
bool operator==(T val) const {
for (amounts_map::const_iterator i = amounts.begin();
i != amounts.end();
i++)
if ((*i).second == val)
return true;
return false;
}
bool operator!=(const balance_t& bal) const {
return ! (*this == bal);
}
bool operator!=(const amount_t& amt) const {
return ! (*this == amt);
}
#if 0
bool operator==(const unsigned int val) const {
return amount() == val;
}
bool operator!=(const unsigned int val) const {
template <typename T>
bool operator!=(T val) const {
return ! (*this == val);
}
#endif
// unary negation
void negate() {
@ -412,11 +462,8 @@ class balance_pair_t
: quantity(_quantity), cost(NULL) {}
balance_pair_t(const amount_t& _quantity)
: quantity(_quantity), cost(NULL) {}
balance_pair_t(const int value)
: quantity(value), cost(NULL) {}
balance_pair_t(const unsigned int value)
: quantity(value), cost(NULL) {}
balance_pair_t(const double value)
template <typename T>
balance_pair_t(T value)
: quantity(value), cost(NULL) {}
// destructor
@ -455,23 +502,8 @@ class balance_pair_t
quantity = amt;
return *this;
}
balance_pair_t& operator=(const int value) {
if (cost) {
delete cost;
cost = NULL;
}
quantity = value;
return *this;
}
balance_pair_t& operator=(const unsigned int value) {
if (cost) {
delete cost;
cost = NULL;
}
quantity = value;
return *this;
}
balance_pair_t& operator=(const double value) {
template <typename T>
balance_pair_t& operator=(T value) {
if (cost) {
delete cost;
cost = NULL;
@ -504,6 +536,10 @@ class balance_pair_t
*cost += amt;
return *this;
}
template <typename T>
balance_pair_t& operator+=(T val) {
return *this += amount_t(val);
}
balance_pair_t& operator-=(const balance_pair_t& bal_pair) {
if (bal_pair.cost && ! cost)
@ -528,6 +564,10 @@ class balance_pair_t
*cost -= amt;
return *this;
}
template <typename T>
balance_pair_t& operator-=(T val) {
return *this -= amount_t(val);
}
// simple arithmetic
balance_pair_t operator+(const balance_pair_t& bal_pair) const {
@ -545,6 +585,12 @@ class balance_pair_t
temp += amt;
return temp;
}
template <typename T>
balance_pair_t operator+(T val) const {
balance_pair_t temp = *this;
temp += val;
return temp;
}
balance_pair_t operator-(const balance_pair_t& bal_pair) const {
balance_pair_t temp = *this;
@ -561,6 +607,12 @@ class balance_pair_t
temp -= amt;
return temp;
}
template <typename T>
balance_pair_t operator-(T val) const {
balance_pair_t temp = *this;
temp -= val;
return temp;
}
// multiplication and division
balance_pair_t& operator*=(const balance_pair_t& bal_pair) {
@ -586,6 +638,10 @@ class balance_pair_t
*cost *= amt;
return *this;
}
template <typename T>
balance_pair_t& operator*=(T val) {
return *this *= amount_t(val);
}
balance_pair_t& operator/=(const balance_pair_t& bal_pair) {
if (bal_pair.cost && ! cost)
@ -610,6 +666,10 @@ class balance_pair_t
*cost /= amt;
return *this;
}
template <typename T>
balance_pair_t& operator/=(T val) {
return *this /= amount_t(val);
}
balance_pair_t operator*(const balance_pair_t& bal_pair) const {
balance_pair_t temp = *this;
@ -626,6 +686,12 @@ class balance_pair_t
temp *= amt;
return temp;
}
template <typename T>
balance_pair_t operator*(T val) const {
balance_pair_t temp = *this;
temp *= val;
return temp;
}
balance_pair_t operator/(const balance_pair_t& bal_pair) const {
balance_pair_t temp = *this;
@ -642,6 +708,12 @@ class balance_pair_t
temp /= amt;
return temp;
}
template <typename T>
balance_pair_t operator/(T val) const {
balance_pair_t temp = *this;
temp /= val;
return temp;
}
// comparison
bool operator<(const balance_pair_t& bal_pair) const {
@ -653,6 +725,11 @@ class balance_pair_t
bool operator<(const amount_t& amt) const {
return quantity < amt;
}
template <typename T>
bool operator<(T val) const {
return quantity < val;
}
bool operator<=(const balance_pair_t& bal_pair) const {
return quantity <= bal_pair.quantity;
}
@ -662,6 +739,10 @@ class balance_pair_t
bool operator<=(const amount_t& amt) const {
return quantity <= amt;
}
template <typename T>
bool operator<=(T val) const {
return quantity <= val;
}
bool operator>(const balance_pair_t& bal_pair) const {
return quantity > bal_pair.quantity;
@ -672,6 +753,11 @@ class balance_pair_t
bool operator>(const amount_t& amt) const {
return quantity > amt;
}
template <typename T>
bool operator>(T val) const {
return quantity > val;
}
bool operator>=(const balance_pair_t& bal_pair) const {
return quantity >= bal_pair.quantity;
}
@ -681,6 +767,10 @@ class balance_pair_t
bool operator>=(const amount_t& amt) const {
return quantity >= amt;
}
template <typename T>
bool operator>=(T val) const {
return quantity >= val;
}
bool operator==(const balance_pair_t& bal_pair) const {
return quantity == bal_pair.quantity;
@ -691,6 +781,11 @@ class balance_pair_t
bool operator==(const amount_t& amt) const {
return quantity == amt;
}
template <typename T>
bool operator==(T val) const {
return quantity == val;
}
bool operator!=(const balance_pair_t& bal_pair) const {
return ! (*this == bal_pair);
}
@ -700,6 +795,10 @@ class balance_pair_t
bool operator!=(const amount_t& amt) const {
return ! (*this == amt);
}
template <typename T>
bool operator!=(T val) const {
return ! (*this == val);
}
// unary negation
void negate() {

View file

@ -1438,7 +1438,7 @@ file:
And two in your company ledger file:
@example
@@ Company XYZ
!account Company XYZ
2004/09/29 Circuit City
Expenses:Computer:Software $100.00
@ -1448,7 +1448,7 @@ And two in your company ledger file:
Accounts Payable:Your Name $100.00
Assets:Checking $-100.00
@@@@
!end
@end example
(Note: The @ above command means that all accounts mentioned in the

View file

@ -532,22 +532,6 @@ unsigned int textual_parser_t::parse(std::istream& in,
parse_automated_transactions(in, account_stack.front(), auto_xacts);
break;
case '@': { // account specific
in >> c;
if (in.peek() == '@') {
in.get(c);
account_stack.pop_front();
break;
}
in.getline(line, MAX_LINE);
linenum++;
account_t * acct = account_stack.front()->find_account(skip_ws(line));
account_stack.push_front(acct);
break;
}
case '!': { // directive
std::string word;
in.get(c);
@ -560,9 +544,21 @@ unsigned int textual_parser_t::parse(std::istream& in,
push_var<std::string> save_path(path);
push_var<automated_transactions_t *>
save_current_auto_xacts(current_auto_xacts);
count += parse_journal_file(skip_ws(line), journal,
account_stack.front());
}
else if (word == "account") {
in.getline(line, MAX_LINE);
linenum++;
account_t * acct;
acct = account_stack.front()->find_account(skip_ws(line));
account_stack.push_front(acct);
}
else if (word == "end") {
account_stack.pop_front();
}
#ifdef USE_BOOST_PYTHON
else if (word == "python") {
in.getline(line, MAX_LINE);

131
value.cc
View file

@ -60,27 +60,32 @@ value_t& value_t::operator=(const value_t& value)
#define DEF_VALUE_OP(OP) \
value_t& value_t::operator OP(const value_t& value) \
{ \
switch (value.type) { \
switch (type) { \
case BOOLEAN: \
case INTEGER: \
switch (type) { \
cast(INTEGER); \
switch (value.type) { \
case BOOLEAN: \
cast(INTEGER); \
*((unsigned int *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \
*((unsigned int *) data) OP *((unsigned int *) value.data); \
break; \
\
case AMOUNT: \
*((amount_t *) data) OP *((unsigned int *) value.data); \
cast(AMOUNT); \
*((amount_t *) data) OP *((amount_t *) value.data); \
break; \
\
case BALANCE: \
*((balance_t *) data) OP amount_t(*((unsigned int *) value.data)); \
cast(BALANCE); \
*((balance_t *) data) OP *((balance_t *) value.data); \
break; \
\
case BALANCE_PAIR: \
*((balance_pair_t *) data) OP amount_t(*((unsigned int *) value.data)); \
cast(BALANCE_PAIR); \
*((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \
break; \
\
default: \
@ -90,10 +95,14 @@ value_t& value_t::operator OP(const value_t& value) \
break; \
\
case AMOUNT: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
*((amount_t *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \
cast(AMOUNT); \
*((amount_t *) data) OP *((unsigned int *) value.data); \
break; \
\
case AMOUNT: \
if (((amount_t *) data)->commodity() != \
@ -105,11 +114,13 @@ value_t& value_t::operator OP(const value_t& value) \
break; \
\
case BALANCE: \
*((balance_t *) data) OP *((amount_t *) value.data); \
cast(BALANCE); \
*((balance_t *) data) OP *((balance_t *) value.data); \
break; \
\
case BALANCE_PAIR: \
*((balance_pair_t *) data) OP *((amount_t *) value.data); \
cast(BALANCE_PAIR); \
*((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \
break; \
\
default: \
@ -119,18 +130,26 @@ value_t& value_t::operator OP(const value_t& value) \
break; \
\
case BALANCE: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
*((balance_t *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \
*((balance_t *) data) OP *((unsigned int *) value.data); \
break; \
\
case AMOUNT: \
cast(BALANCE); \
*((balance_t *) data) OP *((amount_t *) value.data); \
break; \
\
case BALANCE: \
*((balance_t *) data) OP *((balance_t *) value.data); \
break; \
\
case BALANCE_PAIR: \
*((balance_pair_t *) data) OP *((balance_t *) value.data); \
cast(BALANCE_PAIR); \
*((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \
break; \
\
default: \
@ -140,12 +159,22 @@ value_t& value_t::operator OP(const value_t& value) \
break; \
\
case BALANCE_PAIR: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
*((balance_pair_t *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \
*((balance_pair_t *) data) OP *((unsigned int *) value.data); \
break; \
\
case AMOUNT: \
*((balance_pair_t *) data) OP *((amount_t *) value.data); \
break; \
\
case BALANCE: \
cast(BALANCE_PAIR); \
*((balance_pair_t *) data) OP *((balance_t *) value.data); \
break; \
\
case BALANCE_PAIR: \
*((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \
@ -172,23 +201,23 @@ DEF_VALUE_OP(/=)
#define DEF_VALUE_CMP_OP(OP) \
bool value_t::operator OP(const value_t& value) \
{ \
switch (value.type) { \
switch (type) { \
case BOOLEAN: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
return *((bool *) data) OP *((bool *) value.data); \
\
case INTEGER: \
return bool(*((unsigned int *) data)) OP *((bool *) value.data); \
return *((bool *) data) OP bool(*((unsigned int *) value.data)); \
\
case AMOUNT: \
return bool(*((amount_t *) data)) OP *((bool *) value.data); \
return *((bool *) data) OP bool(*((amount_t *) value.data)); \
\
case BALANCE: \
return bool(*((balance_t *) data)) OP *((bool *) value.data); \
return *((bool *) data) OP bool(*((balance_t *) value.data)); \
\
case BALANCE_PAIR: \
return bool(*((balance_pair_t *) data)) OP *((bool *) value.data); \
return *((bool *) data) OP bool(*((balance_pair_t *) value.data)); \
\
default: \
assert(0); \
@ -197,21 +226,26 @@ bool value_t::operator OP(const value_t& value) \
break; \
\
case INTEGER: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
return ((unsigned int) *((bool *) data)) OP *((unsigned int *) value.data); \
return (*((unsigned int *) data) OP \
((unsigned int) *((bool *) value.data))); \
\
case INTEGER: \
return *((unsigned int *) data) OP *((unsigned int *) value.data); \
return (*((unsigned int *) data) OP \
*((unsigned int *) value.data)); \
\
case AMOUNT: \
return ((unsigned int) *((amount_t *) data)) OP *((unsigned int *) value.data); \
return (*((unsigned int *) data) OP \
((unsigned int) *((amount_t *) value.data))); \
\
case BALANCE: \
return ((unsigned int) *((balance_t *) data)) OP *((unsigned int *) value.data); \
return (*((unsigned int *) data) OP \
((unsigned int) *((balance_t *) value.data))); \
\
case BALANCE_PAIR: \
return ((unsigned int) *((balance_pair_t *) data)) OP *((unsigned int *) value.data); \
return (*((unsigned int *) data) OP \
((unsigned int) *((balance_pair_t *) value.data))); \
\
default: \
assert(0); \
@ -220,21 +254,26 @@ bool value_t::operator OP(const value_t& value) \
break; \
\
case AMOUNT: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
return amount_t(*((bool *) data)) OP *((amount_t *) value.data); \
return *((amount_t *) data) OP amount_t(*((bool *) value.data)); \
\
case INTEGER: \
return amount_t(*((unsigned int *) data)) OP *((amount_t *) value.data); \
return (*((amount_t *) data) OP \
amount_t(*((unsigned int *) 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); \
return (*((amount_t *) data) OP \
((balance_t *) value.data)-> \
amount(((amount_t *) data)->commodity())); \
\
case BALANCE_PAIR: \
return ((balance_pair_t *) data)->quantity.amount(((amount_t *) value.data)->commodity()) OP *((amount_t *) value.data); \
return (*((amount_t *) data) OP \
((balance_pair_t *) value.data)-> \
quantity.amount(((amount_t *) data)->commodity())); \
\
default: \
assert(0); \
@ -243,21 +282,22 @@ bool value_t::operator OP(const value_t& value) \
break; \
\
case BALANCE: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
return balance_t(*((bool *) data)) OP *((balance_t *) value.data); \
return *((balance_t *) data) OP (unsigned int)*((bool *) value.data); \
\
case INTEGER: \
return balance_t(*((unsigned int *) data)) OP *((balance_t *) value.data); \
return *((balance_t *) data) OP *((unsigned int *) value.data); \
\
case AMOUNT: \
return balance_t(*((amount_t *) data)) OP *((balance_t *) value.data); \
return *((balance_t *) data) OP *((amount_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); \
return (*((balance_t *) data) OP \
((balance_pair_t *) value.data)->quantity); \
\
default: \
assert(0); \
@ -266,21 +306,26 @@ bool value_t::operator OP(const value_t& value) \
break; \
\
case BALANCE_PAIR: \
switch (type) { \
switch (value.type) { \
case BOOLEAN: \
return balance_pair_t(*((bool *) data)) OP *((balance_pair_t *) value.data); \
return (((balance_pair_t *) data)->quantity OP \
(unsigned int)*((bool *) value.data)); \
\
case INTEGER: \
return balance_pair_t(*((unsigned int *) data)) OP *((balance_pair_t *) value.data); \
return (((balance_pair_t *) data)->quantity OP \
*((unsigned int *) value.data)); \
\
case AMOUNT: \
return balance_pair_t(*((amount_t *) data)) OP *((balance_pair_t *) value.data); \
return (((balance_pair_t *) data)->quantity OP \
*((amount_t *) value.data)); \
\
case BALANCE: \
return balance_pair_t(*((balance_t *) data)) OP *((balance_pair_t *) value.data); \
return (((balance_pair_t *) data)->quantity OP \
*((balance_t *) value.data)); \
\
case BALANCE_PAIR: \
return *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \
return (*((balance_pair_t *) data) OP \
*((balance_pair_t *) value.data)); \
\
default: \
assert(0); \

49
value.h
View file

@ -249,20 +249,59 @@ class value_t
value_t cost() const;
};
template <typename T>
value_t operator+(const T& value, const value_t& obj) {
inline value_t operator+(const balance_pair_t& value, const value_t& obj) {
return value_t(value) + obj;
}
inline value_t operator+(const balance_t& value, const value_t& obj) {
return value_t(value) + obj;
}
inline value_t operator+(const amount_t& value, const value_t& obj) {
return value_t(value) + obj;
}
template <typename T>
value_t operator-(const T& value, const value_t& obj) {
inline value_t operator+(T value, const value_t& obj) {
return value_t(value) + obj;
}
inline value_t operator-(const balance_pair_t& value, const value_t& obj) {
return value_t(value) - obj;
}
inline value_t operator-(const balance_t& value, const value_t& obj) {
return value_t(value) - obj;
}
inline value_t operator-(const amount_t& value, const value_t& obj) {
return value_t(value) - obj;
}
template <typename T>
value_t operator*(const T& value, const value_t& obj) {
inline value_t operator-(T value, const value_t& obj) {
return value_t(value) - obj;
}
inline value_t operator*(const balance_pair_t& value, const value_t& obj) {
return value_t(value) * obj;
}
inline value_t operator*(const balance_t& value, const value_t& obj) {
return value_t(value) * obj;
}
inline value_t operator*(const amount_t& value, const value_t& obj) {
return value_t(value) * obj;
}
template <typename T>
value_t operator/(const T& value, const value_t& obj) {
inline value_t operator*(T value, const value_t& obj) {
return value_t(value) * obj;
}
inline value_t operator/(const balance_pair_t& value, const value_t& obj) {
return value_t(value) / obj;
}
inline value_t operator/(const balance_t& value, const value_t& obj) {
return value_t(value) / obj;
}
inline value_t operator/(const amount_t& value, const value_t& obj) {
return value_t(value) / obj;
}
template <typename T>
inline value_t operator/(T value, const value_t& obj) {
return value_t(value) / obj;
}