Browse Source

add support for struct field type char array (fixed)

master
heck 4 years ago
parent
commit
4147f0a168
  1. 87
      pEpACIDgen/gen_backend/gen_pybind11.ysl2

87
pEpACIDgen/gen_backend/gen_pybind11.ysl2

@ -195,43 +195,66 @@ tstylesheet {
const "array_type", "substring-before($type, '[')"; const "array_type", "substring-before($type, '[')";
const "array_len", "substring-before(substring-after($type, '['),']')"; const "array_len", "substring-before(substring-after($type, '['),']')";
choose { choose {
// If is array type
when "string-length($array_len) > 0" { when "string-length($array_len) > 0" {
|| choose {
.def_property("«$name»", when "$array_type = 'char '" {
[](::«$structname» &obj) -> py::list { ||
//cout << "«$structname»::«$name»: getter called" << endl; .def_property("«$name»",
py::list l; [](::«$structname» &obj) -> py::bytes {
for(int i=0; i < «$array_len»; ++i) { // cout << "«$structname»::«$name»: getter called" << endl;
l.append(obj.«$name»[i]); return py::bytes(obj.«$name», «$array_len»);
} },
return l; [](::«$structname» &obj, py::bytes val) -> void {
}, // cout << "«$structname»::«$name»: setter called" << endl;
[](::«$structname» &obj, py::list val) -> void { std::string strval{val};
// cout << "«$structname»::«$name»: getter called" << endl; for(int i = 0; i < «$array_len»; ++i) {
if(val.size() <= 0) { obj.«$name»[i] = strval[i];
// empty array given, doing nothing (static c array cant be cleared)
} else {
int cpy_amt = std::min(static_cast<int>(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) { py::return_value_policy::copy)
cout << "«$structname»::«$name» - setter error: is not a unityped array of type «$array_type»" << std::endl; ||
} else { } otherwise {
for(int i=0; i < cpy_amt; ++i) { ||
obj.«$name»[i] = val[i].cast<«$array_type»>(); .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 {
py::return_value_policy::copy) // 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<int>(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 { } otherwise {
|| ||
.def_property("«@name»", .def_property("«@name»",

Loading…
Cancel
Save