From 3048323f235a6d9879c256d1338b1d92bf25cdd2 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Sat, 21 Sep 2024 13:57:52 +0000 Subject: [PATCH] #12 Sort classes in inheritance order --- .flake8 | 1 + cppwg/generators.py | 3 +++ cppwg/input/module_info.py | 27 +++++++++++++++++++ examples/shapes/wrapper/package_info.yaml | 4 +-- .../wrapper/wrapper_header_collection.hpp | 8 +++--- pyproject.toml | 4 +-- 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/.flake8 b/.flake8 index a298ec3..21f7699 100644 --- a/.flake8 +++ b/.flake8 @@ -8,4 +8,5 @@ exclude= examples, cppwg/templates, tests, + venv, docstring-convention=numpy diff --git a/cppwg/generators.py b/cppwg/generators.py index 72c3044..f1b4191 100644 --- a/cppwg/generators.py +++ b/cppwg/generators.py @@ -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. diff --git a/cppwg/input/module_info.py b/cppwg/input/module_info.py index ac2c792..f21831f 100644 --- a/cppwg/input/module_info.py +++ b/cppwg/input/module_info.py @@ -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 @@ -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)) diff --git a/examples/shapes/wrapper/package_info.yaml b/examples/shapes/wrapper/package_info.yaml index 0a10610..e41e0c2 100644 --- a/examples/shapes/wrapper/package_info.yaml +++ b/examples/shapes/wrapper/package_info.yaml @@ -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: | diff --git a/examples/shapes/wrapper/wrapper_header_collection.hpp b/examples/shapes/wrapper/wrapper_header_collection.hpp index ca26ea3..6bfa1af 100644 --- a/examples/shapes/wrapper/wrapper_header_collection.hpp +++ b/examples/shapes/wrapper/wrapper_header_collection.hpp @@ -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 @@ -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_ diff --git a/pyproject.toml b/pyproject.toml index 6856659..f8e2429 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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",