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 | |
---|
6 | #include <boost/python/object_operators.hpp> |
---|
7 | #include <boost/python/detail/raw_pyobject.hpp> |
---|
8 | |
---|
9 | namespace boost { namespace python { namespace api { |
---|
10 | |
---|
11 | # define BOOST_PYTHON_COMPARE_OP(op, opid) \ |
---|
12 | BOOST_PYTHON_DECL object operator op(object const& l, object const& r) \ |
---|
13 | { \ |
---|
14 | return object( \ |
---|
15 | detail::new_reference( \ |
---|
16 | PyObject_RichCompare( \ |
---|
17 | l.ptr(), r.ptr(), opid)) \ |
---|
18 | ); \ |
---|
19 | } |
---|
20 | BOOST_PYTHON_COMPARE_OP(>, Py_GT) |
---|
21 | BOOST_PYTHON_COMPARE_OP(>=, Py_GE) |
---|
22 | BOOST_PYTHON_COMPARE_OP(<, Py_LT) |
---|
23 | BOOST_PYTHON_COMPARE_OP(<=, Py_LE) |
---|
24 | BOOST_PYTHON_COMPARE_OP(==, Py_EQ) |
---|
25 | BOOST_PYTHON_COMPARE_OP(!=, Py_NE) |
---|
26 | # undef BOOST_PYTHON_COMPARE_OP |
---|
27 | |
---|
28 | |
---|
29 | #define BOOST_PYTHON_BINARY_OPERATOR(op, name) \ |
---|
30 | BOOST_PYTHON_DECL object operator op(object const& l, object const& r) \ |
---|
31 | { \ |
---|
32 | return object( \ |
---|
33 | detail::new_reference( \ |
---|
34 | PyNumber_##name(l.ptr(), r.ptr())) \ |
---|
35 | ); \ |
---|
36 | } |
---|
37 | |
---|
38 | BOOST_PYTHON_BINARY_OPERATOR(+, Add) |
---|
39 | BOOST_PYTHON_BINARY_OPERATOR(-, Subtract) |
---|
40 | BOOST_PYTHON_BINARY_OPERATOR(*, Multiply) |
---|
41 | BOOST_PYTHON_BINARY_OPERATOR(/, Divide) |
---|
42 | BOOST_PYTHON_BINARY_OPERATOR(%, Remainder) |
---|
43 | BOOST_PYTHON_BINARY_OPERATOR(<<, Lshift) |
---|
44 | BOOST_PYTHON_BINARY_OPERATOR(>>, Rshift) |
---|
45 | BOOST_PYTHON_BINARY_OPERATOR(&, And) |
---|
46 | BOOST_PYTHON_BINARY_OPERATOR(^, Xor) |
---|
47 | BOOST_PYTHON_BINARY_OPERATOR(|, Or) |
---|
48 | #undef BOOST_PYTHON_BINARY_OPERATOR |
---|
49 | |
---|
50 | #define BOOST_PYTHON_INPLACE_OPERATOR(op, name) \ |
---|
51 | BOOST_PYTHON_DECL object& operator op##=(object& l, object const& r) \ |
---|
52 | { \ |
---|
53 | return l = object( \ |
---|
54 | (detail::new_reference) \ |
---|
55 | PyNumber_InPlace##name(l.ptr(), r.ptr())); \ |
---|
56 | } |
---|
57 | |
---|
58 | BOOST_PYTHON_INPLACE_OPERATOR(+, Add) |
---|
59 | BOOST_PYTHON_INPLACE_OPERATOR(-, Subtract) |
---|
60 | BOOST_PYTHON_INPLACE_OPERATOR(*, Multiply) |
---|
61 | BOOST_PYTHON_INPLACE_OPERATOR(/, Divide) |
---|
62 | BOOST_PYTHON_INPLACE_OPERATOR(%, Remainder) |
---|
63 | BOOST_PYTHON_INPLACE_OPERATOR(<<, Lshift) |
---|
64 | BOOST_PYTHON_INPLACE_OPERATOR(>>, Rshift) |
---|
65 | BOOST_PYTHON_INPLACE_OPERATOR(&, And) |
---|
66 | BOOST_PYTHON_INPLACE_OPERATOR(^, Xor) |
---|
67 | BOOST_PYTHON_INPLACE_OPERATOR(|, Or) |
---|
68 | #undef BOOST_PYTHON_INPLACE_OPERATOR |
---|
69 | |
---|
70 | object::object(handle<> const& x) |
---|
71 | : object_base(python::incref(python::expect_non_null(x.get()))) |
---|
72 | {} |
---|
73 | |
---|
74 | }}} // namespace boost::python |
---|