Skip to content

Commit

Permalink
Merge pull request #808 from PowerGridModel/fix/const-senmantics-cpp-…
Browse files Browse the repository at this point in the history
…header

Fix const issue in the cpp header
  • Loading branch information
TonyXiang8787 authored Oct 25, 2024
2 parents 2068ead + 3366824 commit 35cc21d
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ template <auto func> struct DeleterFunctor {
};

// unique pointer definition
template <typename T, auto func> using UniquePtr = std::unique_ptr<T, DeleterFunctor<func>>;
template <typename T, auto func> class UniquePtr : public std::unique_ptr<T, DeleterFunctor<func>> {
public:
using std::unique_ptr<T, DeleterFunctor<func>>::unique_ptr;
using std::unique_ptr<T, DeleterFunctor<func>>::operator=;
T* get() { return static_cast<std::unique_ptr<T, DeleterFunctor<func>>&>(*this).get(); }
T const* get() const { return static_cast<std::unique_ptr<T, DeleterFunctor<func>> const&>(*this).get(); }
};
} // namespace detail

} // namespace power_grid_model_cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ class Buffer {
Buffer(MetaComponent const* component, Idx size)
: component_{component}, size_{size}, buffer_{handle_.call_with(PGM_create_buffer, component, size)} {};

RawDataPtr get() const { return buffer_.get(); }
RawDataConstPtr get() const { return buffer_.get(); }
RawDataPtr get() { return buffer_.get(); }

Idx size() const { return 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);
handle_.call_with(PGM_buffer_set_nan, component_, get(), buffer_offset, size);
}

