next version
This commit is contained in:
47
__logicsample/samplederivedclass.c
Normal file
47
__logicsample/samplederivedclass.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#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)
|
||||
Reference in New Issue
Block a user