summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--balance.h10
-rw-r--r--value.cc53
-rw-r--r--value.h118
3 files changed, 178 insertions, 3 deletions
diff --git a/balance.h b/balance.h
index 0740ea59..e671192f 100644
--- a/balance.h
+++ b/balance.h
@@ -389,6 +389,11 @@ inline balance_t abs(const balance_t& bal) {
return temp;
}
+inline std::ostream& operator<<(std::ostream& out, const balance_t& bal) {
+ bal.write(out, 12);
+}
+
+
class balance_pair_t
{
public:
@@ -754,6 +759,11 @@ inline balance_pair_t abs(const balance_pair_t& bal_pair) {
return temp;
}
+inline std::ostream& operator<<(std::ostream& out,
+ const balance_pair_t& bal_pair) {
+ bal_pair.quantity.write(out, 12);
+}
+
} // namespace ledger
#endif // _BALANCE_H
diff --git a/value.cc b/value.cc
index d48426b4..1741964e 100644
--- a/value.cc
+++ b/value.cc
@@ -563,24 +563,71 @@ void export_value()
.def(init<bool>())
.def(self += self)
+ .def(self += other<balance_pair_t>())
+ .def(self += other<balance_t>())
+ .def(self += other<amount_t>())
+ .def(self += int())
+
.def(self -= self)
+ .def(self -= other<balance_pair_t>())
+ .def(self -= other<balance_t>())
+ .def(self -= other<amount_t>())
+ .def(self -= int())
+
.def(self *= self)
+ .def(self *= other<balance_pair_t>())
+ .def(self *= other<balance_t>())
+ .def(self *= other<amount_t>())
+ .def(self *= int())
+
.def(self /= self)
+ .def(self /= other<balance_pair_t>())
+ .def(self /= other<balance_t>())
+ .def(self /= other<amount_t>())
+ .def(self /= int())
.def(self < self)
+ .def(self < other<balance_pair_t>())
+ .def(self < other<balance_t>())
+ .def(self < other<amount_t>())
+ .def(self < int())
+
.def(self <= self)
+ .def(self <= other<balance_pair_t>())
+ .def(self <= other<balance_t>())
+ .def(self <= other<amount_t>())
+ .def(self <= int())
+
.def(self > self)
+ .def(self > other<balance_pair_t>())
+ .def(self > other<balance_t>())
+ .def(self > other<amount_t>())
+ .def(self > int())
+
.def(self >= self)
+ .def(self >= other<balance_pair_t>())
+ .def(self >= other<balance_t>())
+ .def(self >= other<amount_t>())
+ .def(self >= int())
+
.def(self == self)
+ .def(self == other<balance_pair_t>())
+ .def(self == other<balance_t>())
+ .def(self == other<amount_t>())
+ .def(self == int())
+
.def(self != self)
+ .def(self != other<balance_pair_t>())
+ .def(self != other<balance_t>())
+ .def(self != other<amount_t>())
+ .def(self != int())
+
.def(! self)
-#if 0
.def(abs(self))
- .def(str(self))
+ .def(self_ns::str(self))
.def("cast", &value_t::cast)
-#endif
.def("negate", &value_t::negate)
.def("cost", &value_t::cost)
;
diff --git a/value.h b/value.h
index 10775b1e..617b5fb7 100644
--- a/value.h
+++ b/value.h
@@ -46,6 +46,10 @@ class value_t
*((unsigned int *) data) = value;
type = INTEGER;
}
+ value_t(const int value) {
+ *((unsigned int *) data) = (unsigned int) value;
+ type = INTEGER;
+ }
value_t(const double value) {
new((amount_t *) data) amount_t(value);
type = AMOUNT;
@@ -92,6 +96,9 @@ class value_t
}
return *this;
}
+ value_t& operator=(const int value) {
+ return *this = (unsigned int) value;
+ }
value_t& operator=(const double value) {
return *this = amount_t(value);
}
@@ -143,6 +150,61 @@ class value_t
value_t& operator*=(const value_t& value);
value_t& operator/=(const value_t& value);
+ template <typename T>
+ value_t& operator+=(const T& value) {
+ return *this += value_t(value);
+ }
+ template <typename T>
+ value_t& operator-=(const T& value) {
+ return *this -= value_t(value);
+ }
+ template <typename T>
+ value_t& operator*=(const T& value) {
+ return *this *= value_t(value);
+ }
+ template <typename T>
+ value_t& operator/=(const T& value) {
+ return *this /= value_t(value);
+ }
+
+ value_t operator+(const value_t& value) {
+ value_t temp(*this);
+ temp += value;
+ return temp;
+ }
+ value_t operator-(const value_t& value) {
+ value_t temp(*this);
+ temp -= value;
+ return temp;
+ }
+ value_t operator*(const value_t& value) {
+ value_t temp(*this);
+ temp *= value;
+ return temp;
+ }
+ value_t operator/(const value_t& value) {
+ value_t temp(*this);
+ temp /= value;
+ return temp;
+ }
+
+ template <typename T>
+ value_t operator+(const T& value) {
+ return *this + value_t(value);
+ }
+ template <typename T>
+ value_t operator-(const T& value) {
+ return *this - value_t(value);
+ }
+ template <typename T>
+ value_t operator*(const T& value) {
+ return *this * value_t(value);
+ }
+ template <typename T>
+ value_t operator/(const T& value) {
+ return *this / value_t(value);
+ }
+
bool operator<(const value_t& value);
bool operator<=(const value_t& value);
bool operator>(const value_t& value);
@@ -153,6 +215,31 @@ class value_t
}
template <typename T>
+ bool operator<(const T& value) {
+ return *this < value_t(value);
+ }
+ template <typename T>
+ bool operator<=(const T& value) {
+ return *this <= value_t(value);
+ }
+ template <typename T>
+ bool operator>(const T& value) {
+ return *this > value_t(value);
+ }
+ template <typename T>
+ bool operator>=(const T& value) {
+ return *this >= value_t(value);
+ }
+ template <typename T>
+ bool operator==(const T& value) {
+ return *this == value_t(value);
+ }
+ template <typename T>
+ bool operator!=(const value_t& value) {
+ return ! (*this == value);
+ }
+
+ template <typename T>
operator T() const;
void cast(type_t cast_type);
@@ -185,6 +272,37 @@ value_t::operator T() const
return 0;
}
+inline value_t abs(const value_t& value) {
+ value_t temp(value);
+ temp.abs();
+ return temp;
+}
+
+inline std::ostream& operator<<(std::ostream& out, const value_t& value) {
+ switch (value.type) {
+ case value_t::BOOLEAN:
+ out << *((bool *) value.data);
+ break;
+ case value_t::INTEGER:
+ out << *((unsigned int *) value.data);
+ break;
+ case value_t::AMOUNT:
+ out << *((amount_t *) value.data);
+ break;
+ case value_t::BALANCE:
+ out << *((balance_t *) value.data);
+ break;
+ case value_t::BALANCE_PAIR:
+ out << *((balance_pair_t *) value.data);
+ break;
+
+ default:
+ assert(0);
+ break;
+ }
+ return out;
+}
+
class value_error : public std::exception {
std::string reason;
public: