Skip to content
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 @@ -79,9 +79,6 @@ typedef struct PGM_Handle PGM_Handle;
*/
typedef struct PGM_Options PGM_Options;

// Only enable the opaque struct definition if this header is consumed by the C-API user.
// If this header is included when compiling the C-API, the structs below are decleared/defined in the C++ files.
#ifndef PGM_DLL_EXPORTS
/**
* @brief Opaque struct for the attribute meta class.
*
Expand Down Expand Up @@ -137,7 +134,6 @@ typedef struct PGM_WritableDataset PGM_WritableDataset;
* @brief Opaque struct for the information of the dataset.
*/
typedef struct PGM_DatasetInfo PGM_DatasetInfo;
#endif

// NOLINTEND(modernize-use-using)

Expand Down
11 changes: 6 additions & 5 deletions power_grid_model_c/power_grid_model_c/src/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using namespace power_grid_model;
using meta_data::RawDataConstPtr;
using meta_data::RawDataPtr;
using power_grid_model_c::call_with_catch;
using power_grid_model_c::cast_to_cpp;
using power_grid_model_c::safe_ptr;
using power_grid_model_c::safe_ptr_get;
using power_grid_model_c::safe_ptr_maybe_nullptr;
Expand All @@ -29,7 +30,7 @@ using power_grid_model_c::to_c_size;
// buffer control
RawDataPtr PGM_create_buffer(PGM_Handle* handle, PGM_MetaComponent const* component, PGM_Idx size) {
return call_with_catch(handle, [component, size] {
auto const& safe_component = safe_ptr_get(component);
auto const& safe_component = safe_ptr_get(cast_to_cpp(component));

// alignment should be maximum of alignment of the component and alignment of void*
size_t const alignment = std::max(safe_component.alignment, sizeof(void*));
Expand All @@ -54,14 +55,14 @@ void PGM_destroy_buffer(RawDataPtr ptr) {
void PGM_buffer_set_nan(PGM_Handle* handle, PGM_MetaComponent const* component, void* ptr, PGM_Idx buffer_offset,
PGM_Idx size) {
call_with_catch(handle, [component, ptr, buffer_offset, size] {
safe_ptr_get(component).set_nan(safe_ptr(ptr), buffer_offset, size);
safe_ptr_get(cast_to_cpp(component)).set_nan(safe_ptr(ptr), buffer_offset, size);
});
}

namespace {
// template for get and set attribute
template <bool is_get, class BufferPtr, class ValuePtr>
void buffer_get_set_value(PGM_MetaAttribute const& attribute, BufferPtr buffer_ptr, ValuePtr value_ptr,
void buffer_get_set_value(meta_data::MetaAttribute const& attribute, BufferPtr buffer_ptr, ValuePtr value_ptr,
PGM_Idx buffer_offset, PGM_Idx size, PGM_Idx stride) {
using RawValuePtr = std::conditional_t<is_get, char*, char const*>;

Expand Down Expand Up @@ -90,14 +91,14 @@ void buffer_get_set_value(PGM_MetaAttribute const& attribute, BufferPtr buffer_p
void PGM_buffer_set_value(PGM_Handle* handle, PGM_MetaAttribute const* attribute, RawDataPtr buffer_ptr,
RawDataConstPtr src_ptr, PGM_Idx buffer_offset, PGM_Idx size, PGM_Idx src_stride) {
call_with_catch(handle, [attribute, buffer_ptr, src_ptr, buffer_offset, size, src_stride] {
buffer_get_set_value<false>(safe_ptr_get(attribute), safe_ptr_maybe_nullptr(buffer_ptr),
buffer_get_set_value<false>(safe_ptr_get(cast_to_cpp(attribute)), safe_ptr_maybe_nullptr(buffer_ptr),
safe_ptr_maybe_nullptr(src_ptr), buffer_offset, size, src_stride);
});
}
void PGM_buffer_get_value(PGM_Handle* handle, PGM_MetaAttribute const* attribute, RawDataConstPtr buffer_ptr,
RawDataPtr dest_ptr, PGM_Idx buffer_offset, PGM_Idx size, PGM_Idx dest_stride) {
call_with_catch(handle, [attribute, buffer_ptr, dest_ptr, buffer_offset, size, dest_stride] {
buffer_get_set_value<true>(safe_ptr_get(attribute), safe_ptr_maybe_nullptr(buffer_ptr),
buffer_get_set_value<true>(safe_ptr_get(cast_to_cpp(attribute)), safe_ptr_maybe_nullptr(buffer_ptr),
safe_ptr_maybe_nullptr(dest_ptr), buffer_offset, size, dest_stride);
});
}
80 changes: 48 additions & 32 deletions power_grid_model_c/power_grid_model_c/src/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using namespace power_grid_model;
using namespace power_grid_model::meta_data;
using power_grid_model_c::call_with_catch;
using power_grid_model_c::cast_to_c;
using power_grid_model_c::cast_to_cpp;
using power_grid_model_c::safe_bool;
using power_grid_model_c::safe_ptr;
using power_grid_model_c::safe_ptr_get;
Expand All @@ -29,56 +31,62 @@ using power_grid_model_c::to_c_size;
// dataset info

char const* PGM_dataset_info_name(PGM_Handle* handle, PGM_DatasetInfo const* info) {
return call_with_catch(handle, [info] { return safe_ptr_get(safe_ptr(info)->dataset).name; });
return call_with_catch(handle, [info] { return safe_ptr_get(safe_ptr(cast_to_cpp(info))->dataset).name; });
}

PGM_Idx PGM_dataset_info_is_batch(PGM_Handle* handle, PGM_DatasetInfo const* info) {
return call_with_catch(handle, [info] { return to_c_bool<PGM_Idx>(safe_ptr_get(info).is_batch); });
return call_with_catch(handle, [info] { return to_c_bool<PGM_Idx>(safe_ptr_get(cast_to_cpp(info)).is_batch); });
}

PGM_Idx PGM_dataset_info_batch_size(PGM_Handle* handle, PGM_DatasetInfo const* info) {
return call_with_catch(handle, [info] { return safe_ptr_get(info).batch_size; });
return call_with_catch(handle, [info] { return safe_ptr_get(cast_to_cpp(info)).batch_size; });
}

PGM_Idx PGM_dataset_info_n_components(PGM_Handle* handle, PGM_DatasetInfo const* info) {
return call_with_catch(handle, [info] { return to_c_size(std::ssize(safe_ptr_get(info).component_info)); });
return call_with_catch(handle,
[info] { return to_c_size(std::ssize(safe_ptr_get(cast_to_cpp(info)).component_info)); });
}

char const* PGM_dataset_info_component_name(PGM_Handle* handle, PGM_DatasetInfo const* info, PGM_Idx component_idx) {
return call_with_catch(handle, [info, component_idx] {
return safe_ptr_get(safe_ptr_get(info).component_info.at(component_idx).component).name;
return safe_ptr_get(safe_ptr_get(cast_to_cpp(info)).component_info.at(component_idx).component).name;
});
}

PGM_Idx PGM_dataset_info_elements_per_scenario(PGM_Handle* handle, PGM_DatasetInfo const* info, PGM_Idx component_idx) {
return call_with_catch(handle, [info, component_idx] {
return safe_ptr_get(info).component_info.at(component_idx).elements_per_scenario;
return safe_ptr_get(cast_to_cpp(info)).component_info.at(component_idx).elements_per_scenario;
});
}

PGM_Idx PGM_dataset_info_total_elements(PGM_Handle* handle, PGM_DatasetInfo const* info, PGM_Idx component_idx) {
return call_with_catch(
handle, [info, component_idx] { return safe_ptr_get(info).component_info.at(component_idx).total_elements; });
return call_with_catch(handle, [info, component_idx] {
return safe_ptr_get(cast_to_cpp(info)).component_info.at(component_idx).total_elements;
});
}

PGM_Idx PGM_dataset_info_has_attribute_indications(PGM_Handle* handle, PGM_DatasetInfo const* info,
PGM_Idx component_idx) {
return call_with_catch(handle, [info, component_idx] {
return to_c_bool<PGM_Idx>(safe_ptr_get(info).component_info.at(component_idx).has_attribute_indications);
return to_c_bool<PGM_Idx>(
safe_ptr_get(cast_to_cpp(info)).component_info.at(component_idx).has_attribute_indications);
});
}

PGM_Idx PGM_dataset_info_n_attribute_indications(PGM_Handle* handle, PGM_DatasetInfo const* info,
PGM_Idx component_idx) {
return call_with_catch(handle, [info, component_idx] {
return to_c_size(std::ssize(safe_ptr_get(info).component_info.at(component_idx).attribute_indications));
return to_c_size(
std::ssize(safe_ptr_get(cast_to_cpp(info)).component_info.at(component_idx).attribute_indications));
});
}

char const* PGM_dataset_info_attribute_name(PGM_Handle* handle, PGM_DatasetInfo const* info, PGM_Idx component_idx,
PGM_Idx attribute_idx) {
return call_with_catch(handle, [info, component_idx, attribute_idx] {
return safe_ptr_get(safe_ptr_get(info).component_info.at(component_idx).attribute_indications.at(attribute_idx))
return safe_ptr_get(safe_ptr_get(cast_to_cpp(info))
.component_info.at(component_idx)
.attribute_indications.at(attribute_idx))
.name;
});
}
Expand All @@ -88,72 +96,77 @@ char const* PGM_dataset_info_attribute_name(PGM_Handle* handle, PGM_DatasetInfo
PGM_ConstDataset* PGM_create_dataset_const(PGM_Handle* handle, char const* dataset, PGM_Idx is_batch,
PGM_Idx batch_size) {
return call_with_catch(handle, [dataset, is_batch, batch_size] {
return new ConstDataset{// NOSONAR(S5025)
safe_bool(is_batch), batch_size, safe_str_view(dataset), get_meta_data()};
return cast_to_c(new ConstDataset{// NOSONAR(S5025)
safe_bool(is_batch), batch_size, safe_str_view(dataset), get_meta_data()});
});
}

PGM_ConstDataset* PGM_create_dataset_const_from_writable(PGM_Handle* handle,
PGM_WritableDataset const* writable_dataset) {
return call_with_catch(handle, [writable_dataset] {
return new ConstDataset{safe_ptr_get(writable_dataset)}; // NOSONAR(S5025)
return cast_to_c(new ConstDataset{safe_ptr_get(cast_to_cpp(writable_dataset))}); // NOSONAR(S5025)
});
}

PGM_ConstDataset* PGM_create_dataset_const_from_mutable(PGM_Handle* handle, PGM_MutableDataset const* mutable_dataset) {
return call_with_catch(handle, [mutable_dataset] {
return new ConstDataset{safe_ptr_get(mutable_dataset)}; // NOSONAR(S5025)
return cast_to_c(new ConstDataset{safe_ptr_get(cast_to_cpp(mutable_dataset))}); // NOSONAR(S5025)
});
}

void PGM_destroy_dataset_const(PGM_ConstDataset* dataset) {
delete dataset; // NOSONAR(S5025)
delete cast_to_cpp(dataset); // NOSONAR(S5025)
}

void PGM_dataset_const_add_buffer(PGM_Handle* handle, PGM_ConstDataset* dataset, char const* component,
PGM_Idx elements_per_scenario, PGM_Idx total_elements, PGM_Idx const* indptr,
void const* data) {
call_with_catch(handle, [dataset, component, elements_per_scenario, total_elements, indptr, data] {
safe_ptr_get(dataset).add_buffer(safe_str_view(component), elements_per_scenario, total_elements,
safe_ptr_maybe_nullptr(indptr), safe_ptr_maybe_nullptr(data));
safe_ptr_get(cast_to_cpp(dataset))
.add_buffer(safe_str_view(component), elements_per_scenario, total_elements, safe_ptr_maybe_nullptr(indptr),
safe_ptr_maybe_nullptr(data));
});
}

void PGM_dataset_const_add_attribute_buffer(PGM_Handle* handle, PGM_ConstDataset* dataset, char const* component,
char const* attribute, void const* data) {
call_with_catch(handle, [dataset, component, attribute, data] {
safe_ptr_get(dataset).add_attribute_buffer(safe_str_view(component), safe_str_view(attribute), safe_ptr(data));
safe_ptr_get(cast_to_cpp(dataset))
.add_attribute_buffer(safe_str_view(component), safe_str_view(attribute), safe_ptr(data));
});
}
void PGM_dataset_const_set_next_cartesian_product_dimension(PGM_Handle* handle, PGM_ConstDataset* dataset,
PGM_ConstDataset const* next_dataset) {
call_with_catch(handle, [dataset, next_dataset] {
safe_ptr_get(dataset).set_next_cartesian_product_dimension(safe_ptr(next_dataset));
safe_ptr_get(cast_to_cpp(dataset)).set_next_cartesian_product_dimension(safe_ptr(cast_to_cpp(next_dataset)));
});
}

PGM_DatasetInfo const* PGM_dataset_const_get_info(PGM_Handle* handle, PGM_ConstDataset const* dataset) {
return call_with_catch(handle, [dataset] { return &safe_ptr_get(dataset).get_description(); });
return call_with_catch(handle,
[dataset] { return cast_to_c(&safe_ptr_get(cast_to_cpp(dataset)).get_description()); });
}

// writable dataset

PGM_DatasetInfo const* PGM_dataset_writable_get_info(PGM_Handle* handle, PGM_WritableDataset const* dataset) {
return call_with_catch(handle, [dataset] { return &safe_ptr_get(dataset).get_description(); });
return call_with_catch(handle,
[dataset] { return cast_to_c(&safe_ptr_get(cast_to_cpp(dataset)).get_description()); });
}

void PGM_dataset_writable_set_buffer(PGM_Handle* handle, PGM_WritableDataset* dataset, char const* component,
PGM_Idx* indptr, void* data) {
call_with_catch(handle, [dataset, component, indptr, data] {
safe_ptr_get(dataset).set_buffer(safe_str_view(component), safe_ptr_maybe_nullptr(indptr),
safe_ptr_maybe_nullptr(data));
safe_ptr_get(cast_to_cpp(dataset))
.set_buffer(safe_str_view(component), safe_ptr_maybe_nullptr(indptr), safe_ptr_maybe_nullptr(data));
});
}

void PGM_dataset_writable_set_attribute_buffer(PGM_Handle* handle, PGM_WritableDataset* dataset, char const* component,
char const* attribute, void* data) {
call_with_catch(handle, [dataset, component, attribute, data] {
safe_ptr_get(dataset).set_attribute_buffer(safe_str_view(component), safe_str_view(attribute), safe_ptr(data));
safe_ptr_get(cast_to_cpp(dataset))
.set_attribute_buffer(safe_str_view(component), safe_str_view(attribute), safe_ptr(data));
});
}

Expand All @@ -162,31 +175,34 @@ void PGM_dataset_writable_set_attribute_buffer(PGM_Handle* handle, PGM_WritableD
PGM_MutableDataset* PGM_create_dataset_mutable(PGM_Handle* handle, char const* dataset, PGM_Idx is_batch,
PGM_Idx batch_size) {
return call_with_catch(handle, [dataset, is_batch, batch_size] {
return new MutableDataset{// NOSONAR(S5025)
safe_bool(is_batch), batch_size, safe_str_view(dataset), get_meta_data()};
return cast_to_c(new MutableDataset{// NOSONAR(S5025)
safe_bool(is_batch), batch_size, safe_str_view(dataset), get_meta_data()});
});
}

void PGM_destroy_dataset_mutable(PGM_MutableDataset* dataset) {
delete dataset; // NOSONAR(S5025)
delete cast_to_cpp(dataset); // NOSONAR(S5025)
}

void PGM_dataset_mutable_add_buffer(PGM_Handle* handle, PGM_MutableDataset* dataset, char const* component,
PGM_Idx elements_per_scenario, PGM_Idx total_elements, PGM_Idx const* indptr,
void* data) {
call_with_catch(handle, [dataset, component, elements_per_scenario, total_elements, indptr, data] {
safe_ptr_get(dataset).add_buffer(safe_str_view(component), elements_per_scenario, total_elements,
safe_ptr_maybe_nullptr(indptr), safe_ptr_maybe_nullptr(data));
safe_ptr_get(cast_to_cpp(dataset))
.add_buffer(safe_str_view(component), elements_per_scenario, total_elements, safe_ptr_maybe_nullptr(indptr),
safe_ptr_maybe_nullptr(data));
});
}

void PGM_dataset_mutable_add_attribute_buffer(PGM_Handle* handle, PGM_MutableDataset* dataset, char const* component,
char const* attribute, void* data) {
call_with_catch(handle, [dataset, component, attribute, data] {
safe_ptr_get(dataset).add_attribute_buffer(safe_str_view(component), safe_str_view(attribute), safe_ptr(data));
safe_ptr_get(cast_to_cpp(dataset))
.add_attribute_buffer(safe_str_view(component), safe_str_view(attribute), safe_ptr(data));
});
}

PGM_DatasetInfo const* PGM_dataset_mutable_get_info(PGM_Handle* handle, PGM_MutableDataset const* dataset) {
return call_with_catch(handle, [dataset] { return &safe_ptr_get(dataset).get_description(); });
return call_with_catch(handle,
[dataset] { return cast_to_c(&safe_ptr_get(cast_to_cpp(dataset)).get_description()); });
}
Loading
Loading