I'm using Boost.Python to expose an array of objects within another class:
class Thing {
int i; // and of course other stuff
};
class Container {
Thing *things[128];
int n_things;
}
I'd like to provide to python a read-only list interface to the Things. I have something like this in my boost.python extension source code:
static bp::list EXTget_things(Container &c)
{
bp::list thing_list;
for (int i = 0; i < c.n_things; i++) {
thing_list.append(c.things[i]);
}
return thing_list;
}
I also have the (unusual?) constraint that I can't copy the Thing objects. They don't have a working copy constructor, and I can't really change that. I'd like to just return a python list containing the addresses of the original objects, and correspondingly ensure that python doesn't free the originals when it frees the list.
How can I do that? (Or can it be done?) I recognize it could cause lifetime problems if the Container goes out of scope in python, but some other python code still tries to use the param_list (since it has pointers into the Collection object), but I'm willing to work with that restriction.