Skip to content

Commit

Permalink
Merge pull request #767 from PowerGridModel/feature/add-utils-cpp-wra…
Browse files Browse the repository at this point in the history
…pper

Feature/Add utils to Cpp wrapper
  • Loading branch information
TonyXiang8787 authored Oct 8, 2024
2 parents 36da48e + f169895 commit 31914b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
#include "power_grid_model_cpp/model.hpp"
#include "power_grid_model_cpp/options.hpp"
#include "power_grid_model_cpp/serialization.hpp"
#include "power_grid_model_cpp/utils.hpp"

#endif // POWER_GRID_MODEL_CPP_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace power_grid_model_cpp {

using Idx = PGM_Idx;
using ID = PGM_ID;
using IntS = int8_t;

using PowerGridModel = PGM_PowerGridModel;
using MetaDataset = PGM_MetaDataset;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
//
// SPDX-License-Identifier: MPL-2.0

#pragma once

#ifndef POWER_GRID_MODEL_CPP_UTILS_HPP
#define POWER_GRID_MODEL_CPP_UTILS_HPP

#include "basics.hpp"
#include "handle.hpp"

#include <array>
#include <complex>
#include <limits>

namespace power_grid_model_cpp {
inline bool is_nan(IntS const x) { return x == std::numeric_limits<IntS>::min(); }
inline bool is_nan(ID const x) { return x == std::numeric_limits<ID>::min(); }
inline bool is_nan(double const x) { return std::isnan(x); }
inline bool is_nan(std::complex<double> const& x) { return is_nan(x.real()) || is_nan(x.imag()); }
inline bool is_nan(std::array<double, 3> const& array) {
return is_nan(array[0]) || is_nan(array[1]) || is_nan(array[2]);
}
inline bool is_nan(std::array<std::complex<double>, 3> const& array) {
return is_nan(array[0]) || is_nan(array[1]) || is_nan(array[2]);
}

class UnsupportedPGM_CType : public PowerGridError {
public:
UnsupportedPGM_CType()
: PowerGridError{[&]() {
using namespace std::string_literals;
return "Unsupported PGM_Ctype"s;
}()} {}
};

template <class Functor, class... Args>
decltype(auto) pgm_type_func_selector(enum PGM_CType type, Functor&& f, Args&&... args) {
switch (type) {
case PGM_int32:
return std::forward<Functor>(f).template operator()<ID>(std::forward<Args>(args)...);
case PGM_int8:
return std::forward<Functor>(f).template operator()<IntS>(std::forward<Args>(args)...);
case PGM_double:
return std::forward<Functor>(f).template operator()<double>(std::forward<Args>(args)...);
case PGM_double3:
return std::forward<Functor>(f).template operator()<std::array<double, 3>>(std::forward<Args>(args)...);
default:
throw UnsupportedPGM_CType();
}
}

} // namespace power_grid_model_cpp

#endif // POWER_GRID_MODEL_CPP_UTILS_HPP
37 changes: 1 addition & 36 deletions tests/cpp_validation_tests/test_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ class UnsupportedValidationCase : public PowerGridError {
}()} {}
};

class UnsupportedPGM_CType : public PowerGridError {
public:
UnsupportedPGM_CType()
: PowerGridError{[&]() {
using namespace std::string_literals;
return "Unsupported PGM_Ctype"s;
}()} {}
};

class OptionalNotInitialized : public PowerGridError {
public:
OptionalNotInitialized(std::string const& object)
Expand All @@ -53,32 +44,6 @@ class OptionalNotInitialized : public PowerGridError {
}()} {}
};

inline bool is_nan(std::floating_point auto x) { return std::isnan(x); }
template <std::floating_point T> inline bool is_nan(std::complex<T> const& x) {
return is_nan(x.real()) || is_nan(x.imag());
}
inline bool is_nan(int32_t x) { return x == std::numeric_limits<int32_t>::min(); }
inline bool is_nan(int8_t x) { return x == std::numeric_limits<int8_t>::min(); }
template <typename T, std::size_t N> inline bool is_nan(std::array<T, N> const& array) {
return std::ranges::any_of(array, [](T const& element) { return is_nan(element); });
}

template <class Functor, class... Args>
decltype(auto) pgm_type_func_selector(enum PGM_CType type, Functor&& f, Args&&... args) {
switch (type) {
case PGM_int32:
return std::forward<Functor>(f).template operator()<int32_t>(std::forward<Args>(args)...);
case PGM_int8:
return std::forward<Functor>(f).template operator()<int8_t>(std::forward<Args>(args)...);
case PGM_double:
return std::forward<Functor>(f).template operator()<double>(std::forward<Args>(args)...);
case PGM_double3:
return std::forward<Functor>(f).template operator()<std::array<double, 3>>(std::forward<Args>(args)...);
default:
throw UnsupportedPGM_CType();
}
}

using nlohmann::json;

auto read_file(std::filesystem::path const& path) {
Expand Down Expand Up @@ -184,7 +149,7 @@ template <typename T> std::string get_as_string(T const& attribute_value) {
sstr << std::setprecision(16);
if constexpr (std::is_same_v<std::decay_t<T>, std::array<double, 3>>) {
sstr << "(" << attribute_value[0] << ", " << attribute_value[1] << ", " << attribute_value[2] << ")";
} else if constexpr (std::is_same_v<std::decay_t<T>, int8_t>) {
} else if constexpr (std::is_same_v<std::decay_t<T>, IntS>) {
sstr << std::to_string(attribute_value);
} else {
sstr << attribute_value;
Expand Down

0 comments on commit 31914b2

Please sign in to comment.