Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve cpp templates #49

Merged
merged 9 commits into from
Jan 6, 2025
Prev Previous commit
Next Next commit
Add templates for generation of CIMClassList.hpp and IEC61970.hpp (ge…
…nerated code is unchanged)

Signed-off-by: Thomas Günther <tom@toms-cafe.de>
tom-hg57 committed Jan 5, 2025
commit f96841c765298cad46c2dc7b24924c9d57a3ddf9
73 changes: 13 additions & 60 deletions cimgen/languages/cpp/lang_pack.py
Original file line number Diff line number Diff line change
@@ -46,6 +46,8 @@ def setup(output_path: str, cgmes_profile_details: list[dict], cim_namespace: st
{"filename": "cpp_cgmesProfile_header_template.mustache", "ext": ".hpp"},
{"filename": "cpp_cgmesProfile_object_template.mustache", "ext": ".cpp"},
]
classlist_template = {"filename": "cpp_classlist_template.mustache", "ext": ".hpp"}
iec61970_template = {"filename": "cpp_iec61970_template.mustache", "ext": ".hpp"}

partials = {
"label_without_keyword": "{{#lang_pack.label_without_keyword}}{{label}}{{/lang_pack.label_without_keyword}}",
@@ -230,76 +232,27 @@ def _is_primitive_or_enum_class(file: Path) -> bool:


def _create_header_include_file(
directory: Path,
header_include_filename: str,
header: list[str],
footer: list[str],
before: str,
after: str,
blacklist: list[str],
directory: Path, header_include_filename: str, template_info: dict[str, str], blacklist: list[str]
) -> None:
lines = []
classes = []
for file in sorted(directory.glob("*.hpp"), key=lambda f: f.stem):
basename = file.stem
if not _is_primitive_or_enum_class(file) and basename not in blacklist:
lines.append(before + basename + after)
for line in lines:
header.append(line)
for line in footer:
header.append(line)
header_include_filepath = directory / header_include_filename
with header_include_filepath.open("w", encoding="utf-8") as f:
f.writelines(header)
class_name = file.stem
if not _is_primitive_or_enum_class(file) and class_name not in blacklist:
classes.append(class_name)
path = directory / (header_include_filename + template_info["ext"])
_write_templated_file(path, {"classes": classes}, template_info["filename"])


def resolve_headers(path: str, version: str) -> None: # NOSONAR
class_list_header = [
"#ifndef CIMCLASSLIST_H\n",
"#define CIMCLASSLIST_H\n",
"/*\n",
"Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen\n",
"*/\n",
"#include <list>\n",
'#include "IEC61970.hpp"\n',
"using namespace CIMPP;\n",
"static std::list<BaseClassDefiner> CIMClassList =\n",
"{\n",
]
class_list_footer = [
" UnknownType::declare(),\n",
"};\n",
"#endif // CIMCLASSLIST_H\n",
]

_create_header_include_file(
Path(path),
"CIMClassList.hpp",
class_list_header,
class_list_footer,
" ",
"::declare(),\n",
"CIMClassList",
classlist_template,
class_blacklist,
)

iec61970_header = [
"#ifndef IEC61970_H\n",
"#define IEC61970_H\n",
"/*\n",
"Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen\n",
"*/\n",
"\n",
]
iec61970_footer = [
'#include "UnknownType.hpp"\n',
"#endif",
]

_create_header_include_file(
Path(path),
"IEC61970.hpp",
iec61970_header,
iec61970_footer,
'#include "',
'.hpp"\n',
"IEC61970",
iec61970_template,
iec61970_blacklist,
)
16 changes: 16 additions & 0 deletions cimgen/languages/cpp/templates/cpp_classlist_template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CIMCLASSLIST_H
#define CIMCLASSLIST_H
/*
Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen
*/
#include <list>
#include "IEC61970.hpp"
using namespace CIMPP;
static std::list<BaseClassDefiner> CIMClassList =
{
{{#classes}}
{{.}}::declare(),
{{/classes}}
UnknownType::declare(),
};
#endif // CIMCLASSLIST_H
11 changes: 11 additions & 0 deletions cimgen/languages/cpp/templates/cpp_iec61970_template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef IEC61970_H
#define IEC61970_H
/*
Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen
*/

{{#classes}}
#include "{{.}}.hpp"
{{/classes}}
#include "UnknownType.hpp"
#endif