• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if PY_VERSION_HEX >= 0x03000000
42 // We choose FloorDivide instead of TrueDivide to keep the semantic
43 // conform with C/C++'s '/' operator
44 BOOST_PYTHON_BINARY_OPERATOR(/, FloorDivide)
45 #else
46 BOOST_PYTHON_BINARY_OPERATOR(/, Divide)
47 #endif
48 BOOST_PYTHON_BINARY_OPERATOR(%, Remainder)
49 BOOST_PYTHON_BINARY_OPERATOR(<<, Lshift)
50 BOOST_PYTHON_BINARY_OPERATOR(>>, Rshift)
51 BOOST_PYTHON_BINARY_OPERATOR(&, And)
52 BOOST_PYTHON_BINARY_OPERATOR(^, Xor)
53 BOOST_PYTHON_BINARY_OPERATOR(|, Or)
54 #undef BOOST_PYTHON_BINARY_OPERATOR
55 
56 #define BOOST_PYTHON_INPLACE_OPERATOR(op, name)                         \
57 BOOST_PYTHON_DECL object& operator op##=(object& l, object const& r)    \
58 {                                                                       \
59     return l = object(                                                  \
60         (detail::new_reference)                                         \
61             PyNumber_InPlace##name(l.ptr(), r.ptr()));                  \
62 }
63 
64 BOOST_PYTHON_INPLACE_OPERATOR(+, Add)
65 BOOST_PYTHON_INPLACE_OPERATOR(-, Subtract)
66 BOOST_PYTHON_INPLACE_OPERATOR(*, Multiply)
67 #if PY_VERSION_HEX >= 0x03000000
68 // Same reason as above for choosing FloorDivide instead of TrueDivide
69 BOOST_PYTHON_INPLACE_OPERATOR(/, FloorDivide)
70 #else
71 BOOST_PYTHON_INPLACE_OPERATOR(/, Divide)
72 #endif
73 BOOST_PYTHON_INPLACE_OPERATOR(%, Remainder)
74 BOOST_PYTHON_INPLACE_OPERATOR(<<, Lshift)
75 BOOST_PYTHON_INPLACE_OPERATOR(>>, Rshift)
76 BOOST_PYTHON_INPLACE_OPERATOR(&, And)
77 BOOST_PYTHON_INPLACE_OPERATOR(^, Xor)
78 BOOST_PYTHON_INPLACE_OPERATOR(|, Or)
79 #undef BOOST_PYTHON_INPLACE_OPERATOR
80 
object(handle<> const & x)81 object::object(handle<> const& x)
82      : object_base(python::incref(python::expect_non_null(x.get())))
83 {}
84 
85 }}} // namespace boost::python
86