Skip to content

Commit

Permalink
Char overload auch for signed char / unsigned char
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 21, 2023
1 parent 8a14a72 commit ec96caa
Showing 1 changed file with 55 additions and 24 deletions.
79 changes: 55 additions & 24 deletions src/binding/python/Attributable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <optional>
#include <pybind11/detail/common.h>
#include <string>
#include <type_traits>
#include <vector>

using PyAttributeKeys = std::vector<std::string>;
Expand Down Expand Up @@ -266,29 +267,24 @@ bool setAttributeFromBufferInfo(
}
}

struct SetAttributeFromObject
namespace detail
{
static constexpr char const *errorMsg = "Attributable.set_attribute()";

template <typename RequestedType>
static bool
call(Attributable &attr, std::string const &key, py::object &obj)
template <typename RequestedType>
bool setAttributeFromObject_default(
Attributable &attr, std::string const &key, py::object &obj)
{
if (std::string(py::str(obj.get_type())) == "<class 'list'>")
{
if (std::string(py::str(obj.get_type())) == "<class 'list'>")
{
using ListType = std::vector<RequestedType>;
return attr.setAttribute<ListType>(key, obj.cast<ListType>());
}
else
{
return attr.setAttribute<RequestedType>(
key, obj.cast<RequestedType>());
}
using ListType = std::vector<RequestedType>;
return attr.setAttribute<ListType>(key, obj.cast<ListType>());
}
};
else
{
return attr.setAttribute<RequestedType>(key, obj.cast<RequestedType>());
}
}

template <>
bool SetAttributeFromObject::call<double>(
bool setAttributeFromObject_double(
Attributable &attr, std::string const &key, py::object &obj)
{
if (std::string(py::str(obj.get_type())) == "<class 'list'>")
Expand All @@ -313,8 +309,7 @@ bool SetAttributeFromObject::call<double>(
}
}

template <>
bool SetAttributeFromObject::call<bool>(
bool setAttributeFromObject_bool(
Attributable &attr, std::string const &key, py::object &obj)
{
return attr.setAttribute<bool>(key, obj.cast<bool>());
Expand Down Expand Up @@ -358,11 +353,14 @@ std::optional<TargetType> tryCast(py::object const &obj)
}
}

template <>
bool SetAttributeFromObject::call<char>(
template <typename Char_t>
bool setAttributeFromObject_char(
Attributable &attr, std::string const &key, py::object &obj)
{
using explicit_char_type = typename char_to_explicit_char<>::type;
using explicit_char_type = std::conditional_t<
std::is_same_v<Char_t, char>,
typename char_to_explicit_char<>::type,
Char_t>;
using ListChar = std::vector<char>;
using ListString = std::vector<std::string>;

Expand Down Expand Up @@ -425,6 +423,39 @@ bool SetAttributeFromObject::call<char>(
"object as any char-based type.");
}
}
} // namespace detail

struct SetAttributeFromObject
{
static constexpr char const *errorMsg = "Attributable.set_attribute()";

template <typename RequestedType>
static bool
call(Attributable &attr, std::string const &key, py::object &obj)
{
if constexpr (std::is_same_v<RequestedType, double>)
{
return ::detail::setAttributeFromObject_double(attr, key, obj);
}
else if constexpr (std::is_same_v<RequestedType, bool>)
{
return ::detail::setAttributeFromObject_bool(attr, key, obj);
}
else if constexpr (
std::is_same_v<RequestedType, char> ||
std::is_same_v<RequestedType, signed char> ||
std::is_same_v<RequestedType, unsigned char>)
{
return ::detail::setAttributeFromObject_char<RequestedType>(
attr, key, obj);
}
else
{
return ::detail::setAttributeFromObject_default<RequestedType>(
attr, key, obj);
}
}
};

bool setAttributeFromObject(
Attributable &attr,
Expand Down

0 comments on commit ec96caa

Please sign in to comment.