Skip to content

Commit

Permalink
[.NET] Add a crosswalk to map Handle classes
Browse files Browse the repository at this point in the history
Maps the names parsed from C function prefixes to sensible names of C# SafeHandle-derived classes. If no mapping exists in the config file for a particular prefix, a KeyError will be thrown.
  • Loading branch information
burkenyo authored and speth committed Aug 17, 2022
1 parent 48fc5c9 commit 445fefd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
33 changes: 16 additions & 17 deletions interfaces/sourcegen/sourcegen/csharp/_SourceGenerator.py
Expand Up @@ -55,9 +55,9 @@ def _get_base_handle_text(handle):

handle = normalize(f'''
class {del_clazz} : CanteraHandle
{{
protected override bool ReleaseHandle() =>
Convert.ToBoolean(LibCantera.{name}(Value));
{{
protected override bool ReleaseHandle() =>
Convert.ToBoolean(LibCantera.{name}(Value));
}}
''')

Expand All @@ -73,8 +73,12 @@ def _get_derived_handle_text(derived):
return derived


@classmethod
def _convert_func(cls, parsed: Func):
def __init__(self, out_dir: str, config: dict):
self._out_dir = out_dir
self._config = config


def _convert_func(self, parsed: Func):
ret_type, name, params = parsed
clazz, method = name.split('_', 1)

Expand All @@ -84,7 +88,7 @@ def _convert_func(cls, parsed: Func):
del_clazz = None

if clazz != 'ct':
handle_clazz = clazz.capitalize() + 'Handle'
handle_clazz = self._config['handle_crosswalk'][clazz] + 'Handle'

# It’s not a “global” function, therefore:
# * It wraps a constructor and returns a handle, or
Expand All @@ -97,15 +101,15 @@ def _convert_func(cls, parsed: Func):
_, param_name = params[0]
params[0] = handle_clazz, param_name

for c_type, cs_type in cls._type_map.items():
for c_type, cs_type in self._type_map.items():
if ret_type == c_type:
ret_type = cs_type
break

for i in range(0, len(params)):
param_type, param_name = params[i]

for c_type, cs_type in cls._type_map.items():
for c_type, cs_type in self._type_map.items():
if param_type == c_type:
param_type = cs_type
break
Expand All @@ -118,11 +122,6 @@ def _convert_func(cls, parsed: Func):
return _CsFunc(ret_type, name, params, del_clazz)


def __init__(self, out_dir: str, config: dict):
self._out_dir = out_dir
self._config = config


def generate_source(self, incl_file: os.DirEntry, funcs: list[Func]):
cs_funcs = [self._convert_func(f) for f in funcs]

Expand All @@ -131,10 +130,10 @@ def generate_source(self, incl_file: os.DirEntry, funcs: list[Func]):
interop_text = normalize(f'''
using System.Runtime.InteropServices;
namespace Cantera.Interop;
namespace Cantera.Interop;
static partial class LibCantera
{{
static partial class LibCantera
{{
{normalize(functions_text, 16, True)}
}}
''')
Expand Down Expand Up @@ -163,7 +162,7 @@ def finalize(self):
derived_handles = '\n\n'.join((self._get_derived_handle_text(d) for d in self._config['derived_handles'].items()))

derived_handles_text = normalize(f'''
namespace Cantera.Interop;
namespace Cantera.Interop;
{derived_handles}
''')
Expand Down
16 changes: 15 additions & 1 deletion interfaces/sourcegen/sourcegen/csharp/config.yaml
Expand Up @@ -11,10 +11,24 @@ ignore:
- flowReactor_setMassFlowRate
ctrpath.h: []

# replaces the name as determined by the C function prefix
# with the name of the class that function is designed to expose
handle_crosswalk:
flowdev: FlowDevice
kin: Kinetics
mix: Mixture
reactor: Reactor
reactornet: ReactorNet
reactorsurface: ReactorSurface
surf: Surface
thermo: ThermoPhase
trans: Transport
wall: Wall

# Handles for which there is no special delete function,
# so we need to generate them manually because we can't
# discover the type name from the delete.
# Declare these as
# Derived: Base
derived_handles:
SurfHandle: ThermoHandle
SurfaceHandle: ThermoPhaseHandle

0 comments on commit 445fefd

Please sign in to comment.