-
Notifications
You must be signed in to change notification settings - Fork 5
/
bindings.cpp
46 lines (37 loc) · 1.13 KB
/
bindings.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
43
44
45
46
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
// Simple function to export
int add(int x, int y) { return x + y; }
// Class with properties and a function
class Adder {
public:
Adder(int x) : x_(x){};
int add(int y) { return x_ + y; }
void setAddition(int x) { x_ = x; }
int getAddition() { return x_; }
private:
int x_;
};
// using the stl
std::string join(std::vector<std::string> tojoin) {
std::string ret;
for (auto c : tojoin) {
ret += c;
}
return ret;
}
// define a module to be imported by python
PYBIND11_MODULE(pybindings, mymodule) {
using namespace pybind11::literals; // for _a literal to define arguments
mymodule.doc() = "example module to export code";
// export the add function
mymodule.def("add", &add, "Add 2 numbers together", "x"_a, "y"_a);
// export the Adder class
pybind11::class_<Adder>(mymodule, "Adder")
.def(pybind11::init<int>())
.def("add", &Adder::add)
.def_property("addition", &Adder::getAddition, &Adder::setAddition);
// export the join method
mymodule.def("join", &join, "Join a list into a string", "tojoin"_a);
}