forked from EGheska/python-to-c-bindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
50 lines (44 loc) · 1.46 KB
/
main.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
47
48
49
50
#include <pybind11/pybind11.h>
#include <vector>
#include <pybind11/stl.h>
namespace py = pybind11;
class Calculator{
float multiplier;
public:
float sum(float param1,float param2){
return param1+param2;
}
float subs(float param1,float param2){
return param1-param2;
}
float mult(float param1, float param2){
return param1*param2;
}
float div(float param1, float param2){
return param1/param2;
}
Calculator(float multiplier_) : multiplier(multiplier_){};
float multiply(float input){
return multiplier*input;
}
std::vector<float>multiply_list(std::vector <float> items){
for(auto i = 0; i < items.size(); i++){
items[i] = multiply(items.at(i));
}
return items;
}};
// PYBIND11_MODULE is a function that is used to define the Python bindings for a C++ library.
PYBIND11_MODULE(calculator, handle){
handle.def("sum",[](float param1, float param2){return param1+param2;});
handle.def("subs",[](float param1, float param2){return param1-param2;});
handle.def("mult",[](float param1,float param2){return param1*param2;});
handle.def("div",[](float param1,float param2){return param1/param2;});
py::class_<Calculator>(
handle, "CalculatorClass"
)
.def(py::init<float>())
.def("multiply",&Calculator::multiply)
.def("multiply_list",&Calculator::multiply_list)
.def("multiply_two_nr",[](Calculator &self, float one, float two){
return py::make_tuple(self.multiply(one),self.multiply(two));
});}