diff --git a/python/dumux/CMakeLists.txt b/python/dumux/CMakeLists.txt index db25915644d76cc5f027732674bc671af849bca9..e45fd127636d0e2f73081f762b85657a63c44cb0 100644 --- a/python/dumux/CMakeLists.txt +++ b/python/dumux/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(discretization) add_subdirectory(material) add_subdirectory(io) add_subdirectory(porousmediumflow) +add_subdirectory(wrapping) add_python_targets(dumux __init__ diff --git a/python/dumux/wrapping/CMakeLists.txt b/python/dumux/wrapping/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..9b2e59b4e0914fdfc0524f75fa839238e4dcb3e5 --- /dev/null +++ b/python/dumux/wrapping/CMakeLists.txt @@ -0,0 +1,3 @@ +add_python_targets(wrapping + __init__ +) diff --git a/python/dumux/wrapping/__init__.py b/python/dumux/wrapping/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c32b764f0582ca29864f5ef0ebd241fe7396f48e --- /dev/null +++ b/python/dumux/wrapping/__init__.py @@ -0,0 +1,40 @@ +"""Helper classes and function related Python <-> C++ interaction""" + +import functools +from typing import Callable, Type + + +def cppWrapperCreator(creator: Callable) -> Callable: + """ + Decorator for creator functions that return a C++ type with Python bindings + resulting from C++ code generation and just-in-time compilation + """ + + def makeCreator(aliasClass: Type) -> Callable: + @functools.wraps(creator) + def _makeCreator(*args, **kwargs): + return creator(*args, **kwargs) + + # make the creator assume the name of the alias class + _makeCreator.__name__ = aliasClass.__name__ + return _makeCreator + + return makeCreator + + +def cppWrapperClassAlias(creator: Callable) -> Callable: + """ + Decorator for a class alias corresponding to a creator function + This makes the creator function appear like a class constructor + + Args: + creator (Callable): The corresponding creator function the + decorated class is an alias for + Returns: + Callable: The creator function with the alias name + """ + + def makeCreator(aliasClass: Type) -> Callable: + return creator(aliasClass) + + return makeCreator