Skip to content

Commit

Permalink
Virtual classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Jun 12, 2023
1 parent 75d8a97 commit bf22aed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
26 changes: 12 additions & 14 deletions cppygen/cppclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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())"
Expand Down Expand Up @@ -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
26 changes: 16 additions & 10 deletions cppygen/cppygen_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "")
Expand All @@ -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
Expand Down Expand Up @@ -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()):
Expand Down Expand Up @@ -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"
)
Expand Down
12 changes: 11 additions & 1 deletion example/header_mode/shell/hoge.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,17 @@ class John : public ::Shell::Person {
}
};

template <class foo, class bar = int> struct FooBase {
/******************************************************************************
* Class Inheritence
******************************************************************************/

struct AbstFooBase {
public:
// virtual void hello() = 0;
virtual ~AbstFooBase() = default;
};

template <class foo, class bar = int> struct FooBase : public ::Shell::AbstFooBase {
public:
foo th() {
std::cout << "Foo" << std::endl;
Expand Down

0 comments on commit bf22aed

Please sign in to comment.