Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -81,7 +81,6 @@ 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 +136,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
12 changes: 7 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 @@ -17,19 +17,21 @@
namespace {
using namespace power_grid_model;

using meta_data::MetaAttribute;
using meta_data::RawDataConstPtr;
using meta_data::RawDataPtr;
using power_grid_model_c::call_with_catch;
using power_grid_model_c::safe_ptr;
using power_grid_model_c::safe_ptr_get;
using power_grid_model_c::safe_ptr_maybe_nullptr;
using power_grid_model_c::to_c_size;
using power_grid_model_c::unwrap;
} // namespace

// 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 = unwrap(safe_ptr_get(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 +56,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);
unwrap(safe_ptr_get(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(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 +92,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>(unwrap(safe_ptr_get(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>(unwrap(safe_ptr_get(attribute)), safe_ptr_maybe_nullptr(buffer_ptr),
safe_ptr_maybe_nullptr(dest_ptr), buffer_offset, size, dest_stride);
});
}
50 changes: 41 additions & 9 deletions power_grid_model_c/power_grid_model_c/src/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <power_grid_model/auxiliary/meta_data.hpp>
#include <power_grid_model/common/typing.hpp>

namespace {
using namespace power_grid_model;
using namespace power_grid_model::meta_data;
using power_grid_model_c::call_with_catch;
Expand All @@ -25,6 +26,37 @@ using power_grid_model_c::safe_ptr_maybe_nullptr;
using power_grid_model_c::safe_str_view;
using power_grid_model_c::to_c_bool;
using power_grid_model_c::to_c_size;
using power_grid_model_c::wrap;
} // namespace

struct PGM_ConstDataset : public power_grid_model::meta_data::ConstDataset {
using Dataset::Dataset;
};
struct PGM_MutableDataset : public power_grid_model::meta_data::MutableDataset {
using Dataset::Dataset;
};
struct PGM_WritableDataset : public power_grid_model::meta_data::WritableDataset {
using Dataset::Dataset;
};
struct PGM_DatasetInfo : public power_grid_model::meta_data::DatasetInfo {};

namespace power_grid_model_c {
ConstDataset const* unwrap(PGM_ConstDataset const* instance) {
return static_cast<ConstDataset const*>(instance); // may invoke undefined behavior
}
ConstDataset const& unwrap(PGM_ConstDataset const& instance) {
return static_cast<ConstDataset const&>(instance); // may invoke undefined behavior
}
MutableDataset const& unwrap(PGM_MutableDataset const& instance) {
return static_cast<MutableDataset const&>(instance); // may invoke undefined behavior
}
PGM_WritableDataset& wrap(WritableDataset& instance) {
return static_cast<PGM_WritableDataset&>(instance); // may invoke undefined behavior
}
PGM_DatasetInfo const& wrap(DatasetInfo const& instance) {
return static_cast<PGM_DatasetInfo const&>(instance); // may invoke undefined behavior
}
} // namespace power_grid_model_c
Comment thread
mgovers marked this conversation as resolved.
Outdated

// dataset info

Expand Down Expand Up @@ -88,21 +120,21 @@ 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 new PGM_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 new PGM_ConstDataset{safe_ptr_get(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 new PGM_ConstDataset{safe_ptr_get(mutable_dataset)}; // NOSONAR(S5025)
});
}

Expand Down Expand Up @@ -133,13 +165,13 @@ void PGM_dataset_const_set_next_cartesian_product_dimension(PGM_Handle* handle,
}

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 &wrap(safe_ptr_get(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 &wrap(safe_ptr_get(dataset).get_description()); });
}

void PGM_dataset_writable_set_buffer(PGM_Handle* handle, PGM_WritableDataset* dataset, char const* component,
Expand All @@ -162,8 +194,8 @@ 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 new PGM_MutableDataset{// NOSONAR(S5025)
safe_bool(is_batch), batch_size, safe_str_view(dataset), get_meta_data()};
});
}

Expand All @@ -188,5 +220,5 @@ void PGM_dataset_mutable_add_attribute_buffer(PGM_Handle* handle, PGM_MutableDat
}

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 &wrap(safe_ptr_get(dataset).get_description()); });
}
41 changes: 30 additions & 11 deletions power_grid_model_c/power_grid_model_c/src/forward_declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

#include <power_grid_model/auxiliary/dataset_fwd.hpp>

// forward declare all referenced struct/class in C++ core
// alias them in the root namespace
#include "power_grid_model_c/basics.h"

// // forward declare all referenced struct/class in C++ core
// // alias them in the root namespace

