From fee73296cae1a4984e18c8d8ee8e50ab217b1e4a Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Fri, 8 Dec 2017 08:57:56 +0200 Subject: py: add convert_vector helper Signed-off-by: Tomi Valkeinen --- py/pykms/pykmsbase.cpp | 35 +++++++++++++++-------------------- 1 file 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; using namespace kms; using namespace std; +// Helper to convert vector to vector> +template +static vector> convert_vector(const vector& source) +{ + vector> v; + for (T* p : source) + v.push_back(unique_ptr(p)); + return v; +} + void init_pykmsbase(py::module &m) { py::class_(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> v; - for (Connector* p : self->get_connectors()) - v.push_back(unique_ptr(p)); - return v; + return convert_vector(self->get_connectors()); }) .def_property_readonly("crtcs", [](Card* self) { - vector> v; - for (Crtc* p : self->get_crtcs()) - v.push_back(unique_ptr(p)); - return v; + return convert_vector(self->get_crtcs()); }) .def_property_readonly("encoders", [](Card* self) { - vector> v; - for (Encoder* p : self->get_encoders()) - v.push_back(unique_ptr(p)); - return v; + return convert_vector(self->get_encoders()); }) .def_property_readonly("planes", [](Card* self) { - vector> v; - for (Plane* p : self->get_planes()) - v.push_back(unique_ptr(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> v; - for (Crtc* p : self->get_possible_crtcs()) - v.push_back(unique_ptr(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) -- cgit v1.2.3