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/class.hpp> |
---|
6 | #include <boost/python/module.hpp> |
---|
7 | #include <boost/python/def.hpp> |
---|
8 | #include <boost/python/return_internal_reference.hpp> |
---|
9 | #include <boost/python/call_method.hpp> |
---|
10 | #include <boost/ref.hpp> |
---|
11 | #include <boost/utility.hpp> |
---|
12 | |
---|
13 | using namespace boost::python; |
---|
14 | |
---|
15 | struct X |
---|
16 | { |
---|
17 | explicit X(int x) : x(x), magic(7654321) { ++counter; } |
---|
18 | X(X const& rhs) : x(rhs.x), magic(7654321) { ++counter; } |
---|
19 | virtual ~X() { assert(magic == 7654321); magic = 6666666; x = 9999; --counter; } |
---|
20 | |
---|
21 | void set(int x) { assert(magic == 7654321); this->x = x; } |
---|
22 | int value() const { assert(magic == 7654321); return x; } |
---|
23 | static int count() { return counter; } |
---|
24 | private: |
---|
25 | void operator=(X const&); |
---|
26 | private: |
---|
27 | int x; |
---|
28 | long magic; |
---|
29 | static int counter; |
---|
30 | }; |
---|
31 | |
---|
32 | struct Y : X |
---|
33 | { |
---|
34 | Y(int x) : X(x) {}; |
---|
35 | }; |
---|
36 | |
---|
37 | struct abstract : X |
---|
38 | { |
---|
39 | abstract(int x) : X(x) {}; |
---|
40 | int call_f(Y const& y) { return f(y); } |
---|
41 | virtual int f(Y const& y) = 0; |
---|
42 | abstract& call_g(Y const& y) { return g(y); } |
---|
43 | virtual abstract& g(Y const& y) = 0; |
---|
44 | }; |
---|
45 | |
---|
46 | struct concrete : X |
---|
47 | { |
---|
48 | concrete(int x) : X(x) {}; |
---|
49 | int call_f(Y const& y) { return f(y); } |
---|
50 | virtual int f(Y const& y) { set(y.value()); return y.value(); } |
---|
51 | }; |
---|
52 | |
---|
53 | struct abstract_callback : abstract |
---|
54 | { |
---|
55 | abstract_callback(PyObject* p, int x) |
---|
56 | : abstract(x), self(p) |
---|
57 | {} |
---|
58 | |
---|
59 | int f(Y const& y) |
---|
60 | { |
---|
61 | return call_method<int>(self, "f", boost::ref(y)); |
---|
62 | } |
---|
63 | |
---|
64 | abstract& g(Y const& y) |
---|
65 | { |
---|
66 | return call_method<abstract&>(self, "g", boost::ref(y)); |
---|
67 | } |
---|
68 | |
---|
69 | PyObject* self; |
---|
70 | }; |
---|
71 | |
---|
72 | struct concrete_callback : concrete |
---|
73 | { |
---|
74 | concrete_callback(PyObject* p, int x) |
---|
75 | : concrete(x), self(p) |
---|
76 | {} |
---|
77 | |
---|
78 | concrete_callback(PyObject* p, concrete const& x) |
---|
79 | : concrete(x), self(p) |
---|
80 | {} |
---|
81 | |
---|
82 | int f(Y const& y) |
---|
83 | { |
---|
84 | return call_method<int>(self, "f", boost::ref(y)); |
---|
85 | } |
---|
86 | |
---|
87 | int f_impl(Y const& y) |
---|
88 | { |
---|
89 | return this->concrete::f(y); |
---|
90 | } |
---|
91 | |
---|
92 | PyObject* self; |
---|
93 | }; |
---|
94 | |
---|
95 | int X::counter; |
---|
96 | |
---|
97 | BOOST_PYTHON_MODULE(virtual_functions_ext) |
---|
98 | { |
---|
99 | class_<concrete, concrete_callback>("concrete", init<int>()) |
---|
100 | .def("value", &concrete::value) |
---|
101 | .def("set", &concrete::set) |
---|
102 | .def("call_f", &concrete::call_f) |
---|
103 | .def("f", &concrete_callback::f_impl) |
---|
104 | ; |
---|
105 | |
---|
106 | class_<abstract, boost::noncopyable, abstract_callback |
---|
107 | >("abstract", init<int>()) |
---|
108 | |
---|
109 | .def("value", &abstract::value) |
---|
110 | .def("call_f", &abstract::call_f) |
---|
111 | .def("call_g", &abstract::call_g, return_internal_reference<>()) |
---|
112 | .def("set", &abstract::set) |
---|
113 | ; |
---|
114 | |
---|
115 | class_<Y>("Y", init<int>()) |
---|
116 | .def("value", &Y::value) |
---|
117 | .def("set", &Y::set) |
---|
118 | ; |
---|
119 | } |
---|
120 | |
---|
121 | #include "module_tail.cpp" |
---|