mirror of
https://github.com/boostorg/python.git
synced 2026-01-23 05:42:30 +00:00
Enable automatic downcasting to registered classes for pointers, references, and smart pointers
[SVN r16673]
This commit is contained in:
22
include/boost/python/detail/decref_guard.hpp
Normal file
22
include/boost/python/detail/decref_guard.hpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// 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 DECREF_GUARD_DWA20021220_HPP
|
||||
# define DECREF_GUARD_DWA20021220_HPP
|
||||
|
||||
namespace boost { namespace python { namespace detail {
|
||||
|
||||
struct decref_guard
|
||||
{
|
||||
decref_guard(PyObject* o) : obj(o) {}
|
||||
~decref_guard() { Py_XDECREF(obj); }
|
||||
void cancel() { obj = 0; }
|
||||
private:
|
||||
PyObject* obj;
|
||||
};
|
||||
|
||||
}}} // namespace boost::python::detail
|
||||
|
||||
#endif // DECREF_GUARD_DWA20021220_HPP
|
||||
@@ -8,42 +8,34 @@
|
||||
|
||||
# include <boost/python/object/instance.hpp>
|
||||
# include <boost/python/converter/registered.hpp>
|
||||
# include <boost/python/detail/decref_guard.hpp>
|
||||
|
||||
namespace boost { namespace python { namespace objects {
|
||||
|
||||
struct decref_guard
|
||||
{
|
||||
decref_guard(PyObject* o) : obj(o) {}
|
||||
~decref_guard() { Py_XDECREF(obj); }
|
||||
void cancel() { obj = 0; }
|
||||
private:
|
||||
PyObject* obj;
|
||||
};
|
||||
|
||||
template <class T, class Holder>
|
||||
struct make_instance
|
||||
template <class T, class Holder, class Derived>
|
||||
struct make_instance_impl
|
||||
{
|
||||
typedef objects::instance<Holder> instance_t;
|
||||
|
||||
template <class Arg>
|
||||
static PyObject* execute(Arg& x)
|
||||
static inline PyObject* execute(Arg& x)
|
||||
{
|
||||
BOOST_STATIC_ASSERT(is_class<T>::value);
|
||||
|
||||
PyTypeObject* type = converter::registered<T>::converters.get_class_object();
|
||||
|
||||
PyTypeObject* type = Derived::get_class_object(x);
|
||||
|
||||
PyObject* raw_result = type->tp_alloc(
|
||||
type, objects::additional_instance_size<Holder>::value);
|
||||
|
||||
if (raw_result != 0)
|
||||
{
|
||||
decref_guard protect(raw_result);
|
||||
python::detail::decref_guard protect(raw_result);
|
||||
|
||||
instance_t* instance = (instance_t*)raw_result;
|
||||
|
||||
// construct the new C++ object and install the pointer
|
||||
// in the Python object.
|
||||
construct(instance, x)->install(raw_result);
|
||||
Derived::construct(&instance->storage, (PyObject*)instance, x)->install(raw_result);
|
||||
|
||||
// Note the position of the internally-stored Holder,
|
||||
// for the sake of destruction
|
||||
@@ -54,23 +46,22 @@ struct make_instance
|
||||
}
|
||||
return raw_result;
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
// Kind of a hack to support code re-use. The first form is used
|
||||
// to construct holders around pointers or smart pointers. The
|
||||
// second form is used to construct holders around by-value
|
||||
// returns. We have to pass a pointer to the owning Python object
|
||||
// to the second form in order to make it forward the 2nd argument
|
||||
// on to the constructor of its embedded T object.
|
||||
template <class Arg>
|
||||
static Holder* construct(instance_t* result, Arg& x)
|
||||
{
|
||||
return new ((void*)&result->storage) Holder(x);
|
||||
}
|
||||
|
||||
static Holder* construct(instance_t* result, reference_wrapper<T const> x)
|
||||
template <class T, class Holder>
|
||||
struct make_instance
|
||||
: make_instance_impl<T, Holder, make_instance<T,Holder> >
|
||||
{
|
||||
template <class U>
|
||||
static inline PyTypeObject* get_class_object(U&)
|
||||
{
|
||||
return new ((void*)&result->storage) Holder((PyObject*)result, x);
|
||||
return converter::registered<T>::converters.get_class_object();
|
||||
}
|
||||
|
||||
static inline Holder* construct(void* storage, PyObject* instance, reference_wrapper<T const> x)
|
||||
{
|
||||
return new (storage) Holder(instance, x);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
60
include/boost/python/object/make_ptr_instance.hpp
Normal file
60
include/boost/python/object/make_ptr_instance.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// 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 MAKE_PTR_INSTANCE_DWA200296_HPP
|
||||
# define MAKE_PTR_INSTANCE_DWA200296_HPP
|
||||
|
||||
# include <boost/python/object/make_instance.hpp>
|
||||
# include <boost/python/converter/registry.hpp>
|
||||
# include <boost/type_traits/is_polymorphic.hpp>
|
||||
# include <boost/get_pointer.hpp>
|
||||
# include <typeinfo>
|
||||
|
||||
namespace boost { namespace python { namespace objects {
|
||||
|
||||
template <class T, class Holder>
|
||||
struct make_ptr_instance
|
||||
: make_instance_impl<T, Holder, make_ptr_instance<T,Holder> >
|
||||
{
|
||||
template <class Arg>
|
||||
static inline Holder* construct(void* storage, PyObject*, Arg& x)
|
||||
{
|
||||
return new (storage) Holder(x);
|
||||
}
|
||||
|
||||
template <class Ptr>
|
||||
static inline PyTypeObject* get_class_object(Ptr const& x)
|
||||
{
|
||||
return get_class_object_impl(get_pointer(x));
|
||||
}
|
||||
|
||||
private:
|
||||
template <class U>
|
||||
static inline PyTypeObject* get_class_object_impl(U const volatile* p)
|
||||
{
|
||||
PyTypeObject* derived = get_derived_class_object(is_polymorphic<U>::type(), p);
|
||||
if (derived)
|
||||
return derived;
|
||||
return converter::registered<T>::converters.get_class_object();
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static inline PyTypeObject* get_derived_class_object(mpl::true_c, U const volatile* x)
|
||||
{
|
||||
converter::registration const* r = converter::registry::query(type_info(typeid(*x)));
|
||||
return r ? r->m_class_object : 0;
|
||||
}
|
||||
|
||||
template <class U>
|
||||
static inline PyTypeObject* get_derived_class_object(mpl::false_c, U*)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}}} // namespace boost::python::object
|
||||
|
||||
#endif // MAKE_PTR_INSTANCE_DWA200296_HPP
|
||||
@@ -12,7 +12,7 @@
|
||||
# include <boost/python/object/value_holder.hpp>
|
||||
# include <boost/python/object/pointer_holder.hpp>
|
||||
# include <boost/python/object/class_wrapper.hpp>
|
||||
# include <boost/python/object/make_instance.hpp>
|
||||
# include <boost/python/object/make_ptr_instance.hpp>
|
||||
# include <boost/python/object/instance.hpp>
|
||||
# include <boost/python/detail/force_instantiate.hpp>
|
||||
# include <boost/type.hpp>
|
||||
@@ -142,7 +142,7 @@ namespace detail
|
||||
static inline void register_(mpl::false_c)
|
||||
{
|
||||
python::detail::force_instantiate(
|
||||
objects::class_value_wrapper<Ptr, make_instance<T,type> >());
|
||||
objects::class_value_wrapper<Ptr, make_ptr_instance<T,type> >());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
# include <boost/python/detail/unwind_type.hpp>
|
||||
# include <boost/python/detail/none.hpp>
|
||||
# include <boost/shared_ptr.hpp>
|
||||
# include <boost/python/object/make_instance.hpp>
|
||||
# include <boost/python/object/make_ptr_instance.hpp>
|
||||
# include <memory>
|
||||
|
||||
namespace boost { namespace python {
|
||||
@@ -48,7 +48,7 @@ namespace detail
|
||||
typedef objects::pointer_holder<smart_pointer, T> holder_t;
|
||||
|
||||
smart_pointer ptr(p);
|
||||
return objects::make_instance<T, holder_t>::execute(ptr);
|
||||
return objects::make_ptr_instance<T, holder_t>::execute(ptr);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace detail
|
||||
static result_type execute(T* p)
|
||||
{
|
||||
typedef objects::pointer_holder<T*, T> holder_t;
|
||||
return objects::make_instance<T, holder_t>::execute(p);
|
||||
return objects::make_ptr_instance<T, holder_t>::execute(p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user