I have a C++ function that returns a list of structs. Inside the struct, there are more lists of structs.
struct CameraInfo {
CamName name;
std::list<CamImageFormat> lImgFormats;
std::list<CamControls> lCamControls;
};
std::list<CameraInfo> getCameraInfo()
{
std::list<CameraInfo> lCamerasInfo;
// fill lCamerasInfo
return lCamerasInfo;
}
then for exporting it I was using:
class_<CameraNode....> >("CameraNode", no_init)
...
...
.def("listCameraInfo", make_function(&CameraNode::listCameraInfo))
.staticmethod("listCameraInfo")
...
;
And it was OK since I was using cout to print the data on screen... I would like now to use the return value and it's content from python like properties, this way:
cameras = []
cameras = CameraNode.getCameraInfo()
print cameras[0].name
print cameras[0].lImgFormats[0]
and so on...
Is this even possible?? Should I be using add_property instead? I don't think I can create a class for every struct. This design made sense while I was working on C++ only but now that I would have to wrap it, I'm getting more and more confused.
Any advice on wrapping std::list with boost.python in a general way would be very well accepted.
Edit:
I will add here links that I've found useful: Iterators StlContainers
Does it have to be
std::list
? If you usestd::vector
instead you can useboost::python::vector_indexing_suite
to wrap the list. See this post for details.If you must use
std::list
you'll need to create a helper class that wraps thestd::list
functionality with python'slist
methods. That can be quite involved, but doable.std_item.hpp:
usage:
If one-way (from c++ to python) wrapping is enough, then you can define a direct converter from
list<list<YourClass> >
-- see my vector<vector<string> > converter -- just change types as you need, and don't forget to register the converter.You could also have a method returning
python::list
(which would itself containpython::list
's with your objects) which would iterate over c++ nested list and build native python list out of it, but it would only work in one case you have.For two-way conversions, either have a look at my file (which contains both-way converters for different types) -- advantage being you get native python lists, disadvatage is copying the objects. For two-way conversion of large collections,
indexing_suite
is definitely the way to go.There is
indexing_suite_v2
, which is allegedly much better, including direct support forstd::list
andstd::map
, though unfortunately quite badly documented (last time I looked, about 1.5 years ago) and not official part ofboost::python
.