Generate enum bindings automatically

This commit is contained in:
Hamdi Sahloul
2018-09-28 15:39:51 +09:00
parent 29e88e50ff
commit e104fcc000
8 changed files with 31 additions and 68 deletions

View File

@@ -1669,6 +1669,7 @@ static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name")
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
#include "pyopencv_generated_enums.h"
#include "pyopencv_custom_headers.h"
#include "pyopencv_generated_types.h"
#include "pyopencv_generated_funcs.h"

View File

@@ -688,7 +688,7 @@ class FuncInfo(object):
defval0 = "0"
tp1 = tp.replace("*", "_ptr")
tp_candidates = [a.tp, normalize_class_name(self.namespace + "." + a.tp)]
if any(tp in codegen.enumTypes for tp in tp_candidates):
if any(tp in codegen.enums.keys() for tp in tp_candidates):
defval0 = "static_cast<%s>(%d)" % (a.tp, 0)
amapping = simple_argtype_mapping.get(tp, (tp, "O", defval0))
@@ -863,8 +863,9 @@ class PythonWrapperGenerator(object):
self.classes = {}
self.namespaces = {}
self.consts = {}
self.enumTypes = []
self.enums = {}
self.code_include = StringIO()
self.code_enums = StringIO()
self.code_types = StringIO()
self.code_funcs = StringIO()
self.code_type_reg = StringIO()
@@ -922,11 +923,11 @@ class PythonWrapperGenerator(object):
#print(cname + ' => ' + str(py_name) + ' (value=' + value + ')')
def add_enum(self, name, decl):
enumType = normalize_class_name(name)
if enumType.endswith("<unnamed>"):
enumType = None
wname = normalize_class_name(name)
if wname.endswith("<unnamed>"):
wname = None
else:
self.enumTypes.append(enumType)
self.enums[wname] = name
const_decls = decl[3]
for decl in const_decls:
@@ -1017,6 +1018,21 @@ class PythonWrapperGenerator(object):
self.code_ns_reg.write(' init_submodule(root, MODULESTR"%s", methods_%s, consts_%s);\n' % (ns_name[2:], wname, wname))
self.code_ns_reg.write('};\n')
def gen_enum_reg(self, enum_name):
name_seg = enum_name.split(".")
is_enum_class = False
if len(name_seg) >= 2 and name_seg[-1] == name_seg[-2]:
enum_name = ".".join(name_seg[:-1])
is_enum_class = True
wname = normalize_class_name(enum_name)
cname = enum_name.replace(".", "::")
code = ""
if re.sub(r"^cv\.", "", enum_name) != wname:
code += "typedef {0} {1};\n".format(cname, wname)
code += "CV_PY_FROM_ENUM({0});\nCV_PY_TO_ENUM({0});\n\n".format(wname)
self.code_enums.write(code)
def save(self, path, name, buf):
with open(path + "/" + name, "wt") as f:
@@ -1131,7 +1147,13 @@ class PythonWrapperGenerator(object):
self.gen_namespace(ns_name)
self.gen_namespaces_reg()
# step 4: generate the code for constants
# step 4: generate the code for enum types
enumlist = list(self.enums.values())
enumlist.sort()
for name in enumlist:
self.gen_enum_reg(name)
# step 5: generate the code for constants
constlist = list(self.consts.items())
constlist.sort()
for name, constinfo in constlist:
@@ -1140,6 +1162,7 @@ class PythonWrapperGenerator(object):
# That's it. Now save all the files
self.save(output_path, "pyopencv_generated_include.h", self.code_include)
self.save(output_path, "pyopencv_generated_funcs.h", self.code_funcs)
self.save(output_path, "pyopencv_generated_enums.h", self.code_enums)
self.save(output_path, "pyopencv_generated_types.h", self.code_types)
self.save(output_path, "pyopencv_generated_type_reg.h", self.code_type_reg)
self.save(output_path, "pyopencv_generated_ns_reg.h", self.code_ns_reg)