diff --git a/test/operators.cpp b/test/operators.cpp index 2354202c..c58f2b00 100755 --- a/test/operators.cpp +++ b/test/operators.cpp @@ -7,16 +7,23 @@ #include #include #include "test_class.hpp" +#include +#include +#include +#include //#include +// Just use math.h here; trying to use std::pow() causes too much +// trouble for non-conforming compilers and libraries. +#include + #if __GNUC__ != 2 # include #else # include #endif -// Just use math.h here; trying to use std::pow() causes too much -// trouble for non-conforming compilers and libraries. -#include +using namespace boost::python; + using namespace boost::python; @@ -65,6 +72,35 @@ std::ostream& operator<<(std::ostream& s, X const& x) return s << x.value(); } +struct number + : boost::integer_arithmetic +{ + explicit number(long x_) : x(x_) {} + operator long() const { return x; } + + template + number& operator+=(T const& rhs) + { x += rhs; return *this; } + + template + number& operator-=(T const& rhs) + { x -= rhs; return *this; } + + template + number& operator*=(T const& rhs) + { x *= rhs; return *this; } + + template + number& operator/=(T const& rhs) + { x /= rhs; return *this; } + + template + number& operator%=(T const& rhs) + { x %= rhs; return *this; } + + long x; +}; + BOOST_PYTHON_MODULE(operators_ext) { class_("X", init()) @@ -78,6 +114,7 @@ BOOST_PYTHON_MODULE(operators_ext) .def(self < self) .def(1 < self) .def(self -= self) + .def(abs(self)) .def(str(self)) @@ -94,6 +131,40 @@ BOOST_PYTHON_MODULE(operators_ext) ) ; + class_("number", init()) + // interoperate with self + .def(self += self) + .def(self + self) + .def(self -= self) + .def(self - self) + .def(self *= self) + .def(self * self) + .def(self /= self) + .def(self / self) + .def(self %= self) + .def(self % self) + + // Convert to Python int + .def(int_(self)) + + // interoperate with long + .def(self += long()) + .def(self + long()) + .def(long() + self) + .def(self -= long()) + .def(self - long()) + .def(long() - self) + .def(self *= long()) + .def(self * long()) + .def(long() * self) + .def(self /= long()) + .def(self / long()) + .def(long() / self) + .def(self %= long()) + .def(self % long()) + .def(long() % self) + ; + class_ >("Z", init()) .def(int_(self)) .def(float_(self))