|
| 1 | +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 | + |
| 3 | +<html> |
| 4 | + <head> |
| 5 | + <meta name="generator" content= |
| 6 | + "HTML Tidy for Windows (vers 1st August 2002), see www.w3.org"> |
| 7 | + <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 8 | + <link rel="stylesheet" type="text/css" href="../boost.css"> |
| 9 | + |
| 10 | + <title>Boost.Python - <boost/python/return_by_value.hpp></title> |
| 11 | + </head> |
| 12 | + |
| 13 | + <body> |
| 14 | + <table border="0" cellpadding="7" cellspacing="0" width="100%" summary= |
| 15 | + "header"> |
| 16 | + <tr> |
| 17 | + <td valign="top" width="300"> |
| 18 | + <h3><a href="../../../../index.htm"><img height="86" width="277" |
| 19 | + alt="C++ Boost" src="../../../../c++boost.gif" border="0"></a></h3> |
| 20 | + </td> |
| 21 | + |
| 22 | + <td valign="top"> |
| 23 | + <h1 align="center">Boost.Python</h1> |
| 24 | + |
| 25 | + <h2 align="center">Header |
| 26 | + <boost/python/return_by_value.hpp></h2> |
| 27 | + </td> |
| 28 | + </tr> |
| 29 | + </table> |
| 30 | + <hr> |
| 31 | + |
| 32 | + <h2>Contents</h2> |
| 33 | + |
| 34 | + <dl class="page-index"> |
| 35 | + <dt><a href="#classes">Classes</a></dt> |
| 36 | + |
| 37 | + <dd> |
| 38 | + <dl class="page-index"> |
| 39 | + <dt><a href="#return_by_value-spec">Class |
| 40 | + <code>return_by_value</code></a></dt> |
| 41 | + |
| 42 | + <dd> |
| 43 | + <dl class="page-index"> |
| 44 | + <dt><a href="#return_by_value-spec-synopsis">Class |
| 45 | + <code>return_by_value</code> synopsis</a></dt> |
| 46 | + |
| 47 | + <dt><a href="#return_by_value-spec-metafunctions">Class |
| 48 | + <code>return_by_value</code> metafunctions</a></dt> |
| 49 | + </dl> |
| 50 | + </dd> |
| 51 | + </dl> |
| 52 | + </dd> |
| 53 | + |
| 54 | + <dt><a href="#examples">Example</a></dt> |
| 55 | + </dl> |
| 56 | + <hr> |
| 57 | + |
| 58 | + <h2><a name="classes"></a>Classes</h2> |
| 59 | + |
| 60 | + <h3><a name="return_by_value-spec"></a>Class |
| 61 | + <code>return_by_value</code></h3> |
| 62 | + |
| 63 | + <p><code>return_by_value</code> is a model of <a href= |
| 64 | + "ResultConverter.html#ResultConverterGenerator-concept">ResultConverterGenerator</a> |
| 65 | + which can be used to wrap C++ functions returning any reference or value |
| 66 | + type such that the return value is copied into a new Python object.</p> |
| 67 | + |
| 68 | + <h4><a name="return_by_value-spec-synopsis"></a>Class |
| 69 | + <code>return_by_value</code> synopsis</h4> |
| 70 | +<pre> |
| 71 | +namespace boost { namespace python |
| 72 | +{ |
| 73 | + struct return_by_value |
| 74 | + { |
| 75 | + template <class T> struct apply; |
| 76 | + }; |
| 77 | +}} |
| 78 | +</pre> |
| 79 | + |
| 80 | + <h4><a name="return_by_value-spec-metafunctions"></a>Class |
| 81 | + <code>return_by_value</code> metafunctions</h4> |
| 82 | +<pre> |
| 83 | +template <class T> struct apply |
| 84 | +</pre> |
| 85 | + |
| 86 | + <dl class="metafunction-semantics"> |
| 87 | + <dt><b>Returns:</b> <code>typedef <a href= |
| 88 | + "to_python_value.html#to_python_value-spec">to_python_value</a><T> |
| 89 | + type;</code></dt> |
| 90 | + </dl> |
| 91 | + |
| 92 | + <h2><a name="examples"></a>Example</h2> |
| 93 | + |
| 94 | + <h3>C++ Module Definition</h3> |
| 95 | +<pre> |
| 96 | +#include <boost/python/module.hpp> |
| 97 | +#include <boost/python/class.hpp> |
| 98 | +#include <boost/python/return_by_value.hpp> |
| 99 | +#include <boost/python/return_value_policy.hpp> |
| 100 | + |
| 101 | +// classes to wrap |
| 102 | +struct Bar { }; |
| 103 | + |
| 104 | +Bar global_bar; |
| 105 | + |
| 106 | +// functions to wrap: |
| 107 | +Bar b1(); |
| 108 | +Bar& b2(); |
| 109 | +Bar const& b3(); |
| 110 | + |
| 111 | +// Wrapper code |
| 112 | +using namespace boost::python; |
| 113 | +template <class R> |
| 114 | +void def_void_function(char const* name, R (*f)()) |
| 115 | +{ |
| 116 | + def(name, f, return_value_policy<return_by_value>()); |
| 117 | +} |
| 118 | + |
| 119 | +BOOST_PYTHON_MODULE(my_module) |
| 120 | +{ |
| 121 | + class_<Bar>("Bar"); |
| 122 | + def_void_function("b1", b1); |
| 123 | + def_void_function("b2", b2); |
| 124 | + def_void_function("b3", b3); |
| 125 | +} |
| 126 | +</pre> |
| 127 | + |
| 128 | + <h3>Python Code</h3> |
| 129 | +<pre> |
| 130 | +>>> from my_module import * |
| 131 | +>>> b = b1() # each of these calls |
| 132 | +>>> b = b2() # creates a brand |
| 133 | +>>> b = b3() # new Bar object |
| 134 | +</pre> |
| 135 | + |
| 136 | + <p>Revised |
| 137 | + <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan --> |
| 138 | + 15 October, 2002 |
| 139 | + <!--webbot bot="Timestamp" endspan i-checksum="39359" --> |
| 140 | + </p> |
| 141 | + |
| 142 | + <p><i>© Copyright <a href= |
| 143 | + "../../../../people/dave_abrahams.htm">Dave Abrahams</a> 2002. All Rights |
| 144 | + Reserved.</i></p> |
| 145 | + </body> |
| 146 | +</html> |
| 147 | + |
0 commit comments