-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexample.cpp
40 lines (29 loc) · 892 Bytes
/
example.cpp
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
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
class Pet
{
public:
Pet(const std::string &name, int hunger) : name(name), hunger(hunger) {}
~Pet() {}
void go_for_a_walk() { hunger++; }
const std::string &get_name() const { return name; }
int get_hunger() const { return hunger; }
private:
std::string name;
int hunger;
};
namespace py = pybind11;
PYBIND11_MODULE(example, m) {
// optional module docstring
m.doc() = "pybind11 example plugin";
// define add function
m.def("add", &add, "A function which adds two numbers");
// bindings to Pet class
py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &, int>())
.def("go_for_a_walk", &Pet::go_for_a_walk)
.def("get_hunger", &Pet::get_hunger)
.def("get_name", &Pet::get_name);
}