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