-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #767 from PowerGridModel/feature/add-utils-cpp-wra…
…pper Feature/Add utils to Cpp wrapper
- Loading branch information
Showing
4 changed files
with
59 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters