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/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 new file mode 100644 index 000000000..78e981474 --- /dev/null +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp @@ -0,0 +1,56 @@ +// 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 + +namespace power_grid_model_cpp { +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]); +} +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 { + 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..71035fec9 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) { @@ -184,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;