1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>How can I wrap functions which take C++ containers as arguments?</title> 5<link rel="stylesheet" href="../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Boost.Python"> 8<link rel="up" href="../faq.html" title="Chapter 5. Frequently Asked Questions (FAQs)"> 9<link rel="prev" href="is_return_internal_reference_eff.html" title="Is return_internal_reference efficient?"> 10<link rel="next" href="fatal_error_c1204_compiler_limit.html" title="fatal error C1204:Compiler limit:internal structure overflow"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table> 14<hr> 15<div class="spirit-nav"> 16<a accesskey="p" href="is_return_internal_reference_eff.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="fatal_error_c1204_compiler_limit.html"><img src="../images/next.png" alt="Next"></a> 17</div> 18<div class="section"> 19<div class="titlepage"><div><div><h3 class="title"> 20<a name="faq.how_can_i_wrap_functions_which_t"></a><a class="link" href="how_can_i_wrap_functions_which_t.html" title="How can I wrap functions which take C++ containers as arguments?">How can I wrap 21 functions which take C++ containers as arguments?</a> 22</h3></div></div></div> 23<p> 24 Ralf W. Grosse-Kunstleve provides these notes: 25 </p> 26<div class="orderedlist"><ol class="orderedlist" type="1"> 27<li class="listitem"> 28<p class="simpara"> 29 Using the regular <code class="computeroutput"><span class="identifier">class_</span><span class="special"><></span></code> wrapper: 30 </p> 31<pre class="programlisting"><span class="identifier">class_</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="special">>(</span><span class="string">"std_vector_double"</span><span class="special">)</span> 32 <span class="special">.</span><span class="identifier">def</span><span class="special">(...)</span> 33 <span class="special">...</span> 34<span class="special">;</span> 35</pre> 36<p class="simpara"> 37 This can be moved to a template so that several types (<code class="computeroutput"><span class="keyword">double</span></code>, <code class="computeroutput"><span class="keyword">int</span></code>, 38 <code class="computeroutput"><span class="keyword">long</span></code>, etc.) can be wrapped 39 with the same code. This technique is used in the file <code class="computeroutput"><span class="identifier">scitbx</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">scitbx</span><span class="special">/</span><span class="identifier">array_family</span><span class="special">/</span><span class="identifier">boost_python</span><span class="special">/</span><span class="identifier">flex_wrapper</span><span class="special">.</span><span class="identifier">h</span></code> in the "scitbx" package. 40 The file could easily be modified for wrapping <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><></span></code> instantiations. This type of 41 C++/Python binding is most suitable for containers that may contain a 42 large number of elements (>10000). 43 </p> 44</li> 45<li class="listitem"> 46<p class="simpara"> 47 Using custom rvalue converters. Boost.Python "rvalue converters" 48 match function signatures such as: 49 </p> 50<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="keyword">const</span> <span class="special">&</span><span class="identifier">array</span><span class="special">);</span> <span class="comment">// pass by const-reference</span> 51<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="identifier">array</span><span class="special">);</span> <span class="comment">// pass by value</span> 52</pre> 53<p class="simpara"> 54 Some custom rvalue converters are implemented in the file <code class="computeroutput"><span class="identifier">scitbx</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">scitbx</span><span class="special">/</span><span class="identifier">boost_python</span><span class="special">/</span><span class="identifier">container_conversions</span><span class="special">.</span><span class="identifier">h</span></code> This code can be used to convert 55 from C++ container types such as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><></span></code> or <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special"><></span></code> to Python tuples and vice versa. 56 A few simple examples can be found in the file <code class="computeroutput"><span class="identifier">scitbx</span><span class="special">/</span><span class="identifier">array_family</span><span class="special">/</span><span class="identifier">boost_python</span><span class="special">/</span><span class="identifier">regression_test_module</span><span class="special">.</span><span class="identifier">cpp</span></code> 57 Automatic C++ container <-> Python tuple conversions are most suitable 58 for containers of moderate size. These converters generate significantly 59 less object code compared to alternative 1 above. 60 </p> 61</li> 62</ol></div> 63<p> 64 A disadvantage of using alternative 2 is that operators such as arithmetic 65 +,-,*,/,% are not available. It would be useful to have custom rvalue converters 66 that convert to a "math_array" type instead of tuples. This is 67 currently not implemented but is possible within the framework of Boost.Python 68 V2 as it will be released in the next couple of weeks. [ed.: this was posted 69 on 2002/03/10] 70 </p> 71<p> 72 It would also be useful to also have "custom lvalue converters" 73 such as <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><></span></code> 74 <-> Python list. These converters would support the modification of 75 the Python list from C++. For example: 76 </p> 77<p> 78 C++: 79 </p> 80<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="keyword">double</span><span class="special">></span> <span class="special">&</span><span class="identifier">array</span><span class="special">)</span> 81<span class="special">{</span> 82 <span class="keyword">for</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">i</span><span class="special">=</span><span class="number">0</span><span class="special">;</span><span class="identifier">i</span><span class="special">&</span><span class="identifier">lt</span><span class="special">;</span><span class="identifier">array</span><span class="special">.</span><span class="identifier">size</span><span class="special">();</span><span class="identifier">i</span><span class="special">++)</span> <span class="special">{</span> 83 <span class="identifier">array</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span> <span class="special">*=</span> <span class="number">2</span><span class="special">;</span> 84 <span class="special">}</span> 85<span class="special">}</span> 86</pre> 87<p> 88 Python: 89 </p> 90<pre class="programlisting"><span class="special">>>></span> <span class="identifier">l</span> <span class="special">=</span> <span class="special">[</span><span class="number">1</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">]</span> 91<span class="special">>>></span> <span class="identifier">foo</span><span class="special">(</span><span class="identifier">l</span><span class="special">)</span> 92<span class="special">>>></span> <span class="keyword">print</span> <span class="identifier">l</span> 93<span class="special">[</span><span class="number">2</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="number">6</span><span class="special">]</span> 94</pre> 95<p> 96 Custom lvalue converters require changes to the Boost.Python core library 97 and are currently not available. 98 </p> 99<p> 100 P.S.: 101 </p> 102<p> 103 The "scitbx" files referenced above are available via anonymous 104 CVS: 105 </p> 106<pre class="programlisting"><span class="identifier">cvs</span> <span class="special">-</span><span class="identifier">d</span><span class="special">:</span><span class="identifier">pserver</span><span class="special">:</span><span class="identifier">anonymous</span><span class="error">@</span><span class="identifier">cvs</span><span class="special">.</span><span class="identifier">cctbx</span><span class="special">.</span><span class="identifier">sourceforge</span><span class="special">.</span><span class="identifier">net</span><span class="special">:/</span><span class="identifier">cvsroot</span><span class="special">/</span><span class="identifier">cctbx</span> <span class="identifier">login</span> 107<span class="identifier">cvs</span> <span class="special">-</span><span class="identifier">d</span><span class="special">:</span><span class="identifier">pserver</span><span class="special">:</span><span class="identifier">anonymous</span><span class="error">@</span><span class="identifier">cvs</span><span class="special">.</span><span class="identifier">cctbx</span><span class="special">.</span><span class="identifier">sourceforge</span><span class="special">.</span><span class="identifier">net</span><span class="special">:/</span><span class="identifier">cvsroot</span><span class="special">/</span><span class="identifier">cctbx</span> <span class="identifier">co</span> <span class="identifier">scitbx</span> 108</pre> 109</div> 110<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 111<td align="left"></td> 112<td align="right"><div class="copyright-footer">Copyright © 2002-2015 David 113 Abrahams, Stefan Seefeld<p> 114 Distributed under the Boost Software License, Version 1.0. (See accompanying 115 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 116 </p> 117</div></td> 118</tr></table> 119<hr> 120<div class="spirit-nav"> 121<a accesskey="p" href="is_return_internal_reference_eff.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="fatal_error_c1204_compiler_limit.html"><img src="../images/next.png" alt="Next"></a> 122</div> 123</body> 124</html> 125