Skip to content

Commit

Permalink
Merge pull request #747 from PowerGridModel/fix/remove-static-functions
Browse files Browse the repository at this point in the history
Remove static functions in CPP wrapper
  • Loading branch information
TonyXiang8787 authored Sep 30, 2024
2 parents 56818bd + c5b7e6d commit dec5729
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,32 @@ class Buffer {

Idx size() const { return size_; }

static void set_nan(Buffer& buffer, Idx buffer_offset, Idx size) {
buffer.handle_.call_with(PGM_buffer_set_nan, buffer.component_, buffer.buffer_.get(), buffer_offset, size);
void set_nan() { set_nan(0, size_); }
void set_nan(Idx buffer_offset) { set_nan(buffer_offset, 1); }
void set_nan(Idx buffer_offset, Idx size) {
handle_.call_with(PGM_buffer_set_nan, component_, buffer_.get(), buffer_offset, size);
}
void set_nan() { set_nan(*this, 0, size_); }
void set_nan(Idx buffer_offset) { set_nan(*this, buffer_offset, 1); }
void set_nan(Idx buffer_offset, Idx size) { set_nan(*this, buffer_offset, size); }

static void set_value(MetaAttribute const* attribute, Buffer& buffer, // NOSONAR: no reference-to-const
RawDataConstPtr src_ptr, Idx buffer_offset, Idx size, Idx src_stride) {
buffer.handle_.call_with(PGM_buffer_set_value, attribute, buffer.buffer_.get(), src_ptr, buffer_offset, size,
src_stride);
}
void set_value(MetaAttribute const* attribute, RawDataConstPtr src_ptr, Idx src_stride) {
set_value(attribute, *this, src_ptr, 0, size_, src_stride);
set_value(attribute, src_ptr, 0, size_, src_stride);
}
void set_value(MetaAttribute const* attribute, RawDataConstPtr src_ptr, Idx buffer_offset, Idx src_stride) {
set_value(attribute, *this, src_ptr, buffer_offset, 1, src_stride);
set_value(attribute, src_ptr, buffer_offset, 1, src_stride);
}
void set_value(MetaAttribute const* attribute, RawDataConstPtr src_ptr, Idx buffer_offset, Idx size,
Idx src_stride) {
set_value(attribute, *this, src_ptr, buffer_offset, size, src_stride);
Idx src_stride) { // NOSONAR: no-const
handle_.call_with(PGM_buffer_set_value, attribute, buffer_.get(), src_ptr, buffer_offset, size, src_stride);
}

static void get_value(MetaAttribute const* attribute, Buffer const& buffer, RawDataPtr dest_ptr, Idx buffer_offset,
Idx size, Idx dest_stride) {
buffer.handle_.call_with(PGM_buffer_get_value, attribute, buffer.buffer_.get(), dest_ptr, buffer_offset, size,
dest_stride);
}
void get_value(MetaAttribute const* attribute, RawDataPtr dest_ptr, Idx dest_stride) const {
get_value(attribute, *this, dest_ptr, 0, size_, dest_stride);
get_value(attribute, dest_ptr, 0, size_, dest_stride);
}
void get_value(MetaAttribute const* attribute, RawDataPtr dest_ptr, Idx buffer_offset, Idx dest_stride) const {
get_value(attribute, *this, dest_ptr, buffer_offset, 1, dest_stride);
get_value(attribute, dest_ptr, buffer_offset, 1, dest_stride);
}
void get_value(MetaAttribute const* attribute, RawDataPtr dest_ptr, Idx buffer_offset, Idx size,
Idx dest_stride) const {
get_value(attribute, *this, dest_ptr, buffer_offset, size, dest_stride);
handle_.call_with(PGM_buffer_get_value, attribute, buffer_.get(), dest_ptr, buffer_offset, size, dest_stride);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,25 @@ class DatasetInfo {
DatasetInfo& operator=(const DatasetInfo&) = delete; // No copy assignment
~DatasetInfo() = default;

static std::string name(DatasetInfo const& info) {
return std::string{info.handle_.call_with(PGM_dataset_info_name, info.info_)};
}
std::string name() const { return name(*this); }
std::string name() const { return std::string{handle_.call_with(PGM_dataset_info_name, info_)}; }

static Idx is_batch(DatasetInfo const& info) {
return info.handle_.call_with(PGM_dataset_info_is_batch, info.info_);
}
Idx is_batch() const { return is_batch(*this); }
Idx is_batch() const { return handle_.call_with(PGM_dataset_info_is_batch, info_); }

static Idx batch_size(DatasetInfo const& info) {
return info.handle_.call_with(PGM_dataset_info_batch_size, info.info_);
}
Idx batch_size() const { return batch_size(*this); }
Idx batch_size() const { return handle_.call_with(PGM_dataset_info_batch_size, info_); }

static Idx n_components(DatasetInfo const& info) {
return info.handle_.call_with(PGM_dataset_info_n_components, info.info_);
}
Idx n_components() const { return n_components(*this); }
Idx n_components() const { return handle_.call_with(PGM_dataset_info_n_components, info_); }

static std::string component_name(DatasetInfo const& info, Idx component_idx) {
return std::string{info.handle_.call_with(PGM_dataset_info_component_name, info.info_, component_idx)};
std::string component_name(Idx component_idx) const {
return std::string{handle_.call_with(PGM_dataset_info_component_name, info_, component_idx)};
}
std::string component_name(Idx component_idx) const { return component_name(*this, component_idx); }

static Idx component_elements_per_scenario(DatasetInfo const& info, Idx component_idx) {
return info.handle_.call_with(PGM_dataset_info_elements_per_scenario, info.info_, component_idx);
}
Idx component_elements_per_scenario(Idx component_idx) const {
return component_elements_per_scenario(*this, component_idx);
return handle_.call_with(PGM_dataset_info_elements_per_scenario, info_, component_idx);
}

static Idx component_total_elements(DatasetInfo const& info, Idx component_idx) {
return info.handle_.call_with(PGM_dataset_info_total_elements, info.info_, component_idx);
Idx component_total_elements(Idx component_idx) const {
return handle_.call_with(PGM_dataset_info_total_elements, info_, component_idx);
}
Idx component_total_elements(Idx component_idx) const { return component_total_elements(*this, component_idx); }

private:
Handle handle_{};
Expand All @@ -77,42 +60,24 @@ class DatasetWritable {

RawWritableDataset* get() const { return dataset_; }

static DatasetInfo const& get_info(DatasetWritable const& dataset) { return dataset.info_; }
DatasetInfo const& get_info() const { return get_info(*this); }
DatasetInfo const& get_info() const { return info_; }

static void set_buffer(DatasetWritable& dataset, // NOSONAR: no reference-to-const
std::string const& component, Idx* indptr, RawDataPtr data) {
dataset.handle_.call_with(PGM_dataset_writable_set_buffer, dataset.dataset_, component.c_str(), indptr, data);
}
void set_buffer(std::string const& component, Idx* indptr, RawDataPtr data) {
set_buffer(*this, component, indptr, data);
handle_.call_with(PGM_dataset_writable_set_buffer, dataset_, component.c_str(), indptr, data);
}

static void set_buffer(DatasetWritable& dataset, // NOSONAR: no reference-to-const
std::string const& component, Idx* indptr, Buffer const& data) {
dataset.handle_.call_with(PGM_dataset_writable_set_buffer, dataset.dataset_, component.c_str(), indptr,
data.get());
}
void set_buffer(std::string const& component, Idx* indptr, Buffer const& data) {
set_buffer(*this, component, indptr, data);
handle_.call_with(PGM_dataset_writable_set_buffer, dataset_, component.c_str(), indptr, data.get());
}

static void set_attribute_buffer(DatasetWritable& dataset, // NOSONAR: no reference-to-const
std::string const& component, std::string const& attribute, RawDataPtr data) {
dataset.handle_.call_with(PGM_dataset_writable_set_attribute_buffer, dataset.dataset_, component.c_str(),
attribute.c_str(), data);
}
void set_attribute_buffer(std::string const& component, std::string const& attribute, RawDataPtr data) {
set_attribute_buffer(*this, component, attribute, data);
handle_.call_with(PGM_dataset_writable_set_attribute_buffer, dataset_, component.c_str(), attribute.c_str(),
data);
}

static void set_attribute_buffer(DatasetWritable& dataset, // NOSONAR: no reference-to-const
std::string const& component, std::string const& attribute, Buffer const& data) {
dataset.handle_.call_with(PGM_dataset_writable_set_attribute_buffer, dataset.dataset_, component.c_str(),
attribute.c_str(), data.get());
}
void set_attribute_buffer(std::string const& component, std::string const& attribute, Buffer const& data) {
set_attribute_buffer(*this, component, attribute, data);
handle_.call_with(PGM_dataset_writable_set_attribute_buffer, dataset_, component.c_str(), attribute.c_str(),
data.get());
}

private:
Expand All @@ -129,48 +94,31 @@ class DatasetMutable {

RawMutableDataset* get() const { return dataset_.get(); }

static void add_buffer(DatasetMutable& dataset, // NOSONAR: no reference-to-const
std::string const& component, Idx elements_per_scenario, Idx total_elements,
Idx const* indptr, RawDataPtr data) {
dataset.handle_.call_with(PGM_dataset_mutable_add_buffer, dataset.dataset_.get(), component.c_str(),
elements_per_scenario, total_elements, indptr, data);
}
void add_buffer(std::string const& component, Idx elements_per_scenario, Idx total_elements, Idx const* indptr,
RawDataPtr data) {
add_buffer(*this, component, elements_per_scenario, total_elements, indptr, data);
RawDataPtr data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_mutable_add_buffer, dataset_.get(), component.c_str(), elements_per_scenario,
total_elements, indptr, data);
}

static void add_buffer(DatasetMutable& dataset, // NOSONAR: no reference-to-const
std::string const& component, Idx elements_per_scenario, Idx total_elements,
Idx const* indptr, Buffer const& data) {
dataset.handle_.call_with(PGM_dataset_mutable_add_buffer, dataset.dataset_.get(), component.c_str(),
elements_per_scenario, total_elements, indptr, data.get());
}
void add_buffer(std::string const& component, Idx elements_per_scenario, Idx total_elements, Idx const* indptr,
Buffer const& data) {
add_buffer(*this, component, elements_per_scenario, total_elements, indptr, data);
Buffer const& data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_mutable_add_buffer, dataset_.get(), component.c_str(), elements_per_scenario,
total_elements, indptr, data.get());
}

static void add_attribute_buffer(DatasetMutable& dataset, // NOSONAR: no reference-to-const
std::string const& component, std::string const& attribute, RawDataPtr data) {
dataset.handle_.call_with(PGM_dataset_mutable_add_attribute_buffer, dataset.dataset_.get(), component.c_str(),
attribute.c_str(), data);
}
void add_attribute_buffer(std::string const& component, std::string const& attribute, RawDataPtr data) {
add_attribute_buffer(*this, component, attribute, data);
void add_attribute_buffer(std::string const& component, std::string const& attribute,
RawDataPtr data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_mutable_add_attribute_buffer, dataset_.get(), component.c_str(),
attribute.c_str(), data);
}

static void add_attribute_buffer(DatasetMutable& dataset, // NOSONAR: no reference-to-const
std::string const& component, std::string const& attribute, Buffer const& data) {
dataset.handle_.call_with(PGM_dataset_mutable_add_attribute_buffer, dataset.dataset_.get(), component.c_str(),
attribute.c_str(), data.get());
}
void add_attribute_buffer(std::string const& component, std::string const& attribute, Buffer const& data) {
add_attribute_buffer(*this, component, attribute, data);
void add_attribute_buffer(std::string const& component, std::string const& attribute,
Buffer const& data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_mutable_add_attribute_buffer, dataset_.get(), component.c_str(),
attribute.c_str(), data.get());
}

static DatasetInfo const& get_info(DatasetMutable const& dataset) { return dataset.info_; }
DatasetInfo const& get_info() const { return get_info(*this); }
DatasetInfo const& get_info() const { return info_; }

private:
Handle handle_{};
Expand All @@ -192,48 +140,31 @@ class DatasetConst {

RawConstDataset* get() const { return dataset_.get(); }

static void add_buffer(DatasetConst& dataset, // NOSONAR: no reference-to-const
std::string const& component, Idx elements_per_scenario, Idx total_elements,
Idx const* indptr, RawDataConstPtr data) {
dataset.handle_.call_with(PGM_dataset_const_add_buffer, dataset.dataset_.get(), component.c_str(),
elements_per_scenario, total_elements, indptr, data);
}
void add_buffer(std::string const& component, Idx elements_per_scenario, Idx total_elements, Idx const* indptr,
RawDataConstPtr data) {
add_buffer(*this, component, elements_per_scenario, total_elements, indptr, data);
RawDataConstPtr data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_const_add_buffer, dataset_.get(), component.c_str(), elements_per_scenario,
total_elements, indptr, data);
}

static void add_buffer(DatasetConst& dataset, // NOSONAR: no reference-to-const
std::string const& component, Idx elements_per_scenario, Idx total_elements,
Idx const* indptr, Buffer const& data) {
dataset.handle_.call_with(PGM_dataset_const_add_buffer, dataset.dataset_.get(), component.c_str(),
elements_per_scenario, total_elements, indptr, data.get());
}
void add_buffer(std::string const& component, Idx elements_per_scenario, Idx total_elements, Idx const* indptr,
Buffer const& data) {
add_buffer(*this, component, elements_per_scenario, total_elements, indptr, data);
Buffer const& data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_const_add_buffer, dataset_.get(), component.c_str(), elements_per_scenario,
total_elements, indptr, data.get());
}

static void add_attribute_buffer(DatasetConst& dataset, // NOSONAR: no reference-to-const
std::string const& component, std::string const& attribute, RawDataConstPtr data) {
dataset.handle_.call_with(PGM_dataset_const_add_attribute_buffer, dataset.dataset_.get(), component.c_str(),
attribute.c_str(), data);
}
void add_attribute_buffer(std::string const& component, std::string const& attribute, RawDataConstPtr data) {
add_attribute_buffer(*this, component, attribute, data);
void add_attribute_buffer(std::string const& component, std::string const& attribute,
RawDataConstPtr data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_const_add_attribute_buffer, dataset_.get(), component.c_str(), attribute.c_str(),
data);
}

static void add_attribute_buffer(DatasetConst& dataset, // NOSONAR: no reference-to-const
std::string const& component, std::string const& attribute, Buffer const& data) {
dataset.handle_.call_with(PGM_dataset_const_add_attribute_buffer, dataset.dataset_.get(), component.c_str(),
attribute.c_str(), data.get());
}
void add_attribute_buffer(std::string const& component, std::string const& attribute, Buffer const& data) {
add_attribute_buffer(*this, component, attribute, data);
void add_attribute_buffer(std::string const& component, std::string const& attribute,
Buffer const& data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_const_add_attribute_buffer, dataset_.get(), component.c_str(), attribute.c_str(),
data.get());
}

static DatasetInfo const& get_info(DatasetConst const& dataset) { return dataset.info_; }
DatasetInfo const& get_info() const { return get_info(*this); }
DatasetInfo const& get_info() const { return info_; }

private:
Handle handle_{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ class Handle {
public:
RawHandle* get() const { return handle_.get(); }

static void clear_error(Handle const& handle) { PGM_clear_error(handle.get()); }
void clear_error() const { clear_error(*this); }
void clear_error() const { PGM_clear_error(get()); }

static void check_error(Handle const& handle) {
RawHandle const* handle_ptr = handle.get();
void check_error() const {
RawHandle const* handle_ptr = get();
Idx const error_code = PGM_error_code(handle_ptr);
std::string const error_message = error_code == PGM_no_error ? "" : PGM_error_message(handle_ptr);
switch (error_code) {
case PGM_no_error:
return;
case PGM_regular_error:
Handle::clear_error(handle);
clear_error();
throw PowerGridRegularError{error_message};
case PGM_batch_error: {
Idx const n_failed_scenarios = PGM_n_failed_scenarios(handle_ptr);
Expand All @@ -75,18 +74,17 @@ class Handle {
failed_scenarios[i] =
PowerGridBatchError::FailedScenario{failed_scenario_seqs[i], failed_scenario_messages[i]};
}
Handle::clear_error(handle);
clear_error();
throw PowerGridBatchError{error_message, std::move(failed_scenarios)};
}
case PGM_serialization_error:
Handle::clear_error(handle);
clear_error();
throw PowerGridSerializationError{error_message};
default:
Handle::clear_error(handle);
clear_error();
throw PowerGridError{error_message};
}
}
void check_error() const { check_error(*this); }

template <typename Func, typename... Args> auto call_with(Func&& func, Args&&... args) const {
if constexpr (std::is_void_v<decltype(std::forward<Func>(func)(get(), std::forward<Args>(args)...))>) {
Expand Down
Loading

0 comments on commit dec5729

Please sign in to comment.