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
|
// 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/def.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/manage_new_object.hpp>
#include <boost/python/return_internal_reference.hpp>
#include <boost/python/class.hpp>
using namespace boost::python;
int a_instances = 0;
int num_a_instances() { return a_instances; }
struct inner
{
inner(std::string const& s)
: s(s)
{}
void change(std::string const& new_s)
{
this->s = new_s;
}
std::string s;
};
struct Base
{
virtual ~Base() {}
};
struct A : Base
{
A(std::string const& s)
: x(s)
{
++a_instances;
}
~A()
{
--a_instances;
}
std::string content() const
{
return x.s;
}
inner& get_inner()
{
return x;
}
inner x;
};
struct B
{
B() : x(0) {}
B(A* x_) : x(x_) {}
inner const* adopt(A* _x) { this->x = _x; return &_x->get_inner(); }
std::string a_content()
{
return x ? x->content() : std::string("empty");
}
A* x;
};
A* create(std::string const& s)
{
return new A(s);
}
A* as_A(Base* b)
{
return dynamic_cast<A*>(b);
}
BOOST_PYTHON_MODULE(test_pointer_adoption_ext)
{
def("num_a_instances", num_a_instances);
// Specify the manage_new_object return policy to take
// ownership of create's result
def("create", create, return_value_policy<manage_new_object>());
def("as_A", as_A, return_internal_reference<>());
class_<Base>("Base")
;
class_<A, bases<Base> >("A", no_init)
.def("content", &A::content)
.def("get_inner", &A::get_inner, return_internal_reference<>())
;
class_<inner>("inner", no_init)
.def("change", &inner::change)
;
class_<B>("B")
.def(init<A*>()[with_custodian_and_ward_postcall<1,2>()])
.def("adopt", &B::adopt
// Adopt returns a pointer referring to a subobject of its 2nd argument (1st being "self")
, return_internal_reference<2
// Meanwhile, self holds a reference to the 2nd argument.
, with_custodian_and_ward<1,2> >()
)
.def("a_content", &B::a_content)
;
}
#include "module_tail.cpp"
|