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" {
param "structname";
param "name", "@name";
param "type", "@type";
const "is_fptr" call "type_is_fptr" with "type", "$type";
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" {
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];
}
},
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 "$is_fptr != 'true'" { // function pointers not supported, currently
choose {
// If is array type
when "string-length($array_len) > 0" {
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;
},
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) {
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
} otherwise {
||
.def_property("«@name»",
[](::«$structname» &obj) -> «$type» {
//cout << "«$structname» ::«@name»: getter called" << endl;
return obj.«@name»;
},
||
choose {
when "$type = 'const char *'" {
||
[](::«$structname» &obj, «$type» cstr) -> void {
//cout << "«$structname»::«@name»: setter called" << cstr << endl;
obj.«@name» = strdup(cstr);
},
py::return_value_policy::copy)
||
} otherwise {
||
[](::«$structname» &obj, «$type» val) -> void {
//cout << "«$structname»::«@name»: setter called" << endl;
obj.«@name» = val;
},
py::return_value_policy::copy)
||
// not array type
} otherwise {
||
.def_property("«@name»",
[](::«$structname» &obj) -> «$type» {
//cout << "«$structname» ::«@name»: getter called" << endl;
return obj.«@name»;
},
||
choose {
when "$type = 'const char *'" {
||
[](::«$structname» &obj, «$type» cstr) -> void {
//cout << "«$structname»::«@name»: setter called" << cstr << endl;
obj.«@name» = strdup(cstr);
},
py::return_value_policy::copy)
||
} otherwise {
||
[](::«$structname» &obj, «$type» val) -> void {
//cout << "«$structname»::«@name»: setter called" << endl;
obj.«@name» = val;
},
py::return_value_policy::copy)
||
}
}
}
}
}
}
template "enum" {

Loading…
Cancel
Save