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: - To include entries from a file into a specific account, use:
@ ACCOUNT !account ACCOUNT
!include FILE !include FILE
@@ !end
Entries specified within "@ ACCOUNT/@@" will affect only that Entries specified within an !account block will affect only that
account. account.
- Register reports now show only the account affected by default. Use - Register reports now show only the account affected by default. Use

View file

@ -463,54 +463,11 @@ void amount_t::negate()
} }
} }
// integer comparisons int amount_t::sign() const
template <typename T> {
static inline void parse_num(amount_t& amt, T num) { return quantity ? mpz_sgn(MPZ(quantity)) : 0;
std::string str;
{ std::ostringstream strstr(str);
strstr << num;
}
{ std::istringstream strstr(str);
amt.parse(strstr);
}
} }
#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 // comparisons between amounts
#define AMOUNT_CMP_AMOUNT(OP) \ #define AMOUNT_CMP_AMOUNT(OP) \
bool amount_t::operator OP(const amount_t& amt) const \ 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 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); return *this += amount_t(value);
} }
amount_t& operator-=(const int value) { template <typename T>
amount_t& operator-=(T value) {
return *this -= amount_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); return *this *= amount_t(value);
} }
amount_t& operator/=(const int value) { template <typename T>
amount_t& operator/=(T value) {
return *this /= amount_t(value); return *this /= amount_t(value);
} }
@ -126,22 +130,26 @@ class amount_t
return temp; return temp;
} }
amount_t operator+(const int value) const { template <typename T>
amount_t operator+(T value) const {
amount_t temp = *this; amount_t temp = *this;
temp += value; temp += value;
return temp; return temp;
} }
amount_t operator-(const int value) const { template <typename T>
amount_t operator-(T value) const {
amount_t temp = *this; amount_t temp = *this;
temp -= value; temp -= value;
return temp; return temp;
} }
amount_t operator*(const int value) const { template <typename T>
amount_t operator*(T value) const {
amount_t temp = *this; amount_t temp = *this;
temp *= value; temp *= value;
return temp; return temp;
} }
amount_t operator/(const int value) const { template <typename T>
amount_t operator/(T value) const {
amount_t temp = *this; amount_t temp = *this;
temp /= value; temp /= value;
return temp; return temp;
@ -161,25 +169,6 @@ class amount_t
// test for non-zero (use ! for zero) // test for non-zero (use ! for zero)
operator bool() const; 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 // comparisons between amounts
bool operator<(const amount_t& amt) const; bool operator<(const amount_t& amt) const;
bool operator<=(const amount_t& amt) const; bool operator<=(const amount_t& amt) const;
@ -192,6 +181,43 @@ class amount_t
return ! (*this == amt); 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; amount_t value(const std::time_t moment) const;
void abs() { void abs() {

View file

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

247
balance.h
View file

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

View file

@ -1438,7 +1438,7 @@ file:
And two in your company ledger file: And two in your company ledger file:
@example @example
@@ Company XYZ !account Company XYZ
2004/09/29 Circuit City 2004/09/29 Circuit City
Expenses:Computer:Software $100.00 Expenses:Computer:Software $100.00
@ -1448,7 +1448,7 @@ And two in your company ledger file:
Accounts Payable:Your Name $100.00 Accounts Payable:Your Name $100.00
Assets:Checking $-100.00 Assets:Checking $-100.00
@@@@ !end
@end example @end example
(Note: The @ above command means that all accounts mentioned in the (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); parse_automated_transactions(in, account_stack.front(), auto_xacts);
break; 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 case '!': { // directive
std::string word; std::string word;
in.get(c); 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<std::string> save_path(path);
push_var<automated_transactions_t *> push_var<automated_transactions_t *>
save_current_auto_xacts(current_auto_xacts); save_current_auto_xacts(current_auto_xacts);
count += parse_journal_file(skip_ws(line), journal, count += parse_journal_file(skip_ws(line), journal,
account_stack.front()); 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 #ifdef USE_BOOST_PYTHON
else if (word == "python") { else if (word == "python") {
in.getline(line, MAX_LINE); 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) \ #define DEF_VALUE_OP(OP) \
value_t& value_t::operator OP(const value_t& value) \ value_t& value_t::operator OP(const value_t& value) \
{ \ { \
switch (value.type) { \
case BOOLEAN: \
case INTEGER: \
switch (type) { \ switch (type) { \
case BOOLEAN: \ case BOOLEAN: \
case INTEGER: \
cast(INTEGER); \ cast(INTEGER); \
switch (value.type) { \
case BOOLEAN: \
*((unsigned int *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\ \
case INTEGER: \ case INTEGER: \
*((unsigned int *) data) OP *((unsigned int *) value.data); \ *((unsigned int *) data) OP *((unsigned int *) value.data); \
break; \ break; \
\ \
case AMOUNT: \ case AMOUNT: \
*((amount_t *) data) OP *((unsigned int *) value.data); \ cast(AMOUNT); \
*((amount_t *) data) OP *((amount_t *) value.data); \
break; \ break; \
\ \
case BALANCE: \ case BALANCE: \
*((balance_t *) data) OP amount_t(*((unsigned int *) value.data)); \ cast(BALANCE); \
*((balance_t *) data) OP *((balance_t *) value.data); \
break; \ break; \
\ \
case BALANCE_PAIR: \ 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; \ break; \
\ \
default: \ default: \
@ -90,10 +95,14 @@ value_t& value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case AMOUNT: \ case AMOUNT: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
*((amount_t *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \ case INTEGER: \
cast(AMOUNT); \ *((amount_t *) data) OP *((unsigned int *) value.data); \
break; \
\ \
case AMOUNT: \ case AMOUNT: \
if (((amount_t *) data)->commodity() != \ if (((amount_t *) data)->commodity() != \
@ -105,11 +114,13 @@ value_t& value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case BALANCE: \ case BALANCE: \
*((balance_t *) data) OP *((amount_t *) value.data); \ cast(BALANCE); \
*((balance_t *) data) OP *((balance_t *) value.data); \
break; \ break; \
\ \
case BALANCE_PAIR: \ 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; \ break; \
\ \
default: \ default: \
@ -119,18 +130,26 @@ value_t& value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case BALANCE: \ case BALANCE: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
*((balance_t *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \ case INTEGER: \
*((balance_t *) data) OP *((unsigned int *) value.data); \
break; \
\
case AMOUNT: \ case AMOUNT: \
cast(BALANCE); \ *((balance_t *) data) OP *((amount_t *) value.data); \
break; \
\ \
case BALANCE: \ case BALANCE: \
*((balance_t *) data) OP *((balance_t *) value.data); \ *((balance_t *) data) OP *((balance_t *) value.data); \
break; \ break; \
\ \
case BALANCE_PAIR: \ 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; \ break; \
\ \
default: \ default: \
@ -140,12 +159,22 @@ value_t& value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case BALANCE_PAIR: \ case BALANCE_PAIR: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
*((balance_pair_t *) data) OP (*((bool *) value.data) ? 1U : 0U); \
break; \
\
case INTEGER: \ case INTEGER: \
*((balance_pair_t *) data) OP *((unsigned int *) value.data); \
break; \
\
case AMOUNT: \ case AMOUNT: \
*((balance_pair_t *) data) OP *((amount_t *) value.data); \
break; \
\
case BALANCE: \ case BALANCE: \
cast(BALANCE_PAIR); \ *((balance_pair_t *) data) OP *((balance_t *) value.data); \
break; \
\ \
case BALANCE_PAIR: \ case BALANCE_PAIR: \
*((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \ *((balance_pair_t *) data) OP *((balance_pair_t *) value.data); \
@ -172,23 +201,23 @@ DEF_VALUE_OP(/=)
#define DEF_VALUE_CMP_OP(OP) \ #define DEF_VALUE_CMP_OP(OP) \
bool value_t::operator OP(const value_t& value) \ bool value_t::operator OP(const value_t& value) \
{ \ { \
switch (value.type) { \
case BOOLEAN: \
switch (type) { \ switch (type) { \
case BOOLEAN: \
switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
return *((bool *) data) OP *((bool *) value.data); \ return *((bool *) data) OP *((bool *) value.data); \
\ \
case INTEGER: \ case INTEGER: \
return bool(*((unsigned int *) data)) OP *((bool *) value.data); \ return *((bool *) data) OP bool(*((unsigned int *) value.data)); \
\ \
case AMOUNT: \ case AMOUNT: \
return bool(*((amount_t *) data)) OP *((bool *) value.data); \ return *((bool *) data) OP bool(*((amount_t *) value.data)); \
\ \
case BALANCE: \ case BALANCE: \
return bool(*((balance_t *) data)) OP *((bool *) value.data); \ return *((bool *) data) OP bool(*((balance_t *) value.data)); \
\ \
case BALANCE_PAIR: \ case BALANCE_PAIR: \
return bool(*((balance_pair_t *) data)) OP *((bool *) value.data); \ return *((bool *) data) OP bool(*((balance_pair_t *) value.data)); \
\ \
default: \ default: \
assert(0); \ assert(0); \
@ -197,21 +226,26 @@ bool value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case INTEGER: \ case INTEGER: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
return ((unsigned int) *((bool *) data)) OP *((unsigned int *) value.data); \ return (*((unsigned int *) data) OP \
((unsigned int) *((bool *) value.data))); \
\ \
case INTEGER: \ case INTEGER: \
return *((unsigned int *) data) OP *((unsigned int *) value.data); \ return (*((unsigned int *) data) OP \
*((unsigned int *) value.data)); \
\ \
case AMOUNT: \ 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: \ 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: \ 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: \ default: \
assert(0); \ assert(0); \
@ -220,21 +254,26 @@ bool value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case AMOUNT: \ case AMOUNT: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
return amount_t(*((bool *) data)) OP *((amount_t *) value.data); \ return *((amount_t *) data) OP amount_t(*((bool *) value.data)); \
\ \
case INTEGER: \ 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: \ case AMOUNT: \
return *((amount_t *) data) OP *((amount_t *) value.data); \ return *((amount_t *) data) OP *((amount_t *) value.data); \
\ \
case BALANCE: \ 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: \ 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: \ default: \
assert(0); \ assert(0); \
@ -243,21 +282,22 @@ bool value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case BALANCE: \ case BALANCE: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ case BOOLEAN: \
return balance_t(*((bool *) data)) OP *((balance_t *) value.data); \ return *((balance_t *) data) OP (unsigned int)*((bool *) value.data); \
\ \
case INTEGER: \ case INTEGER: \
return balance_t(*((unsigned int *) data)) OP *((balance_t *) value.data); \ return *((balance_t *) data) OP *((unsigned int *) value.data); \
\ \
case AMOUNT: \ case AMOUNT: \
return balance_t(*((amount_t *) data)) OP *((balance_t *) value.data); \ return *((balance_t *) data) OP *((amount_t *) value.data); \
\ \
case BALANCE: \ case BALANCE: \
return *((balance_t *) data) OP *((balance_t *) value.data); \ return *((balance_t *) data) OP *((balance_t *) value.data); \
\ \
case BALANCE_PAIR: \ 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: \ default: \
assert(0); \ assert(0); \
@ -266,21 +306,26 @@ bool value_t::operator OP(const value_t& value) \
break; \ break; \
\ \
case BALANCE_PAIR: \ case BALANCE_PAIR: \
switch (type) { \ switch (value.type) { \
case BOOLEAN: \ 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: \ 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: \ 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: \ 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: \ 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: \ default: \
assert(0); \ assert(0); \

49
value.h
View file

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