Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/src/object_operators.cpp @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.1 KB
Line 
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
9namespace boost { namespace python { namespace api {
10
11# define BOOST_PYTHON_COMPARE_OP(op, opid)                              \
12BOOST_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}
20BOOST_PYTHON_COMPARE_OP(>, Py_GT)
21BOOST_PYTHON_COMPARE_OP(>=, Py_GE)
22BOOST_PYTHON_COMPARE_OP(<, Py_LT)
23BOOST_PYTHON_COMPARE_OP(<=, Py_LE)
24BOOST_PYTHON_COMPARE_OP(==, Py_EQ)
25BOOST_PYTHON_COMPARE_OP(!=, Py_NE)
26# undef BOOST_PYTHON_COMPARE_OP
27   
28
29#define BOOST_PYTHON_BINARY_OPERATOR(op, name)                          \
30BOOST_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
38BOOST_PYTHON_BINARY_OPERATOR(+, Add)
39BOOST_PYTHON_BINARY_OPERATOR(-, Subtract)
40BOOST_PYTHON_BINARY_OPERATOR(*, Multiply)
41BOOST_PYTHON_BINARY_OPERATOR(/, Divide)
42BOOST_PYTHON_BINARY_OPERATOR(%, Remainder)
43BOOST_PYTHON_BINARY_OPERATOR(<<, Lshift)
44BOOST_PYTHON_BINARY_OPERATOR(>>, Rshift)
45BOOST_PYTHON_BINARY_OPERATOR(&, And)
46BOOST_PYTHON_BINARY_OPERATOR(^, Xor)
47BOOST_PYTHON_BINARY_OPERATOR(|, Or)
48#undef BOOST_PYTHON_BINARY_OPERATOR
49
50#define BOOST_PYTHON_INPLACE_OPERATOR(op, name)                         \
51BOOST_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   
58BOOST_PYTHON_INPLACE_OPERATOR(+, Add)
59BOOST_PYTHON_INPLACE_OPERATOR(-, Subtract)
60BOOST_PYTHON_INPLACE_OPERATOR(*, Multiply)
61BOOST_PYTHON_INPLACE_OPERATOR(/, Divide)
62BOOST_PYTHON_INPLACE_OPERATOR(%, Remainder)
63BOOST_PYTHON_INPLACE_OPERATOR(<<, Lshift)
64BOOST_PYTHON_INPLACE_OPERATOR(>>, Rshift)
65BOOST_PYTHON_INPLACE_OPERATOR(&, And)
66BOOST_PYTHON_INPLACE_OPERATOR(^, Xor)
67BOOST_PYTHON_INPLACE_OPERATOR(|, Or)
68#undef BOOST_PYTHON_INPLACE_OPERATOR
69
70object::object(handle<> const& x)
71     : object_base(python::incref(python::expect_non_null(x.get())))
72{}
73
74}}} // namespace boost::python
Note: See TracBrowser for help on using the repository browser.