2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 18:12:43 +00:00

Clean up Boost.Python v1 flotsam, update documentation

[SVN r15815]
This commit is contained in:
Dave Abrahams
2002-10-09 02:52:47 +00:00
parent 920125794a
commit 4fd20185e9
39 changed files with 1180 additions and 7626 deletions

62
include/boost/python.hpp Normal file
View File

@@ -0,0 +1,62 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef PYTHON_DWA2002810_HPP
# define PYTHON_DWA2002810_HPP
# include <args.hpp>
# include <args_fwd.hpp>
# include <back_reference.hpp>
# include <bases.hpp>
# include <borrowed.hpp>
# include <call.hpp>
# include <call_method.hpp>
# include <class.hpp>
# include <copy_const_reference.hpp>
# include <copy_non_const_reference.hpp>
# include <data_members.hpp>
# include <def.hpp>
# include <default_call_policies.hpp>
# include <dict.hpp>
# include <enum.hpp>
# include <errors.hpp>
# include <exception_translator.hpp>
# include <extract.hpp>
# include <handle.hpp>
# include <has_back_reference.hpp>
# include <implicit.hpp>
# include <init.hpp>
# include <instance_holder.hpp>
# include <iterator.hpp>
# include <list.hpp>
# include <long.hpp>
# include <lvalue_from_pytype.hpp>
# include <make_function.hpp>
# include <manage_new_object.hpp>
# include <module.hpp>
# include <numeric.hpp>
# include <object.hpp>
# include <object_protocol.hpp>
# include <object_protocol_core.hpp>
# include <operators.hpp>
# include <other.hpp>
# include <overloads.hpp>
# include <pointee.hpp>
# include <ptr.hpp>
# include <reference_existing_object.hpp>
# include <return_internal_reference.hpp>
# include <return_value_policy.hpp>
# include <scope.hpp>
# include <self.hpp>
# include <slice_nil.hpp>
# include <str.hpp>
# include <to_python_converter.hpp>
# include <to_python_indirect.hpp>
# include <to_python_value.hpp>
# include <tuple.hpp>
# include <type_id.hpp>
# include <with_custodian_and_ward.hpp>
#endif PYTHON_DWA2002810_HPP

View File

@@ -1,53 +0,0 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifdef BOOST_PYTHON_V2
# error obsolete
#endif
#ifndef MODULE_INIT_DWA2002529_HPP
# define MODULE_INIT_DWA2002529_HPP
# ifndef BOOST_PYTHON_MODULE_INIT
# if defined(_WIN32) || defined(__CYGWIN__)
# define BOOST_PYTHON_MODULE_INIT(name) \
void init_module_##name(); \
extern "C" __declspec(dllexport) void init##name() \
{ \
boost::python::handle_exception(&init_module_##name); \
} \
void init_module_##name()
# elif defined(_AIX)
# include <boost/python/detail/aix_init_module.hpp>
# define BOOST_PYTHON_MODULE_INIT(name) \
void init_module_##name(); \
extern "C" \
{ \
extern PyObject* _PyImport_LoadDynamicModule(char*, char*, FILE *); \
void init##name() \
{ \
boost::python::detail::aix_init_module(_PyImport_LoadDynamicModule, &init_module_##name); \
} \
} \
void init_module_##name()
# else
# define BOOST_PYTHON_MODULE_INIT(name) \
void init_module_##name(); \
extern "C" void init##name() \
{ \
boost::python::handle_exception(&init_module_##name); \
} \
void init_module_##name()
# endif
# endif
#endif // MODULE_INIT_DWA2002529_HPP

View File

@@ -46,11 +46,9 @@ inline T* expect_non_null(T* x)
return x;
}
# ifdef BOOST_PYTHON_V2
// Return source if it is an instance of pytype; throw an appropriate
// exception otherwise.
BOOST_PYTHON_DECL PyObject* pytype_check(PyTypeObject* pytype, PyObject* source);
# endif
}} // namespace boost::python

View File

@@ -270,13 +270,13 @@ class init : public init_base<init<BOOST_PYTHON_OVERLOAD_ARGS> >
typedef typename mpl::fold<
required_args
, mpl::list0<>
, mpl::push_front<mpl::_,mpl::_>
, mpl::push_front<>
>::type reversed_required;
typedef typename mpl::fold<
optional_args
, reversed_required
, mpl::push_front<mpl::_,mpl::_>
, mpl::push_front<>
>::type reversed_args;
// Count the maximum number of arguments
@@ -310,7 +310,7 @@ namespace detail
typedef typename mpl::fold<
ReversedArgs
, mpl::list0<>
, mpl::push_front<mpl::_,mpl::_>
, mpl::push_front<>
>::type args;
typedef typename ClassT::holder_selector holder_selector_t;

