Skip to content

Commit

Permalink
#12 Sort classes in inheritance order
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Sep 21, 2024
1 parent 19b0bb7 commit 3048323
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ exclude=
examples,
cppwg/templates,
tests,
venv,
docstring-convention=numpy
3 changes: 3 additions & 0 deletions cppwg/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ def add_class_decls(self) -> None:

class_info.decls.append(class_decl)

# Sort the class info collection in inheritance order
module_info.sort_classes()

def add_discovered_free_functions(self) -> None:
"""
Add discovered free function.
Expand Down
27 changes: 27 additions & 0 deletions cppwg/input/module_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Module information structure."""

import os
from functools import cmp_to_key
from typing import Any, Dict, List, Optional

from cppwg.input.base_info import BaseInfo
Expand Down Expand Up @@ -75,3 +76,29 @@ def is_decl_in_source_path(self, decl: "declaration_t") -> bool: # noqa: F821
return True

return False

def sort_classes(self) -> None:
"""Sort the class info collection in inheritance order."""

def compare(class_info_0, class_info_1):
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

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
]

child_0 = int(any(base in class_info_1.decls for base in bases_0))
child_1 = int(any(base in class_info_0.decls for base in bases_1))

return child_0 - child_1

self.class_info_collection.sort(key=lambda x: x.name)
self.class_info_collection.sort(key=cmp_to_key(compare))
4 changes: 2 additions & 2 deletions examples/shapes/wrapper/package_info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ modules:
- name: primitives
source_locations:
classes:
- name: Shape
- name: Cuboid
- name: Rectangle
- name: Shape
- name: Triangle
excluded: True # Exclude this class from wrapping.

- name: mesh
source_locations:
classes:
- name: AbstractMesh
- name: ConcreteMesh
- name: AbstractMesh

# Text to add at the top of all wrappers
prefix_text: |
Expand Down
8 changes: 4 additions & 4 deletions examples/shapes/wrapper/wrapper_header_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ template class Point<2>;
template class Point<3>;
template class Shape<2>;
template class Shape<3>;
template class AbstractMesh<2,2>;
template class AbstractMesh<3,3>;
template class ConcreteMesh<2>;
template class ConcreteMesh<3>;
template class AbstractMesh<2,2>;
template class AbstractMesh<3,3>;

// Typedefs for nicer naming
namespace cppwg
Expand All @@ -33,10 +33,10 @@ typedef Point<2> Point2;
typedef Point<3> Point3;
typedef Shape<2> Shape2;
typedef Shape<3> Shape3;
typedef AbstractMesh<2,2> AbstractMesh2_2;
typedef AbstractMesh<3,3> AbstractMesh3_3;
typedef ConcreteMesh<2> ConcreteMesh2;
typedef ConcreteMesh<3> ConcreteMesh3;
typedef AbstractMesh<2,2> AbstractMesh2_2;
typedef AbstractMesh<3,3> AbstractMesh3_3;
} // namespace cppwg

#endif // pyshapes_HEADERS_HPP_
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ authors = [
license = { file = "LICENSE" }
keywords = ["C++", "Python", "Pybind11"]
readme = "README.md"
version = "0.0.1-alpha"
version = "0.2.1"

classifiers = [
"Development Status :: 3 - Alpha",
"Development Status :: 4 - Beta",
"Environment :: Console",
"Intended Audience :: Developers",
"Operating System :: MacOS :: MacOS X",
Expand Down

0 comments on commit 3048323

Please sign in to comment.