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?