[C++-sig] Exposing a C-style array data member to Python via Boost.Python

Ashish Sadanandan ashish.sadanandan at gmail.com
Mon Jul 29 16:22:30 CEST 2013


Holger Brandsmeier <brandsmeier <at> gmx.de> writes:

> 
> Ashish,
> 
> if you really need it to be a `list` in python, then there is no way
> around it, you have to copy it. If you just need it to behave like a
> list, then that is possible. Just export your struct to python and
> implement methods such as __get__(),  __set__(), __len()__, __eq__(),
> ...
> 
> You may also read up on boost python indexing suite (optionally
> version 2) and see if that helps you to expose all these methods. If
> you write you class very much like std::vector, then the indexing
> suite can be of great help. Otherwise, just export those functions by
> hand.
> 
> -Holger
> 

Holger,
Thanks for the response. I ended up doing what you suggested. Created an
array_ref class that presents a non-owning view of an array; this has all
the member functions that vector does, except for the ones that mutate the
size of the container. The getter for the char array member wraps it in an
array_ref, and then another class, array_indexing_suite, copied from Boost's
vector_indexing_suite takes care of the rest of the interop.

Thanks again,
Ashish.



More information about the Cplusplus-sig mailing list