Skip to content

Commit

Permalink
Merge pull request #430 from PowerGridModel/feature/standard-layout-i…
Browse files Browse the repository at this point in the history
…nput

Feature/standard layout input
  • Loading branch information
TonyXiang8787 committed Nov 22, 2023
2 parents 9521280 + a5653b4 commit eea11cc
Show file tree
Hide file tree
Showing 41 changed files with 2,183 additions and 655 deletions.
16 changes: 12 additions & 4 deletions code_generation/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ def render_attribute_classes(self, template_path: Path, data_path: Path, output_
for attribute_class in dataset_meta_data.classes:
if attribute_class.is_template:
attribute_class.full_name = f"{attribute_class.name}<sym>"
attribute_class.specification_names = [
f"{attribute_class.name}<true>",
f"{attribute_class.name}<false>",
f"Sym{attribute_class.name}",
f"Asym{attribute_class.name}",
]
else:
attribute_class.full_name = attribute_class.name
attribute_class.specification_names = [attribute_class.name]
new_attribute_list = []
for attribute in attribute_class.attributes:
if isinstance(attribute.names, str):
Expand All @@ -60,8 +67,11 @@ def render_attribute_classes(self, template_path: Path, data_path: Path, output_
if attribute_class.base is not None:
base_class = list(filter(lambda x: x.name == attribute_class.base, dataset_meta_data.classes))[0]
attribute_class.full_attributes = base_class.full_attributes + attribute_class.attributes
attribute_class.base_attributes = base_class.base_attributes.copy()
attribute_class.base_attributes[base_class.name] = base_class.full_attributes
else:
attribute_class.full_attributes = attribute_class.attributes
attribute_class.base_attributes = {}
# add to class dict
self.all_classes[attribute_class.name] = attribute_class

Expand Down Expand Up @@ -102,17 +112,15 @@ def code_gen(self):
}

# render attribute classes
for template_name in render_funcs.keys():
for template_name, render_func in render_funcs.items():
for template_path in TEMPLATE_DIR.rglob(f"{template_name}.*.jinja"):
output_suffix = template_path.with_suffix("").suffix
output_dir = template_path.parent.relative_to(TEMPLATE_DIR)
for data_path in DATA_DIR.glob(f"{template_name}/*.json"):
output_path = self.base_output_path / output_dir / data_path.with_suffix(output_suffix).name
output_path.parent.mkdir(parents=True, exist_ok=True)
print(f"Generating file: {output_path}")
render_funcs[template_name](
template_path=template_path, data_path=data_path, output_path=output_path
)
render_func(template_path=template_path, data_path=data_path, output_path=output_path)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ namespace power_grid_model {
{%- if attribute_class.is_template -%}
template <bool sym>
{% endif -%}
{% if attribute_class.base -%}
struct {{ attribute_class.name }} : {{ attribute_class.base }} {
{%- else -%}
struct {{ attribute_class.name }} {
{%- endif %}
{%- for attribute in attribute_class.attributes %}
struct {{ attribute_class.name }} {
{%- for attribute in attribute_class.full_attributes %}
{{ attribute.data_type }} {{ attribute.names }};
{%- if attribute.description %} // {{ attribute.description }}{%- endif %}
{%- endfor %}
{% for base_class in attribute_class.base_attributes %}
// implicit conversions to {{ base_class }}
operator {{ base_class }}&() { return reinterpret_cast<{{ base_class }}&>(*this); }
operator {{ base_class }} const&() const { return reinterpret_cast<{{ base_class }} const&>(*this); }
{% endfor -%}
};
{% if attribute_class.is_template -%}
{% if attribute_class.is_template %}
using Sym{{ attribute_class.name }} = {{ attribute_class.name }}<true>;
using Asym{{ attribute_class.name }} = {{ attribute_class.name }}<false>;
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model project <dynamic.grid.calculation@alliander.com>
//
// SPDX-License-Identifier: MPL-2.0

// This header file is automatically generated. DO NOT modify it manually!

// clang-format off
#pragma once
#ifndef POWER_GRID_MODEL_AUXILIARY_STATIC_ASSERTS_{{ include_guard }}_HPP
#define POWER_GRID_MODEL_AUXILIARY_STATIC_ASSERTS_{{ include_guard }}_HPP

#include "../{{ name }}.hpp" // NOLINT

#include <cstddef>

namespace power_grid_model::test {

{% for attribute_class in classes -%}

{% for specification in attribute_class.specification_names -%}
// static asserts for {{ specification }}
static_assert(std::is_standard_layout_v<{{ specification }}>);
{% for base_class, base_attributes in attribute_class.base_attributes.items() -%}
// static asserts for conversion of {{ specification }} to {{ base_class }}
static_assert(std::alignment_of_v<{{ specification }}> >= std::alignment_of_v<{{ attribute_class.base }}>);
{% for attribute in base_attributes -%}
static_assert(std::same_as<decltype({{ specification }}::{{ attribute.names }}), decltype({{ base_class }}::{{ attribute.names }})>);
{% endfor -%}
{% for attribute in base_attributes -%}
static_assert(offsetof({{ specification }}, {{ attribute.names }}) == offsetof({{ base_class }}, {{ attribute.names }}));
{% endfor -%}
{% endfor -%}
{% endfor %}
{% endfor %}

} // namespace power_grid_model::test

#endif
// clang-format on

0 comments on commit eea11cc

Please sign in to comment.