diff --git a/cppygen/cppclass.py b/cppygen/cppclass.py index b4b7a34..5185fde 100644 --- a/cppygen/cppclass.py +++ b/cppygen/cppclass.py @@ -16,17 +16,17 @@ class MemberFunctionSignature(TypedDict): description: str call_guards: list[str] - def __init__(self, is_template=False, defined_template_classes=[]): + def __init__(self, is_template=False): self._name: str | None = None self._sanitized_name: str | None = None - self._base_classes: list[CppClass] = [] + self._base_classes: list[str] = [] self._namespace: list[str] = [] self._members: list[dict[str, str]] = [] self._member_funcs: list[CppClass.MemberFunctionSignature] = [] self._module: str | None = None self._description = "" self._is_template = is_template - self._defined_template_classes: list[CppClass] = defined_template_classes + # self._defined_template_classes: list[CppClass] = defined_template_classes self._template_parameter: list[tuple[str, str | None]] = [] def set_name(self, name: str, namespace: list[str] | None = None): @@ -53,11 +53,7 @@ def add_member( ) def add_base_class(self, name: str): - for i in self._defined_template_classes: - if i._full_name in name: - base_class = copy.deepcopy(i) - base_class.set_name(name.split("::")[-1]) - self._base_classes.append(base_class) + self._base_classes.append(name) def add_member_func( self, @@ -99,13 +95,9 @@ def to_pybind_string(self): print("Parse Error Skipping ...") return "" return ( - # BaseClass - "\n".join([i.to_pybind_string() for i in self._base_classes]) + "\n" # Class - + f"pybind11::class_<" - + ", ".join( - [f"::{self._full_name}", *[i._full_name for i in self._base_classes]] - ) + f"pybind11::class_<" + + ", ".join([f"::{self._full_name}", *self._base_classes]) + ">" + f'({self._module}, "{self._sanitized_name}")\n' "\t\t.def(pybind11::init())" @@ -140,3 +132,9 @@ def to_pybind_string(self): def signature(self) -> str: return f"{self._full_name}" + + def __eq__(self, obj): + if isinstance(obj, CppClass): + return self._full_name == obj._full_name + else: + return False diff --git a/cppygen/cppygen_parser.py b/cppygen/cppygen_parser.py index 7d8eee1..4f2c374 100644 --- a/cppygen/cppygen_parser.py +++ b/cppygen/cppygen_parser.py @@ -35,6 +35,8 @@ def __init__( self._functions: list[Function] = [] self._submodules: list[Submodule] = [] self._cpp_classes: list[CppClass] = [] + self._export_classes: list[CppClass] = [] + self._cpp_template_classes: list[CppClass] = [] self._hpp_includes: list[str] = [] self._namespace = namespace or "cppygen" self._verbose = verbose @@ -113,10 +115,7 @@ def _extract_struct_and_class( ): def visit(i: Cursor, namespace: list[str], is_template): print(i.kind) - cpp_class = CppClass( - is_template, - copy.deepcopy([j for j in self._cpp_classes if j._is_template]), - ) + cpp_class = CppClass(is_template) cpp_class.set_name(i.spelling, namespace) cpp_class.set_module(module_name) cpp_class.set_description(i.brief_comment or "") @@ -132,6 +131,16 @@ def visit(i: Cursor, namespace: list[str], is_template): + j.spelling ) cpp_class.add_base_class(j.spelling) + teplate_declares = copy.deepcopy( + [j for j in self._cpp_classes if j._is_template] + ) + + for k in teplate_declares: + if k._full_name in j.spelling: + base_class = copy.deepcopy(k) + base_class.set_name(j.spelling.split("::")[-1]) + if base_class not in self._export_classes: + self._export_classes.append(base_class) if j.kind == CursorKind.STRUCT_DECL or j.kind == CursorKind.CLASS_DECL: # type: ignore visit(j, [*namespace, i.spelling], False) if j.kind == CursorKind.CLASS_TEMPLATE: # type: ignore @@ -181,6 +190,8 @@ def visit(i: Cursor, namespace: list[str], is_template): + j.result_type.spelling ) self._cpp_classes.append(cpp_class) + if not cpp_class._is_template: + self._export_classes.append(cpp_class) i: Cursor for i in list(cu.get_children()): @@ -293,12 +304,7 @@ def to_export_string(self): + "\t/* Function Export End */\n\n" + "\t/* Structs and Classes Export Start */\n" + "\n".join( - [ - "\t" + i.to_pybind_string() - for i in self._cpp_classes - if not i._is_template - ] - + [""] + ["\t" + i.to_pybind_string() for i in self._export_classes] + [""] ) + "\t/* Structs and Classes Export End */\n\n" ) diff --git a/example/header_mode/shell/hoge.hpp b/example/header_mode/shell/hoge.hpp index 144aefe..e6882ea 100644 --- a/example/header_mode/shell/hoge.hpp +++ b/example/header_mode/shell/hoge.hpp @@ -53,7 +53,17 @@ class John : public ::Shell::Person { } }; -template struct FooBase { +/****************************************************************************** + * Class Inheritence + ******************************************************************************/ + +struct AbstFooBase { +public: + // virtual void hello() = 0; + virtual ~AbstFooBase() = default; +}; + +template struct FooBase : public ::Shell::AbstFooBase { public: foo th() { std::cout << "Foo" << std::endl;