Reputation: 45
I have a c++ class, call it Example, which I want to wrap to Python using pybind11. Example contains a member which is a MemberClassInterface pointer, call it memberClass. memberClass is assigned an instance of MemberClass, which is derived from MemberClassInterface. In the python wrapper, I want to be able to access the memberClass object's public methods, but dont know how to do so. Below is the structure which I tried to explain here in text.
class MemberClass : public MemberClassInterface{
public:
MemberClass();
~MemberClass();
someFunc(); //virtual func in interface.
}
someFunc(){
//implementation..
}
class Example{
public:
Example();
MemberClassInterface * memberClass;
}
Example(){
this->memberClass = new MemberClass();
}
Some wrapping like this is what I want to wrap; this is obv non-functioning, but I havent found how to do it. I also don't possess the best knowledge in c++ (or in python for that matter), so haven't been able to figure it out on my own either.
PYBIND_MODULE(pyexample,m){
py::class_<Example>(m,"Example")
.def(py::init<>())
.def("memberClass", &Example::memberClass); <--- either something like this, or something like the
row below.
.def("someFunc", &Example::memberClass->someFunc); <-- how to achieve something similar to this?
It complains about someFunc not being non-static,
which I can understand...
}
I would like to do be able of doing this:
>>> import pyexample
>>> a = pyexample.Example()
>>> a.memberClass.someFunc() <--- this is what I want to achieve.
>>> a.someFunc() <---- or possibly this. But this seems to require someFunc to be static..
or embedding memberClass.someFunc() into another function calling
memberClass.someFunc...
I do not have any prior experience in wrapping and feel therefore rather lost here. I dont expext a perfect answer, but if someone have any idea or could guide me towards someone or something talking about this sort of wrapping, I would greatly appreciate it.
Update: pschill's answer solved it for me!
Upvotes: 1
Views: 4614
Reputation: 5599
This can be achieved by first exporting the MemberClassInterface
. The following example should work as expected:
PYBIND11_MODULE(pyexample, m)
{
pybind11::class_<MemberClassInterface>(m, "MemberClassInterface")
.def("someFunc", &MemberClassInterface::someFunc);
pybind11::class_<Example>(m, "Example")
.def(pybind11::init())
.def_readonly("memberClass", &Example::memberClass);
}
Usage in python:
>>> import pyexample
>>> a = pyexample.Example()
>>> a.memberClass.someFunc()
In case that you need it, here is a full example that compiled successfully on my machine. I renamed memberClass
to member
because I found the variable name confusing. I also changed the raw pointer to a unique_ptr
because it avoids some ownership problems. To make the unique_ptr
work, I had to exchange the .def_readonly
with a .def_property_readonly
and a lambda function.
#include "pybind11/pybind11.h"
#include <iostream>
#include <memory>
class MemberClassInterface
{
public:
virtual ~MemberClassInterface() = default;
virtual void someFunc() = 0;
};
class MemberClass : public MemberClassInterface
{
public:
virtual void someFunc() override
{
std::cout << "Hello from MemberClass" << std::endl;
}
};
class Example
{
public:
Example()
:
member(std::make_unique<MemberClass>())
{}
std::unique_ptr<MemberClassInterface> member;
};
PYBIND11_MODULE(pyexample, m)
{
pybind11::class_<MemberClassInterface>(m, "MemberClassInterface")
.def("someFunc", &MemberClassInterface::someFunc);
pybind11::class_<Example>(m, "Example")
.def(pybind11::init())
.def_property_readonly("member", [](Example const& e) { return e.member.get(); });
}
Upvotes: 2