views:

659

answers:

2

I'm currently writing python bindings for a c++ library I'm working on. The library reads some binary file format and reading speed is very important. While optimizing the library for speed, I noticed that std::vector (used in the instances I'm reading) was eating up a lot of processing time, so I replaced those with simple arrays allocated with new[] (whether this was a good/wise thing to do is another question probably).

Now I'm stuck with the problem of how to give python access to these arrays. There seems to be no solution built into boost::python (I haven't been able to find one at least).

Example code to illustrate the situation:

// Instance.cpp
class Instance
{
    int * data;
    int dataLength;
    Foo ()
    {
        data = new int[10];
        dataLength = 10;
    }
};

// Class pythonBindings.cpp
BOOST_PYTHON_MODULE(db)
{
    class_<Instance>("Instance", init<>())
        .add_property("data", ........)
    ;
}

I guess I could use a wrapper function that constructs a boost::python::list out of the arrays whenever python wants to access them. Since I am quite new to boost::python, I figured I should ask if there are any nice, standard or built-in solutions to this problem before I start hacking away though.

So, how would you recommend wrapping Instance's data array using boost::python?

+1  A: 

I will recomend a wrap data and dataLength with proxy class and returns from Instance this proxy. In our project we use this way to export data from our app to python.

If you want I can give you few links to our implementation and explain how it works.

W55tKQbuRu28Q4xv
+1  A: 

If you change your class to work with std::vector instances, take a look at the vector indexing suite (http://www.boost.org/doc/libs/1_41_0/libs/python/doc/v2/indexing.html), which allows you to expose vectors to python with a native list interface, without creating copies from/to python.

Bruno Oliveira