views:

50

answers:

1

I have a class in C++ that I can't modify. However, that class holds an std::list<> of items that I need to be able to access in a Python extension. Since Boost::Python doesn't seem to have a built-in conversion between an std::list and a Python list, I was hoping to be able to write a method in C++ that could do this conversion for me and later, when I am mapping the C++ classes to Python classes, I could attach this method.

I would prefer if I could just call the method like

baseClassInstance.get_std_list_of_items_as_python_list()
+1  A: 

Boost provides a helper to wrap iterators which is documented here: http://www.boost.org/doc/libs/1_42_0/libs/python/doc/v2/iterator.html

The example hear the end of that page worked for me, you just need to explicitly create the conversion, for example:

  class_<std::list<Item> >("ItemList")
    .def("__iter__", iterator<std::list<Item> >());

To modify the C++ class without changing it, I am in the habit of creating a thin wrapper that subclasses the real class. This makes a nice place to separate out all the crud that makes my C++ objects feel comfortable from Python.

  class Py_BaseClass : public BaseClass {
  public:
    std::list<Item> & py_get_items();
  }
Ross Kinder