Browse Source

handle structs containing function pointers (not supported, currently)

master
heck 4 years ago
parent
commit
02df91e8b8
  1. 171
      pEpACIDgen/gen_backend/gen_pybind11.ysl2

171
pEpACIDgen/gen_backend/gen_pybind11.ysl2

@ -187,104 +187,119 @@ tstylesheet {
| |
} }
function "type_is_fptr" {
param "type";
choose {
when "contains($type,'(')" {
> true
} otherwise {
> false
}
}
}
template "field" { template "field" {
param "structname"; param "structname";
param "name", "@name"; param "name", "@name";
param "type", "@type"; param "type", "@type";
const "is_fptr" call "type_is_fptr" with "type", "$type";
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 { if "$is_fptr != 'true'" { // function pointers not supported, currently
// If is array type choose {
when "string-length($array_len) > 0" { // If is array type
choose { when "string-length($array_len) > 0" {
when "$array_type = 'char '" { choose {
|| when "$array_type = 'char '" {
.def_property("«$name»", ||
[](::«$structname» &obj) -> py::bytes { .def_property("«$name»",
// cout << "«$structname»::«$name»: getter called" << endl; [](::«$structname» &obj) -> py::bytes {
return py::bytes(obj.«$name», «$array_len»); // 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; [](::«$structname» &obj, py::bytes val) -> void {
std::string strval{val}; // cout << "«$structname»::«$name»: setter called" << endl;
for(int i = 0; i < «$array_len»; ++i) { std::string strval{val};
obj.«$name»[i] = strval[i]; for(int i = 0; i < «$array_len»; ++i) {
} obj.«$name»[i] = strval[i];
},
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]);
}
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<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; 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]);
}
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 { } else {
int cpy_amt = std::min(static_cast<int>(val.size()), «$array_len»);
bool isUnitypedArrayOf = true;
for(int i=0; i < cpy_amt; ++i) { for(int i=0; i < cpy_amt; ++i) {
obj.«$name»[i] = val[i].cast<«$array_type»>(); 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)
py::return_value_policy::copy)
|| ||
}
} }
} // not array type
// not array type } otherwise {
} otherwise { ||
|| .def_property("«@name»",
.def_property("«@name»", [](::«$structname» &obj) -> «$type» {
[](::«$structname» &obj) -> «$type» { //cout << "«$structname» ::«@name»: getter called" << endl;
//cout << "«$structname» ::«@name»: getter called" << endl; return obj.«@name»;
return obj.«@name»; },
}, ||
|| choose {
choose { when "$type = 'const char *'" {
when "$type = 'const char *'" { ||
|| [](::«$structname» &obj, «$type» cstr) -> void {
[](::«$structname» &obj, «$type» cstr) -> void { //cout << "«$structname»::«@name»: setter called" << cstr << endl;
//cout << "«$structname»::«@name»: setter called" << cstr << endl; obj.«@name» = strdup(cstr);
obj.«@name» = strdup(cstr); },
}, py::return_value_policy::copy)
py::return_value_policy::copy) ||
|| } otherwise {
} otherwise { ||
|| [](::«$structname» &obj, «$type» val) -> void {
[](::«$structname» &obj, «$type» val) -> void { //cout << "«$structname»::«@name»: setter called" << endl;
//cout << "«$structname»::«@name»: setter called" << endl; obj.«@name» = val;
obj.«@name» = val; },
}, py::return_value_policy::copy)
py::return_value_policy::copy)
||
|| }
} }
} }
} }
} }
} }
template "enum" { template "enum" {

Loading…
Cancel
Save