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), diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 8653fc19..57961c5a 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -146,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 { @@ -304,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 { @@ -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> {