-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
106 lines (92 loc) · 2.74 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <chrono>
#include <thread>
#include <iostream>
namespace py = pybind11;
float some_fn(float arg1, float arg2)
{
return arg1 + arg2;
}
class SomeClass
{
float multiplier;
public:
SomeClass(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;
}
// py::tuple multiply_two(float one, float two) {
// return py::make_tuple(multiply(one), multiply(two));
// }
std::vector<std::vector<uint8_t>> make_image()
{
auto out = std::vector<std::vector<uint8_t>>();
for (auto i = 0; i < 128; i++)
{
out.push_back(std::vector<uint8_t>(64));
}
for (auto i = 0; i < 30; i++)
{
for (auto j = 0; j < 30; j++)
{
out[i][j] = 255;
}
}
return out;
}
void set_mult(float val)
{
multiplier = val;
}
float get_mult()
{
return multiplier;
}
void function_that_takes_a_while()
{
py::gil_scoped_release release;
std::cout << "starting" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout << "ended" << std::endl;
// py::gil_scoped_acquire acquire;
// auto list = py::list();
// list.append(1);
}
};
SomeClass some_class_factory(float multiplier)
{
return SomeClass(multiplier);
}
PYBIND11_MODULE(module_name, module_handle)
{
module_handle.doc() = "I'm a docstring";
module_handle.def("some_fn_python_name", &some_fn);
module_handle.def("some_class_factory", &some_class_factory);
py::class_<SomeClass>(
module_handle, "PySomeClass")
.def(py::init<float>())
.def_property("multiplier", &SomeClass::get_mult, &SomeClass::set_mult)
.def("multiply", &SomeClass::multiply)
.def("multiply_list", &SomeClass::multiply_list)
// .def_property_readonly("image", &SomeClass::make_image)
.def_property_readonly("image", [](SomeClass &self)
{
py::array out = py::cast(self.make_image());
return out; })
// .def("multiply_two", &SomeClass::multiply_two)
.def("multiply_two", [](SomeClass &self, float one, float two)
{ return py::make_tuple(self.multiply(one), self.multiply(two)); })
.def("function_that_takes_a_while", &SomeClass::function_that_takes_a_while);
}