]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [section boost/python/return_value_policy.hpp] |
2 | [section Introduction] | |
3 | return_value_policy instantiations are simply models of [link concepts.callpolicies `CallPolicies`] which are composed of a [link concepts.resultconverter.resultconvertergenerator_concept `ResultConverterGenerator`] and optional `Base` [link concepts.callpolicies `CallPolicies`]. | |
4 | [endsect] | |
5 | [section Class template `return_value_policy`] | |
6 | [table | |
7 | [[Parameter][Requirements][Default]] | |
8 | [[ResultConverterGenerator][A model of [link concepts.resultconverter.resultconvertergenerator_concept `ResultConverterGenerator`]][]] | |
9 | [[Base][A model of [link concepts.callpolicies `CallPolicies`]][default_call_policies]] | |
10 | ] | |
11 | `` | |
12 | namespace boost { namespace python | |
13 | { | |
14 | template <class ResultConverterGenerator, class Base = default_call_policies> | |
15 | struct return_value_policy : Base | |
16 | { | |
17 | typedef ResultConverterGenerator result_converter; | |
18 | }; | |
19 | }} | |
20 | `` | |
21 | [endsect] | |
22 | [section Example] | |
23 | C++ module definition: | |
24 | `` | |
25 | #include <boost/python/module.hpp> | |
26 | #include <boost/python/class.hpp> | |
27 | #include <boost/python/copy_const_reference.hpp> | |
28 | #include <boost/python/return_value_policy.hpp> | |
29 | ||
30 | // classes to wrap | |
31 | struct Bar { int x; } | |
32 | ||
33 | struct Foo { | |
34 | Foo(int x) : { b.x = x; } | |
35 | Bar const& get_bar() const { return b; } | |
36 | private: | |
37 | Bar b; | |
38 | }; | |
39 | ||
40 | // Wrapper code | |
41 | using namespace boost::python; | |
42 | BOOST_PYTHON_MODULE(my_module) | |
43 | { | |
44 | class_<Bar>("Bar"); | |
45 | ||
46 | class_<Foo>("Foo", init<int>()) | |
47 | .def("get_bar", &Foo::get_bar | |
48 | , return_value_policy<copy_const_reference>()) | |
49 | ; | |
50 | } | |
51 | `` | |
52 | Python code: | |
53 | `` | |
54 | >>> from my_module import * | |
55 | >>> f = Foo(3) # create a Foo object | |
56 | >>> b = f.get_bar() # make a copy of the internal Bar object | |
57 | `` | |
58 | [endsect] | |
59 | [endsect] |