operators.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/python/operators.hpp>
  6. #include <boost/python/class.hpp>
  7. #include <boost/python/module.hpp>
  8. #include <boost/python/def.hpp>
  9. #include "test_class.hpp"
  10. #include <boost/python/module.hpp>
  11. #include <boost/python/class.hpp>
  12. #include <boost/python/operators.hpp>
  13. #include <boost/operators.hpp>
  14. //#include <boost/python/str.hpp>
  15. // Just use math.h here; trying to use std::pow() causes too much
  16. // trouble for non-conforming compilers and libraries.
  17. #include <math.h>
  18. #if __GNUC__ != 2
  19. # include <ostream>
  20. #else
  21. # include <ostream.h>
  22. #endif
  23. using namespace boost::python;
  24. using namespace boost::python;
  25. struct X : test_class<>
  26. {
  27. typedef test_class<> base_t;
  28. X(int x) : base_t(x) {}
  29. X const operator+(X const& r) const { return X(value() + r.value()); }
  30. // typedef int (X::*safe_bool)() const;
  31. // operator safe_bool() const { return value() != 0 ? &X::value : 0; }
  32. };
  33. X operator-(X const& l, X const& r) { return X(l.value() - r.value()); }
  34. X operator-(int l, X const& r) { return X(l - r.value()); }
  35. X operator-(X const& l, int r) { return X(l.value() - r); }
  36. X operator-(X const& x) { return X(-x.value()); }
  37. X& operator-=(X& l, X const& r) { l.set(l.value() - r.value()); return l; }
  38. bool operator<(X const& x, X const& y) { return x.value() < y.value(); }
  39. bool operator<(X const& x, int y) { return x.value() < y; }
  40. bool operator<(int x, X const& y) { return x < y.value(); }
  41. X abs(X x) { return X(x.value() < 0 ? -x.value() : x.value()); }
  42. X pow(X x, int y)
  43. {
  44. return X(int(pow(double(x.value()), double(y))));
  45. }
  46. X pow(X x, X y)
  47. {
  48. return X(int(pow(double(x.value()), double(y.value()))));
  49. }
  50. int pow(int x, X y)
  51. {
  52. return int(pow(double(x), double(y.value())));
  53. }
  54. std::ostream& operator<<(std::ostream& s, X const& x)
  55. {
  56. return s << x.value();
  57. }
  58. struct number
  59. : boost::integer_arithmetic<number>
  60. {
  61. explicit number(long x_) : x(x_) {}
  62. operator long() const { return x; }
  63. template <class T>
  64. number& operator+=(T const& rhs)
  65. { x += rhs; return *this; }
  66. template <class T>
  67. number& operator-=(T const& rhs)
  68. { x -= rhs; return *this; }
  69. template <class T>
  70. number& operator*=(T const& rhs)
  71. { x *= rhs; return *this; }
  72. template <class T>
  73. number& operator/=(T const& rhs)
  74. { x /= rhs; return *this; }
  75. template <class T>
  76. number& operator%=(T const& rhs)
  77. { x %= rhs; return *this; }
  78. long x;
  79. };
  80. BOOST_PYTHON_MODULE(operators_ext)
  81. {
  82. class_<X>("X", init<int>())
  83. .def("value", &X::value)
  84. .def(self + self)
  85. .def(self - self)
  86. .def(self - int())
  87. .def(other<int>() - self)
  88. .def(-self)
  89. .def(self < other<int>())
  90. .def(self < self)
  91. .def(1 < self)
  92. .def(self -= self)
  93. .def(abs(self))
  94. .def(str(self))
  95. .def(pow(self,self))
  96. .def(pow(self,int()))
  97. .def(pow(int(),self))
  98. .def(
  99. !self
  100. // "not self" is legal here but causes friction on a few
  101. // nonconforming compilers; it's cute because it looks
  102. // like python, but doing it here doesn't prove much and
  103. // just causes tests to fail or complicated workarounds to
  104. // be enacted.
  105. )
  106. ;
  107. class_<number>("number", init<long>())
  108. // interoperate with self
  109. .def(self += self)
  110. .def(self + self)
  111. .def(self -= self)
  112. .def(self - self)
  113. .def(self *= self)
  114. .def(self * self)
  115. .def(self /= self)
  116. .def(self / self)
  117. .def(self %= self)
  118. .def(self % self)
  119. // Convert to Python int
  120. .def(int_(self))
  121. // interoperate with long
  122. .def(self += long())
  123. .def(self + long())
  124. .def(long() + self)
  125. .def(self -= long())
  126. .def(self - long())
  127. .def(long() - self)
  128. .def(self *= long())
  129. .def(self * long())
  130. .def(long() * self)
  131. .def(self /= long())
  132. .def(self / long())
  133. .def(long() / self)
  134. .def(self %= long())
  135. .def(self % long())
  136. .def(long() % self)
  137. ;
  138. class_<test_class<1> >("Z", init<int>())
  139. .def(int_(self))
  140. .def(float_(self))
  141. .def(complex_(self))
  142. ;
  143. }
  144. #include "module_tail.cpp"