Skip to content

Commit

Permalink
Add enum_conv_pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov-Dmitriy-Ivanovich committed Dec 4, 2023
1 parent c9aa028 commit 2c96a37
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/python_bindings/opt_to_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@ using ConvFunction = std::function<py::object(boost::any)>;

template <typename T>
std::pair<std::type_index, ConvFunction> normal_conv_pair{
std::type_index(typeid(T)),
[](boost::any value) { return py::cast(boost::any_cast<T>(value)); }};
std::type_index(typeid(T)),
[](boost::any value) { return py::cast(boost::any_cast<T>(value)); }
};

template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<bool>{
std::type_index(typeid(bool)),
[](boost::any value) { return py::cast(boost::any_cast<bool>(value)); }};
std::type_index(typeid(bool)),
[](boost::any value) { return py::cast(boost::any_cast<bool>(value)); }
};

template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<algos::metric::MetricAlgo>{
std::type_index(typeid(algos::metric::MetricAlgo)), [](boost::any value) {
auto opt_value = boost::any_cast<algos::metric::MetricAlgo>(value);
return py::cast(opt_value._to_string());
}};
template <>
std::pair<std::type_index, ConvFunction> normal_conv_pair<algos::metric::Metric>{
std::type_index(typeid(algos::metric::Metric)), [](boost::any value) {
auto opt_value = boost::any_cast<algos::metric::Metric>(value);
return py::cast(opt_value._to_string());
}};
template <typename T>
std::pair<std::type_index, ConvFunction> enum_conv_pair{
std::type_index(typeid(T)),
[](boost::any value){ return py::cast(boost::any_cast<T>(value)._to_string());}
};
const std::unordered_map<std::type_index, ConvFunction> converters{
normal_conv_pair<int>,
normal_conv_pair<double>,
Expand All @@ -35,8 +30,8 @@ const std::unordered_map<std::type_index, ConvFunction> converters{
normal_conv_pair<config::MaxLhsType>,
normal_conv_pair<config::ErrorType>,
normal_conv_pair<config::IndicesType>,
normal_conv_pair<algos::metric::MetricAlgo>,
normal_conv_pair<algos::metric::Metric>};
enum_conv_pair<algos::metric::MetricAlgo>,
enum_conv_pair<algos::metric::Metric>};
} // namespace

namespace python_bindings {
Expand Down

0 comments on commit 2c96a37

Please sign in to comment.