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

Initial pointer adoption tests

Have instances actually dispose of their held C++ objects!


[SVN r12653]
This commit is contained in:
Dave Abrahams
2002-02-02 20:54:06 +00:00
parent 8cc9080d36
commit 90647f30f8
10 changed files with 300 additions and 10 deletions

View File

@@ -6,6 +6,7 @@
#ifndef CLASS_DWA200216_HPP
# define CLASS_DWA200216_HPP
# include <boost/python/class_fwd.hpp>
# include <boost/python/reference.hpp>
# include <boost/python/object/class.hpp>
# include <boost/python/converter/type_id.hpp>
@@ -14,7 +15,7 @@
# include <boost/python/object/class_converters.hpp>
# include <boost/mpl/size.hpp>
# include <boost/mpl/for_each.hpp>
# include <boost/mpl/type_list.hpp>
# include <boost/python/detail/type_list.hpp>
namespace // put some convenience classes into the unnamed namespace for the user
{
@@ -80,8 +81,8 @@ namespace detail
//
template <
class T // class being wrapped
, class Bases = mpl::type_list<>::type
, class HolderGenerator = objects::value_holder_generator
, class Bases
, class HolderGenerator
>
class class_ : private objects::class_base
{

View File

@@ -0,0 +1,30 @@
// 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 CLASS_FWD_DWA200222_HPP
# define CLASS_FWD_DWA200222_HPP
namespace boost { namespace python {
namespace detail
{
struct empty_list;
}
namespace objects
{
struct value_holder_generator;
}
template <
class T // class being wrapped
, class Bases = detail::empty_list
, class HolderGenerator = objects::value_holder_generator
>
class class_;
}} // namespace boost::python
#endif // CLASS_FWD_DWA200222_HPP

View File

@@ -0,0 +1,34 @@
// 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 CV_CATEGORY_DWA200222_HPP
# define CV_CATEGORY_DWA200222_HPP
# include <boost/type_traits/cv_traits.hpp>
namespace boost { namespace python { namespace detail {
template <bool is_const_, bool is_volatile_>
struct cv_tag
{
BOOST_STATIC_CONSTANT(bool, is_const = is_const_);
BOOST_STATIC_CONSTANT(bool, is_volatile = is_const_);
};
typedef cv_tag<false,false> cv_unqualified;
typedef cv_tag<true,false> const_;
typedef cv_tag<false,true> volatile_;
typedef cv_tag<true,true> const_volatile_;
template <class T>
struct cv_category
{
BOOST_STATIC_CONSTANT(bool, c = is_const<T>::value);
BOOST_STATIC_CONSTANT(bool, v = is_volatile<T>::value);
typedef cv_tag<c,v> type;
};
}}} // namespace boost::python::detail
#endif // CV_CATEGORY_DWA200222_HPP

View File

@@ -0,0 +1,16 @@
// 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_LIST_DWA200222_HPP
# define TYPE_LIST_DWA200222_HPP
# include <boost/mpl/type_list.hpp>
namespace boost { namespace python { namespace detail {
struct empty_list : boost::mpl::type_list<>::type {};
}}} // namespace boost::python::detail
#endif // TYPE_LIST_DWA200222_HPP

View File

@@ -0,0 +1,51 @@
// 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 UNWIND_TYPE_DWA200222_HPP
# define UNWIND_TYPE_DWA200222_HPP
# include <boost/python/detail/cv_category.hpp>
namespace boost { namespace python { namespace detail {
template <class Generator, class U>
inline typename Generator::result_type
unwind_type_cv(U* p, cv_unqualified, Generator* = 0)
{
return Generator::execute(p);
}
template <class Generator, class U>
inline typename Generator::result_type
unwind_type_cv(U const* p, const_, Generator* = 0)
{
return unwind_type(const_cast<U*>(p), (Generator*)0);
}
template <class Generator, class U>
inline typename Generator::result_type
unwind_type_cv(U volatile* p, volatile_, Generator* = 0)
{
return unwind_type(const_cast<U*>(p), (Generator*)0);
}
template <class Generator, class U>
inline typename Generator::result_type
unwind_type_cv(U const volatile* p, const_volatile_, Generator* = 0)
{
return unwind_type(const_cast<U*>(p), (Generator*)0);
}
template <class Generator, class U>
inline typename Generator::result_type
unwind_type(U* p, Generator* = 0)
{
typedef typename cv_category<U>::type tag;
return unwind_type_cv<Generator>(p, tag());
}
}}} // namespace boost::python::detail
#endif // UNWIND_TYPE_DWA200222_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 MANAGE_NEW_OBJECT_DWA200222_HPP
# define MANAGE_NEW_OBJECT_DWA200222_HPP
# include <boost/python/detail/indirect_traits.hpp>
# include <boost/mpl/select_type.hpp>
# include <boost/python/to_python_owner.hpp>
# include <boost/type_traits/composite_traits.hpp>
namespace boost { namespace python {
namespace detail
{
template <class R>
struct manage_new_object_requires_a_pointer_return_type
# if defined(__GNUC__) && __GNUC__ >= 3 || defined(__EDG__)
{}
# endif
;
}
template <class T> struct to_python_value;
struct manage_new_object
{
template <class T>
struct apply
{
typedef typename mpl::select_type<
boost::is_pointer<T>::value
, to_python_owner<T>
, detail::manage_new_object_requires_a_pointer_return_type<T>
>::type type;
};
};
}} // namespace boost::python
#endif // MANAGE_NEW_OBJECT_DWA200222_HPP

View File

@@ -12,11 +12,10 @@
# include <boost/python/make_function.hpp>
# include <boost/python/objects.hpp>
# include <boost/python/detail/wrap_python.hpp>
# include <boost/python/class_fwd.hpp>
namespace boost { namespace python {
template <class T, class Bases, class HolderGenerator> class class_;
class BOOST_PYTHON_DECL module_base
{
public:

View File

@@ -0,0 +1,26 @@
// 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 CLASS_OBJECT_DWA200222_HPP
# define CLASS_OBJECT_DWA200222_HPP
# include <boost/python/converter/type_id.hpp>
# include <boost/python/converter/registry.hpp>
namespace boost { namespace python { namespace objects {
template <class T>
struct class_object
{
static PyTypeObject*& reference;
};
template <class T>
PyTypeObject*& class_object<T>::reference = converter::registry::class_object(
converter::undecorated_type_id<T>());
}}} // namespace boost::python::objects
#endif // CLASS_OBJECT_DWA200222_HPP

View File

@@ -25,9 +25,9 @@ struct class_wrapper
static PyObject* convert(T const& x)
{
// 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.
// Don't call the type directly to do the construction, since
// that would require the registration of an appropriate
// __init__ function.
PyObject* raw_result = m_class_object->tp_alloc(m_class_object, 0);
if (raw_result == 0)
@@ -37,8 +37,6 @@ struct class_wrapper
// exceptions.
ref result(raw_result, ref::allow_null());
((instance*)raw_result)->objects = 0;
// Build a value_holder to contain the object using the copy
// constructor
value_holder<T>* p = new value_holder<T>(raw_result, cref(x));

View File

@@ -0,0 +1,93 @@
// 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_OWNER_DWA200221_HPP
# define TO_PYTHON_OWNER_DWA200221_HPP
# include <boost/type_traits/object_traits.hpp>
# include <boost/python/object/pointer_holder.hpp>
# include <boost/python/object/class_object.hpp>
# include <boost/python/detail/unwind_type.hpp>
# include <memory>
namespace boost { namespace python {
template <class T>
struct to_python_owner
{
static bool convertible();
PyObject* operator()(T ptr) const;
private:
static PyTypeObject* type();
};
//
// implementations
//
namespace detail
{
struct make_owning_holder
{
typedef objects::instance_holder* result_type;
template <class T>
static result_type execute(T* p)
{
return new objects::pointer_holder<std::auto_ptr<T>, T>(
std::auto_ptr<T>(p));
}
};
struct get_pointer_class
{
typedef PyTypeObject* result_type;
template <class T>
static result_type execute(T* p)
{
BOOST_STATIC_ASSERT(is_class<T>::value);
return python::objects::class_object<T>::reference;
}
};
}
template <class T>
inline bool to_python_owner<T>::convertible()
{
BOOST_STATIC_ASSERT(is_pointer<T>::value);
return type() != 0;
}
template <class T>
inline PyObject* to_python_owner<T>::operator()(T x) const
{
PyObject* raw_result = type()->tp_alloc(type(), 0);
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::instance_holder* p =
detail::unwind_type<detail::make_owning_holder>(x);
// Install it in the instance
p->install(raw_result);
// Return the new result
return result.release();
}
template <class T>
inline PyTypeObject* to_python_owner<T>::type()
{
return detail::unwind_type<detail::get_pointer_class>(T(0));
}
}} // namespace boost::python
#endif // TO_PYTHON_OWNER_DWA200221_HPP