From 053247024f6567da414137c84b0318b82ed9721d Mon Sep 17 00:00:00 2001 From: Santiago Figueroa Manrique Date: Mon, 7 Oct 2024 14:23:01 +0200 Subject: [PATCH 1/4] added utils.hpp Signed-off-by: Santiago Figueroa Manrique --- .../include/power_grid_model_cpp.hpp | 1 + .../include/power_grid_model_cpp/utils.hpp | 65 +++++++++++++++++++ .../cpp_validation_tests/test_validation.cpp | 35 ---------- 3 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp.hpp index aae047a68..08c49adc7 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp.hpp @@ -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 diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp new file mode 100644 index 000000000..c2488cc49 --- /dev/null +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// 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 +#include +#include +#include + +namespace power_grid_model_cpp { +template +inline typename std::enable_if::value, // NOLINT(modernize-use-constraints, + // modernize-type-traits) + bool>::type +is_nan(T x) { + return std::isnan(x); +} +template +inline typename std::enable_if::value, // NOLINT(modernize-use-constraints, + // modernize-type-traits) + bool>::type +is_nan(std::complex const& x) { + return is_nan(x.real()) || is_nan(x.imag()); +} +inline bool is_nan(int32_t x) { return x == std::numeric_limits::min(); } +inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } +template inline bool is_nan(std::array const& array) { + return std::any_of(array.begin(), array.end(), [](T const& element) { return is_nan(element); }); +} + +class UnsupportedPGM_CType : public PowerGridError { + public: + UnsupportedPGM_CType() + : PowerGridError{[&]() { + using namespace std::string_literals; + return "Unsupported PGM_Ctype"s; + }()} {} +}; + +template +decltype(auto) pgm_type_func_selector(enum PGM_CType type, Functor&& f, Args&&... args) { + switch (type) { + case PGM_int32: + return std::forward(f).template operator()(std::forward(args)...); + case PGM_int8: + return std::forward(f).template operator()(std::forward(args)...); + case PGM_double: + return std::forward(f).template operator()(std::forward(args)...); + case PGM_double3: + return std::forward(f).template operator()>(std::forward(args)...); + default: + throw UnsupportedPGM_CType(); + } +} + +} // namespace power_grid_model_cpp + +#endif // POWER_GRID_MODEL_CPP_UTILS_HPP diff --git a/tests/cpp_validation_tests/test_validation.cpp b/tests/cpp_validation_tests/test_validation.cpp index c3b6d7a6e..71e8d58d8 100644 --- a/tests/cpp_validation_tests/test_validation.cpp +++ b/tests/cpp_validation_tests/test_validation.cpp @@ -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) @@ -53,32 +44,6 @@ class OptionalNotInitialized : public PowerGridError { }()} {} }; -inline bool is_nan(std::floating_point auto x) { return std::isnan(x); } -template inline bool is_nan(std::complex const& x) { - return is_nan(x.real()) || is_nan(x.imag()); -} -inline bool is_nan(int32_t x) { return x == std::numeric_limits::min(); } -inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } -template inline bool is_nan(std::array const& array) { - return std::ranges::any_of(array, [](T const& element) { return is_nan(element); }); -} - -template -decltype(auto) pgm_type_func_selector(enum PGM_CType type, Functor&& f, Args&&... args) { - switch (type) { - case PGM_int32: - return std::forward(f).template operator()(std::forward(args)...); - case PGM_int8: - return std::forward(f).template operator()(std::forward(args)...); - case PGM_double: - return std::forward(f).template operator()(std::forward(args)...); - case PGM_double3: - return std::forward(f).template operator()>(std::forward(args)...); - default: - throw UnsupportedPGM_CType(); - } -} - using nlohmann::json; auto read_file(std::filesystem::path const& path) { From bab3dad38d98af38d435e459cb5ffdd948a0389b Mon Sep 17 00:00:00 2001 From: Santiago Figueroa Manrique Date: Tue, 8 Oct 2024 11:23:35 +0200 Subject: [PATCH 2/4] Addressed comments Signed-off-by: Santiago Figueroa Manrique --- .../include/power_grid_model_cpp/basics.hpp | 1 + .../include/power_grid_model_cpp/utils.hpp | 33 +++++++------------ .../cpp_validation_tests/test_validation.cpp | 2 +- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/basics.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/basics.hpp index 65cfab603..378216c32 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/basics.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/basics.hpp @@ -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; diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp index c2488cc49..5acd25653 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp @@ -9,30 +9,21 @@ #include "basics.hpp" #include "handle.hpp" -#include +#include //all_of #include #include -#include +#include //enable_if and if_floating_point namespace power_grid_model_cpp { -template -inline typename std::enable_if::value, // NOLINT(modernize-use-constraints, - // modernize-type-traits) - bool>::type -is_nan(T x) { - return std::isnan(x); +inline bool is_nan(IntS x) { return x == std::numeric_limits::min(); } +inline bool is_nan(ID x) { return x == std::numeric_limits::min(); } +inline bool is_nan(double x) { return std::isnan(x); } +inline bool is_nan(std::complex const& x) { return is_nan(x.real()) || is_nan(x.imag()); } +inline bool is_nan(std::array const& array) { + return is_nan(array[0]) || is_nan(array[1]) || is_nan(array[2]); } -template -inline typename std::enable_if::value, // NOLINT(modernize-use-constraints, - // modernize-type-traits) - bool>::type -is_nan(std::complex const& x) { - return is_nan(x.real()) || is_nan(x.imag()); -} -inline bool is_nan(int32_t x) { return x == std::numeric_limits::min(); } -inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } -template inline bool is_nan(std::array const& array) { - return std::any_of(array.begin(), array.end(), [](T const& element) { return is_nan(element); }); +inline bool is_nan(std::array, 3> const& array) { + return is_nan(array[0]) || is_nan(array[1]) || is_nan(array[2]); } class UnsupportedPGM_CType : public PowerGridError { @@ -48,9 +39,9 @@ template decltype(auto) pgm_type_func_selector(enum PGM_CType type, Functor&& f, Args&&... args) { switch (type) { case PGM_int32: - return std::forward(f).template operator()(std::forward(args)...); + return std::forward(f).template operator()(std::forward(args)...); case PGM_int8: - return std::forward(f).template operator()(std::forward(args)...); + return std::forward(f).template operator()(std::forward(args)...); case PGM_double: return std::forward(f).template operator()(std::forward(args)...); case PGM_double3: diff --git a/tests/cpp_validation_tests/test_validation.cpp b/tests/cpp_validation_tests/test_validation.cpp index 71e8d58d8..71035fec9 100644 --- a/tests/cpp_validation_tests/test_validation.cpp +++ b/tests/cpp_validation_tests/test_validation.cpp @@ -149,7 +149,7 @@ template std::string get_as_string(T const& attribute_value) { sstr << std::setprecision(16); if constexpr (std::is_same_v, std::array>) { sstr << "(" << attribute_value[0] << ", " << attribute_value[1] << ", " << attribute_value[2] << ")"; - } else if constexpr (std::is_same_v, int8_t>) { + } else if constexpr (std::is_same_v, IntS>) { sstr << std::to_string(attribute_value); } else { sstr << attribute_value; From 838a9ef681d2963d07dc0297b60126bdf2adb4be Mon Sep 17 00:00:00 2001 From: Santiago Figueroa Manrique Date: Tue, 8 Oct 2024 11:26:17 +0200 Subject: [PATCH 3/4] minor Signed-off-by: Santiago Figueroa Manrique --- .../include/power_grid_model_cpp/utils.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp index 5acd25653..57112ee4c 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp @@ -3,16 +3,15 @@ // 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 //all_of #include #include -#include //enable_if and if_floating_point namespace power_grid_model_cpp { inline bool is_nan(IntS x) { return x == std::numeric_limits::min(); } From f169895eecec80c321427053588233768add7dee Mon Sep 17 00:00:00 2001 From: Santiago Figueroa Manrique Date: Tue, 8 Oct 2024 11:41:57 +0200 Subject: [PATCH 4/4] minor fix Signed-off-by: Santiago Figueroa Manrique --- .../include/power_grid_model_cpp/utils.hpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp index 57112ee4c..78e981474 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp @@ -10,13 +10,14 @@ #include "basics.hpp" #include "handle.hpp" +#include #include #include namespace power_grid_model_cpp { -inline bool is_nan(IntS x) { return x == std::numeric_limits::min(); } -inline bool is_nan(ID x) { return x == std::numeric_limits::min(); } -inline bool is_nan(double x) { return std::isnan(x); } +inline bool is_nan(IntS const x) { return x == std::numeric_limits::min(); } +inline bool is_nan(ID const x) { return x == std::numeric_limits::min(); } +inline bool is_nan(double const x) { return std::isnan(x); } inline bool is_nan(std::complex const& x) { return is_nan(x.real()) || is_nan(x.imag()); } inline bool is_nan(std::array const& array) { return is_nan(array[0]) || is_nan(array[1]) || is_nan(array[2]);