diff --git a/power_grid_model_c/power_grid_model_c/include/power_grid_model_c/basics.h b/power_grid_model_c/power_grid_model_c/include/power_grid_model_c/basics.h index 3e60e67f1..4f3911d2b 100644 --- a/power_grid_model_c/power_grid_model_c/include/power_grid_model_c/basics.h +++ b/power_grid_model_c/power_grid_model_c/include/power_grid_model_c/basics.h @@ -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. * @@ -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) diff --git a/power_grid_model_c/power_grid_model_c/src/buffer.cpp b/power_grid_model_c/power_grid_model_c/src/buffer.cpp index 4df924640..bc9881754 100644 --- a/power_grid_model_c/power_grid_model_c/src/buffer.cpp +++ b/power_grid_model_c/power_grid_model_c/src/buffer.cpp @@ -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; @@ -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*)); @@ -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 -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; @@ -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(safe_ptr_get(attribute), safe_ptr_maybe_nullptr(buffer_ptr), + buffer_get_set_value(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(safe_ptr_get(attribute), safe_ptr_maybe_nullptr(buffer_ptr), + buffer_get_set_value(safe_ptr_get(cast_to_cpp(attribute)), safe_ptr_maybe_nullptr(buffer_ptr), safe_ptr_maybe_nullptr(dest_ptr), buffer_offset, size, dest_stride); }); } diff --git a/power_grid_model_c/power_grid_model_c/src/dataset.cpp b/power_grid_model_c/power_grid_model_c/src/dataset.cpp index 15d15f5dc..241eaae88 100644 --- a/power_grid_model_c/power_grid_model_c/src/dataset.cpp +++ b/power_grid_model_c/power_grid_model_c/src/dataset.cpp @@ -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; @@ -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(safe_ptr_get(info).is_batch); }); + return call_with_catch(handle, [info] { return to_c_bool(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(safe_ptr_get(info).component_info.at(component_idx).has_attribute_indications); + return to_c_bool( + 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; }); } @@ -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)); }); } @@ -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()); }); } diff --git a/power_grid_model_c/power_grid_model_c/src/forward_declarations.hpp b/power_grid_model_c/power_grid_model_c/src/forward_declarations.hpp index f68d7def3..6dfcd0684 100644 --- a/power_grid_model_c/power_grid_model_c/src/forward_declarations.hpp +++ b/power_grid_model_c/power_grid_model_c/src/forward_declarations.hpp @@ -4,16 +4,20 @@ #pragma once -#ifndef PGM_DLL_EXPORTS -#define PGM_DLL_EXPORTS -#endif +#include "power_grid_model_c/basics.h" #include +#include + // forward declare all referenced struct/class in C++ core // alias them in the root namespace -namespace power_grid_model::meta_data { +namespace power_grid_model { + +class MainModel; + +namespace meta_data { struct MetaAttribute; struct MetaComponent; @@ -25,14 +29,78 @@ template class Dataset; struct DatasetInfo; -} // namespace power_grid_model::meta_data - -using PGM_MetaAttribute = power_grid_model::meta_data::MetaAttribute; -using PGM_MetaComponent = power_grid_model::meta_data::MetaComponent; -using PGM_MetaDataset = power_grid_model::meta_data::MetaDataset; -using PGM_Serializer = power_grid_model::meta_data::Serializer; -using PGM_Deserializer = power_grid_model::meta_data::Deserializer; -using PGM_ConstDataset = power_grid_model::meta_data::Dataset; -using PGM_MutableDataset = power_grid_model::meta_data::Dataset; -using PGM_WritableDataset = power_grid_model::meta_data::Dataset; -using PGM_DatasetInfo = power_grid_model::meta_data::DatasetInfo; +} // namespace meta_data + +} // namespace power_grid_model + +namespace power_grid_model_c { + +namespace detail { + +template struct c_cpp_type_map { + using c_type = c_type_input; + using cpp_type = cpp_type_input; + static_assert(!std::is_same_v, "C and C++ types in type map cannot be the same!"); +}; + +template struct type_mapping_list_impl { + template using get_cpp_type_t = void; + template using get_c_type_t = void; +}; +template struct type_mapping_list_impl { + + template + using get_cpp_type_t = + std::conditional_t, typename first_map::cpp_type, + typename type_mapping_list_impl::template get_cpp_type_t>; + + template + using get_c_type_t = + std::conditional_t, typename first_map::c_type, + typename type_mapping_list_impl::template get_c_type_t>; +}; + +using type_mapping_list = type_mapping_list_impl< + c_cpp_type_map, + c_cpp_type_map, + c_cpp_type_map, + c_cpp_type_map, + c_cpp_type_map, + c_cpp_type_map, + c_cpp_type_map>, + c_cpp_type_map>, + c_cpp_type_map>, + c_cpp_type_map>; + +template struct convert_ptr_to_cpp { + static constexpr bool is_const = std::is_const_v>; + using base_c_type = std::remove_const_t>; + using mapped_cpp_raw_type = type_mapping_list::get_cpp_type_t; + using mapped_cpp_type = std::conditional_t, mapped_cpp_raw_type>; + using type = mapped_cpp_type*; +}; +template using convert_ptr_to_cpp_t = typename convert_ptr_to_cpp::type; + +template struct convert_ptr_to_c { + static constexpr bool is_const = std::is_const_v>; + using base_cpp_type = std::remove_const_t>; + using mapped_c_raw_type = type_mapping_list::get_c_type_t; + using mapped_c_type = std::conditional_t, mapped_c_raw_type>; + using type = mapped_c_type*; +}; +template using convert_ptr_to_c_t = typename convert_ptr_to_c::type; + +template auto cast_to_cpp(CTypePtr ptr) { + return reinterpret_cast>(ptr); +} + +template auto cast_to_c(CPPTypePtr ptr) { + return reinterpret_cast>(ptr); +} + +} // namespace detail + +using detail::cast_to_c; +using detail::cast_to_cpp; + +} // namespace power_grid_model_c diff --git a/power_grid_model_c/power_grid_model_c/src/meta_data.cpp b/power_grid_model_c/power_grid_model_c/src/meta_data.cpp index bc31abdcd..0a698b6da 100644 --- a/power_grid_model_c/power_grid_model_c/src/meta_data.cpp +++ b/power_grid_model_c/power_grid_model_c/src/meta_data.cpp @@ -20,6 +20,8 @@ namespace { using namespace power_grid_model; 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_ptr_get; using power_grid_model_c::safe_str_view; using power_grid_model_c::to_c_bool; @@ -63,31 +65,32 @@ PGM_MetaDataset const* PGM_meta_get_dataset_by_idx(PGM_Handle* handle, PGM_Idx i if (idx < 0 || idx >= get_meta_data().n_datasets()) { throw std::out_of_range{"Index out of range!\n"}; } - return &get_meta_data().datasets[idx]; + return cast_to_c(&get_meta_data().datasets[idx]); }, ranged_exception_handler); } PGM_MetaDataset const* PGM_meta_get_dataset_by_name(PGM_Handle* handle, char const* dataset) { return call_with_catch( - handle, [dataset] { return &get_meta_data().get_dataset(safe_str_view(dataset)); }, ranged_exception_handler); + handle, [dataset] { return cast_to_c(&get_meta_data().get_dataset(safe_str_view(dataset))); }, + ranged_exception_handler); } char const* PGM_meta_dataset_name(PGM_Handle* handle, PGM_MetaDataset const* dataset) { - return call_with_catch(handle, [dataset] { return safe_ptr_get(dataset).name; }); + return call_with_catch(handle, [dataset] { return safe_ptr_get(cast_to_cpp(dataset)).name; }); } // component PGM_Idx PGM_meta_n_components(PGM_Handle* handle, PGM_MetaDataset const* dataset) { - return call_with_catch(handle, [dataset] { return safe_ptr_get(dataset).n_components(); }); + return call_with_catch(handle, [dataset] { return safe_ptr_get(cast_to_cpp(dataset)).n_components(); }); } PGM_MetaComponent const* PGM_meta_get_component_by_idx(PGM_Handle* handle, PGM_MetaDataset const* dataset, PGM_Idx idx) { return call_with_catch( handle, [idx, dataset] { - auto const& safe_dataset = safe_ptr_get(dataset); + auto const& safe_dataset = safe_ptr_get(cast_to_cpp(dataset)); if (idx < 0 || idx >= safe_dataset.n_components()) { throw std::out_of_range{"Index out of range!\n"}; } - return &safe_dataset.components[idx]; + return cast_to_c(&safe_dataset.components[idx]); }, ranged_exception_handler); } @@ -96,33 +99,34 @@ PGM_MetaComponent const* PGM_meta_get_component_by_name(PGM_Handle* handle, char return call_with_catch( handle, [component, dataset] { - return &get_meta_data().get_dataset(safe_str_view(dataset)).get_component(safe_str_view(component)); + return cast_to_c( + &get_meta_data().get_dataset(safe_str_view(dataset)).get_component(safe_str_view(component))); }, ranged_exception_handler); } char const* PGM_meta_component_name(PGM_Handle* handle, PGM_MetaComponent const* component) { - return call_with_catch(handle, [component] { return safe_ptr_get(component).name; }); + return call_with_catch(handle, [component] { return safe_ptr_get(cast_to_cpp(component)).name; }); } size_t PGM_meta_component_size(PGM_Handle* handle, PGM_MetaComponent const* component) { - return call_with_catch(handle, [component] { return safe_ptr_get(component).size; }); + return call_with_catch(handle, [component] { return safe_ptr_get(cast_to_cpp(component)).size; }); } size_t PGM_meta_component_alignment(PGM_Handle* handle, PGM_MetaComponent const* component) { - return call_with_catch(handle, [component] { return safe_ptr_get(component).alignment; }); + return call_with_catch(handle, [component] { return safe_ptr_get(cast_to_cpp(component)).alignment; }); } // attributes PGM_Idx PGM_meta_n_attributes(PGM_Handle* handle, PGM_MetaComponent const* component) { - return call_with_catch(handle, [component] { return safe_ptr_get(component).n_attributes(); }); + return call_with_catch(handle, [component] { return safe_ptr_get(cast_to_cpp(component)).n_attributes(); }); } PGM_MetaAttribute const* PGM_meta_get_attribute_by_idx(PGM_Handle* handle, PGM_MetaComponent const* component, PGM_Idx idx) { return call_with_catch( handle, [idx, component] { - auto const& safe_component = safe_ptr_get(component); + auto const& safe_component = safe_ptr_get(cast_to_cpp(component)); if (idx < 0 || idx >= safe_component.n_attributes()) { throw std::out_of_range{"Index out of range!\n"}; } - return &safe_component.attributes[idx]; + return cast_to_c(&safe_component.attributes[idx]); }, ranged_exception_handler); } @@ -131,20 +135,20 @@ PGM_MetaAttribute const* PGM_meta_get_attribute_by_name(PGM_Handle* handle, char return call_with_catch( handle, [component, dataset, attribute] { - return &get_meta_data() - .get_dataset(safe_str_view(dataset)) - .get_component(safe_str_view(component)) - .get_attribute(safe_str_view(attribute)); + return cast_to_c(&get_meta_data() + .get_dataset(safe_str_view(dataset)) + .get_component(safe_str_view(component)) + .get_attribute(safe_str_view(attribute))); }, ranged_exception_handler); } char const* PGM_meta_attribute_name(PGM_Handle* handle, PGM_MetaAttribute const* attribute) { - return call_with_catch(handle, [attribute] { return safe_ptr_get(attribute).name; }); + return call_with_catch(handle, [attribute] { return safe_ptr_get(cast_to_cpp(attribute)).name; }); } PGM_Idx PGM_meta_attribute_ctype(PGM_Handle* handle, PGM_MetaAttribute const* attribute) { - return call_with_catch(handle, [attribute] { return to_c_enum(safe_ptr_get(attribute).ctype); }); + return call_with_catch(handle, [attribute] { return to_c_enum(safe_ptr_get(cast_to_cpp(attribute)).ctype); }); } size_t PGM_meta_attribute_offset(PGM_Handle* handle, PGM_MetaAttribute const* attribute) { - return call_with_catch(handle, [attribute] { return safe_ptr_get(attribute).offset; }); + return call_with_catch(handle, [attribute] { return safe_ptr_get(cast_to_cpp(attribute)).offset; }); } int PGM_is_little_endian(PGM_Handle* /* handle */) { return to_c_bool(meta_data::is_little_endian()); } diff --git a/power_grid_model_c/power_grid_model_c/src/model.cpp b/power_grid_model_c/power_grid_model_c/src/model.cpp index 00ab5bee3..6e8378003 100644 --- a/power_grid_model_c/power_grid_model_c/src/model.cpp +++ b/power_grid_model_c/power_grid_model_c/src/model.cpp @@ -23,6 +23,8 @@ namespace { using namespace power_grid_model; 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::get_math_solver_dispatcher; using power_grid_model_c::safe_enum; using power_grid_model_c::safe_ptr; @@ -31,31 +33,28 @@ using power_grid_model_c::safe_ptr_maybe_nullptr; using power_grid_model_c::safe_str_view; } // namespace -// aliases main class -struct PGM_PowerGridModel : public MainModel { - using MainModel::MainModel; -}; - // create model PGM_PowerGridModel* PGM_create_model(PGM_Handle* handle, double system_frequency, PGM_ConstDataset const* input_dataset) { return call_with_catch(handle, [system_frequency, input_dataset] { - return new PGM_PowerGridModel{// NOSONAR(S5025) - system_frequency, safe_ptr_get(input_dataset), get_math_solver_dispatcher(), 0}; + return cast_to_c( + new MainModel{// NOSONAR(S5025) + system_frequency, safe_ptr_get(cast_to_cpp(input_dataset)), get_math_solver_dispatcher(), 0}); }); } // update model void PGM_update_model(PGM_Handle* handle, PGM_PowerGridModel* model, PGM_ConstDataset const* update_dataset) { call_with_catch(handle, [model, update_dataset] { - model->update_components(safe_ptr_get(update_dataset)); + safe_ptr_get(cast_to_cpp(model)) + .update_components(safe_ptr_get(cast_to_cpp(update_dataset))); }); } // copy model PGM_PowerGridModel* PGM_copy_model(PGM_Handle* handle, PGM_PowerGridModel const* model) { return call_with_catch(handle, [model] { - return new PGM_PowerGridModel{safe_ptr_get(model)}; // NOSONAR(S5025) + return cast_to_c(new MainModel{safe_ptr_get(cast_to_cpp(model))}); // NOSONAR(S5025) }); } @@ -63,7 +62,7 @@ PGM_PowerGridModel* PGM_copy_model(PGM_Handle* handle, PGM_PowerGridModel const* void PGM_get_indexer(PGM_Handle* handle, PGM_PowerGridModel const* model, char const* component, PGM_Idx size, PGM_ID const* ids, PGM_Idx* indexer) { call_with_catch(handle, [model, component, size, ids, indexer] { - safe_ptr_get(model).get_indexer(safe_str_view(component), safe_ptr(ids), size, safe_ptr(indexer)); + safe_ptr_get(cast_to_cpp(model)).get_indexer(safe_str_view(component), safe_ptr(ids), size, safe_ptr(indexer)); }); } @@ -157,7 +156,7 @@ class BadCalculationRequest : public PowerGridError { explicit BadCalculationRequest(std::string msg) : PowerGridError{std::move(msg)} {} }; -void calculate_single_batch_dimension_impl(PGM_PowerGridModel& model, PGM_Options const& opt, +void calculate_single_batch_dimension_impl(MainModel& model, PGM_Options const& opt, MutableDataset const& output_dataset, ConstDataset const* batch_dataset) { // check dataset integrity if ((batch_dataset != nullptr) && (!batch_dataset->is_batch() || !output_dataset.is_batch())) { @@ -165,9 +164,9 @@ void calculate_single_batch_dimension_impl(PGM_PowerGridModel& model, PGM_Option "If batch_dataset is provided. Both batch_dataset and output_dataset should be a batch!\n"}; } - ConstDataset const& exported_update_dataset = - batch_dataset != nullptr ? safe_ptr_get(batch_dataset) - : PGM_ConstDataset{false, 1, "update", output_dataset.meta_data()}; + ConstDataset const& exported_update_dataset = batch_dataset != nullptr + ? safe_ptr_get(batch_dataset) + : ConstDataset{false, 1, "update", output_dataset.meta_data()}; check_calculate_valid_options(opt); auto const options = extract_calculation_options(opt); @@ -240,7 +239,7 @@ class MDBatchExceptionHandler : public power_grid_model_c::DefaultExceptionHandl Idx stride_size_{}; }; -Idx get_batch_dimension(PGM_ConstDataset const* batch_dataset) { +Idx get_batch_dimension(ConstDataset const* batch_dataset) { Idx dimension = 0; while (batch_dataset != nullptr) { ++dimension; @@ -249,9 +248,9 @@ Idx get_batch_dimension(PGM_ConstDataset const* batch_dataset) { return dimension; } -Idx get_stride_size(PGM_ConstDataset const* batch_dataset) { +Idx get_stride_size(ConstDataset const* batch_dataset) { Idx size = 1; - PGM_ConstDataset const* current = batch_dataset->get_next_cartesian_product_dimension(); + ConstDataset const* current = batch_dataset->get_next_cartesian_product_dimension(); while (current != nullptr) { size *= current->batch_size(); current = current->get_next_cartesian_product_dimension(); @@ -260,8 +259,8 @@ Idx get_stride_size(PGM_ConstDataset const* batch_dataset) { } // run calculation -void calculate_multi_dimensional_impl(PGM_PowerGridModel& model, PGM_Options const& opt, - PGM_MutableDataset const& output_dataset, PGM_ConstDataset const* batch_dataset) { +void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt, MutableDataset const& output_dataset, + ConstDataset const* batch_dataset) { // for dimension < 2 (one-time or 1D batch), call implementation directly if (auto const batch_dimension = get_batch_dimension(batch_dataset); batch_dimension < 2) { calculate_single_batch_dimension_impl(model, opt, output_dataset, batch_dataset); @@ -283,12 +282,12 @@ void calculate_multi_dimensional_impl(PGM_PowerGridModel& model, PGM_Options con &local_handle, [&model, &opt, &output_dataset, &safe_batch_dataset, i, stride_size] { // create sliced datasets for the rest of dimensions - PGM_ConstDataset const single_update_dataset = safe_batch_dataset.get_individual_scenario(i); - PGM_MutableDataset const sliced_output_dataset = + ConstDataset const single_update_dataset = safe_batch_dataset.get_individual_scenario(i); + MutableDataset const sliced_output_dataset = output_dataset.get_slice_scenario(i * stride_size, (i + 1) * stride_size); // create a model copy - PGM_PowerGridModel local_model{model}; + MainModel local_model{model}; // apply the update local_model.update_components(single_update_dataset); @@ -314,13 +313,14 @@ void PGM_calculate(PGM_Handle* handle, PGM_PowerGridModel* model, PGM_Options co call_with_catch( handle, [model, opt, output_dataset, batch_dataset] { - calculate_multi_dimensional_impl(safe_ptr_get(model), safe_ptr_get(opt), safe_ptr_get(output_dataset), - safe_ptr_maybe_nullptr(batch_dataset)); + calculate_multi_dimensional_impl(safe_ptr_get(cast_to_cpp(model)), safe_ptr_get(opt), + safe_ptr_get(cast_to_cpp(output_dataset)), + safe_ptr_maybe_nullptr(cast_to_cpp(batch_dataset))); }, batch_exception_handler); } // destroy model void PGM_destroy_model(PGM_PowerGridModel* model) { - delete model; // NOSONAR(S5025) + delete cast_to_cpp(model); // NOSONAR(S5025) } diff --git a/power_grid_model_c/power_grid_model_c/src/serialization.cpp b/power_grid_model_c/power_grid_model_c/src/serialization.cpp index 44b2c8d07..a98ffd87a 100644 --- a/power_grid_model_c/power_grid_model_c/src/serialization.cpp +++ b/power_grid_model_c/power_grid_model_c/src/serialization.cpp @@ -21,6 +21,8 @@ namespace { 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_enum; using power_grid_model_c::safe_ptr; @@ -40,11 +42,11 @@ PGM_Deserializer* PGM_create_deserializer_from_binary_buffer(PGM_Handle* handle, return call_with_catch( handle, [data, size, serialization_format] { - return new PGM_Deserializer{// NOSONAR(S5025) - from_buffer, - {safe_ptr(data), safe_size(size)}, - safe_enum(serialization_format), - get_meta_data()}; + return cast_to_c(new Deserializer{// NOSONAR(S5025) + from_buffer, + {safe_ptr(data), safe_size(size)}, + safe_enum(serialization_format), + get_meta_data()}); }, serialization_exception_handler); } @@ -54,26 +56,28 @@ PGM_Deserializer* PGM_create_deserializer_from_null_terminated_string(PGM_Handle return call_with_catch( handle, [data_string, serialization_format] { - return new PGM_Deserializer{// NOSONAR(S5025) - from_string, safe_ptr(data_string), - safe_enum(serialization_format), - get_meta_data()}; + return cast_to_c(new Deserializer{// NOSONAR(S5025) + from_string, safe_ptr(data_string), + safe_enum(serialization_format), + get_meta_data()}); }, serialization_exception_handler); } PGM_WritableDataset* PGM_deserializer_get_dataset(PGM_Handle* handle, PGM_Deserializer* deserializer) { - return call_with_catch(handle, [deserializer] { return &safe_ptr_get(deserializer).get_dataset_info(); }); + return call_with_catch( + handle, [deserializer] { return cast_to_c(&safe_ptr_get(cast_to_cpp(deserializer)).get_dataset_info()); }); } void PGM_deserializer_parse_to_buffer(PGM_Handle* handle, PGM_Deserializer* deserializer) { - call_with_catch(handle, [deserializer] { safe_ptr_get(deserializer).parse(); }, serialization_exception_handler); + call_with_catch( + handle, [deserializer] { safe_ptr_get(cast_to_cpp(deserializer)).parse(); }, serialization_exception_handler); } // false warning from clang-tidy // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDelete) void PGM_destroy_deserializer(PGM_Deserializer* deserializer) { - delete deserializer; // NOSONAR(S5025) + delete cast_to_cpp(deserializer); // NOSONAR(S5025) } PGM_Serializer* PGM_create_serializer(PGM_Handle* handle, PGM_ConstDataset const* dataset, @@ -81,9 +85,9 @@ PGM_Serializer* PGM_create_serializer(PGM_Handle* handle, PGM_ConstDataset const return call_with_catch( handle, [dataset, serialization_format] { - return new PGM_Serializer{// NOSONAR(S5025) - safe_ptr_get(dataset), - safe_enum(serialization_format)}; + return cast_to_c(new Serializer{// NOSONAR(S5025) + safe_ptr_get(cast_to_cpp(dataset)), + safe_enum(serialization_format)}); }, serialization_exception_handler); } @@ -93,7 +97,8 @@ void PGM_serializer_get_to_binary_buffer(PGM_Handle* handle, PGM_Serializer* ser call_with_catch( handle, [serializer, use_compact_list, data, size] { - auto const buffer_data = safe_ptr_get(serializer).get_binary_buffer(safe_bool(use_compact_list)); + auto const buffer_data = + safe_ptr_get(cast_to_cpp(serializer)).get_binary_buffer(safe_bool(use_compact_list)); *data = buffer_data.data(); *size = to_c_size(std::ssize(buffer_data)); }, @@ -105,11 +110,11 @@ char const* PGM_serializer_get_to_zero_terminated_string(PGM_Handle* handle, PGM return call_with_catch( handle, [serializer, use_compact_list, indent] { - return safe_ptr_get(serializer).get_string(safe_bool(use_compact_list), indent).c_str(); + return safe_ptr_get(cast_to_cpp(serializer)).get_string(safe_bool(use_compact_list), indent).c_str(); }, serialization_exception_handler); } void PGM_destroy_serializer(PGM_Serializer* serializer) { - delete serializer; // NOSONAR(S5025) + delete cast_to_cpp(serializer); // NOSONAR(S5025) }