Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:07:13

0001 // Copyright David Abrahams 2002.
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef OBJECT_OPERATORS_DWA2002617_HPP
0006 # define OBJECT_OPERATORS_DWA2002617_HPP
0007 
0008 # include <boost/python/detail/prefix.hpp>
0009 
0010 # include <boost/python/object_core.hpp>
0011 # include <boost/python/call.hpp>
0012 # include <boost/type_traits/enable_if.hpp>
0013 # include <boost/mpl/bool.hpp>
0014 
0015 # include <boost/iterator/detail/config_def.hpp>
0016 
0017 namespace boost { namespace python { namespace api {
0018 
0019 template <class X>
0020 char is_object_operators_helper(object_operators<X> const*);
0021     
0022 typedef char (&no_type)[2];
0023 no_type is_object_operators_helper(...);
0024 
0025 template <class X> X* make_ptr();
0026 
0027 template <class L, class R = L>
0028 struct is_object_operators
0029 {
0030     enum {
0031         value 
0032         = (sizeof(api::is_object_operators_helper(api::make_ptr<L>()))
0033            + sizeof(api::is_object_operators_helper(api::make_ptr<R>()))
0034            < 4
0035         )
0036     };
0037     typedef mpl::bool_<value> type;
0038 };
0039 
0040 # if !defined(BOOST_NO_SFINAE) && !defined(BOOST_NO_IS_CONVERTIBLE)
0041 template <class L, class R, class T>
0042 struct enable_binary
0043   : boost::enable_if_<is_object_operators<L,R>::value, T>
0044 {};
0045 #  define BOOST_PYTHON_BINARY_RETURN(T) typename enable_binary<L,R,T>::type
0046 # else
0047 #  define BOOST_PYTHON_BINARY_RETURN(T) T
0048 # endif
0049 
0050 template <class U>
0051 object object_operators<U>::operator()() const
0052 {
0053     object_cref2 f = *static_cast<U const*>(this);
0054     return call<object>(f.ptr());
0055 }
0056 
0057 
0058 template <class U>
0059 inline
0060 object_operators<U>::operator bool_type() const
0061 {
0062     object_cref2 x = *static_cast<U const*>(this);
0063     int is_true = PyObject_IsTrue(x.ptr());
0064     if (is_true < 0) throw_error_already_set();
0065     return is_true ? &object::ptr : 0;
0066 }
0067 
0068 template <class U>
0069 inline bool
0070 object_operators<U>::operator!() const
0071 {
0072     object_cref2 x = *static_cast<U const*>(this);
0073     int is_true = PyObject_IsTrue(x.ptr());
0074     if (is_true < 0) throw_error_already_set();
0075     return !is_true;
0076 }
0077 
0078 # define BOOST_PYTHON_COMPARE_OP(op, opid)                              \
0079 template <class L, class R>                                             \
0080 BOOST_PYTHON_BINARY_RETURN(object) operator op(L const& l, R const& r)    \
0081 {                                                                       \
0082     return PyObject_RichCompare(                                    \
0083         object(l).ptr(), object(r).ptr(), opid);                        \
0084 }
0085 # undef BOOST_PYTHON_COMPARE_OP
0086     
0087 # define BOOST_PYTHON_BINARY_OPERATOR(op)                               \
0088 BOOST_PYTHON_DECL object operator op(object const& l, object const& r); \
0089 template <class L, class R>                                             \
0090 BOOST_PYTHON_BINARY_RETURN(object) operator op(L const& l, R const& r)  \
0091 {                                                                       \
0092     return object(l) op object(r);                                      \
0093 }
0094 BOOST_PYTHON_BINARY_OPERATOR(>)
0095 BOOST_PYTHON_BINARY_OPERATOR(>=)
0096 BOOST_PYTHON_BINARY_OPERATOR(<)
0097 BOOST_PYTHON_BINARY_OPERATOR(<=)
0098 BOOST_PYTHON_BINARY_OPERATOR(==)
0099 BOOST_PYTHON_BINARY_OPERATOR(!=)
0100 BOOST_PYTHON_BINARY_OPERATOR(+)
0101 BOOST_PYTHON_BINARY_OPERATOR(-)
0102 BOOST_PYTHON_BINARY_OPERATOR(*)
0103 BOOST_PYTHON_BINARY_OPERATOR(/)
0104 BOOST_PYTHON_BINARY_OPERATOR(%)
0105 BOOST_PYTHON_BINARY_OPERATOR(<<)
0106 BOOST_PYTHON_BINARY_OPERATOR(>>)
0107 BOOST_PYTHON_BINARY_OPERATOR(&)
0108 BOOST_PYTHON_BINARY_OPERATOR(^)
0109 BOOST_PYTHON_BINARY_OPERATOR(|)
0110 # undef BOOST_PYTHON_BINARY_OPERATOR
0111 
0112         
0113 # define BOOST_PYTHON_INPLACE_OPERATOR(op)                              \
0114 BOOST_PYTHON_DECL object& operator op(object& l, object const& r);      \
0115 template <class R>                                                      \
0116 object& operator op(object& l, R const& r)                              \
0117 {                                                                       \
0118     return l op object(r);                                              \
0119 }
0120 BOOST_PYTHON_INPLACE_OPERATOR(+=)
0121 BOOST_PYTHON_INPLACE_OPERATOR(-=)
0122 BOOST_PYTHON_INPLACE_OPERATOR(*=)
0123 BOOST_PYTHON_INPLACE_OPERATOR(/=)
0124 BOOST_PYTHON_INPLACE_OPERATOR(%=)
0125 BOOST_PYTHON_INPLACE_OPERATOR(<<=)
0126 BOOST_PYTHON_INPLACE_OPERATOR(>>=)
0127 BOOST_PYTHON_INPLACE_OPERATOR(&=)
0128 BOOST_PYTHON_INPLACE_OPERATOR(^=)
0129 BOOST_PYTHON_INPLACE_OPERATOR(|=)
0130 # undef BOOST_PYTHON_INPLACE_OPERATOR
0131 
0132 }}} // namespace boost::python
0133 
0134 #include <boost/iterator/detail/config_undef.hpp>
0135 
0136 #endif // OBJECT_OPERATORS_DWA2002617_HPP