From 4147f0a1687808b885825b9f7b96fb20e8623043 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 9 Feb 2021 00:32:59 +0100 Subject: [PATCH] add support for struct field type char array (fixed) --- pEpACIDgen/gen_backend/gen_pybind11.ysl2 | 87 +++++++++++++++--------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/pEpACIDgen/gen_backend/gen_pybind11.ysl2 b/pEpACIDgen/gen_backend/gen_pybind11.ysl2 index a5f77e5..bc86d48 100644 --- a/pEpACIDgen/gen_backend/gen_pybind11.ysl2 +++ b/pEpACIDgen/gen_backend/gen_pybind11.ysl2 @@ -195,43 +195,66 @@ tstylesheet { const "array_type", "substring-before($type, '[')"; const "array_len", "substring-before(substring-after($type, '['),']')"; + choose { + // If is array type when "string-length($array_len) > 0" { - || - .def_property("«$name»", - [](::«$structname» &obj) -> py::list { - //cout << "«$structname»::«$name»: getter called" << endl; - py::list l; - for(int i=0; i < «$array_len»; ++i) { - l.append(obj.«$name»[i]); - } - return l; - }, - [](::«$structname» &obj, py::list val) -> void { - // cout << "«$structname»::«$name»: getter called" << endl; - if(val.size() <= 0) { - // empty array given, doing nothing (static c array cant be cleared) - } else { - int cpy_amt = std::min(static_cast(val.size()), «$array_len»); - bool isUnitypedArrayOf = true; - for(int i=0; i < cpy_amt; ++i) { - if (!py::isinstance<«$array_type»>(val[i])) { - isUnitypedArrayOf = false; - break; + choose { + when "$array_type = 'char '" { + || + .def_property("«$name»", + [](::«$structname» &obj) -> py::bytes { + // cout << "«$structname»::«$name»: getter called" << endl; + return py::bytes(obj.«$name», «$array_len»); + }, + [](::«$structname» &obj, py::bytes val) -> void { + // cout << "«$structname»::«$name»: setter called" << endl; + std::string strval{val}; + for(int i = 0; i < «$array_len»; ++i) { + obj.«$name»[i] = strval[i]; } - } - if(!isUnitypedArrayOf) { - cout << "«$structname»::«$name» - setter error: is not a unityped array of type «$array_type»" << std::endl; - } else { - for(int i=0; i < cpy_amt; ++i) { - obj.«$name»[i] = val[i].cast<«$array_type»>(); + }, + py::return_value_policy::copy) + || + } otherwise { + || + .def_property("«$name»", + [](::«$structname» &obj) -> py::list { + //cout << "«$structname»::«$name»: getter called" << endl; + py::list l; + for(int i=0; i < «$array_len»; ++i) { + l.append(obj.«$name»[i]); } - } - } - }, - py::return_value_policy::copy) + return l; + }, + [](::«$structname» &obj, py::list val) -> void { + // cout << "«$structname»::«$name»: setter called" << endl; + if(val.size() <= 0) { + // empty array given, doing nothing (static c array cant be cleared) + } else { + int cpy_amt = std::min(static_cast(val.size()), «$array_len»); + bool isUnitypedArrayOf = true; + for(int i=0; i < cpy_amt; ++i) { + if (!py::isinstance<«$array_type»>(val[i])) { + isUnitypedArrayOf = false; + break; + } + } + if(!isUnitypedArrayOf) { + cout << "«$structname»::«$name» - setter error: is not a unityped array of type «$array_type»" << std::endl; + } else { + for(int i=0; i < cpy_amt; ++i) { + obj.«$name»[i] = val[i].cast<«$array_type»>(); + } + } + } + }, + py::return_value_policy::copy) - || + || + } + } + // not array type } otherwise { || .def_property("«@name»",