88#include < boost/python/manage_new_object.hpp>
99#include < boost/python/return_internal_reference.hpp>
1010#include < boost/python/class.hpp>
11+ #include < boost/python/make_constructor.hpp>
1112
1213using namespace boost ::python;
1314
@@ -86,6 +87,11 @@ A* as_A(Base* b)
8687 return dynamic_cast <A*>(b);
8788}
8889
90+ B* create_B (A* a)
91+ {
92+ return new B (a);
93+ }
94+
8995BOOST_PYTHON_MODULE (test_pointer_adoption_ext)
9096{
9197 def (" num_a_instances" , num_a_instances);
@@ -102,6 +108,8 @@ BOOST_PYTHON_MODULE(test_pointer_adoption_ext)
102108 class_<A, bases<Base> >(" A" , no_init)
103109 .def (" content" , &A::content)
104110 .def (" get_inner" , &A::get_inner, return_internal_reference<>())
111+ .def (" create_B" , &create_B, with_custodian_and_ward_postcall<0 ,1 ,
112+ return_value_policy<manage_new_object> >())
105113 ;
106114
107115 class_<inner>(" inner" , no_init)
@@ -120,6 +128,16 @@ BOOST_PYTHON_MODULE(test_pointer_adoption_ext)
120128
121129 .def (" a_content" , &B::a_content)
122130 ;
131+
132+ class_<B>(" B1" )
133+ .def (init<A*>()[construct_custodian_for<1 >()])
134+ .def (" a_content" , &B::a_content)
135+ ;
136+
137+ class_<B>(" B2" )
138+ .def (" __init__" , make_constructor (&create_B, construct_custodian_for<1 >()))
139+ .def (" a_content" , &B::a_content)
140+ ;
123141}
124142
125143#include " module_tail.cpp"
0 commit comments