2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-27 19:12:16 +00:00

Use call policies

[SVN r12618]
This commit is contained in:
Dave Abrahams
2002-02-01 04:36:46 +00:00
parent 6a75fa83b5
commit b3117c2b02
19 changed files with 1380 additions and 351 deletions

View File

@@ -0,0 +1,70 @@
// 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 SMART_PTR_DWA2002123_HPP
# define SMART_PTR_DWA2002123_HPP
# include <boost/python/converter/class.hpp>
# include <boost/python/object/pointer_holder.hpp>
namespace boost { namespace python { namespace converter {
template <class Pointer, class Value>
class smart_ptr_wrapper
: wrapper<Pointer const&>
{
smart_ptr_wrapper(ref const& type_)
: m_class_object(type_)
{
assert(type_->ob_type == (PyTypeObject*)class_metatype().get());
}
PyObject* convert(Pointer x) const;
private:
ref m_class_object;
smart_ptr_converters();
}
//
// implementations
//
template <class Pointer, class Value>
PyObject* smart_ptr_wrapper<Pointer,Value>::convert(Pointer x) const
{
if (x.operator->() == 0)
return detail::none();
// Don't call the type to do the construction, since that
// would require the registration of an __init__ copy
// constructor. Instead, just construct the object in place.
PyObject* raw_result = (PyObject*)PyObject_New(
instance, (PyTypeObject*)m_class_object.get());
if (raw_result == 0)
return 0;
// Everything's OK; Bypass NULL checks but guard against
// exceptions.
ref result(raw_result, ref::allow_null());
// Build a value_holder to contain the object using the copy
// constructor
objects::pointer_holder<Pointer,Value>*
p = new objects::pointer_holder<Pointer,Value>(x);
// Install it in the instance
p->install(raw_result);
// Return the new result
return result.release();
}
}}} // namespace boost::python::converter
#endif // SMART_PTR_DWA2002123_HPP

View File

@@ -101,7 +101,7 @@ template <class T>
inline bool
to_python_lookup<T>::convertible() const
{
return m_converter != 0;
return m_convert != 0;
}
template <class T>

View File

