Browse Source

pyBind11-gen: use lambda wrap for methods and struct members, to be able to handle type specificly. (const char * for now)

master
heck 5 years ago
parent
commit
1084be80ca
  1. 12
      pEpACIDgen/acid_yml.py
  2. 77
      pEpACIDgen/gen_backend/gen_pybind11.ysl2

12
pEpACIDgen/acid_yml.py

@ -19,6 +19,7 @@ def generate_acid_yml(acid, module_name):
def _grammar_header(): def _grammar_header():
grammar = """decl module @name; grammar = """decl module @name;
decl method @name; decl method @name;
decl use @name;
decl struct @name; decl struct @name;
decl field @name; decl field @name;
@ -35,7 +36,7 @@ def _generate_functions(acid):
# Main # Main
tmpl_main = 'method {name} {{\n' tmpl_main = 'method {name} {{\n'
tmpl_main += ' signature string="{sig}";\n' # tmpl_main += ' signature string="{sig}";\n'
tmpl_main += ' return type="{return_type}";\n' tmpl_main += ' return type="{return_type}";\n'
tmpl_main += '{subitems}' tmpl_main += '{subitems}'
tmpl_main += '}};\n\n' tmpl_main += '}};\n\n'
@ -61,12 +62,17 @@ def _generate_functions(acid):
for subitem in subitems: for subitem in subitems:
subitems_str += tmpl_sub.format(type=subitem["type"], name=subitem["name"]) + "\n" subitems_str += tmpl_sub.format(type=subitem["type"], name=subitem["name"]) + "\n"
# (PEP_STATUS(*)(void*,const char*,const char*,const char*,const char*,const char*)) #e.g: (PEP_STATUS(*)(void*,const char*,const char*,const char*,const char*,const char*))
sig_ret = item["result_type"] + "(*)" sig_ret = item["result_type"] + "(*)"
sig_parms = "" sig_parms = ""
for subitem in subitems: for subitem in subitems:
sig_parms += subitem["type"] + ", " # TODO: utter PEP-HACK!!!! FIXME!
type = subitem["type"]
if type == "PEP_SESSION":
type = "void *"
sig_parms += type + ", "
sig_parms = sig_parms[:-2] sig_parms = sig_parms[:-2]

77
pEpACIDgen/gen_backend/gen_pybind11.ysl2

@ -15,27 +15,84 @@ tstylesheet {
template "method" { template "method" {
|| ||
m.def("«@name»",`apply "signature"` &«@name», "");
m.def("«@name»",
[](`apply "use", mode="lambda_sig"`) -> `apply return` {
return ::«@name»(
``apply "use", mode="arg_list"
);
},
pybind11::return_value_policy::copy);
|| ||
}
template "return" {
> «@type»
}
template "use", mode="arg_list" {
const "name", "@name";
const "type", "@type";
choose {
when "$type = 'const char *'" {
|> strdup(«$name»)`if "position()!=last()" > ,`
} otherwise {
|> «$name»`if "position()!=last()" > ,`
}
}
}
template "use", mode="lambda_sig" {
const "name", "@name";
const "type", "@type";
> «$type» «$name»`if "position()!=last()"> , `
} }
template "signature" { template "signature" {
> «@string» const "string", "@string";
} > «$string»
}
template "struct" { template "struct" {
|| | pybind11::class_<«@name»>(m, "«@name»")
pybind11::class_<«@name»>(m, "«@name»") apply "field" {
``apply "field" with "structname", "@name"
; }
| ;
|| |
|
} }
template "field" { template "field" {
param "structname";
|| ||
.def_readwrite("«@name»", &::«../@name»::«@name») .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);
},
pybind11::return_value_policy::copy)
||
} otherwise {
||
[](::«$structname» &obj, «@type» val) -> void {
//cout << "«$structname»::«@name»: setter called" << endl;
obj.«@name» = val;
},
pybind11::return_value_policy::copy)
||
}
}
} }
template "enum" { template "enum" {

Loading…
Cancel
Save