source: NonGTP/Boost/boost/python/object_operators.hpp @ 857

Revision 857, 4.4 KB checked in by igarcia, 19 years ago (diff)
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#ifndef OBJECT_OPERATORS_DWA2002617_HPP
6# define OBJECT_OPERATORS_DWA2002617_HPP
7
8# include <boost/python/detail/prefix.hpp>
9
10# include <boost/python/object_core.hpp>
11# include <boost/python/call.hpp>
12# include <boost/iterator/detail/enable_if.hpp>
13# include <boost/mpl/bool.hpp>
14
15# include <boost/iterator/detail/config_def.hpp>
16
17namespace boost { namespace python { namespace api {
18
19template <class X>
20char is_object_operators_helper(object_operators<X> const*);
21   
22typedef char (&no_type)[2];
23no_type is_object_operators_helper(...);
24
25template <class X> X* make_ptr();
26
27template <class L, class R = L>
28struct is_object_operators
29{
30    enum {
31        value
32        = (sizeof(api::is_object_operators_helper(api::make_ptr<L>()))
33           + sizeof(api::is_object_operators_helper(api::make_ptr<R>()))
34           < 4
35        )
36    };
37    typedef mpl::bool_<value> type;
38};
39
40# if !defined(BOOST_NO_SFINAE) && !defined(BOOST_NO_IS_CONVERTIBLE)
41template <class L, class R, class T>
42struct enable_binary
43  : boost::iterators::enable_if<is_object_operators<L,R>, T>
44{};
45#  define BOOST_PYTHON_BINARY_RETURN(T) typename enable_binary<L,R,T>::type
46# else
47#  define BOOST_PYTHON_BINARY_RETURN(T) T
48# endif
49
50template <class U>
51object object_operators<U>::operator()() const
52{
53    object_cref2 f = *static_cast<U const*>(this);
54    return call<object>(f.ptr());
55}
56
57
58template <class U>
59inline
60object_operators<U>::operator bool_type() const
61{
62    object_cref2 x = *static_cast<U const*>(this);
63    return PyObject_IsTrue(x.ptr()) ? &object::ptr : 0;
64}
65
66template <class U>
67inline bool
68object_operators<U>::operator!() const
69{
70    object_cref2 x = *static_cast<U const*>(this);
71    return !PyObject_IsTrue(x.ptr());
72}
73
74# define BOOST_PYTHON_COMPARE_OP(op, opid)                              \
75template <class L, class R>                                             \
76BOOST_PYTHON_BINARY_RETURN(bool) operator op(L const& l, R const& r)    \
77{                                                                       \
78    return PyObject_RichCompareBool(                                    \
79        object(l).ptr(), object(r).ptr(), opid);                        \
80}
81BOOST_PYTHON_COMPARE_OP(>, Py_GT)
82BOOST_PYTHON_COMPARE_OP(>=, Py_GE)
83BOOST_PYTHON_COMPARE_OP(<, Py_LT)
84BOOST_PYTHON_COMPARE_OP(<=, Py_LE)
85BOOST_PYTHON_COMPARE_OP(==, Py_EQ)
86BOOST_PYTHON_COMPARE_OP(!=, Py_NE)
87# undef BOOST_PYTHON_COMPARE_OP
88   
89# define BOOST_PYTHON_BINARY_OPERATOR(op)                               \
90BOOST_PYTHON_DECL object operator op(object const& l, object const& r); \
91template <class L, class R>                                             \
92BOOST_PYTHON_BINARY_RETURN(object) operator op(L const& l, R const& r)  \
93{                                                                       \
94    return object(l) op object(r);                                      \
95}
96BOOST_PYTHON_BINARY_OPERATOR(+)
97BOOST_PYTHON_BINARY_OPERATOR(-)
98BOOST_PYTHON_BINARY_OPERATOR(*)
99BOOST_PYTHON_BINARY_OPERATOR(/)
100BOOST_PYTHON_BINARY_OPERATOR(%)
101BOOST_PYTHON_BINARY_OPERATOR(<<)
102BOOST_PYTHON_BINARY_OPERATOR(>>)
103BOOST_PYTHON_BINARY_OPERATOR(&)
104BOOST_PYTHON_BINARY_OPERATOR(^)
105BOOST_PYTHON_BINARY_OPERATOR(|)
106# undef BOOST_PYTHON_BINARY_OPERATOR
107
108       
109# define BOOST_PYTHON_INPLACE_OPERATOR(op)                              \
110BOOST_PYTHON_DECL object& operator op(object& l, object const& r);      \
111template <class R>                                                      \
112object& operator op(object& l, R const& r)                              \
113{                                                                       \
114    return l op object(r);                                              \
115}
116BOOST_PYTHON_INPLACE_OPERATOR(+=)
117BOOST_PYTHON_INPLACE_OPERATOR(-=)
118BOOST_PYTHON_INPLACE_OPERATOR(*=)
119BOOST_PYTHON_INPLACE_OPERATOR(/=)
120BOOST_PYTHON_INPLACE_OPERATOR(%=)
121BOOST_PYTHON_INPLACE_OPERATOR(<<=)
122BOOST_PYTHON_INPLACE_OPERATOR(>>=)
123BOOST_PYTHON_INPLACE_OPERATOR(&=)
124BOOST_PYTHON_INPLACE_OPERATOR(^=)
125BOOST_PYTHON_INPLACE_OPERATOR(|=)
126# undef BOOST_PYTHON_INPLACE_OPERATOR
127
128}}} // namespace boost::python
129
130#include <boost/iterator/detail/config_undef.hpp>
131
132#endif // OBJECT_OPERATORS_DWA2002617_HPP
Note: See TracBrowser for help on using the repository browser.