]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [section boost/python/manage_new_object.hpp] |
2 | [section Class `manage_new_object`] | |
3 | `manage_new_object` is a model of [link concepts.resultconverter.resultconvertergenerator_concept ResultConverterGenerator] which can be used to wrap C++ functions which return a pointer to an object allocated with a new-expression, and expect the caller to take responsibility for deleting that object. | |
4 | `` | |
5 | namespace boost { namespace python | |
6 | { | |
7 | struct manage_new_object | |
8 | { | |
9 | template <class T> struct apply; | |
10 | }; | |
11 | }} | |
12 | `` | |
13 | [endsect] | |
14 | [section Class `manage_new_object` metafunctions] | |
15 | ``template <class T> struct apply`` | |
16 | [variablelist | |
17 | [[Requires][`T` is `U*` for some `U`.]] | |
18 | [[Returns][`typedef to_python_indirect<T> type;`]] | |
19 | ] | |
20 | [endsect] | |
21 | [section Example] | |
22 | In C++: | |
23 | `` | |
24 | #include <boost/python/module.hpp> | |
25 | #include <boost/python/class.hpp> | |
26 | #include <boost/python/manage_new_object.hpp> | |
27 | #include <boost/python/return_value_policy.hpp> | |
28 | ||
29 | ||
30 | struct Foo { | |
31 | Foo(int x) : x(x){} | |
32 | int get_x() { return x; } | |
33 | int x; | |
34 | }; | |
35 | ||
36 | Foo* make_foo(int x) { return new Foo(x); } | |
37 | ||
38 | // Wrapper code | |
39 | using namespace boost::python; | |
40 | BOOST_PYTHON_MODULE(my_module) | |
41 | { | |
42 | def("make_foo", make_foo, return_value_policy<manage_new_object>()) | |
43 | class_<Foo>("Foo") | |
44 | .def("get_x", &Foo::get_x) | |
45 | ; | |
46 | } | |
47 | `` | |
48 | Python code: | |
49 | `` | |
50 | >>> from my_module import * | |
51 | >>> f = make_foo(3) # create a Foo object | |
52 | >>> f.get_x() | |
53 | 3 | |
54 | `` | |
55 | [endsect] | |
56 | [endsect] |