@@ -6,10 +6,8 @@
#ifndef TYPE_ID_DWA20011127_HPP
# define TYPE_ID_DWA20011127_HPP
# include <boost/python/detail/config.hpp>
# include <boost/python/detail/config.hpp>
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/type_traits/cv_traits.hpp>
# include <boost/type_traits/composite_traits.hpp>
# include <boost/operators.hpp>
# include <typeinfo>
# include <iosfwd>
@@ -79,80 +77,6 @@ struct type_id_t : totally_ordered<type_id_t>
base_id_t m_base_type;
};
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T>
struct is_reference_to_const
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_reference_to_const<T const&>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template <class T>
struct is_reference_to_volatile
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_reference_to_volatile<T volatile&>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
# else
template <typename V>
struct is_const_help
{
typedef typename mpl::select_type<
is_const<V>::value
, type_traits::yes_type
, type_traits::no_type
>::type type;
};
template <typename V>
struct is_volatile_help
{
typedef typename mpl::select_type<
is_volatile<V>::value
, type_traits::yes_type
, type_traits::no_type
>::type type;
};
template <typename V>
typename is_const_help<V>::type reference_to_const_helper(V&);
type_traits::no_type
reference_to_const_helper(...);
template <class T>
struct is_reference_to_const
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(reference_to_const_helper(t)) == sizeof(type_traits::yes_type));
};
template <typename V>
typename is_volatile_help<V>::type reference_to_volatile_helper(V&);
type_traits::no_type reference_to_volatile_helper(...);
template <class T>
struct is_reference_to_volatile
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(reference_to_volatile_helper(t)) == sizeof(type_traits::yes_type));
};
# endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T>
inline undecorated_type_id_t undecorated_type_id(detail::dummy<T>* = 0)
{
@@ -165,9 +89,9 @@ inline type_id_t type_id(detail::dummy<T>* = 0)
return type_id_t(
undecorated_type_id<T>()
, type_id_t::decoration(
(is_const<T>::value || is_reference_to_const<T>::value
(is_const<T>::value || python::detail::is_reference_to_const<T>::value
? type_id_t::const_ : 0)
| (is_volatile<T>::value || is_reference_to_volatile<T>::value
| (is_volatile<T>::value || python::detail::is_reference_to_volatile<T>::value
? type_id_t::volatile_ : 0)
| (is_reference<T>::value ? type_id_t::reference : 0)
)

View File

@@ -0,0 +1,42 @@
// 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 COPY_CONST_REFERENCE_DWA2002131_HPP
# define COPY_CONST_REFERENCE_DWA2002131_HPP
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/to_python.hpp>
namespace boost { namespace python {
namespace detail
{
template <class R>
struct copy_const_reference_expects_a_const_reference_return_type
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
}
template <class T> struct to_python;
struct copy_const_reference
{
template <class T>
struct apply
{
typedef typename mpl::select_type<
detail::is_reference_to_const<T>::value
, to_python<T>
, detail::copy_const_reference_expects_a_const_reference_return_type<T>
>::type type;
};
};
}} // namespace boost::python
#endif // COPY_CONST_REFERENCE_DWA2002131_HPP

View File

@@ -0,0 +1,42 @@
// 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 COPY_NON_CONST_REFERENCE_DWA2002131_HPP
# define COPY_NON_CONST_REFERENCE_DWA2002131_HPP
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/to_python.hpp>
namespace boost { namespace python {
namespace detail
{
template <class R>
struct copy_non_const_reference_expects_a_non_const_reference_return_type
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
}
template <class T> struct to_python;
struct copy_non_const_reference
{
template <class T>
struct apply
{
typedef typename mpl::select_type<
boost::python::detail::is_reference_to_non_const<T>::value
, to_python<T>
, detail::copy_non_const_reference_expects_a_non_const_reference_return_type<T>
>::type type;
};
};
}} // namespace boost::python
#endif // COPY_NON_CONST_REFERENCE_DWA2002131_HPP

View File

@@ -0,0 +1,72 @@
// 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 DEFAULT_CALL_POLICIES_DWA2002131_HPP
# define DEFAULT_CALL_POLICIES_DWA2002131_HPP
# include <boost/python/detail/wrap_python.hpp>
# include <boost/mpl/select_type.hpp>
namespace boost { namespace python {
template <class T> struct to_python;
namespace detail
{
// for "readable" error messages
template <class T> struct specify_a_result_policy_to_wrap_functions_returning
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
}
struct default_result_converter;
struct default_call_policies
{
// Nothing to do
static bool precall(PyObject*)
{
return true;
}
// Pass the result through
static PyObject* postcall(PyObject*, PyObject* result)
{
return result;
}
typedef default_result_converter result_converter;
};
struct default_result_converter
{
template <class R>
struct apply
{
typedef typename mpl::select_type<
is_reference<R>::value | is_pointer<R>::value
, detail::specify_a_result_policy_to_wrap_functions_returning<R>
, to_python<R>
>::type type;
};
};
// Exceptions for c strings an PyObject*s
template <>
struct default_result_converter::apply<char const*>
{
typedef boost::python::to_python<char const*> type;
};
template <>
struct default_result_converter::apply<PyObject*>
{
typedef boost::python::to_python<PyObject*> type;
};
}} // namespace boost::python
#endif // DEFAULT_CALL_POLICIES_DWA2002131_HPP

View File

@@ -6,19 +6,229 @@
#ifndef CALLER_DWA20011214_HPP
# define CALLER_DWA20011214_HPP
# include <boost/python/call.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/detail/eval.hpp>
# include <boost/python/detail/returning.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/type_traits/composite_traits.hpp>
# include <boost/type_traits/same_traits.hpp>
namespace boost { namespace python { namespace detail {
namespace boost { namespace python
{
template <class T> struct to_python;
}}
namespace boost { namespace python { namespace detail {
// for "readable" error messages
template <class T> struct must_use_a_result_handler_to_wrap_functions_returning
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
struct to_python_generator
{
template <class R>
struct apply
{
typedef typename mpl::select_type<
is_reference<R>::value | is_pointer<R>::value
, must_use_a_result_handler_to_wrap_functions_returning<R>
, to_python<R>
>::type type;
};
};
struct caller
{
typedef PyObject* result_type;
template <class F>
PyObject* operator()(F f, PyObject* args, PyObject* keywords)
template <class P, class R>
PyObject* operator()(R (*f)(), PyObject* args, PyObject* keywords, P const& policies) const
{
return call(f, args, keywords);
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0>
PyObject* operator()(R (*f)(A0), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1>
PyObject* operator()(R (*f)(A0, A1), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2>
PyObject* operator()(R (*f)(A0, A1, A2), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3>
PyObject* operator()(R (*f)(A0, A1, A2, A3), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4>
PyObject* operator()(R (*f)(A0, A1, A2, A3, A4), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4, class A5>
PyObject* operator()(R (*f)(A0, A1, A2, A3, A4, A5), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
// Member functions
template <class P, class R, class A0>
PyObject* operator()(R (A0::*f)(), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1>
PyObject* operator()(R (A0::*f)(A1), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2>
PyObject* operator()(R (A0::*f)(A1, A2), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3>
PyObject* operator()(R (A0::*f)(A1, A2, A3), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4, class A5>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4, A5), PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0>
PyObject* operator()(R (A0::*f)() const, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1>
PyObject* operator()(R (A0::*f)(A1) const, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2>
PyObject* operator()(R (A0::*f)(A1, A2) const, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3>
PyObject* operator()(R (A0::*f)(A1, A2, A3) const, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4) const, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4, class A5>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4, A5) const, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0>
PyObject* operator()(R (A0::*f)() volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1>
PyObject* operator()(R (A0::*f)(A1) volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2>
PyObject* operator()(R (A0::*f)(A1, A2) volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3>
PyObject* operator()(R (A0::*f)(A1, A2, A3) volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4) volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4, class A5>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4, A5) volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0>
PyObject* operator()(R (A0::*f)() const volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1>
PyObject* operator()(R (A0::*f)(A1) const volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2>
PyObject* operator()(R (A0::*f)(A1, A2) const volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3>
PyObject* operator()(R (A0::*f)(A1, A2, A3) const volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4) const volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
template <class P, class R, class A0, class A1, class A2, class A3, class A4, class A5>
PyObject* operator()(R (A0::*f)(A1, A2, A3, A4, A5) const volatile, PyObject* args, PyObject* keywords, P const& policies) const
{
return returning<R>::call(f, args, keywords, policies);
}
};

View File

@@ -0,0 +1,116 @@
// 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 INDIRECT_TRAITS_DWA2002131_HPP
# define INDIRECT_TRAITS_DWA2002131_HPP
# include <boost/type_traits/cv_traits.hpp>
# include <boost/type_traits/composite_traits.hpp>
# include <boost/mpl/select_type.hpp>
namespace boost { namespace python { namespace detail {
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T>
struct is_reference_to_const
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_reference_to_const<T const&>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template <class T>
struct is_reference_to_non_const
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_reference_to_non_const<T&>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
template <class T>
struct is_reference_to_volatile
{
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_reference_to_volatile<T volatile&>
{
BOOST_STATIC_CONSTANT(bool, value = true);
};
# else
typedef char (&inner_yes_type)[3];
typedef char (&inner_no_type)[2];
typedef char (&outer_no_type)[1];
template <typename V>
struct is_const_help
{
typedef typename mpl::select_type<
is_const<V>::value
, inner_yes_type
, inner_no_type
>::type type;
};
template <typename V>
struct is_volatile_help
{
typedef typename mpl::select_type<
is_volatile<V>::value
, inner_yes_type
, inner_no_type
>::type type;
};
template <typename V>
typename is_const_help<V>::type reference_to_const_helper(V&);
outer_no_type
reference_to_const_helper(...);
template <class T>
struct is_reference_to_const
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(reference_to_const_helper(t)) == sizeof(inner_yes_type));
};
template <class T>
struct is_reference_to_non_const
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(reference_to_const_helper(t)) == sizeof(inner_no_type));
};
template <typename V>
typename is_volatile_help<V>::type reference_to_volatile_helper(V&);
outer_no_type reference_to_volatile_helper(...);
template <class T>
struct is_reference_to_volatile
{
static T t;
BOOST_STATIC_CONSTANT(
bool, value
= sizeof(reference_to_volatile_helper(t)) == sizeof(inner_yes_type));
};
# endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
}}} // namespace boost::python::detail
#endif // INDIRECT_TRAITS_DWA2002131_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,42 @@
// 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 FROM_PYTHON_DWA2002128_HPP
# define FROM_PYTHON_DWA2002128_HPP
# include <boost/python/converter/from_python.hpp>
# include <boost/python/converter/target.hpp>
namespace boost { namespace python {
template <class T>
struct from_python
: converter::from_python_lookup<typename converter::target<T>::type>
{
typedef converter::from_python_lookup<typename converter::target<T>::type> base;
from_python(PyObject*);
};
// specialization for PyObject*
template <>
struct from_python<PyObject*>
{
from_python(PyObject*) {}
bool convertible() const { return true; }
PyObject* operator()(PyObject* source) const { return source; }
};
//
// implementations
//
template <class T>
from_python<T>::from_python(PyObject* source)
: base(source)
{
}
}} // namespace boost::python
#endif // FROM_PYTHON_DWA2002128_HPP

View File

@@ -15,6 +15,7 @@
# include <boost/mpl/size.hpp>
# include <boost/function.hpp>
# include <boost/bind.hpp>
# include <boost/python/default_call_policies.hpp>
namespace boost { namespace python {
@@ -24,7 +25,17 @@ objects::function* make_function(F f)
converter::acquire_registrations(detail::signature(f));
return new objects::function(
objects::py_function(
::boost::bind<PyObject*>(detail::caller(), f, _1, _2))
::boost::bind<PyObject*>(detail::caller(), f, _1, _2, default_call_policies()))
, detail::arg_tuple_size<F>::value);
}
template <class F, class Policies>
objects::function* make_function(F f, Policies const& policies)
{
converter::acquire_registrations(detail::signature(f));
return new objects::function(
objects::py_function(
::boost::bind<PyObject*>(detail::caller(), f, _1, _2, policies))
, detail::arg_tuple_size<F>::value);
}
@@ -41,7 +52,7 @@ objects::function* make_constructor(T* = 0, ArgList* = 0, Generator* = 0)
::boost::bind<PyObject*>(detail::caller(),
objects::make_holder<nargs>
::template apply<T,Generator,ArgList>::execute
, _1, _2))
, _1, _2, default_call_policies()))
, nargs + 1);
}

View File

@@ -58,20 +58,20 @@ class module : public module_base
return *this;
}
# if 0
template <class Fn>
void def_raw(char const* name, Fn fn)
{
add(detail::new_raw_arguments_function(fn), name);
}
# endif
template <class Fn>
module& def(char const* name, Fn fn)
{
this->setattr(name, boost::python::make_function(fn));
return *this;
}
template <class Fn, class ResultHandler>
module& def(char const* name, Fn fn, ResultHandler handler)
{
this->setattr(name, boost::python::make_function(fn, handler));
return *this;
}
};
//

View File

@@ -0,0 +1,45 @@
// 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 RETURN_INTERNAL_REFERENCE_DWA2002131_HPP
# define RETURN_INTERNAL_REFERENCE_DWA2002131_HPP
# include <boost/python/default_call_policies.hpp>
# include <boost/type_traits/object_traits.hpp>
namespace boost { namespace python {
namespace detail
{
template <class T>
struct return_internal_reference_requires_a_pointer_or_reference_return_type
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
};
struct internal_reference_to_python_generator
{
template <class T>
struct apply
{
typedef typename mpl::select_type<
!is_object<T>::value
, internal_reference_to_python<T>
, detail::return_internal_reference_requires_a_pointer_or_reference_return_type
>::type type;
};
};
template <std::size_t owner_arg, class Base = default_call_policies>
struct return_internal_reference : Base
{
typedef wrap_internal_reference<owner_arg> result_converter;
};
}} // namespace boost::python
#endif // RETURN_INTERNAL_REFERENCE_DWA2002131_HPP

View File

@@ -0,0 +1,20 @@
// 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 RETURN_VALUE_POLICY_DWA2002131_HPP
# define RETURN_VALUE_POLICY_DWA2002131_HPP
# include <boost/python/default_call_policies.hpp>
namespace boost { namespace python {
template <class Handler, class Base = default_call_policies>
struct return_value_policy : Base
{
typedef Handler result_converter;
};
}} // namespace boost::python
#endif // RETURN_VALUE_POLICY_DWA2002131_HPP

View File

@@ -0,0 +1,37 @@
// 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 TO_PYTHON_DWA2002128_HPP
# define TO_PYTHON_DWA2002128_HPP
# include <boost/python/converter/to_python.hpp>
# include <boost/python/converter/source.hpp>
namespace boost { namespace python {
template <class T>
struct to_python
: converter::to_python_lookup<typename converter::source<T>::type>
{
};
// specialization for PyObject*
template <>
struct to_python<PyObject*>
{
bool convertible() const { return true; }
PyObject* operator()(PyObject* source) const { return source; }
};
template <>
struct to_python<PyObject*const&>
{
bool convertible() const { return true; }
PyObject* operator()(PyObject* source) const { return source; }
};
}} // namespace boost::python
#endif // TO_PYTHON_DWA2002128_HPP

View File

@@ -0,0 +1,23 @@
// 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 TYPE_FROM_PYTHON_DWA2002130_HPP
# define TYPE_FROM_PYTHON_DWA2002130_HPP
namespace boost { namespace python {
template <PyTypeObject const* python_type>
struct type_from_python
{
static void* convertible(PyObject* op)
{
return PyObject_TypeCheck(
op, const_cast<PyTypeObject*>(python_type)) ? op : 0;
}
};
}} // namespace boost::python
#endif // TYPE_FROM_PYTHON_DWA2002130_HPP

View File

@@ -8,7 +8,9 @@
// TODO: Move inline implementations from objects.cpp here
#define BOOST_PYTHON_SOURCE
#ifndef BOOST_PYTHON_SOURCE
r# define BOOST_PYTHON_SOURCE
#endif
#include <boost/python/objects.hpp>
#include <boost/python/detail/none.hpp>

View File

@@ -12,7 +12,8 @@
#include <boost/python/object/value_holder.hpp>
#include <boost/python/object/pointer_holder.hpp>
#include <boost/python/object/class.hpp>
//#include <boost/python/object/inheritance.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/converter/class.hpp>
#include <boost/python/reference_from_python.hpp>
#include <boost/python/value_from_python.hpp>
@@ -198,6 +199,9 @@ BOOST_PYTHON_MODULE_INIT(m1)
using boost::python::value_from_python;
using boost::python::type_from_python;
using boost::python::get_member;
using boost::python::copy_const_reference;
using boost::python::return_value_policy;
using boost::mpl::type_list;
// Create the converters; they are self-registering/unregistering.
static to_python_converter<simple const&> c1(simple_to_python);
@@ -239,7 +243,8 @@ BOOST_PYTHON_MODULE_INIT(m1)
.def("f", f)
// Expose g()
.def("g", g)
.def("g", g , return_value_policy<copy_const_reference>()
)
.def("take_a", take_a)
.def("take_b", take_b)
@@ -279,8 +284,8 @@ BOOST_PYTHON_MODULE_INIT(m1)
.add(
class_<complicated>("complicated")
.def_init(args<simple const&,int>())
.def_init(args<simple const&>())
.def_init(type_list<simple const&,int>())
.def_init(type_list<simple const&>())
.def("get_n", &complicated::get_n)
)
;

View File

@@ -8,6 +8,9 @@
// by exposing raw Python extension functions that use wrap<> and
// unwrap<> objects.
#include <boost/python/module.hpp>
#include <boost/python/copy_non_const_reference.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/return_value_policy.hpp>
#include "simple_type.hpp"
// Get a simple (by value) from the argument, and return the
@@ -59,6 +62,10 @@ struct rewrap
BOOST_PYTHON_MODULE_INIT(m2)
{
using boost::python::return_value_policy;
using boost::python::copy_const_reference;
using boost::python::copy_non_const_reference;
boost::python::module("m2")
.def("unwrap_int", unwrap_int)
.def("unwrap_int_ref", unwrap_int_ref)
@@ -68,11 +75,24 @@ BOOST_PYTHON_MODULE_INIT(m2)
.def("unwrap_simple_const_ref", unwrap_simple_const_ref)
.def("wrap_int", &rewrap<int>::f)
.def("wrap_int_ref", &rewrap<int&>::f)
.def("wrap_int_const_ref", &rewrap<int const&>::f)
.def("wrap_int_ref", &rewrap<int&>::f
, return_value_policy<copy_non_const_reference>()
)
.def("wrap_int_const_ref", &rewrap<int const&>::f
, return_value_policy<copy_const_reference>()
)
.def("wrap_simple", &rewrap<simple>::f)
.def("wrap_simple_ref", &rewrap<simple&>::f)
.def("wrap_simple_const_ref", &rewrap<simple const&>::f)
.def("wrap_simple_ref", &rewrap<simple&>::f
, return_value_policy<copy_non_const_reference>()
)
.def("wrap_simple_const_ref", &rewrap<simple const&>::f
, return_value_policy<copy_const_reference>()
)
;
}