Skip to content

Commit

Permalink
#4 move free function exclusion_criteria method
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Mar 25, 2024
1 parent b161cc5 commit 8325caf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
38 changes: 1 addition & 37 deletions cppwg/writers/base_writer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Base for wrapper code writers."""

from collections import OrderedDict
from typing import Dict, List

from pygccxml.declarations import free_function_t
from typing import Dict


class CppBaseWrapperWriter:
Expand Down Expand Up @@ -56,40 +54,6 @@ def tidy_name(self, name: str) -> str:

return name

# TODO: Consider moving this implementation of exclusion_criteria to the
# free function writer it is only used there. exclusion_criteria is
# currently overriden in method writer and constructor writer.
def exclusion_criteria(
self, decl: free_function_t, exclusion_args: List[str]
) -> bool:
"""
Check if the function should be excluded from the wrapper code.
Parameters
----------
decl : free_function_t
The declaration of the function or class
exclusion_args : List[str]
A list of arguments to exclude from the wrapper code
Returns
-------
bool
True if the function should be excluded from the wrapper code
"""
# Check if any return types are not wrappable
return_type = decl.return_type.decl_string.replace(" ", "")
if return_type in exclusion_args:
return True

# Check if any arguments not wrappable
for decl_arg_type in decl.argument_types:
arg_type = decl_arg_type.decl_string.split()[0].replace(" ", "")
if arg_type in exclusion_args:
return True

return False

# TODO: This method is currently a placeholder. Consider implementing or removing.
def default_arg_exclusion_criteria(self) -> bool:
"""
Expand Down
26 changes: 25 additions & 1 deletion cppwg/writers/free_function_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def add_self(self, wrapper_string) -> str:
The updated C++ wrapper code string
"""
# Skip this free function if it uses any excluded arg types or return types
if self.exclusion_criteria(self.free_function_info.decl, self.exclusion_args):
if self.exclusion_criteria():
return wrapper_string

# Pybind11 def type e.g. "_static" for def_static()
Expand All @@ -69,3 +69,27 @@ def add_self(self, wrapper_string) -> str:
wrapper_string += self.wrapper_templates["free_function"].format(**func_dict)

return wrapper_string

def exclusion_criteria(self) -> bool:
"""
Check if the function should be excluded from the wrapper code.
Returns
-------
bool
True if the function should be excluded from wrapper code, False otherwise.
"""
# Check if any return types are not wrappable
return_type = self.free_function_info.decl.return_type.decl_string.replace(
" ", ""
)
if return_type in self.exclusion_args:
return True

# Check if any arguments not wrappable
for decl_arg_type in self.free_function_info.decl.argument_types:
arg_type = decl_arg_type.decl_string.split()[0].replace(" ", "")
if arg_type in self.exclusion_args:
return True

return False

0 comments on commit 8325caf

Please sign in to comment.