void set_value(MetaAttribute const* attribute, RawDataConstPtr src_ptr, Idx src_stride) {
Expand All @@ -34,8 +35,8 @@ class Buffer {
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) { // NOSONAR: no-const
handle_.call_with(PGM_buffer_set_value, attribute, buffer_.get(), src_ptr, buffer_offset, size, src_stride);
Idx src_stride) {
handle_.call_with(PGM_buffer_set_value, attribute, get(), src_ptr, buffer_offset, size, src_stride);
}

void get_value(MetaAttribute const* attribute, RawDataPtr dest_ptr, Idx dest_stride) const {
Expand All @@ -46,7 +47,7 @@ class Buffer {
}
void get_value(MetaAttribute const* attribute, RawDataPtr dest_ptr, Idx buffer_offset, Idx size,
Idx dest_stride) const {
handle_.call_with(PGM_buffer_get_value, attribute, buffer_.get(), dest_ptr, buffer_offset, size, dest_stride);
handle_.call_with(PGM_buffer_get_value, attribute, get(), dest_ptr, buffer_offset, size, dest_stride);
}

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

RawWritableDataset* get() const { return dataset_; }
RawWritableDataset const* get() const { return dataset_; }
RawWritableDataset* get() { return dataset_; }

DatasetInfo const& get_info() const { return info_; }

void set_buffer(std::string const& component, Idx* indptr, RawDataPtr data) {
handle_.call_with(PGM_dataset_writable_set_buffer, dataset_, component.c_str(), indptr, data);
handle_.call_with(PGM_dataset_writable_set_buffer, get(), component.c_str(), indptr, data);
}

void set_buffer(std::string const& component, Idx* indptr, Buffer const& data) {
handle_.call_with(PGM_dataset_writable_set_buffer, dataset_, component.c_str(), indptr, data.get());
void set_buffer(std::string const& component, Idx* indptr, Buffer& data) {
handle_.call_with(PGM_dataset_writable_set_buffer, get(), component.c_str(), indptr, data.get());
}

void set_attribute_buffer(std::string const& component, std::string const& attribute, RawDataPtr data) {
handle_.call_with(PGM_dataset_writable_set_attribute_buffer, dataset_, component.c_str(), attribute.c_str(),
data);
handle_.call_with(PGM_dataset_writable_set_attribute_buffer, get(), component.c_str(), attribute.c_str(), data);
}

void set_attribute_buffer(std::string const& component, std::string const& attribute, Buffer const& data) {
handle_.call_with(PGM_dataset_writable_set_attribute_buffer, dataset_, component.c_str(), attribute.c_str(),
void set_attribute_buffer(std::string const& component, std::string const& attribute, Buffer& data) {
handle_.call_with(PGM_dataset_writable_set_attribute_buffer, get(), component.c_str(), attribute.c_str(),
data.get());
}

Expand All @@ -90,32 +90,30 @@ class DatasetMutable {
public:
DatasetMutable(std::string const& dataset, Idx is_batch, Idx batch_size)
: dataset_{handle_.call_with(PGM_create_dataset_mutable, dataset.c_str(), is_batch, batch_size)},
info_{handle_.call_with(PGM_dataset_mutable_get_info, dataset_.get())} {}
info_{handle_.call_with(PGM_dataset_mutable_get_info, get())} {}

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

void add_buffer(std::string const& component, Idx elements_per_scenario, Idx total_elements, Idx const* indptr,
RawDataPtr data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_mutable_add_buffer, dataset_.get(), component.c_str(), elements_per_scenario,
RawDataPtr data) {
handle_.call_with(PGM_dataset_mutable_add_buffer, 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,
Buffer const& data) { // NOSONAR: no-const
handle_.call_with(PGM_dataset_mutable_add_buffer, dataset_.get(), component.c_str(), elements_per_scenario,
Buffer& data) {
handle_.call_with(PGM_dataset_mutable_add_buffer, get(), component.c_str(), elements_per_scenario,
total_elements, indptr, data.get());
}

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);
void add_attribute_buffer(std::string const& component, std::string const& attribute, RawDataPtr data) {
handle_.call_with(PGM_dataset_mutable_add_attribute_buffer, get(), component.c_str(), attribute.c_str(), 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());
void add_attribute_buffer(std::string const& component, std::string const& attribute, Buffer& data) {
handle_.call_with(PGM_dataset_mutable_add_attribute_buffer, get(), component.c_str(), attribute.c_str(),
data.get());
}

DatasetInfo const& get_info() const { return info_; }
Expand All @@ -130,37 +128,35 @@ class DatasetConst {
public:
DatasetConst(std::string const& dataset, Idx is_batch, Idx batch_size)
: dataset_{handle_.call_with(PGM_create_dataset_const, dataset.c_str(), is_batch, batch_size)},
info_{handle_.call_with(PGM_dataset_const_get_info, dataset_.get())} {}
info_{handle_.call_with(PGM_dataset_const_get_info, get())} {}
DatasetConst(DatasetWritable const& writable_dataset)
: dataset_{handle_.call_with(PGM_create_dataset_const_from_writable, writable_dataset.get())},
info_{handle_.call_with(PGM_dataset_const_get_info, dataset_.get())} {}
info_{handle_.call_with(PGM_dataset_const_get_info, get())} {}
DatasetConst(DatasetMutable const& mutable_dataset)
: dataset_{handle_.call_with(PGM_create_dataset_const_from_mutable, mutable_dataset.get())},
info_{handle_.call_with(PGM_dataset_const_get_info, dataset_.get())} {}
info_{handle_.call_with(PGM_dataset_const_get_info, get())} {}

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

void add_buffer(std::string const& component, Idx elements_per_scenario, Idx total_elements, Idx const* indptr,
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);
RawDataConstPtr data) {
handle_.call_with(PGM_dataset_const_add_buffer, 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,
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());
Buffer const& data) {
handle_.call_with(PGM_dataset_const_add_buffer, get(), component.c_str(), elements_per_scenario, total_elements,
indptr, data.get());
}

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);
void add_attribute_buffer(std::string const& component, std::string const& attribute, RawDataConstPtr data) {
handle_.call_with(PGM_dataset_const_add_attribute_buffer, get(), component.c_str(), attribute.c_str(), 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(),
void add_attribute_buffer(std::string const& component, std::string const& attribute, Buffer const& data) {
handle_.call_with(PGM_dataset_const_add_attribute_buffer, get(), component.c_str(), attribute.c_str(),
data.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ class Handle {
}

private:
detail::UniquePtr<RawHandle, &PGM_destroy_handle> handle_{PGM_create_handle()};
// For handle the const semantics are not needed.
// It is meant to be mutated even in const situations.
std::unique_ptr<RawHandle, detail::DeleterFunctor<&PGM_destroy_handle>> handle_{PGM_create_handle()};
};
} // namespace power_grid_model_cpp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class Model {
public:
Model(double system_frequency, DatasetConst const& input_dataset)
: model_{handle_.call_with(PGM_create_model, system_frequency, input_dataset.get())} {}
Model(Model const& other) : model_{handle_.call_with(PGM_copy_model, other.model_.get())} {}
Model(Model const& other) : model_{handle_.call_with(PGM_copy_model, other.get())} {}
Model& operator=(Model const& other) {
if (this != &other) {
model_.reset(handle_.call_with(PGM_copy_model, other.model_.get()));
model_.reset(handle_.call_with(PGM_copy_model, other.get()));
}
return *this;
}
Expand All @@ -35,22 +35,23 @@ class Model {
}
~Model() = default;

PowerGridModel* get() const { return model_.get(); }
PowerGridModel const* get() const { return model_.get(); }
PowerGridModel* get() { return model_.get(); }

void update(DatasetConst const& update_dataset) const {
handle_.call_with(PGM_update_model, model_.get(), update_dataset.get());
void update(DatasetConst const& update_dataset) {
handle_.call_with(PGM_update_model, get(), update_dataset.get());
}

void get_indexer(std::string const& component, Idx size, ID const* ids, Idx* indexer) const {
handle_.call_with(PGM_get_indexer, model_.get(), component.c_str(), size, ids, indexer);
handle_.call_with(PGM_get_indexer, get(), component.c_str(), size, ids, indexer);
}

void calculate(Options const& opt, DatasetMutable const& output_dataset, DatasetConst const& batch_dataset) const {
handle_.call_with(PGM_calculate, model_.get(), opt.get(), output_dataset.get(), batch_dataset.get());
void calculate(Options const& opt, DatasetMutable const& output_dataset, DatasetConst const& batch_dataset) {
handle_.call_with(PGM_calculate, get(), opt.get(), output_dataset.get(), batch_dataset.get());
}

void calculate(Options const& opt, DatasetMutable const& output_dataset) const {
handle_.call_with(PGM_calculate, model_.get(), opt.get(), output_dataset.get(), nullptr);
void calculate(Options const& opt, DatasetMutable const& output_dataset) {
handle_.call_with(PGM_calculate, get(), opt.get(), output_dataset.get(), nullptr);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,30 @@ class Options {
public:
Options() : options_{handle_.call_with(PGM_create_options)} {}

RawOptions* get() const { return options_.get(); }
RawOptions* get() { return options_.get(); }
RawOptions const* get() const { return options_.get(); }

void set_calculation_type(Idx type) const { handle_.call_with(PGM_set_calculation_type, get(), type); }
void set_calculation_type(Idx type) { handle_.call_with(PGM_set_calculation_type, get(), type); }

void set_calculation_method(Idx method) const { handle_.call_with(PGM_set_calculation_method, get(), method); }
void set_calculation_method(Idx method) { handle_.call_with(PGM_set_calculation_method, get(), method); }

void set_symmetric(Idx sym) const { handle_.call_with(PGM_set_symmetric, get(), sym); }
void set_symmetric(Idx sym) { handle_.call_with(PGM_set_symmetric, get(), sym); }

void set_err_tol(double err_tol) const { handle_.call_with(PGM_set_err_tol, get(), err_tol); }
void set_err_tol(double err_tol) { handle_.call_with(PGM_set_err_tol, get(), err_tol); }

void set_max_iter(Idx max_iter) const { handle_.call_with(PGM_set_max_iter, get(), max_iter); }
void set_max_iter(Idx max_iter) { handle_.call_with(PGM_set_max_iter, get(), max_iter); }

void set_threading(Idx threading) const { handle_.call_with(PGM_set_threading, get(), threading); }
void set_threading(Idx threading) { handle_.call_with(PGM_set_threading, get(), threading); }

void set_short_circuit_voltage_scaling(Idx short_circuit_voltage_scaling) const {
void set_short_circuit_voltage_scaling(Idx short_circuit_voltage_scaling) {
handle_.call_with(PGM_set_short_circuit_voltage_scaling, get(), short_circuit_voltage_scaling);
}

void set_tap_changing_strategy(Idx tap_changing_strategy) const {
void set_tap_changing_strategy(Idx tap_changing_strategy) {
handle_.call_with(PGM_set_tap_changing_strategy, get(), tap_changing_strategy);
}

void set_experimental_features(Idx experimental_features) const {
void set_experimental_features(Idx experimental_features) {
handle_.call_with(PGM_set_experimental_features, get(), experimental_features);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ class Deserializer {
serialization_format)},
dataset_{handle_.call_with(PGM_deserializer_get_dataset, get())} {}

RawDeserializer* get() const { return deserializer_.get(); }
RawDeserializer* get() { return deserializer_.get(); }
RawDeserializer const* get() const { return deserializer_.get(); }

DatasetWritable& get_dataset() { return dataset_; }

void parse_to_buffer() const { handle_.call_with(PGM_deserializer_parse_to_buffer, get()); }
void parse_to_buffer() { handle_.call_with(PGM_deserializer_parse_to_buffer, get()); }

private:
Handle handle_{};
Expand All @@ -48,9 +49,10 @@ class Serializer {
Serializer(DatasetConst const& dataset, Idx serialization_format)
: serializer_{handle_.call_with(PGM_create_serializer, dataset.get(), serialization_format)} {}

RawSerializer* get() const { return serializer_.get(); }
RawSerializer* get() { return serializer_.get(); }
RawSerializer const* get() const { return serializer_.get(); }

std::string_view get_to_binary_buffer(Idx use_compact_list) const {
std::string_view get_to_binary_buffer(Idx use_compact_list) {
char const* temp_data{};
Idx buffer_size{};
handle_.call_with(PGM_serializer_get_to_binary_buffer, get(), use_compact_list, &temp_data, &buffer_size);
Expand All @@ -60,7 +62,7 @@ class Serializer {
return std::string_view{temp_data, static_cast<size_t>(buffer_size)};
}

void get_to_binary_buffer(Idx use_compact_list, std::vector<std::byte>& data) const {
void get_to_binary_buffer(Idx use_compact_list, std::vector<std::byte>& data) {
auto temp_data = get_to_binary_buffer(use_compact_list);
if (!temp_data.empty()) {
data.resize(temp_data.size());
Expand All @@ -70,7 +72,7 @@ class Serializer {
}
}

void get_to_binary_buffer(Idx use_compact_list, std::vector<char>& data) const {
void get_to_binary_buffer(Idx use_compact_list, std::vector<char>& data) {
auto temp_data = get_to_binary_buffer(use_compact_list);
if (!temp_data.empty()) {
data.assign(temp_data.begin(), temp_data.end());
Expand All @@ -79,7 +81,7 @@ class Serializer {
}
}

std::string get_to_zero_terminated_string(Idx use_compact_list, Idx indent) const {
std::string get_to_zero_terminated_string(Idx use_compact_list, Idx indent) {
return std::string{
handle_.call_with(PGM_serializer_get_to_zero_terminated_string, get(), use_compact_list, indent)};
}
Expand Down
Loading

0 comments on commit 35cc21d

Please sign in to comment.