From edbd4cb0a7f8a67280be27c7f7dcdafbed60a010 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 15 Aug 2016 01:23:59 +0100 Subject: [PATCH 1/3] Decay const qualifiers in is_pod_struct<> --- include/pybind11/numpy.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 8653fc19..e2b5a16a 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -29,6 +29,8 @@ NAMESPACE_BEGIN(pybind11) namespace detail { template struct npy_format_descriptor { }; template struct is_pod_struct; +template using decay_cv_ref = + typename std::remove_cv::type>::type; struct npy_api { enum constants { @@ -321,14 +323,15 @@ template struct is_std_array> : std::tru template struct is_pod_struct { enum { value = std::is_pod::value && // offsetof only works correctly for POD types + !std::is_reference::value && !std::is_array::value && !is_std_array::value && !std::is_integral::value && - !std::is_same::value && - !std::is_same::value && - !std::is_same::value && - !std::is_same>::value && - !std::is_same>::value }; + !std::is_same::type, float>::value && + !std::is_same::type, double>::value && + !std::is_same::type, bool>::value && + !std::is_same::type, std::complex>::value && + !std::is_same::type, std::complex>::value }; }; template struct npy_format_descriptor::value>::type> { From 67b3daeea4c621849ba4636d1d9865c14d089629 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 15 Aug 2016 01:24:28 +0100 Subject: [PATCH 2/3] Always decay type param of npy_format_descriptor --- include/pybind11/numpy.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index e2b5a16a..57961c5a 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -29,8 +29,6 @@ NAMESPACE_BEGIN(pybind11) namespace detail { template struct npy_format_descriptor { }; template struct is_pod_struct; -template using decay_cv_ref = - typename std::remove_cv::type>::type; struct npy_api { enum constants { @@ -148,7 +146,7 @@ public: } template static dtype of() { - return detail::npy_format_descriptor::dtype(); + return detail::npy_format_descriptor::type>::dtype(); } size_t itemsize() const { @@ -306,7 +304,9 @@ public: template struct format_descriptor::value>::type> { - static std::string format() { return detail::npy_format_descriptor::format(); } + static std::string format() { + return detail::npy_format_descriptor::type>::format(); + } }; template struct format_descriptor { From 6956b655f0179a4c0afa9079eab0690ef4855af6 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Mon, 15 Aug 2016 01:24:59 +0100 Subject: [PATCH 3/3] Simplify code in eigen.h using new array ctors --- include/pybind11/eigen.h | 95 ++++++++-------------------------------- 1 file changed, 18 insertions(+), 77 deletions(-) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 2ea7d486..0035cc6e 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -85,7 +85,7 @@ struct type_caster::value && if (!buffer.check()) return false; - buffer_info info = buffer.request(); + auto info = buffer.request(); if (info.ndim == 1) { typedef Eigen::InnerStride<> Strides; if (!isVector && @@ -127,37 +127,19 @@ struct type_caster::value && static handle cast(const Type &src, return_value_policy /* policy */, handle /* parent */) { if (isVector) { - return array(buffer_info( - /* Pointer to buffer */ - const_cast(src.data()), - /* Size of one scalar */ - sizeof(Scalar), - /* Python struct-style format descriptor */ - format_descriptor::format(), - /* Number of dimensions */ - 1, - /* Buffer dimensions */ - { (size_t) src.size() }, - /* Strides (in bytes) for each index */ - { sizeof(Scalar) * static_cast(src.innerStride()) } - )).release(); + return array( + { (size_t) src.size() }, // shape + { sizeof(Scalar) * static_cast(src.innerStride()) }, // strides + src.data() // data + ).release(); } else { - return array(buffer_info( - /* Pointer to buffer */ - const_cast(src.data()), - /* Size of one scalar */ - sizeof(Scalar), - /* Python struct-style format descriptor */ - format_descriptor::format(), - /* Number of dimensions */ - isVector ? 1 : 2, - /* Buffer dimensions */ - { (size_t) src.rows(), + return array( + { (size_t) src.rows(), // shape (size_t) src.cols() }, - /* Strides (in bytes) for each index */ - { sizeof(Scalar) * static_cast(src.rowStride()), - sizeof(Scalar) * static_cast(src.colStride()) } - )).release(); + { sizeof(Scalar) * static_cast(src.rowStride()), // strides + sizeof(Scalar) * static_cast(src.colStride()) }, + src.data() // data + ).release(); } } @@ -248,9 +230,9 @@ struct type_caster::value>:: !outerIndicesArray.check()) return false; - buffer_info outerIndices = outerIndicesArray.request(); - buffer_info innerIndices = innerIndicesArray.request(); - buffer_info values = valuesArray.request(); + auto outerIndices = outerIndicesArray.request(); + auto innerIndices = innerIndicesArray.request(); + auto values = valuesArray.request(); value = Eigen::MappedSparseMatrix( shape[0].cast(), @@ -270,50 +252,9 @@ struct type_caster::value>:: object matrix_type = module::import("scipy.sparse").attr( rowMajor ? "csr_matrix" : "csc_matrix"); - array data(buffer_info( - // Pointer to buffer - const_cast(src.valuePtr()), - // Size of one scalar - sizeof(Scalar), - // Python struct-style format descriptor - format_descriptor::format(), - // Number of dimensions - 1, - // Buffer dimensions - { (size_t) src.nonZeros() }, - // Strides - { sizeof(Scalar) } - )); - - array outerIndices(buffer_info( - // Pointer to buffer - const_cast(src.outerIndexPtr()), - // Size of one scalar - sizeof(StorageIndex), - // Python struct-style format descriptor - format_descriptor::format(), - // Number of dimensions - 1, - // Buffer dimensions - { (size_t) (rowMajor ? src.rows() : src.cols()) + 1 }, - // Strides - { sizeof(StorageIndex) } - )); - - array innerIndices(buffer_info( - // Pointer to buffer - const_cast(src.innerIndexPtr()), - // Size of one scalar - sizeof(StorageIndex), - // Python struct-style format descriptor - format_descriptor::format(), - // Number of dimensions - 1, - // Buffer dimensions - { (size_t) src.nonZeros() }, - // Strides - { sizeof(StorageIndex) } - )); + array data((size_t) src.nonZeros(), src.valuePtr()); + array outerIndices((size_t) (rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr()); + array innerIndices((size_t) src.nonZeros(), src.innerIndexPtr()); return matrix_type( std::make_tuple(data, innerIndices, outerIndices),