// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef OPERATORS_DWA2002530_HPP
# define OPERATORS_DWA2002530_HPP
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
namespace boost { namespace python {
namespace detail
{
// This is essentially the old v1 to_python(). It will be eliminated
// once the public interface for to_python is settled on.
template
PyObject* convert_result(T const& x)
{
return converter::arg_to_python(x).release();
}
// Operator implementation template declarations. The nested apply
// declaration here keeps MSVC6 happy.
template struct operator_l
{
template struct apply;
};
template struct operator_r
{
template struct apply;
};
template struct operator_1
{
template struct apply;
};
// MSVC6 doesn't want us to do this sort of inheritance on a nested
// class template, so we use this layer of indirection to avoid
// ::template on the nested apply functions below
template
struct operator_l_inner
: operator_l::template apply
{};
template
struct operator_r_inner
: operator_r::template apply
{};
template
struct operator_1_inner
: operator_1::template apply
{};
// Define three different binary_op templates which take care of
// these cases:
// self op self
// self op R
// L op self
//
// The inner apply metafunction is used to adjust the operator to
// the class type being defined. Inheritance of the outer class is
// simply used to provide convenient access to the operation's
// name().
// self op self
template
struct binary_op : operator_l
{
template
struct apply : operator_l_inner
{
};
};
// self op R
template
struct binary_op_l : operator_l
{
template
struct apply : operator_l_inner
{
};
};
// L op self
template
struct binary_op_r : operator_r
{
template
struct apply : operator_r_inner
{
};
};
template
struct unary_op : operator_1
{
template
struct apply : operator_1_inner
{
};
};
// This type is what actually gets returned from operators used on
// self_t
template
struct operator_
: def_visitor
{
private:
template
void visit(ClassT& cl) const
{
typedef typename mpl::eval_if<
is_same
, mpl::if_<
is_same
, binary_op
, binary_op_l<
id
, BOOST_DEDUCED_TYPENAME unwrap_other::type
>
>
, mpl::if_<
is_same
, unary_op
, binary_op_r<
id
, BOOST_DEDUCED_TYPENAME unwrap_other::type
>
>
>::type generator;
cl.def(
generator::name()
, &generator::template apply<
BOOST_DEDUCED_TYPENAME ClassT::wrapped_type
>::execute
);
}
friend class python::def_visitor_access;
};
}
# define BOOST_PYTHON_BINARY_OPERATION(id, rid, expr) \
namespace detail \
{ \
template \
struct operator_l \
{ \
template \
struct apply \
{ \
typedef typename unwrap_wrapper_::type lhs; \
typedef typename unwrap_wrapper_::type rhs; \
static PyObject* execute(lhs& l, rhs const& r) \
{ \
return detail::convert_result(expr); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
\
template \
struct operator_r \
{ \
template \
struct apply \
{ \
typedef typename unwrap_wrapper_::type lhs; \
typedef typename unwrap_wrapper_::type rhs; \
static PyObject* execute(rhs& r, lhs const& l) \
{ \
return detail::convert_result(expr); \
} \
}; \
static char const* name() { return "__" #rid "__"; } \
}; \
}
# define BOOST_PYTHON_BINARY_OPERATOR(id, rid, op) \
BOOST_PYTHON_BINARY_OPERATION(id, rid, l op r) \
namespace self_ns \
{ \
template \
inline detail::operator_ \
operator op(L const&, R const&) \
{ \
return detail::operator_(); \
} \
}
BOOST_PYTHON_BINARY_OPERATOR(add, radd, +)
BOOST_PYTHON_BINARY_OPERATOR(sub, rsub, -)
BOOST_PYTHON_BINARY_OPERATOR(mul, rmul, *)
#if PY_VERSION_HEX >= 0x03000000
BOOST_PYTHON_BINARY_OPERATOR(truediv, rtruediv, /)
#else
BOOST_PYTHON_BINARY_OPERATOR(div, rdiv, /)
#endif
BOOST_PYTHON_BINARY_OPERATOR(mod, rmod, %)
BOOST_PYTHON_BINARY_OPERATOR(lshift, rlshift, )
BOOST_PYTHON_BINARY_OPERATOR(and, rand, &)
BOOST_PYTHON_BINARY_OPERATOR(xor, rxor, ^)
BOOST_PYTHON_BINARY_OPERATOR(or, ror, |)
BOOST_PYTHON_BINARY_OPERATOR(gt, lt, >)
BOOST_PYTHON_BINARY_OPERATOR(ge, le, >=)
BOOST_PYTHON_BINARY_OPERATOR(lt, gt,