@@ -29,7 +29,12 @@ struct inner
2929 std::string s;
3030};
3131
32- struct A
32+ struct Base
33+ {
34+ virtual ~Base () {}
35+ };
36+
37+ struct A : Base
3338{
3439 A (std::string const & s)
3540 : x(s)
@@ -76,40 +81,47 @@ A* create(std::string const& s)
7681 return new A (s);
7782}
7883
84+ A* as_A (Base* b)
85+ {
86+ return dynamic_cast <A*>(b);
87+ }
88+
7989BOOST_PYTHON_MODULE_INIT (test_pointer_adoption_ext)
8090{
81- boost::python::module (" test_pointer_adoption_ext" )
82- .def (" num_a_instances" , num_a_instances)
91+ boost::python::module m (" test_pointer_adoption_ext" );
92+ m .def (" num_a_instances" , num_a_instances)
8393
8494 // Specify the manage_new_object return policy to take
8595 // ownership of create's result
8696 .def (" create" , create, return_value_policy<manage_new_object>())
87-
88- .add (
89-
90- class_<A>(no_init)
91- .def (" content" , &A::content)
92- .def (" get_inner" , &A::get_inner, return_internal_reference<>())
93- )
9497
98+ .def (" as_A" , as_A, return_internal_reference<>())
9599 .add (
96- class_<inner>(no_init)
97- . def ( " change " , &inner::change )
98- )
100+
101+ class_<Base>( " Base " )
102+ );
99103
100- .add (
101- class_<B>(" B" )
102- .def_init (args<A*>(), with_custodian_and_ward_postcall<1 ,2 >())
103-
104- .def (" adopt" , &B::adopt
105- // Adopt returns a pointer referring to a subobject of its 2nd argument (1st being "self")
106- , return_internal_reference<2
107- // Meanwhile, self holds a reference to the 2nd argument.
108- , with_custodian_and_ward<1 ,2 > >()
109- )
104+ m.add (class_<A, bases<Base> >(no_init)
105+ .def (" content" , &A::content)
106+ .def (" get_inner" , &A::get_inner, return_internal_reference<>())
107+ )
108+
109+ .add (class_<inner>(no_init)
110+ .def (" change" , &inner::change)
111+ )
112+
113+ .add (class_<B>(" B" )
114+ .def_init (args<A*>(), with_custodian_and_ward_postcall<1 ,2 >())
110115
111- .def (" a_content" , &B::a_content)
116+ .def (" adopt" , &B::adopt
117+ // Adopt returns a pointer referring to a subobject of its 2nd argument (1st being "self")
118+ , return_internal_reference<2
119+ // Meanwhile, self holds a reference to the 2nd argument.
120+ , with_custodian_and_ward<1 ,2 > >()
112121 )
122+
123+ .def (" a_content" , &B::a_content))
113124 ;
114125}
115126
127+ #include " module_tail.cpp"
0 commit comments