From 38bef3dd3265f7785f66237050bfeabd9448e69d Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Sat, 4 Jan 2025 18:57:32 -0700 Subject: [PATCH] [sourcegen] Handle custom code --- interfaces/sourcegen/sourcegen/_data/ctsol_auto.yaml | 12 +++++++++++- .../sourcegen/sourcegen/clib/_CLibSourceGenerator.py | 11 ++++++----- interfaces/sourcegen/sourcegen/clib/config.yaml | 4 ++-- interfaces/sourcegen/sourcegen/clib/templates.yaml | 8 ++++++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/interfaces/sourcegen/sourcegen/_data/ctsol_auto.yaml b/interfaces/sourcegen/sourcegen/_data/ctsol_auto.yaml index 3e7fed73a3..4f0b8b6b54 100644 --- a/interfaces/sourcegen/sourcegen/_data/ctsol_auto.yaml +++ b/interfaces/sourcegen/sourcegen/_data/ctsol_auto.yaml @@ -23,7 +23,17 @@ recipes: - name: thermo - name: kinetics - name: transport -- name: setTransport # currently disabled in CLib's config.yaml +- name: setTransportModel + code: |- + try { + auto obj = SolutionCabinet::at(handle); + TransportCabinet::del( + TransportCabinet::index(*(obj->transport()), handle)); + obj->setTransportModel(model); + return TransportCabinet::add(obj->transport(), handle); + } catch (...) { + return handleAllExceptions(-2, ERR); + } - name: nAdjacent - name: adjacent implements: Solution::adjacent(size_t) diff --git a/interfaces/sourcegen/sourcegen/clib/_CLibSourceGenerator.py b/interfaces/sourcegen/sourcegen/clib/_CLibSourceGenerator.py index 0f62de860c..319ea298d0 100644 --- a/interfaces/sourcegen/sourcegen/clib/_CLibSourceGenerator.py +++ b/interfaces/sourcegen/sourcegen/clib/_CLibSourceGenerator.py @@ -297,7 +297,12 @@ def _scaffold_body(self, c_func: CFunc, recipe: Recipe) -> tuple[str, set[str]]: args, bases = self._reverse_crosswalk(c_func, recipe.base) args["what"] = recipe.what - if recipe.what == "noop": + if recipe.code: + # override auto-generated code + template = loader.from_string(self._templates["clib-custom-code"]) + args["lines"] = recipe.code.strip(" \n").split("\n") + + elif recipe.what == "noop": template = loader.from_string(self._templates["clib-noop"]) elif recipe.what == "function": @@ -383,10 +388,6 @@ def merge_params(implements, cxx_func: CFunc) -> tuple[list[Param], int]: args = [] brief = "" - if recipe.code: - _LOGGER.warning("Custom code is currently not implemented: " - "continuing with auto-generated code.") - if recipe.implements: if not quiet: _LOGGER.debug(f" generating {func_name!r} -> {recipe.implements}") diff --git a/interfaces/sourcegen/sourcegen/clib/config.yaml b/interfaces/sourcegen/sourcegen/clib/config.yaml index 6dbe2782a9..f5059f5830 100644 --- a/interfaces/sourcegen/sourcegen/clib/config.yaml +++ b/interfaces/sourcegen/sourcegen/clib/config.yaml @@ -7,8 +7,8 @@ ignore_files: [] # Ignore these specific functions: -ignore_funcs: - ctsol_auto.yaml: [setTransport] +ignore_funcs: {} + # ctsol_auto.yaml: [setTransport] # Cabinets with associated includes includes: diff --git a/interfaces/sourcegen/sourcegen/clib/templates.yaml b/interfaces/sourcegen/sourcegen/clib/templates.yaml index 79e1ace148..40f5e7f8a3 100644 --- a/interfaces/sourcegen/sourcegen/clib/templates.yaml +++ b/interfaces/sourcegen/sourcegen/clib/templates.yaml @@ -250,6 +250,14 @@ clib-noop: |- // no-op return 0; +clib-custom-code: |- + // {{ what }}: {{ cxx_implements }} + // ********** custom code begin ********** + {% for line in lines %} + {{ line }} + {% endfor -%} + // *********** custom code end *********** + clib-implementation: |- {{ declaration }} {