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

Refactor meta data generator to remove lambda hack #693

Merged
merged 8 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct get_attributes_list<{{ attribute_class.name }}> {
static constexpr std::array<MetaAttribute, {{ attribute_class.full_attributes|length }}> value{
// all attributes including base class
{% for attribute in attribute_class.full_attributes %}
meta_data_gen::get_meta_attribute<{{ attribute_class.full_name }}, &{{ attribute_class.full_name }}::{{ attribute.names }}, offsetof({{ attribute_class.full_name }}, {{ attribute.names }}), []{ return "{{ attribute.names }}"; }>::value,
meta_data_gen::get_meta_attribute<&{{ attribute_class.full_name }}::{{ attribute.names }}>(offsetof({{ attribute_class.full_name }}, {{ attribute.names }}), "{{ attribute.names }}"),
{%- endfor %}
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ template <dataset_type_tag dataset_type_> class Dataset {
}

// get buffer by component type
template <template <class> class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter<ComponentType>::type>>
template <class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter::template type<ComponentType>>>
std::span<StructType> get_buffer_span(Idx scenario = invalid_index) const {
assert(scenario < batch_size());

Expand All @@ -317,8 +317,8 @@ template <dataset_type_tag dataset_type_> class Dataset {
return get_buffer_span_impl<StructType>(scenario, idx);
}

template <template <class> class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter<ComponentType>::type>>
template <class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter::template type<ComponentType>>>
RangeObject<StructType> get_columnar_buffer_span(Idx scenario = invalid_index) const {
assert(scenario < batch_size());

Expand All @@ -331,8 +331,8 @@ template <dataset_type_tag dataset_type_> class Dataset {
}

// get buffer by component type for all scenarios in vector span
template <template <class> class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter<ComponentType>::type>>
template <class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter::template type<ComponentType>>>
std::vector<std::span<StructType>> get_buffer_span_all_scenarios() const {
Idx const idx = find_component(ComponentType::name, false);
std::vector<std::span<StructType>> result(batch_size());
Expand All @@ -342,8 +342,8 @@ template <dataset_type_tag dataset_type_> class Dataset {
return result;
}

template <template <class> class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter<ComponentType>::type>>
template <class type_getter, class ComponentType,
class StructType = DataStruct<typename type_getter::template type<ComponentType>>>
std::vector<RangeObject<StructType>> get_columnar_buffer_span_all_scenarios() const {
Idx const idx = find_component(ComponentType::name, false);
std::vector<RangeObject<StructType>> result(batch_size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@

namespace power_grid_model::meta_data {

// pointer to member
template <class T> struct trait_pointer_to_member;
template <class StructType, class ValueType> struct trait_pointer_to_member<ValueType StructType::*> {
using value_type = ValueType;
};

// primary template to get the attribute list of a component
// the specializations will contain static constexpr "value" field
// which is a std::array
Expand Down Expand Up @@ -185,20 +179,25 @@ struct MetaData {
constexpr bool is_little_endian() { return std::endian::native == std::endian::little; }

// list of all dataset names
template <class T> struct input_getter_s {
using type = typename T::InputType;
struct input_getter_s {
static constexpr char const* name = "input";
template <class T> using type = typename T::InputType;
};
template <class T> struct update_getter_s {
using type = typename T::UpdateType;
struct update_getter_s {
static constexpr char const* name = "update";
template <class T> using type = typename T::UpdateType;
};
template <class T> struct sym_output_getter_s {
using type = typename T::template OutputType<symmetric_t>;
struct sym_output_getter_s {
static constexpr char const* name = "sym_output";
template <class T> using type = typename T::template OutputType<symmetric_t>;
};
template <class T> struct asym_output_getter_s {
using type = typename T::template OutputType<asymmetric_t>;
struct asym_output_getter_s {
static constexpr char const* name = "asym_output";
template <class T> using type = typename T::template OutputType<asymmetric_t>;
};
template <class T> struct sc_output_getter_s {
using type = typename T::ShortCircuitOutputType;
struct sc_output_getter_s {
static constexpr char const* name = "sc_output";
template <class T> using type = typename T::ShortCircuitOutputType;
};

} // namespace power_grid_model::meta_data
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
namespace power_grid_model::meta_data::meta_data_gen {

// generate meta data
constexpr MetaData meta_data = get_meta_data<AllComponents, // all components list
dataset_mark<[] { return "input"; }, input_getter_s>,
dataset_mark<[] { return "update"; }, update_getter_s>,
dataset_mark<[] { return "sym_output"; }, sym_output_getter_s>,
dataset_mark<[] { return "asym_output"; }, asym_output_getter_s>,
dataset_mark<[] { return "sc_output"; }, sc_output_getter_s>
// end list of all marks
>::value;
constexpr MetaData meta_data =
get_meta_data<AllComponents, // all components list
input_getter_s, update_getter_s, sym_output_getter_s, asym_output_getter_s, sc_output_getter_s
// end list of all marks
>::value;

} // namespace power_grid_model::meta_data::meta_data_gen
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@

namespace power_grid_model::meta_data::meta_data_gen {

// pointer to member
template <class T> struct trait_pointer_to_member;
template <class StructType, class ValueType> struct trait_pointer_to_member<ValueType StructType::*> {
using value_type = ValueType;
using struct_type = StructType;
};

// getter for meta attribute
template <class StructType, auto member_ptr, size_t offset, auto attribute_name_getter> struct get_meta_attribute {
using ValueType = typename trait_pointer_to_member<decltype(member_ptr)>::value_type;
template <auto member_ptr, class MemberPtr = decltype(member_ptr)>
constexpr MetaAttribute get_meta_attribute(size_t offset, char const* attribute_name) {
using ValueType = typename trait_pointer_to_member<MemberPtr>::value_type;
using StructType = typename trait_pointer_to_member<MemberPtr>::struct_type;

static constexpr MetaAttribute value{
.name = attribute_name_getter(),
return MetaAttribute{
.name = attribute_name,
.ctype = ctype_v<ValueType>,
.offset = offset,
.size = sizeof(ValueType),
Expand Down Expand Up @@ -53,9 +62,9 @@ template <class StructType, auto member_ptr, size_t offset, auto attribute_name_
};

// getter for meta component
template <class StructType, auto component_name_getter> struct get_meta_component {
static constexpr MetaComponent value{
.name = component_name_getter(),
template <class StructType> constexpr MetaComponent get_meta_component(char const* component_name) {
return MetaComponent{
.name = component_name,
.size = sizeof(StructType),
.alignment = alignof(StructType),
.attributes = get_attributes_list<StructType>::value,
Expand All @@ -70,25 +79,22 @@ template <class StructType, auto component_name_getter> struct get_meta_componen
};

// getter for meta dataset
template <auto dataset_name_getter, template <class> class struct_getter, class comp_list> struct get_meta_dataset;
template <auto dataset_name_getter, template <class> class struct_getter, class... ComponentType>
struct get_meta_dataset<dataset_name_getter, struct_getter, ComponentList<ComponentType...>> {
template <class struct_getter, class comp_list> struct get_meta_dataset;
template <class struct_getter, class... ComponentType>
struct get_meta_dataset<struct_getter, ComponentList<ComponentType...>> {
static constexpr size_t n_components = sizeof...(ComponentType);
static constexpr std::array<MetaComponent, n_components> components{
get_meta_component<typename struct_getter<ComponentType>::type, [] { return ComponentType::name; }>::value...};
get_meta_component<typename struct_getter::template type<ComponentType>>(ComponentType::name)...};
static constexpr MetaDataset value{
.name = dataset_name_getter(),
.name = struct_getter::name,
.components = components,
};
};

// get meta data
template <auto dataset_name_getter, template <class> class struct_getter> struct dataset_mark;
template <class comp_list, class... T> struct get_meta_data;
template <class comp_list, auto... dataset_name_getter, template <class> class... struct_getter>
struct get_meta_data<comp_list, dataset_mark<dataset_name_getter, struct_getter>...> {
static constexpr std::array<MetaDataset, sizeof...(dataset_name_getter)> datasets{
get_meta_dataset<dataset_name_getter, struct_getter, comp_list>::value...};
template <class comp_list, class... struct_getter> struct get_meta_data {
static constexpr std::array<MetaDataset, sizeof...(struct_getter)> datasets{
get_meta_dataset<struct_getter, comp_list>::value...};
static constexpr MetaData value{
.datasets = datasets,
};
Expand Down
Loading