47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
#include <vector>
|
|
#include <boost/python.hpp>
|
|
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
|
|
|
|
struct base
|
|
{
|
|
virtual ~base() {}
|
|
virtual int perform() = 0;
|
|
};
|
|
|
|
struct base_wrap: base, boost::python::wrapper<base>
|
|
{
|
|
int perform() { return int(this->get_override("perform")()) - 10; }
|
|
};
|
|
|
|
BOOST_PYTHON_MODULE(example)
|
|
{
|
|
namespace python = boost::python;
|
|
python::class_<base_wrap, boost::noncopyable>("Base", python::init<>())
|
|
.def("perform", python::pure_virtual(&base::perform))
|
|
;
|
|
|
|
python::class_<std::vector<base*>>("BaseList")
|
|
.def(python::vector_indexing_suite<std::vector<base*>>())
|
|
;
|
|
|
|
python::def("do_perform", +[](base* object) {
|
|
return object->perform();
|
|
});
|
|
}
|
|
|
|
|
|
|
|
>>> import example
|
|
>>> class derived(example.Base):
|
|
... def __init__(self):
|
|
... self.name = "test"
|
|
... example.Base.__init__(self)
|
|
... def perform(self):
|
|
... return 42
|
|
...
|
|
>>> d = derived()
|
|
>>> base_list = example.BaseList()
|
|
>>> base_list.append(d)
|
|
>>> assert(len(base_list) == 1)
|
|
>>> assert(base_list[0].perform() == 42)
|
|
>>> assert(example.do_perform(base_list[0]) == 32) |