Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/" rel="noreferrer">This blog post</a> has a pretty clear description of how to write these converters. The basic pattern is to define a class that has the form:</p> <pre><code>struct SomeType_from_PyObject { SomeType_from_PyObject(); static void* convertible(PyObject* obj_ptr); static void construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data); }; </code></pre> <p>Where the constructor is responsible for adding this converter to Boost.Python's registry:</p> <pre><code>SomeType_from_PyObject::SomeType_from_PyObject() { converter::registry::push_back(&amp;convertible, &amp;construct, type_id&lt;SomeType&gt;()); } </code></pre> <p>The function <code>convertible</code> tells Boost whether or not this converter can convert the specified Python object:</p> <pre><code>void* SomeType_from_PyObject::convertible(PyObject* obj_ptr) { if (PyMapping_Check(obj_ptr)) { return obj_ptr; } else { return NULL; } } </code></pre> <p>The <code>construct</code> function actually creates an object of the conversion type:</p> <pre><code>void SomeType_from_PyObject::construct(PyObject* obj_ptr, converter::rvalue_from_python_stage1_data* data) { typedef converter::rvalue_from_python_storage&lt;SomeType&gt; storage_t; storage_t* the_storage = reinterpret_cast&lt;storage_t*&gt;(data); void* memory_chunk = the_storage-&gt;storage.bytes; object obj(handle&lt;&gt;(borrowed(obj_ptr))); SomeType* output = new (memory_chunk) SomeType(); // Use the contents of obj to populate output, e.g. using extract&lt;&gt; data-&gt;convertible = memory_chunk; } </code></pre> <p>and then in your inside your BOOST_PYTHON_MODULE, include the line</p> <pre><code>SomeType_from_PyObject(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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