]>
git.proxmox.com Git - ceph.git/blob - 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.
25 using namespace boost::python
;
28 using namespace boost::python
;
30 struct X
: test_class
<>
32 typedef test_class
<> base_t
;
34 X(int x
) : base_t(x
) {}
35 X
const operator+(X
const& r
) const { return X(value() + r
.value()); }
37 // typedef int (X::*safe_bool)() const;
38 // operator safe_bool() const { return value() != 0 ? &X::value : 0; }
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
); }
45 X
operator-(X
const& x
) { return X(-x
.value()); }
47 X
& operator-=(X
& l
, X
const& r
) { l
.set(l
.value() - r
.value()); return l
; }
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(); }
53 X
abs(X x
) { return X(x
.value() < 0 ? -x
.value() : x
.value()); }
57 return X(int(pow(double(x
.value()), double(y
))));
62 return X(int(pow(double(x
.value()), double(y
.value()))));
67 return int(pow(double(x
), double(y
.value())));
70 std::ostream
& operator<<(std::ostream
& s
, X
const& x
)
72 return s
<< x
.value();
76 : boost::integer_arithmetic
<number
>
78 explicit number(long x_
) : x(x_
) {}
79 operator long() const { return x
; }
82 number
& operator+=(T
const& rhs
)
83 { x
+= rhs
; return *this; }
86 number
& operator-=(T
const& rhs
)
87 { x
-= rhs
; return *this; }
90 number
& operator*=(T
const& rhs
)
91 { x
*= rhs
; return *this; }
94 number
& operator/=(T
const& rhs
)
95 { x
/= rhs
; return *this; }
98 number
& operator%=(T
const& rhs
)
99 { x
%= rhs
; return *this; }
104 BOOST_PYTHON_MODULE(operators_ext
)
106 class_
<X
>("X", init
<int>())
107 .def("value", &X::value
)
111 .def(other
<int>() - self
)
113 .def(self
< other
<int>())
122 .def(pow(self
,int()))
123 .def(pow(int(),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
134 class_
<number
>("number", init
<long>())
135 // interoperate with self
147 // Convert to Python int
150 // interoperate with long
168 class_
<test_class
<1> >("Z", init
<int>())
175 #include "module_tail.cpp"