diff --git a/src/binding/python/Attributable.cpp b/src/binding/python/Attributable.cpp index e89043aa6c..0149748555 100644 --- a/src/binding/python/Attributable.cpp +++ b/src/binding/python/Attributable.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include using PyAttributeKeys = std::vector; @@ -266,29 +267,24 @@ bool setAttributeFromBufferInfo( } } -struct SetAttributeFromObject +namespace detail { - static constexpr char const *errorMsg = "Attributable.set_attribute()"; - - template - static bool - call(Attributable &attr, std::string const &key, py::object &obj) +template +bool setAttributeFromObject_default( + Attributable &attr, std::string const &key, py::object &obj) +{ + if (std::string(py::str(obj.get_type())) == "") { - if (std::string(py::str(obj.get_type())) == "") - { - using ListType = std::vector; - return attr.setAttribute(key, obj.cast()); - } - else - { - return attr.setAttribute( - key, obj.cast()); - } + using ListType = std::vector; + return attr.setAttribute(key, obj.cast()); } -}; + else + { + return attr.setAttribute(key, obj.cast()); + } +} -template <> -bool SetAttributeFromObject::call( +bool setAttributeFromObject_double( Attributable &attr, std::string const &key, py::object &obj) { if (std::string(py::str(obj.get_type())) == "") @@ -313,8 +309,7 @@ bool SetAttributeFromObject::call( } } -template <> -bool SetAttributeFromObject::call( +bool setAttributeFromObject_bool( Attributable &attr, std::string const &key, py::object &obj) { return attr.setAttribute(key, obj.cast()); @@ -358,8 +353,8 @@ std::optional tryCast(py::object const &obj) } } -template <> -bool SetAttributeFromObject::call( +template +bool setAttributeFromObject_char( Attributable &attr, std::string const &key, py::object &obj) { using explicit_char_type = typename char_to_explicit_char<>::type; @@ -425,6 +420,39 @@ bool SetAttributeFromObject::call( "object as any char-based type."); } } +} // namespace detail + +struct SetAttributeFromObject +{ + static constexpr char const *errorMsg = "Attributable.set_attribute()"; + + template + static bool + call(Attributable &attr, std::string const &key, py::object &obj) + { + if constexpr (std::is_same_v) + { + return ::detail::setAttributeFromObject_double(attr, key, obj); + } + else if constexpr (std::is_same_v) + { + return ::detail::setAttributeFromObject_bool(attr, key, obj); + } + else if constexpr ( + std::is_same_v || + std::is_same_v || + std::is_same_v) + { + return ::detail::setAttributeFromObject_char( + attr, key, obj); + } + else + { + return ::detail::setAttributeFromObject_default( + attr, key, obj); + } + } +}; bool setAttributeFromObject( Attributable &attr,