namespace power_grid_model::meta_data {

Expand All @@ -22,17 +24,34 @@ class Serializer;
class Deserializer;

template <dataset_type_tag dataset_type> class Dataset;
using ConstDataset = Dataset<const_dataset_t>;
using MutableDataset = Dataset<mutable_dataset_t>;
using WritableDataset = Dataset<writable_dataset_t>;

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<power_grid_model::const_dataset_t>;
using PGM_MutableDataset = power_grid_model::meta_data::Dataset<power_grid_model::mutable_dataset_t>;
using PGM_WritableDataset = power_grid_model::meta_data::Dataset<power_grid_model::writable_dataset_t>;
using PGM_DatasetInfo = power_grid_model::meta_data::DatasetInfo;
namespace power_grid_model_c {
using power_grid_model::meta_data::ConstDataset;
using power_grid_model::meta_data::Dataset;
using power_grid_model::meta_data::DatasetInfo;
using power_grid_model::meta_data::Deserializer;
using power_grid_model::meta_data::MetaAttribute;
using power_grid_model::meta_data::MetaComponent;
using power_grid_model::meta_data::MetaDataset;
using power_grid_model::meta_data::MutableDataset;
using power_grid_model::meta_data::Serializer;
using power_grid_model::meta_data::WritableDataset;

MetaComponent const& unwrap(PGM_MetaComponent const&);
MetaAttribute const& unwrap(PGM_MetaAttribute const&);
ConstDataset const& unwrap(PGM_ConstDataset const&);
ConstDataset const* unwrap(PGM_ConstDataset const*);
MutableDataset const& unwrap(PGM_MutableDataset const&);
PGM_WritableDataset& wrap(WritableDataset&);
PGM_DatasetInfo const& wrap(DatasetInfo const&);
PGM_MetaDataset const& wrap(MetaDataset const&);
PGM_MetaComponent const& wrap(MetaComponent const&);
PGM_MetaAttribute const& wrap(MetaAttribute const&);
} // namespace power_grid_model_c
42 changes: 33 additions & 9 deletions power_grid_model_c/power_grid_model_c/src/meta_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ using power_grid_model_c::safe_ptr_get;
using power_grid_model_c::safe_str_view;
using power_grid_model_c::to_c_bool;
using power_grid_model_c::to_c_enum;
using power_grid_model_c::wrap;

// assert index type
static_assert(std::is_same_v<PGM_Idx, Idx>);
Expand All @@ -49,6 +50,28 @@ struct RangedExceptionHandler : public power_grid_model_c::DefaultExceptionHandl
constexpr RangedExceptionHandler ranged_exception_handler{};
} // namespace

struct PGM_MetaAttribute : public power_grid_model::meta_data::MetaAttribute {};
struct PGM_MetaComponent : public power_grid_model::meta_data::MetaComponent {};
struct PGM_MetaDataset : public power_grid_model::meta_data::MetaDataset {};

namespace power_grid_model_c {
MetaComponent const& unwrap(PGM_MetaComponent const& instance) {
return static_cast<MetaComponent const&>(instance); // may invoke undefined behavior
}
MetaAttribute const& unwrap(PGM_MetaAttribute const& instance) {
return static_cast<MetaAttribute const&>(instance); // may invoke undefined behavior
}
PGM_MetaAttribute const& wrap(MetaAttribute const& instance) {
return static_cast<PGM_MetaAttribute const&>(instance); // may invoke undefined behavior
}
PGM_MetaComponent const& wrap(MetaComponent const& instance) {
return static_cast<PGM_MetaComponent const&>(instance); // may invoke undefined behavior
}
PGM_MetaDataset const& wrap(MetaDataset const& instance) {
return static_cast<PGM_MetaDataset const&>(instance); // may invoke undefined behavior
}
} // namespace power_grid_model_c

// retrieve meta data
power_grid_model::meta_data::MetaData const& get_meta_data() {
return power_grid_model::meta_data::meta_data_gen::meta_data;
Expand All @@ -63,13 +86,14 @@ 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 &wrap(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 &wrap(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; });
Expand All @@ -87,7 +111,7 @@ PGM_MetaComponent const* PGM_meta_get_component_by_idx(PGM_Handle* handle, PGM_M
if (idx < 0 || idx >= safe_dataset.n_components()) {
throw std::out_of_range{"Index out of range!\n"};
}
return &safe_dataset.components[idx];
return &wrap(safe_dataset.components[idx]);
},
ranged_exception_handler);
}
Expand All @@ -96,7 +120,7 @@ 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 &wrap(get_meta_data().get_dataset(safe_str_view(dataset)).get_component(safe_str_view(component)));
},
ranged_exception_handler);
}
Expand All @@ -122,7 +146,7 @@ PGM_MetaAttribute const* PGM_meta_get_attribute_by_idx(PGM_Handle* handle, PGM_M
if (idx < 0 || idx >= safe_component.n_attributes()) {
throw std::out_of_range{"Index out of range!\n"};
}
return &safe_component.attributes[idx];
return &wrap(safe_component.attributes[idx]);
},
ranged_exception_handler);
}
Expand All @@ -131,10 +155,10 @@ 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 &wrap(get_meta_data()
.get_dataset(safe_str_view(dataset))
.get_component(safe_str_view(component))
.get_attribute(safe_str_view(attribute)));
},
ranged_exception_handler);
}
Expand Down
Loading
Loading