Skip to content

Commit

Permalink
#12 Document class sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Sep 21, 2024
1 parent 3048323 commit 847912c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cppwg/input/module_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,27 @@ def is_decl_in_source_path(self, decl: "declaration_t") -> bool: # noqa: F821
def sort_classes(self) -> None:
"""Sort the class info collection in inheritance order."""

def compare(class_info_0, class_info_1):
def compare(class_info_0: "ClassInfo", class_info_1: "ClassInfo"):
# Sort classes with no declarations to the bottom
if class_info_0.decls == class_info_1.decls:
return 0
if class_info_0.decls is None:
return 1
if class_info_1.decls is None:
return -1

# Get the base classes for each class
bases_0 = [
base.related_class for decl in class_info_0.decls for base in decl.bases
]
bases_1 = [
base.related_class for decl in class_info_1.decls for base in decl.bases
]

# 1 if class_0 is a child of class_1
child_0 = int(any(base in class_info_1.decls for base in bases_0))

# 1 if class_1 is a child of class 0
child_1 = int(any(base in class_info_0.decls for base in bases_1))

return child_0 - child_1
Expand Down

0 comments on commit 847912c

Please sign in to comment.