]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/python/doc/reference/return_by_value.qbk
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / python / doc / reference / return_by_value.qbk
1 [section boost/python/return_by_value.hpp]
2 [section Class `return_by_value`]
3 `return_by_value` is a model of [link concepts.resultconverter.resultconvertergenerator_concept ResultConverterGenerator] which can be used to wrap C++ functions returning any reference or value type such that the return value is copied into a new Python object.
4 ``
5 namespace boost { namespace python
6 {
7 struct return_by_value
8 {
9 template <class T> struct apply;
10 };
11 }}
12 ``
13 [endsect]
14 [section Class `return_by_value` metafunctions]
15 ``template <class T> struct apply``
16 [variablelist
17 [[Returns][`typedef to_python_value<T> type;`]]
18 ]
19 [endsect]
20 [section Example]
21 In C++:
22 ``
23 #include <boost/python/module.hpp>
24 #include <boost/python/class.hpp>
25 #include <boost/python/return_by_value.hpp>
26 #include <boost/python/return_value_policy.hpp>
27
28 // classes to wrap
29 struct Bar { };
30
31 Bar global_bar;
32
33 // functions to wrap:
34 Bar b1();
35 Bar& b2();
36 Bar const& b3();
37
38 // Wrapper code
39 using namespace boost::python;
40 template <class R>
41 void def_void_function(char const* name, R (*f)())
42 {
43 def(name, f, return_value_policy<return_by_value>());
44 }
45
46 BOOST_PYTHON_MODULE(my_module)
47 {
48 class_<Bar>("Bar");
49 def_void_function("b1", b1);
50 def_void_function("b2", b2);
51 def_void_function("b3", b3);
52 }
53 ``
54 Python code:
55 ``
56 >>> from my_module import *
57 >>> b = b1() # each of these calls
58 >>> b = b2() # creates a brand
59 >>> b = b3() # new Bar object
60 ``
61 [endsect]
62 [endsect]