简体   繁体   中英

How can I use Boost::Python to add a method to an exported class without modifying the base class?

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()

To answer the question in a more general way, you can attach any c++ function that has the right signature to the python export in the class_ declaration.

assume a class foo:

struct foo
{
    //class stuff here
}

You can define a free function that takes a reference to foo as the first argument:

int do_things_to_a_foo(foo& self, int a, std::string b, other_type c)
{
    //Do things to self
}

And export it just like a member of foo:

class_<foo>("foo")
   ...
   .def("do_things_to_a_foo", &do_things_to_a_foo)
   ...
   ;

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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM