-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (40 loc) · 1.04 KB
/
main.cpp
File metadata and controls
53 lines (40 loc) · 1.04 KB
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
49
50
51
52
53
#include <iostream>
#include <pybind11/functional.h>
#include "pybind11/pybind11.h"
int add(int i, int j) {
return i + j;
}
void inadd() {
int a, b;
std::cin >> a >> b;
std::cout << a + b;
}
void call_function(const std::function<std::string(int)> &func) {
std::string result = func(10);
std::cout << result << std::endl;
}
class TestClass {
public:
TestClass() {
std::cout << "construct" << std::endl;
}
~TestClass() {
std::cout << "destroy" << std::endl;
}
void setName(const std::string &name) { name_ = name; }
const std::string &getName() const { return name_; }
private:
int a_;
std::string name_ = "init name";
};
namespace py = pybind11;
PYBIND11_MODULE(TestPybind11, m) {
m.doc() = "pybind11 example plugin";
m.def("add", &add, "A function which adds two numbers");
m.def("inadd", &inadd, "cin and count");
m.def("call_function", &call_function);
py::class_<TestClass>(m, "TestClass")
.def(py::init<>())
.def("setName", &TestClass::setName)
.def("getName", &TestClass::getName);
}