-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample02.cpp
More file actions
48 lines (35 loc) · 975 Bytes
/
sample02.cpp
File metadata and controls
48 lines (35 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <pybind11/pybind11.h>
int add(int a, int b) { return a + b; }
class Animal {
public:
virtual ~Animal() {}
virtual std::string go(int n_times) = 0;
};
class Dog : public Animal {
std::string go(int n_times) override {
std::string result;
for (int i = 0; i < n_times; i++) {
result += u8"ワン! ";
}
return result;
}
};
class PyAnimal : public Animal {
public:
using Animal::Animal;
std::string go(int n_times) override {
PYBIND11_OVERLOAD_PURE(std::string, Animal, go, n_times);
}
};
std::string call_go(Animal* animal) { return animal->go(3); }
namespace py = pybind11;
PYBIND11_PLUGIN(sample02) {
py::module m("sample02", "pybind11 module sample.");
m.def("add", &add, u8"2つの数値を足し合わせます");
py::class_<Animal, PyAnimal> animal(m, "Animal");
animal.def(py::init<>()).def("go", &Animal::go);
py::class_<Dog> dog(m, "Dog", animal);
dog.def(py::init<>());
m.def("call_go", &call_go);
return m.ptr();
}