Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Does it have to be <code>std::list</code> ? If you use <code>std::vector</code> instead you can use <code>boost::python::vector_indexing_suite</code> to wrap the list. See <a href="https://stackoverflow.com/questions/6157409/stdvector-to-boostpythonlist/6158370#6158370" title="this post">this post</a> for details.</p> <p>If you must use <code>std::list</code> you'll need to create a helper class that wraps the <code>std::list</code> functionality with python's <code>list</code> methods. That can be quite involved, but doable.</p> <p><strong>std_item.hpp:</strong></p> <pre><code>#include &lt;list&gt; #include &lt;algorithm&gt; #include &lt;boost/python.hpp&gt; template&lt;class T&gt; struct listwrap { typedef typename T::value_type value_type; typedef typename T::iterator iter_type; static void add(T &amp; x, value_type const&amp; v) { x.push_back(v); } static bool in(T const&amp; x, value_type const&amp; v) { return std::find(x.begin(), x.end(), v) != x.end(); } static int index(T const&amp; x, value_type const&amp; v) { int i = 0; for(T::const_iterator it=x.begin(); it!=x.end(); ++it,++i) if( *it == v ) return i; PyErr_SetString(PyExc_ValueError, "Value not in the list"); throw boost::python::error_already_set(); } static void del(T&amp; x, int i) { if( i&lt;0 ) i += x.size(); iter_type it = x.begin(); for (int pos = 0; pos &lt; i; ++pos) ++it; if( i &gt;= 0 &amp;&amp; i &lt; (int)x.size() ) { x.erase(it); } else { PyErr_SetString(PyExc_IndexError, "Index out of range"); boost::python::throw_error_already_set(); } } static value_type&amp; get(T&amp; x, int i) { if( i &lt; 0 ) i += x.size(); if( i &gt;= 0 &amp;&amp; i &lt; (int)x.size() ) { iter_type it = x.begin(); for(int pos = 0; pos &lt; i; ++pos) ++it; return *it; } else { PyErr_SetString(PyExc_IndexError, "Index out of range"); throw boost::python::error_already_set(); } } static void set(T&amp; x, int i, value_type const&amp; v) { if( i &lt; 0 ) i += x.size(); if( i &gt;= 0 &amp;&amp; i &lt; (int)x.size() ) { iter_type it = x.begin(); for(int pos = 0; pos &lt; i; ++pos) ++it; *it = v; } else { PyErr_SetString(PyExc_IndexError, "Index out of range"); boost::python::throw_error_already_set(); } } }; template&lt;class T&gt; void export_STLList(const char* typeName) { using namespace boost::python; class_&lt;std::list&lt;T&gt; &gt;(typeName) .def("__len__", &amp;std::list&lt;T&gt;::size) .def("clear", &amp;std::list&lt;T&gt;::clear) .def("append", &amp;listwrap&lt;T&gt;::add, with_custodian_and_ward&lt;1,2&gt;()) // to let container keep value .def("__getitem__", &amp;listwrap&lt;T&gt;::get, return_value_policy&lt;copy_non_const_reference&gt;()) .def("__setitem__", &amp;listwrap&lt;T&gt;::set, with_custodian_and_ward&lt;1,2&gt;()) // to let container keep value .def("__delitem__", &amp;listwrap&lt;T&gt;::del) .def("__contains__", &amp;listwrap&lt;T&gt;::in) .def("__iter__", iterator&lt;std::list&lt;T&gt; &gt;()) .def("index", &amp;listwrap&lt;T&gt;::index); } </code></pre> <p><strong>usage:</strong></p> <pre><code>typedef std::list&lt;int&gt; intlist; export_STLList&lt;int&gt;("intlist"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload