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