aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomi Valkeinen2017-12-08 00:57:56 -0600
committerTomi Valkeinen2017-12-08 00:57:59 -0600
commitfee73296cae1a4984e18c8d8ee8e50ab217b1e4a (patch)
tree72327b09477b707c22e8ec85f51faf8275f1fb61
parent260f66b0863a77a7d6c3682afaa67fe2b9bb24b6 (diff)
downloadexternal-kmsxx-fee73296cae1a4984e18c8d8ee8e50ab217b1e4a.tar.gz
external-kmsxx-fee73296cae1a4984e18c8d8ee8e50ab217b1e4a.tar.xz
external-kmsxx-fee73296cae1a4984e18c8d8ee8e50ab217b1e4a.zip
py: add convert_vector helper
Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ti.com>
-rw-r--r--py/pykms/pykmsbase.cpp35
1 files changed, 15 insertions, 20 deletions
diff --git a/py/pykms/pykmsbase.cpp b/py/pykms/pykmsbase.cpp
index dd09fae..2c97bd7 100644
--- a/py/pykms/pykmsbase.cpp
+++ b/py/pykms/pykmsbase.cpp
@@ -7,6 +7,16 @@ namespace py = pybind11;
7using namespace kms; 7using namespace kms;
8using namespace std; 8using namespace std;
9 9
10// Helper to convert vector<T*> to vector<unique_ptr<T, py::nodelete>>
11template<typename T>
12static vector<unique_ptr<T, py::nodelete>> convert_vector(const vector<T*>& source)
13{
14 vector<unique_ptr<T, py::nodelete>> v;
15 for (T* p : source)
16 v.push_back(unique_ptr<T, py::nodelete>(p));
17 return v;
18}
19
10void init_pykmsbase(py::module &m) 20void init_pykmsbase(py::module &m)
11{ 21{
12 py::class_<Card>(m, "Card") 22 py::class_<Card>(m, "Card")
@@ -18,31 +28,19 @@ void init_pykmsbase(py::module &m)
18 // RuntimeError: return_value_policy = move, but the object is neither movable nor copyable! 28 // RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
19 // So we do this manually. 29 // So we do this manually.
20 .def_property_readonly("connectors", [](Card* self) { 30 .def_property_readonly("connectors", [](Card* self) {
21 vector<unique_ptr<Connector, py::nodelete>> v; 31 return convert_vector(self->get_connectors());
22 for (Connector* p : self->get_connectors())
23 v.push_back(unique_ptr<Connector, py::nodelete>(p));
24 return v;
25 }) 32 })
26 33
27 .def_property_readonly("crtcs", [](Card* self) { 34 .def_property_readonly("crtcs", [](Card* self) {
28 vector<unique_ptr<Crtc, py::nodelete>> v; 35 return convert_vector(self->get_crtcs());
29 for (Crtc* p : self->get_crtcs())
30 v.push_back(unique_ptr<Crtc, py::nodelete>(p));
31 return v;
32 }) 36 })
33 37
34 .def_property_readonly("encoders", [](Card* self) { 38 .def_property_readonly("encoders", [](Card* self) {
35 vector<unique_ptr<Encoder, py::nodelete>> v; 39 return convert_vector(self->get_encoders());
36 for (Encoder* p : self->get_encoders())
37 v.push_back(unique_ptr<Encoder, py::nodelete>(p));
38 return v;
39 }) 40 })
40 41
41 .def_property_readonly("planes", [](Card* self) { 42 .def_property_readonly("planes", [](Card* self) {
42 vector<unique_ptr<Plane, py::nodelete>> v; 43 return convert_vector(self->get_planes());
43 for (Plane* p : self->get_planes())
44 v.push_back(unique_ptr<Plane, py::nodelete>(p));
45 return v;
46 }) 44 })
47 45
48 .def_property_readonly("has_atomic", &Card::has_atomic) 46 .def_property_readonly("has_atomic", &Card::has_atomic)
@@ -69,10 +67,7 @@ void init_pykmsbase(py::module &m)
69 .def("get_default_mode", &Connector::get_default_mode) 67 .def("get_default_mode", &Connector::get_default_mode)
70 .def("get_current_crtc", &Connector::get_current_crtc) 68 .def("get_current_crtc", &Connector::get_current_crtc)
71 .def("get_possible_crtcs", [](Connector* self) { 69 .def("get_possible_crtcs", [](Connector* self) {
72 vector<unique_ptr<Crtc, py::nodelete>> v; 70 return convert_vector(self->get_possible_crtcs());
73 for (Crtc* p : self->get_possible_crtcs())
74 v.push_back(unique_ptr<Crtc, py::nodelete>(p));
75 return v;
76 }) 71 })
77 .def("get_modes", &Connector::get_modes) 72 .def("get_modes", &Connector::get_modes)
78 .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode) 73 .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)