1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/reference_existing_object.hpp>
#include <boost/python/call_method.hpp>
#include <boost/python/pure_virtual.hpp>
#include <boost/python/def.hpp>
#include <boost/utility.hpp>
using namespace boost::python;
struct Callback
{
Callback(PyObject* o) : mSelf(o) {}
PyObject* mSelf;
};
struct P
{
virtual ~P(){}
virtual std::string f() = 0;
std::string g() { return "P::g()"; }
};
struct PCallback : P, Callback
{
PCallback (PyObject* self) : Callback(self) {}
std::string f()
{
return call_method<std::string>(mSelf, "f");
}
};
struct Q : virtual P
{
std::string f() { return "Q::f()"; }
};
struct A
{
virtual ~A(){}
virtual std::string f() { return "A::f()"; }
};
struct ACallback : A, Callback
{
ACallback (PyObject* self) : Callback(self) {}
std::string f()
{
return call_method<std::string>(mSelf, "f");
}
std::string default_f()
{
return A::f();
}
};
struct B : A
{
virtual std::string f() { return "B::f()"; }
};
struct C : A
{
virtual std::string f() { return "C::f()"; }
};
struct D : A
{
virtual std::string f() { return "D::f()"; }
std::string g() { return "D::g()"; }
};
struct DCallback : D, Callback
{
DCallback (PyObject* self) : Callback(self) {}
std::string f()
{
return call_method<std::string>(mSelf, "f");
}
std::string default_f()
{
return A::f();
}
};
A& getBCppObj ()
{
static B b;
return b;
}
std::string call_f(A& a) { return a.f(); }
A* factory(unsigned choice)
{
switch (choice % 3)
{
case 0: return new A;
break;
case 1: return new B;
break;
default: return new C;
break;
}
}
C& getCCppObj ()
{
static C c;
return c;
}
A* pass_a(A* x) { return x; }
BOOST_PYTHON_MODULE_INIT(polymorphism_ext)
{
class_<A,boost::noncopyable,ACallback>("A")
.def("f", &A::f, &ACallback::default_f)
;
def("getBCppObj", getBCppObj, return_value_policy<reference_existing_object>());
class_<C,bases<A>,boost::noncopyable>("C")
.def("f", &C::f)
;
class_<D,bases<A>,DCallback,boost::noncopyable>("D")
.def("f", &D::f, &DCallback::default_f)
.def("g", &D::g)
;
def("pass_a", &pass_a, return_internal_reference<>());
def("getCCppObj", getCCppObj, return_value_policy<reference_existing_object>());
def("factory", factory, return_value_policy<manage_new_object>());
def("call_f", call_f);
class_<P,boost::noncopyable,PCallback>("P")
.def("f", pure_virtual(&P::f))
;
class_<Q, bases<P> >("Q")
.def("g", &P::g) // make sure virtual inheritance doesn't interfere
;
}
//#include "module_tail.cpp"
|