use ngcore pybind list caster only for non-numpy types

we should Array<T> for all numpy dtypes T
This commit is contained in:
mhochsteger@cerbsim.com 2022-02-21 11:18:17 +01:00
parent 775b97f6b3
commit d3e0ae6fd7
2 changed files with 25 additions and 15 deletions

View File

@ -15,6 +15,24 @@
#include "profiler.hpp"
namespace py = pybind11;
namespace ngcore
{
namespace detail
{
template<typename T>
struct HasPyFormat
{
private:
template<typename T2>
static auto check(T2*) -> std::enable_if_t<std::is_same_v<decltype(std::declval<py::format_descriptor<T2>>().format()), std::string>, std::true_type>;
static auto check(...) -> std::false_type;
public:
static constexpr bool value = decltype(check((T*) nullptr))::value;
};
} // namespace detail
} // namespace ngcore
////////////////////////////////////////////////////////////////////////////////
// automatic conversion of python list to Array<>
namespace pybind11 {
@ -57,7 +75,8 @@ public:
PYBIND11_TYPE_CASTER(Type, _("Array[") + value_conv::name + _("]"));
};
template <typename Type> struct type_caster<ngcore::Array<Type>>
template <typename Type> struct type_caster<ngcore::Array<Type>, enable_if_t<!ngcore::detail::HasPyFormat<Type>::value>>
: ngcore_list_caster<ngcore::Array<Type>, Type> { };
@ -151,20 +170,6 @@ namespace ngcore
return arr;
}
namespace detail
{
template<typename T>
struct HasPyFormat
{
private:
template<typename T2>
static auto check(T2*) -> std::enable_if_t<std::is_same_v<decltype(std::declval<py::format_descriptor<T2>>().format()), std::string>, std::true_type>;
static auto check(...) -> std::false_type;
public:
static constexpr bool value = decltype(check((T*) nullptr))::value;
};
} // namespace detail
template <typename T, typename TIND=typename FlatArray<T>::index_type>
void ExportArray (py::module &m)
{

View File

@ -18,6 +18,11 @@ PYBIND11_MODULE(pyngcore, m) // NOLINT
ExportArray<unsigned>(m);
ExportArray<size_t>(m);
ExportArray<double>(m);
ExportArray<float>(m);
ExportArray<signed short>(m);
ExportArray<signed char>(m);
ExportArray<unsigned short>(m);
ExportArray<unsigned char>(m);
ExportTable<int>(m);