From fa956ed7c0da8f1da9a8b700cf31b8b488ca92b5 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 29 Jul 2024 15:16:10 +0000 Subject: [PATCH 1/3] #21 add constructor_signature_excludes --- cppwg/input/base_info.py | 11 +++--- cppwg/parsers/package_info_parser.py | 1 + cppwg/writers/constructor_writer.py | 44 +++++++++++++++-------- examples/shapes/wrapper/package_info.yaml | 21 ++++++----- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/cppwg/input/base_info.py b/cppwg/input/base_info.py index d5783ce..87a3513 100644 --- a/cppwg/input/base_info.py +++ b/cppwg/input/base_info.py @@ -38,12 +38,14 @@ class BaseInfo: Do not include these methods. excluded_variables : List[str] Do not include these variables. + arg_type_excludes : List[str] + List of exclude patterns for arg types in methods. constructor_arg_type_excludes : List[str] - List of exclude patterns for ctors. + List of exclude patterns for arg types in constructors. + constructor_signature_excludes : List[List[str]] + List of exclude patterns for constructor signatures. return_type_excludes : List[str] List of exclude patterns for return types. - arg_type_excludes : List[str] - List of exclude patterns for arg types. name_replacements : Dict[str, str] A dictionary of name replacements e.g. {"double":"Double", "unsigned int":"Unsigned"} @@ -62,9 +64,10 @@ def __init__(self, name): self.custom_generator: Optional[str] = None self.excluded_methods: List[str] = [] self.excluded_variables: List[str] = [] + self.arg_type_excludes: List[str] = [] self.constructor_arg_type_excludes: List[str] = [] + self.constructor_signature_excludes: List[List[str]] = [] self.return_type_excludes: List[str] = [] - self.arg_type_excludes: List[str] = [] self.name_replacements: Dict[str, str] = { "double": "Double", "unsigned int": "Unsigned", diff --git a/cppwg/parsers/package_info_parser.py b/cppwg/parsers/package_info_parser.py index 552c404..876d178 100644 --- a/cppwg/parsers/package_info_parser.py +++ b/cppwg/parsers/package_info_parser.py @@ -123,6 +123,7 @@ def parse(self) -> PackageInfo: "pointer_call_policy": None, "reference_call_policy": None, "constructor_arg_type_excludes": None, + "constructor_signature_excludes": None, "excluded_methods": [], "excluded_variables": [], "custom_generator": None, diff --git a/cppwg/writers/constructor_writer.py b/cppwg/writers/constructor_writer.py index 1e95b8c..44f2460 100644 --- a/cppwg/writers/constructor_writer.py +++ b/cppwg/writers/constructor_writer.py @@ -94,36 +94,52 @@ def exclude(self) -> bool: ): return True - # Check for excluded argument patterns + # Get arg type strings with spaces removed + # e.g. ::std::vector const & -> ::std::vectorconst& + arg_types = [ + x.decl_string.replace(" ", "") for x in self.ctor_decl.argument_types + ] + + # Exclude constructors with "iterator" in args + for arg_type in arg_types: + if "iterator" in arg_type.lower(): + return True + + # Exclude constructors with args matching patterns in calldef_excludes calldef_excludes = [ x.replace(" ", "") for x in self.class_info.hierarchy_attribute_gather("calldef_excludes") ] + for arg_type in arg_types: + if arg_type in calldef_excludes: + return True + # Exclude constructors with args matching patterns in constructor_arg_type_excludes ctor_arg_type_excludes = [ x.replace(" ", "") for x in self.class_info.hierarchy_attribute_gather( "constructor_arg_type_excludes" ) ] + for exclude_type in ctor_arg_type_excludes: + if exclude_type in arg_type: + return True - for arg_type in self.ctor_decl.argument_types: - # e.g. ::std::vector const & -> ::std::vectorconst& - arg_type_str = arg_type.decl_string.replace(" ", "") + # Exclude constructors matching a signature in constructor_signature_excludes + ctor_signature_excludes = self.class_info.hierarchy_attribute_gather( + "constructor_signature_excludes" + ) - # Exclude constructors with "iterator" in args - if "iterator" in arg_type_str.lower(): - return True + for exclude_types in ctor_signature_excludes: + if len(exclude_types) != len(arg_types): + continue - # Exclude constructors with args matching calldef_excludes - if arg_type_str in calldef_excludes: + if all( + exclude_type in arg_type + for arg_type, exclude_type in zip(arg_types, exclude_types) + ): return True - # Exclude constructurs with args matching constructor_arg_type_excludes - for excluded_type in ctor_arg_type_excludes: - if excluded_type in arg_type_str: - return True - return False def generate_wrapper(self) -> str: diff --git a/examples/shapes/wrapper/package_info.yaml b/examples/shapes/wrapper/package_info.yaml index 51efc20..826a970 100644 --- a/examples/shapes/wrapper/package_info.yaml +++ b/examples/shapes/wrapper/package_info.yaml @@ -16,14 +16,6 @@ source_includes: # Exclude default arguments from wrapped methods. exclude_default_args: False -# Exclude any methods that have these arg types. -# arg_type_excludes: -# - double - -# Exclude any constructors that have these arg types. -# constructor_arg_type_excludes: -# - double - # Signature/replacement settings for explicit template instantiations. template_substitutions: - signature: @@ -53,6 +45,19 @@ modules: excluded_methods: - ExcludedMethod + # Exclude any methods that have these arg types. + # arg_type_excludes: + # - double + + # Exclude any constructors that have these arg types. + # constructor_arg_type_excludes: + # - double + + # Exclude any constructors that have these signatures. + # constructor_signature_excludes: + # - [double] + # - [double, int] + # Path to a custom script for generating wrappers for this class. # CPPWG_SOURCEROOT points to the supplied source root directory. # custom_generator: "CPPWG_SOURCEROOT/point_generator.py" From a37a6d37a9eb1dffb4ccc8096dc8c6d5127e0140 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 29 Jul 2024 15:31:01 +0000 Subject: [PATCH 2/3] #21 add test for constructor_signature_excludes --- examples/shapes/src/geometry/Point.cpp | 8 ++++++++ examples/shapes/src/geometry/Point.hpp | 5 +++++ examples/shapes/wrapper/package_info.yaml | 5 ++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/examples/shapes/src/geometry/Point.cpp b/examples/shapes/src/geometry/Point.cpp index 87630e1..49191fc 100644 --- a/examples/shapes/src/geometry/Point.cpp +++ b/examples/shapes/src/geometry/Point.cpp @@ -18,6 +18,14 @@ Point::Point(double x, double y, double z) } } +template +Point::Point(int x, int y, int z) + : Point(static_cast(x), + static_cast(y), + static_cast(z)) +{ +} + template Point::~Point() { diff --git a/examples/shapes/src/geometry/Point.hpp b/examples/shapes/src/geometry/Point.hpp index fe93805..7eae659 100644 --- a/examples/shapes/src/geometry/Point.hpp +++ b/examples/shapes/src/geometry/Point.hpp @@ -31,6 +31,11 @@ class Point */ Point(double x, double y, double z = (DIM - DIM)); + /** + * Constructor to exclude from wrapping + */ + Point(int x, int y, int z = 0); + /** * Destructor */ diff --git a/examples/shapes/wrapper/package_info.yaml b/examples/shapes/wrapper/package_info.yaml index 826a970..e9d8cc9 100644 --- a/examples/shapes/wrapper/package_info.yaml +++ b/examples/shapes/wrapper/package_info.yaml @@ -54,9 +54,8 @@ modules: # - double # Exclude any constructors that have these signatures. - # constructor_signature_excludes: - # - [double] - # - [double, int] + constructor_signature_excludes: + - [int, int, int] # Path to a custom script for generating wrappers for this class. # CPPWG_SOURCEROOT points to the supplied source root directory. From 2fcfe5c7679ccbad4947f915b6d8db77d4da76b5 Mon Sep 17 00:00:00 2001 From: Kwabena N Amponsah Date: Mon, 29 Jul 2024 16:56:53 +0000 Subject: [PATCH 3/3] #21 fix constructor arg excludes loop --- cppwg/writers/constructor_writer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cppwg/writers/constructor_writer.py b/cppwg/writers/constructor_writer.py index 44f2460..d8b4281 100644 --- a/cppwg/writers/constructor_writer.py +++ b/cppwg/writers/constructor_writer.py @@ -122,8 +122,9 @@ def exclude(self) -> bool: ) ] for exclude_type in ctor_arg_type_excludes: - if exclude_type in arg_type: - return True + for arg_type in arg_types: + if exclude_type in arg_type: + return True # Exclude constructors matching a signature in constructor_signature_excludes ctor_signature_excludes = self.class_info.hierarchy_attribute_gather(