pybind11 - Serbipunk/notes GitHub Wiki
functions
#include <iostream>
#include <pybind11/pybind11.h>
namespace py = pybind11;
int add(int i, int j) {
return i+j;
}
int add2(int i, int j) {
return i+j;
}
PYBIND11_MODULE(ilab_mt_sdk_binding_python, m) {
m.doc() = "pybind11 example plugin";
m.def("add", &add, "A function that adds two numbers");
m.def("add2", &add2, "A function that adds 2 numbers", py::arg("i")=1, py::arg("j")=1);
}
stype 1
struct Pet {
Pet(const std::string &name) : name(name) { }
void setName(const std::string &name_) { name = name_; }
const std::string &getName() const { return name; }
std::string name;
};
PYBIND11_MODULE(ilab_mt_sdk_binding_python, m) {
m.doc() = "pybind11 example plugin";
py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &>())
.def("setName", &Pet::setName)
.def("getName", &Pet::getName)
.def("__repr__",
[](const Pet &a) {
return "<example.Pet named '" + a.name + "'>";
}
)
;
m.def("add", &add, "A function that adds two numbers");
m.def("add2", &add2, "A function that adds 2 numbers", py::arg("i")=1, py::arg("j")=1);
}
style 2
struct Pet {
void setName(const std::string &name_) { name = name_; }
const std::string &getName() const { return name; }
std::string name;
enum Kind {
Dog = 0,
Cat
};
Pet(const std::string &name, Kind type) : name(name), type(type) { }
Kind type;
};
PYBIND11_MODULE(ilab_mt_sdk_binding_python, m) {
m.doc() = "pybind11 example plugin";
py::class_<Pet> pet(m, "Pet");
pet.def(py::init<const std::string &, Pet::Kind>())
.def("SetName", &Pet::setName)
.def("getName", &Pet::getName);
py::enum_<Pet::Kind>(pet, "Kind")
.value("Dog", Pet::Kind::Dog)
.value("Cat", Pet::Kind::Cat)
.export_values();
}