1 // Copyright David Abrahams 2002.
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #include <boost/python/module.hpp>
6 #include <boost/python/class.hpp>
7 #include <boost/python/return_value_policy.hpp>
8 #include <boost/python/manage_new_object.hpp>
9 #include <boost/python/reference_existing_object.hpp>
10 #include <boost/python/pure_virtual.hpp>
11 #include <boost/python/wrapper.hpp>
12 #include <boost/python/def.hpp>
13 #include <boost/python/call.hpp>
14 #include <boost/utility.hpp>
18 #ifdef HELD_BY_AUTO_PTR
19 # define HELD_PTR(X) , std::auto_ptr< X >
24 using namespace boost::python
;
29 virtual char const* f() = 0;
30 char const* g() { return "P::g()"; }
33 struct PCallback
: P
, wrapper
<P
>
37 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
38 return call
<char const*>(this->get_override("f").ptr());
40 return this->get_override("f")();
47 char const* f() { return "Q::f()"; }
53 virtual char const* f() { return "A::f()"; }
56 struct ACallback
: A
, wrapper
<A
>
60 if (override f
= this->get_override("f"))
61 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
62 return call
<char const*>(f
.ptr());
70 char const* default_f() { return this->A::f(); }
75 virtual char const* f() { return "B::f()"; }
80 virtual char const* f() { return "C::f()"; }
85 virtual char const* f() { return "D::f()"; }
86 char const* g() { return "D::g()"; }
89 struct DCallback
: D
, wrapper
<D
>
93 if (override f
= this->get_override("f"))
94 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
95 return call
<char const*>(f
.ptr());
111 char const* call_f(A
& a
) { return a
.f(); }
113 A
* factory(unsigned choice
)
117 case 0: return new A
;
119 case 1: return new B
;
121 default: return new C
;
132 A
* pass_a(A
* x
) { return x
; }
134 #ifdef HELD_BY_AUTO_PTR
135 BOOST_PYTHON_MODULE_INIT(polymorphism2_auto_ptr_ext
)
137 BOOST_PYTHON_MODULE_INIT(polymorphism2_ext
)
140 class_
<ACallback
HELD_PTR(A
),boost::noncopyable
>("A")
141 .def("f", &A::f
, &ACallback::default_f
)
144 def("getBCppObj", getBCppObj
, return_value_policy
<reference_existing_object
>());
146 class_
<C
HELD_PTR(C
),bases
<A
>,boost::noncopyable
>("C")
150 class_
<DCallback
HELD_PTR(D
),bases
<A
>,boost::noncopyable
>("D")
155 def("pass_a", &pass_a
, return_internal_reference
<>());
157 def("getCCppObj", getCCppObj
, return_value_policy
<reference_existing_object
>());
159 def("factory", factory
, return_value_policy
<manage_new_object
>());
161 def("call_f", call_f
);
163 class_
<PCallback
,boost::noncopyable
>("P")
164 .def("f", pure_virtual(&P::f
))
167 class_
<Q
HELD_PTR(Q
), bases
<P
> >("Q")
168 .def("g", &P::g
) // make sure virtual inheritance doesn't interfere
172 //#include "module_tail.cpp"