py: add convert_vector helper
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Fri, 8 Dec 2017 06:57:56 +0000 (08:57 +0200)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Fri, 8 Dec 2017 06:57:59 +0000 (08:57 +0200)
Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ti.com>
py/pykms/pykmsbase.cpp

index dd09fae6c6243eef25cca9084aefd0c8f603a8d0..2c97bd7161ddbb8ec8f38ce3e7d30ca7040ebdd2 100644 (file)
@@ -7,6 +7,16 @@ namespace py = pybind11;
 using namespace kms;
 using namespace std;
 
+// Helper to convert vector<T*> to vector<unique_ptr<T, py::nodelete>>
+template<typename T>
+static vector<unique_ptr<T, py::nodelete>> convert_vector(const vector<T*>& source)
+{
+       vector<unique_ptr<T, py::nodelete>> v;
+       for (T* p : source)
+               v.push_back(unique_ptr<T, py::nodelete>(p));
+       return v;
+}
+
 void init_pykmsbase(py::module &m)
 {
        py::class_<Card>(m, "Card")
@@ -18,31 +28,19 @@ void init_pykmsbase(py::module &m)
                        // RuntimeError: return_value_policy = move, but the object is neither movable nor copyable!
                        // So we do this manually.
                        .def_property_readonly("connectors", [](Card* self) {
-                               vector<unique_ptr<Connector, py::nodelete>> v;
-                               for (Connector* p : self->get_connectors())
-                                       v.push_back(unique_ptr<Connector, py::nodelete>(p));
-                               return v;
+                               return convert_vector(self->get_connectors());
                        })
 
                        .def_property_readonly("crtcs", [](Card* self) {
-                               vector<unique_ptr<Crtc, py::nodelete>> v;
-                               for (Crtc* p : self->get_crtcs())
-                                       v.push_back(unique_ptr<Crtc, py::nodelete>(p));
-                               return v;
+                               return convert_vector(self->get_crtcs());
                        })
 
                        .def_property_readonly("encoders", [](Card* self) {
-                               vector<unique_ptr<Encoder, py::nodelete>> v;
-                               for (Encoder* p : self->get_encoders())
-                                       v.push_back(unique_ptr<Encoder, py::nodelete>(p));
-                               return v;
+                               return convert_vector(self->get_encoders());
                        })
 
                        .def_property_readonly("planes", [](Card* self) {
-                               vector<unique_ptr<Plane, py::nodelete>> v;
-                               for (Plane* p : self->get_planes())
-                                       v.push_back(unique_ptr<Plane, py::nodelete>(p));
-                               return v;
+                               return convert_vector(self->get_planes());
                        })
 
                        .def_property_readonly("has_atomic", &Card::has_atomic)
@@ -69,10 +67,7 @@ void init_pykmsbase(py::module &m)
                        .def("get_default_mode", &Connector::get_default_mode)
                        .def("get_current_crtc", &Connector::get_current_crtc)
                        .def("get_possible_crtcs", [](Connector* self) {
-                               vector<unique_ptr<Crtc, py::nodelete>> v;
-                               for (Crtc* p : self->get_possible_crtcs())
-                                       v.push_back(unique_ptr<Crtc, py::nodelete>(p));
-                               return v;
+                               return convert_vector(self->get_possible_crtcs());
                        })
                        .def("get_modes", &Connector::get_modes)
                        .def("get_mode", (Videomode (Connector::*)(const string& mode) const)&Connector::get_mode)