View File

@@ -1,555 +1,332 @@
// (C) Copyright Ullrich Koethe and David Abrahams 2000-2001. Permission to
// copy, use, modify, sell and distribute this software is granted provided
// this copyright notice appears in all copies. This software is provided "as
// is" without express or implied warranty, and with no claim as to its
// suitability for any purpose.
//
// The authors gratefully acknowlege the support of Dragon Systems, Inc., in
// producing this work.
//
// Revision History:
// 23 Jan 2001 - Another stupid typo fix by Ralf W. Grosse-Kunstleve (David Abrahams)
// 20 Jan 2001 - Added a fix from Ralf W. Grosse-Kunstleve (David Abrahams)
#ifndef OPERATORS_UK112000_H_
# define OPERATORS_UK112000_H_
# ifdef BOOST_PYTHON_V2
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef OPERATORS_DWA2002530_HPP
# define OPERATORS_DWA2002530_HPP
# include <boost/python/operators2.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/converter/arg_to_python.hpp>
# include <boost/python/detail/operator_id.hpp>
# include <boost/python/detail/not_specified.hpp>
# include <boost/python/back_reference.hpp>
# include <boost/mpl/if.hpp>
# include <boost/python/self.hpp>
# include <boost/python/other.hpp>
# include <boost/lexical_cast.hpp>
# include <boost/python/refcount.hpp>
# include <string>
# include <complex>
# else
# include <boost/python/reference.hpp>
# include <boost/python/detail/functions.hpp>
// When STLport is used with native streams, _STL::ostringstream().str() is not
// _STL::string, but std::string. This confuses to_python(), so we'll use
// strstream instead. Also, GCC 2.95.2 doesn't have sstream.
# if defined(__SGI_STL_PORT) ? defined(__SGI_STL_OWN_IOSTREAMS) : (!defined(__GNUC__) || __GNUC__ > 2)
# define BOOST_PYTHON_USE_SSTREAM
# endif
# if defined(BOOST_PYTHON_USE_SSTREAM)
# include <sstream>
# else
# include <strstream>
# endif
namespace boost { namespace python {
BOOST_PYTHON_DECL tuple standard_coerce(ref l, ref r);
namespace detail {
// helper class for automatic operand type detection
// during operator wrapping.
struct auto_operand {};
}
// Define operator ids that can be or'ed together
// (boost::python::op_add | boost::python::op_sub | boost::python::op_mul).
// This allows to wrap several operators in one line.
enum operator_id
{
op_add = 0x1,
op_sub = 0x2,
op_mul = 0x4,
op_div = 0x8,
op_mod = 0x10,
op_divmod =0x20,
op_pow = 0x40,
op_lshift = 0x80,
op_rshift = 0x100,
op_and = 0x200,
op_xor = 0x400,
op_or = 0x800,
op_neg = 0x1000,
op_pos = 0x2000,
op_abs = 0x4000,
op_invert = 0x8000,
op_int = 0x10000,
op_long = 0x20000,
op_float = 0x40000,
op_str = 0x80000,
op_cmp = 0x100000,
op_gt = 0x200000,
op_ge = 0x400000,
op_lt = 0x800000,
op_le = 0x1000000,
op_eq = 0x2000000,
op_ne = 0x4000000
};
// Wrap the operators given by "which". Usage:
// foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub)>());
template <long which, class operand = boost::python::detail::auto_operand>
struct operators {};
// Wrap heterogeneous operators with given left operand type. Usage:
// foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub)>(),
// boost::python::left_operand<int>());
template <class T>
struct left_operand {};
// Wrap heterogeneous operators with given right operand type. Usage:
// foo_class.def(boost::python::operators<(boost::python::op_add | boost::python::op_sub)>(),
// boost::python::right_operand<int>());
template <class T>
struct right_operand {};
namespace boost { namespace python {
namespace detail
{
template <class Specified>
struct operand_select
// This is essentially the old v1 to_python(). It will be eliminated
// once the public interface for to_python is settled on.
template <class T>
PyObject* convert_result(T const& x)
{
template <class wrapped_type>
struct wrapped
{
typedef Specified type;
};
};
template <>
struct operand_select<auto_operand>
{
template <class wrapped_type>
struct wrapped
{
typedef const wrapped_type& type;
};
};
template <long> struct define_operator;
// Base class which grants access to extension_class_base::add_method() to its derived classes
struct add_operator_base
{
protected:
static inline void add_method(extension_class_base* target, function* method, const char* name)
{ target->add_method(method, name); }
};
//
// choose_op, choose_unary_op, and choose_rop
//
// These templates use "poor man's partial specialization" to generate the
// appropriate add_method() call (if any) for a given operator and argument set.
//
// Usage:
// choose_op<(which & op_add)>::template args<left_t,right_t>::add(ext_class);
//
// (see extension_class<>::def_operators() for more examples).
//
template <long op_selector>
struct choose_op
{
template <class Left, class Right = Left>
struct args : add_operator_base
{
static inline void add(extension_class_base* target)
{
typedef define_operator<op_selector> def_op;
add_method(target,
new typename def_op::template operator_function<Left, Right>(),
def_op::name());
}
};
};
// specialization for 0 has no effect
template <>
struct choose_op<0>
{
template <class Left, class Right = Left>
struct args
{
static inline void add(extension_class_base*)
{
}
};
};
template <long op_selector>
struct choose_unary_op
{
template <class Operand>
struct args : add_operator_base
{
static inline void add(extension_class_base* target)
{
typedef define_operator<op_selector> def_op;
add_method(target,
new typename def_op::template operator_function<Operand>(),
def_op::name());
}
};
};
// specialization for 0 has no effect
template <>
struct choose_unary_op<0>
{
template <class Operand>
struct args
{
static inline void add(extension_class_base*)
{
}
};
};
template <long op_selector>
struct choose_rop
{
template <class Left, class Right = Left>
struct args : add_operator_base
{
static inline void add(extension_class_base* target)
{
typedef define_operator<op_selector> def_op;
add_method(target,
new typename def_op::template roperator_function<Right, Left>(),
def_op::rname());
}
};
};
// specialization for 0 has no effect
template <>
struct choose_rop<0>
{
template <class Left, class Right = Left>
struct args
{
static inline void add(extension_class_base*)
{
}
};
};
// Fully specialize define_operator for all operators defined in operator_id above.
// Every specialization defines one function object for normal operator calls and one
// for operator calls with operands reversed ("__r*__" function variants).
// Specializations for most operators follow a standard pattern: execute the expression
// that uses the operator in question. This standard pattern is realized by the following
// macros so that the actual specialization can be done by just calling a macro.
# define PY_DEFINE_BINARY_OPERATORS(id, oper) \
template <> \
struct define_operator<op_##id> \
{ \
template <class Left, class Right = Left> \
struct operator_function : function \
{ \
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const \
{ \
tuple args(ref(arguments, ref::increment_count)); \
\
return BOOST_PYTHON_CONVERSION::to_python( \
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) oper \
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())); \
} \
\
const char* description() const \
{ return "__" #id "__"; } \
}; \
\
template <class Right, class Left> \
struct roperator_function : function \
{ \
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const \
{ \
tuple args(ref(arguments, ref::increment_count)); \
\
return BOOST_PYTHON_CONVERSION::to_python( \
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) oper \
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())); \
} \
\
const char* description() const \
{ return "__r" #id "__"; } \
\
}; \
\
static const char * name() { return "__" #id "__"; } \
static const char * rname() { return "__r" #id "__"; } \
return converter::arg_to_python<T>(x).release();
}
# define PY_DEFINE_UNARY_OPERATORS(id, oper) \
template <> \
struct define_operator<op_##id> \
{ \
template <class operand> \
struct operator_function : function \
{ \
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const \
{ \
tuple args(ref(arguments, ref::increment_count)); \
\
return BOOST_PYTHON_CONVERSION::to_python( \
oper(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<operand>()))); \
} \
\
const char* description() const \
{ return "__" #id "__"; } \
}; \
\
static const char * name() { return "__" #id "__"; } \
}
PY_DEFINE_BINARY_OPERATORS(add, +);
PY_DEFINE_BINARY_OPERATORS(sub, -);
PY_DEFINE_BINARY_OPERATORS(mul, *);
PY_DEFINE_BINARY_OPERATORS(div, /);
PY_DEFINE_BINARY_OPERATORS(mod, %);
PY_DEFINE_BINARY_OPERATORS(lshift, <<);
PY_DEFINE_BINARY_OPERATORS(rshift, >>);
PY_DEFINE_BINARY_OPERATORS(and, &);
PY_DEFINE_BINARY_OPERATORS(xor, ^);
PY_DEFINE_BINARY_OPERATORS(or, |);
PY_DEFINE_BINARY_OPERATORS(gt, >);
PY_DEFINE_BINARY_OPERATORS(ge, >=);
PY_DEFINE_BINARY_OPERATORS(lt, <);
PY_DEFINE_BINARY_OPERATORS(le, <=);
PY_DEFINE_BINARY_OPERATORS(eq, ==);
PY_DEFINE_BINARY_OPERATORS(ne, !=);
PY_DEFINE_UNARY_OPERATORS(neg, -);
PY_DEFINE_UNARY_OPERATORS(pos, +);
PY_DEFINE_UNARY_OPERATORS(abs, abs);
PY_DEFINE_UNARY_OPERATORS(invert, ~);
PY_DEFINE_UNARY_OPERATORS(int, long);
PY_DEFINE_UNARY_OPERATORS(long, PyLong_FromLong);
PY_DEFINE_UNARY_OPERATORS(float, double);
# undef PY_DEFINE_BINARY_OPERATORS
# undef PY_DEFINE_UNARY_OPERATORS
// Some operators need special treatment, e.g. because there is no corresponding
// expression in C++. These are specialized manually.
// pow(): Manual specialization needed because an error message is required if this
// function is called with three arguments. The "power modulo" operator is not
// supported by define_operator, but can be wrapped manually (see special.html).
template <>
struct define_operator<op_pow>
// Operator implementation template declarations. The nested apply
// declaration here keeps MSVC6 happy.
template <operator_id> struct operator_l
{
template <class Left, class Right = Left>
struct operator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const
{
tuple args(ref(arguments, ref::increment_count));
if (args.size() == 3 && args[2]->ob_type != Py_None->ob_type)
{
PyErr_SetString(PyExc_TypeError, "expected 2 arguments, got 3");
throw_argument_error();
}
return BOOST_PYTHON_CONVERSION::to_python(
pow(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()),
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())));
}
const char* description() const
{ return "__pow__"; }
};
template <class Right, class Left>
struct roperator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const
{
tuple args(ref(arguments, ref::increment_count));
if (args.size() == 3 && args[2]->ob_type != Py_None->ob_type)
{
PyErr_SetString(PyExc_TypeError, "bad operand type(s) for pow()");
throw_argument_error();
}
return BOOST_PYTHON_CONVERSION::to_python(
pow(BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()),
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())));
}
const char* description() const
{ return "__rpow__"; }
};
static const char * name() { return "__pow__"; }
static const char * rname() { return "__rpow__"; }
template <class L, class R> struct apply;
};
// divmod(): Manual specialization needed because we must actually call two operators and
// return a tuple containing both results
template <>
struct define_operator<op_divmod>
{
template <class Left, class Right = Left>
struct operator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const
{
tuple args(ref(arguments, ref::increment_count));
PyObject * res = PyTuple_New(2);
PyTuple_SET_ITEM(res, 0,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) /
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())));
PyTuple_SET_ITEM(res, 1,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) %
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())));
return res;
}
const char* description() const
{ return "__divmod__"; }
};
template <class Right, class Left>
struct roperator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const
{
tuple args(ref(arguments, ref::increment_count));
PyObject * res = PyTuple_New(2);
PyTuple_SET_ITEM(res, 0,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) /
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())));
PyTuple_SET_ITEM(res, 1,
BOOST_PYTHON_CONVERSION::to_python(
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) %
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())));
return res;
}
const char* description() const
{ return "__rdivmod__"; }
};
static const char * name() { return "__divmod__"; }
static const char * rname() { return "__rdivmod__"; }
};
// cmp(): Manual specialization needed because there is no three-way compare in C++.
// It is implemented by two one-way comparisons with operators reversed in the second.
template <>
struct define_operator<op_cmp>
{
template <class Left, class Right = Left>
struct operator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const
{
tuple args(ref(arguments, ref::increment_count));
return BOOST_PYTHON_CONVERSION::to_python(
(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>()) <
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>())) ?
- 1 :
(BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Right>()) <
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Left>())) ?
1 :
0) ;
}
const char* description() const
{ return "__cmp__"; }
};
template <class Right, class Left>
struct roperator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject* /* keywords */) const
{
tuple args(ref(arguments, ref::increment_count));
return BOOST_PYTHON_CONVERSION::to_python(
(BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>()) <
BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>())) ?
- 1 :
(BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<Right>()) <
BOOST_PYTHON_CONVERSION::from_python(args[1].get(), boost::python::type<Left>())) ?
1 :
0) ;
}
const char* description() const
{ return "__rcmp__"; }
};
static const char * name() { return "__cmp__"; }
static const char * rname() { return "__rcmp__"; }
};
# ifndef BOOST_PYTHON_USE_SSTREAM
class unfreezer {
public:
unfreezer(std::ostrstream& s) : m_stream(s) {}
~unfreezer() { m_stream.freeze(false); }
private:
std::ostrstream& m_stream;
};
# endif
// str(): Manual specialization needed because the string conversion does not follow
// the standard pattern relized by the macros.
template <>
struct define_operator<op_str>
template <operator_id> struct operator_r
{
template <class operand>
struct operator_function : function
{
PyObject* do_call(PyObject* arguments, PyObject*) const
{
tuple args(ref(arguments, ref::increment_count));
// When STLport is used with native streams, _STL::ostringstream().str() is not
// _STL::string, but std::string.
# ifdef BOOST_PYTHON_USE_SSTREAM
std::ostringstream s;
s << BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<operand>());
return BOOST_PYTHON_CONVERSION::to_python(s.str());
# else
std::ostrstream s;
s << BOOST_PYTHON_CONVERSION::from_python(args[0].get(), boost::python::type<operand>()) << char();
auto unfreezer unfreeze(s);
return BOOST_PYTHON_CONVERSION::to_python(const_cast<char const *>(s.str()));
# endif
}
const char* description() const
{ return "__str__"; }
};
static const char * name() { return "__str__"; }
template <class L, class R> struct apply;
};
template <operator_id> struct operator_1
{
template <class T> struct apply;
};
} // namespace detail
// 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 <operator_id id, class L, class R>
struct operator_l_inner
: operator_l<id>::template apply<L,R>
{};
template <operator_id id, class L, class R>
struct operator_r_inner
: operator_r<id>::template apply<L,R>
{};
template <operator_id id, class T>
struct operator_1_inner
: operator_1<id>::template apply<T>
{};
// 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 <operator_id id>
struct binary_op : operator_l<id>
{
template <class T>
struct apply : operator_l_inner<id,T,T>
{
};
};
// self op R
template <operator_id id, class R>
struct binary_op_l : operator_l<id>
{
template <class T>
struct apply : operator_l_inner<id,T,R>
{
};
};
// L op self
template <operator_id id, class L>
struct binary_op_r : operator_r<id>
{
template <class T>
struct apply : operator_r_inner<id,L,T>
{
};
};
template <operator_id id>
struct unary_op : operator_1<id>
{
template <class T>
struct apply : operator_1_inner<id,T>
{
};
};
// This type is what actually gets returned from operators used on
// self_t
template <operator_id id, class L = not_specified, class R = not_specified>
struct operator_
: mpl::if_<
is_same<L,self_t>
, typename mpl::if_<
is_same<R,self_t>
, binary_op<id>
, binary_op_l<id,typename unwrap_other<R>::type>
>::type
, typename mpl::if_<
is_same<L,not_specified>
, unary_op<id>
, binary_op_r<id,typename unwrap_other<L>::type>
>::type
>::type
{
};
}
# define BOOST_PYTHON_BINARY_OPERATION(id, rid, expr) \
namespace detail \
{ \
template <> \
struct operator_l<op_##id> \
{ \
template <class L, class R> \
struct apply \
{ \
static inline PyObject* execute(L const& l, R const& r) \
{ \
return detail::convert_result(expr); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
\
template <> \
struct operator_r<op_##id> \
{ \
template <class L, class R> \
struct apply \
{ \
static inline PyObject* execute(R const& r, L 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 <class L, class R> \
inline detail::operator_<detail::op_##id,L,R> \
operator##op(L const&, R const&) \
{ \
return detail::operator_<detail::op_##id,L,R>(); \
} \
}
BOOST_PYTHON_BINARY_OPERATOR(add, radd, +)
BOOST_PYTHON_BINARY_OPERATOR(sub, rsub, -)
BOOST_PYTHON_BINARY_OPERATOR(mul, rmul, *)
BOOST_PYTHON_BINARY_OPERATOR(div, rdiv, /)
BOOST_PYTHON_BINARY_OPERATOR(mod, rmod, %)
BOOST_PYTHON_BINARY_OPERATOR(lshift, rlshift, <<)
BOOST_PYTHON_BINARY_OPERATOR(rshift, rrshift, >>)
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, <)
BOOST_PYTHON_BINARY_OPERATOR(le, ge, <=)
BOOST_PYTHON_BINARY_OPERATOR(eq, eq, ==)
BOOST_PYTHON_BINARY_OPERATOR(ne, ne, !=)
# undef BOOST_PYTHON_BINARY_OPERATOR
// pow isn't an operator in C++; handle it specially.
BOOST_PYTHON_BINARY_OPERATION(pow, rpow, pow(l,r))
# undef BOOST_PYTHON_BINARY_OPERATION
namespace self_ns
{
# ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
template <class L, class R>
inline detail::operator_<detail::op_pow,L,R>
pow(L const&, R const&)
{
return detail::operator_<detail::op_pow,L,R>();
}
# else
// When there's no argument-dependent lookup, we need these
// overloads to handle the case when everything is imported into the
// global namespace. Note that the plain overload below does /not/
// take const& arguments. This is needed by MSVC6 at least, or it
// complains of ambiguities, since there's no partial ordering.
inline detail::operator_<detail::op_pow,self_t,self_t>
pow(self_t, self_t)
{
return detail::operator_<detail::op_pow,self_t,self_t>();
}
template <class R>
inline detail::operator_<detail::op_pow,self_t,R>
pow(self_t const&, R const&)
{
return detail::operator_<detail::op_pow,self_t,R>();
}
template <class L>
inline detail::operator_<detail::op_pow,L,self_t>
pow(L const&, self_t const&)
{
return detail::operator_<detail::op_pow,L,self_t>();
}
# endif
}
# define BOOST_PYTHON_INPLACE_OPERATOR(id, op) \
namespace detail \
{ \
template <> \
struct operator_l<op_##id> \
{ \
template <class L, class R> \
struct apply \
{ \
static inline PyObject* \
execute(back_reference<L&> l, R const& r) \
{ \
l.get() op r; \
return python::incref(l.source().ptr()); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
} \
namespace self_ns \
{ \
template <class R> \
inline detail::operator_<detail::op_##id,self_t,R> \
operator##op(self_t const&, R const&) \
{ \
return detail::operator_<detail::op_##id,self_t,R>(); \
} \
}
BOOST_PYTHON_INPLACE_OPERATOR(iadd,+=)
BOOST_PYTHON_INPLACE_OPERATOR(isub,-=)
BOOST_PYTHON_INPLACE_OPERATOR(imul,*=)
BOOST_PYTHON_INPLACE_OPERATOR(idiv,/=)
BOOST_PYTHON_INPLACE_OPERATOR(imod,%=)
BOOST_PYTHON_INPLACE_OPERATOR(ilshift,<<=)
BOOST_PYTHON_INPLACE_OPERATOR(irshift,>>=)
BOOST_PYTHON_INPLACE_OPERATOR(iand,&=)
BOOST_PYTHON_INPLACE_OPERATOR(ixor,^=)
BOOST_PYTHON_INPLACE_OPERATOR(ior,|=)
# define BOOST_PYTHON_UNARY_OPERATOR(id, op, func_name) \
namespace detail \
{ \
template <> \
struct operator_1<op_##id> \
{ \
template <class T> \
struct apply \
{ \
static PyObject* execute(T const& x) \
{ \
return detail::convert_result(op(x)); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
} \
namespace self_ns \
{ \
inline detail::operator_<detail::op_##id> \
func_name(self_t const&) \
{ \
return detail::operator_<detail::op_##id>(); \
} \
}
# undef BOOST_PYTHON_INPLACE_OPERATOR
BOOST_PYTHON_UNARY_OPERATOR(neg, -, operator-)
BOOST_PYTHON_UNARY_OPERATOR(pos, +, operator+)
BOOST_PYTHON_UNARY_OPERATOR(abs, abs, abs)
BOOST_PYTHON_UNARY_OPERATOR(invert, ~, operator~)
BOOST_PYTHON_UNARY_OPERATOR(int, long, int_)
BOOST_PYTHON_UNARY_OPERATOR(long, PyLong_FromLong, long_)
BOOST_PYTHON_UNARY_OPERATOR(float, double, float_)
BOOST_PYTHON_UNARY_OPERATOR(complex, std::complex<double>, complex_)
BOOST_PYTHON_UNARY_OPERATOR(str, lexical_cast<std::string>, str)
# undef BOOST_PYTHON_UNARY_OPERATOR
}} // namespace boost::python
# undef BOOST_PYTHON_USE_SSTREAM
# endif
#endif /* OPERATORS_UK112000_H_ */
# ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
using boost::python::self_ns::abs;
using boost::python::self_ns::int_;
using boost::python::self_ns::long_;
using boost::python::self_ns::float_;
using boost::python::self_ns::complex_;
using boost::python::self_ns::str;
using boost::python::self_ns::pow;
# endif
#endif // OPERATORS_DWA2002530_HPP

View File

@@ -1,340 +0,0 @@
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#ifndef OPERATORS2_DWA2002530_HPP
# define OPERATORS2_DWA2002530_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/converter/arg_to_python.hpp>
# include <boost/python/detail/operator_id.hpp>
# include <boost/python/detail/not_specified.hpp>
# include <boost/python/back_reference.hpp>
# include <boost/mpl/if.hpp>
# include <boost/python/self.hpp>
# include <boost/python/other.hpp>
# include <boost/lexical_cast.hpp>
# include <boost/python/refcount.hpp>
# include <string>
# include <complex>
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 <class T>
PyObject* convert_result(T const& x)
{
return converter::arg_to_python<T>(x).release();
}
// Operator implementation template declarations. The nested apply
// declaration here keeps MSVC6 happy.
template <operator_id> struct operator_l
{
template <class L, class R> struct apply;
};
template <operator_id> struct operator_r
{
template <class L, class R> struct apply;
};
template <operator_id> struct operator_1
{
template <class T> 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 <operator_id id, class L, class R>
struct operator_l_inner
: operator_l<id>::template apply<L,R>
{};
template <operator_id id, class L, class R>
struct operator_r_inner
: operator_r<id>::template apply<L,R>
{};
template <operator_id id, class T>
struct operator_1_inner
: operator_1<id>::template apply<T>
{};
// 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 <operator_id id>
struct binary_op : operator_l<id>
{
template <class T>
struct apply : operator_l_inner<id,T,T>
{
};
};
// self op R
template <operator_id id, class R>
struct binary_op_l : operator_l<id>
{
template <class T>
struct apply : operator_l_inner<id,T,R>
{
};
};
// L op self
template <operator_id id, class L>
struct binary_op_r : operator_r<id>
{
template <class T>
struct apply : operator_r_inner<id,L,T>
{
};
};
template <operator_id id>
struct unary_op : operator_1<id>
{
template <class T>
struct apply : operator_1_inner<id,T>
{
};
};
// This type is what actually gets returned from operators used on
// self_t
template <operator_id id, class L = not_specified, class R = not_specified>
struct operator_
: mpl::if_<
is_same<L,self_t>
, typename mpl::if_<
is_same<R,self_t>
, binary_op<id>
, binary_op_l<id,typename unwrap_other<R>::type>
>::type
, typename mpl::if_<
is_same<L,not_specified>
, unary_op<id>
, binary_op_r<id,typename unwrap_other<L>::type>
>::type
>::type
{
};
}
# define BOOST_PYTHON_BINARY_OPERATION(id, rid, expr) \
namespace detail \
{ \
template <> \
struct operator_l<op_##id> \
{ \
template <class L, class R> \
struct apply \
{ \
static inline PyObject* execute(L const& l, R const& r) \
{ \
return detail::convert_result(expr); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
\
template <> \
struct operator_r<op_##id> \
{ \
template <class L, class R> \
struct apply \
{ \
static inline PyObject* execute(R const& r, L 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 <class L, class R> \
inline detail::operator_<detail::op_##id,L,R> \
operator##op(L const&, R const&) \
{ \
return detail::operator_<detail::op_##id,L,R>(); \
} \
}
BOOST_PYTHON_BINARY_OPERATOR(add, radd, +)
BOOST_PYTHON_BINARY_OPERATOR(sub, rsub, -)
BOOST_PYTHON_BINARY_OPERATOR(mul, rmul, *)
BOOST_PYTHON_BINARY_OPERATOR(div, rdiv, /)
BOOST_PYTHON_BINARY_OPERATOR(mod, rmod, %)
BOOST_PYTHON_BINARY_OPERATOR(lshift, rlshift, <<)
BOOST_PYTHON_BINARY_OPERATOR(rshift, rrshift, >>)
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, <)
BOOST_PYTHON_BINARY_OPERATOR(le, ge, <=)
BOOST_PYTHON_BINARY_OPERATOR(eq, eq, ==)
BOOST_PYTHON_BINARY_OPERATOR(ne, ne, !=)
# undef BOOST_PYTHON_BINARY_OPERATOR
// pow isn't an operator in C++; handle it specially.
BOOST_PYTHON_BINARY_OPERATION(pow, rpow, pow(l,r))
# undef BOOST_PYTHON_BINARY_OPERATION
namespace self_ns
{
# ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
template <class L, class R>
inline detail::operator_<detail::op_pow,L,R>
pow(L const&, R const&)
{
return detail::operator_<detail::op_pow,L,R>();
}
# else
// When there's no argument-dependent lookup, we need these
// overloads to handle the case when everything is imported into the
// global namespace. Note that the plain overload below does /not/
// take const& arguments. This is needed by MSVC6 at least, or it
// complains of ambiguities, since there's no partial ordering.
inline detail::operator_<detail::op_pow,self_t,self_t>
pow(self_t, self_t)
{
return detail::operator_<detail::op_pow,self_t,self_t>();
}
template <class R>
inline detail::operator_<detail::op_pow,self_t,R>
pow(self_t const&, R const&)
{
return detail::operator_<detail::op_pow,self_t,R>();
}
template <class L>
inline detail::operator_<detail::op_pow,L,self_t>
pow(L const&, self_t const&)
{
return detail::operator_<detail::op_pow,L,self_t>();
}
# endif
}
# define BOOST_PYTHON_INPLACE_OPERATOR(id, op) \
namespace detail \
{ \
template <> \
struct operator_l<op_##id> \
{ \
template <class L, class R> \
struct apply \
{ \
static inline PyObject* \
execute(back_reference<L&> l, R const& r) \
{ \
l.get() op r; \
return python::incref(l.source().ptr()); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
} \
namespace self_ns \
{ \
template <class R> \
inline detail::operator_<detail::op_##id,self_t,R> \
operator##op(self_t const&, R const&) \
{ \
return detail::operator_<detail::op_##id,self_t,R>(); \
} \
}
BOOST_PYTHON_INPLACE_OPERATOR(iadd,+=)
BOOST_PYTHON_INPLACE_OPERATOR(isub,-=)
BOOST_PYTHON_INPLACE_OPERATOR(imul,*=)
BOOST_PYTHON_INPLACE_OPERATOR(idiv,/=)
BOOST_PYTHON_INPLACE_OPERATOR(imod,%=)
BOOST_PYTHON_INPLACE_OPERATOR(ilshift,<<=)
BOOST_PYTHON_INPLACE_OPERATOR(irshift,>>=)
BOOST_PYTHON_INPLACE_OPERATOR(iand,&=)
BOOST_PYTHON_INPLACE_OPERATOR(ixor,^=)
BOOST_PYTHON_INPLACE_OPERATOR(ior,|=)
# define BOOST_PYTHON_UNARY_OPERATOR(id, op, func_name) \
namespace detail \
{ \
template <> \
struct operator_1<op_##id> \
{ \
template <class T> \
struct apply \
{ \
static PyObject* execute(T const& x) \
{ \
return detail::convert_result(op(x)); \
} \
}; \
static char const* name() { return "__" #id "__"; } \
}; \
} \
namespace self_ns \
{ \
inline detail::operator_<detail::op_##id> \
func_name(self_t const&) \
{ \
return detail::operator_<detail::op_##id>(); \
} \
}
# undef BOOST_PYTHON_INPLACE_OPERATOR
BOOST_PYTHON_UNARY_OPERATOR(neg, -, operator-)
BOOST_PYTHON_UNARY_OPERATOR(pos, +, operator+)
BOOST_PYTHON_UNARY_OPERATOR(abs, abs, abs)
BOOST_PYTHON_UNARY_OPERATOR(invert, ~, operator~)
BOOST_PYTHON_UNARY_OPERATOR(int, long, int_)
BOOST_PYTHON_UNARY_OPERATOR(long, PyLong_FromLong, long_)
BOOST_PYTHON_UNARY_OPERATOR(float, double, float_)
BOOST_PYTHON_UNARY_OPERATOR(complex, std::complex<double>, complex_)
BOOST_PYTHON_UNARY_OPERATOR(str, lexical_cast<std::string>, str)
# undef BOOST_PYTHON_UNARY_OPERATOR
}} // namespace boost::python
# ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
using boost::python::self_ns::abs;
using boost::python::self_ns::int_;
using boost::python::self_ns::long_;
using boost::python::self_ns::float_;
using boost::python::self_ns::complex_;
using boost::python::self_ns::str;
using boost::python::self_ns::pow;
# endif
#endif // OPERATORS2_DWA2002530_HPP