-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
42 lines (32 loc) · 1.11 KB
/
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
41
42
#include <pybind11/pybind11.h>
namespace py = pybind11;
std::string name = "John Wick";
int add(int i, int j) { return i + j; }
class ExampleClass {
float multiplier;
public:
explicit ExampleClass(float multiplier_) : multiplier(multiplier_) {}
[[nodiscard]] float multiply(float input = 2) const {
return input * multiplier;
}
};
py::tuple multiply_two(float one, float two) {
return py::make_tuple(one * 2, two * 2);
}
PYBIND11_MODULE(example, handler) {
// export doc
handler.doc() =
"Homemade pybind11 module implemented using C++ (of course) 🙃";
// export variable
handler.attr("name") = name;
// export function
handler.def("add", &add, "A function that adds two numbers");
handler.def("multiply_two", &multiply_two,
"A function that creates a tuple from the given inputs");
// export class
py::class_<ExampleClass>(handler, "ExampleClass")
.def(py::init<float>()) // export constructor
.def("multiply", &ExampleClass::multiply,
"A member function that multiple the given number",
py::arg("input") = 2); // export function member
}