From 3718c38e68c42edcf1db82b3db978482691d057a Mon Sep 17 00:00:00 2001 From: Jason Newton Date: Fri, 2 Sep 2016 17:10:02 -0400 Subject: [PATCH 1/4] default all fields in all ctors --- include/pybind11/common.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index eff105cfc..32e245b46 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -203,15 +203,15 @@ enum class return_value_policy : uint8_t { /// Information record describing a Python buffer object struct buffer_info { - void *ptr; // Pointer to the underlying storage - size_t itemsize; // Size of individual items in bytes - size_t size; // Total number of entries + void *ptr = nullptr; // Pointer to the underlying storage + size_t itemsize = 0; // Size of individual items in bytes + size_t size = 0; // Total number of entries std::string format; // For homogeneous buffers, this should be set to format_descriptor::format() - size_t ndim; // Number of dimensions + size_t ndim = 0; // Number of dimensions std::vector shape; // Shape of the tensor (1 entry per dimension) std::vector strides; // Number of entries between adjacent entries (for each per dimension) - buffer_info() : ptr(nullptr), view(nullptr) {} + buffer_info(){} buffer_info(void *ptr, size_t itemsize, const std::string &format, size_t ndim, const std::vector &shape, const std::vector &strides) From 514c6dad7065a77f7fc1a0b93c7c4983b8f35fb8 Mon Sep 17 00:00:00 2001 From: Jason Newton Date: Fri, 2 Sep 2016 17:10:25 -0400 Subject: [PATCH 2/4] add field for ownership --- include/pybind11/common.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 32e245b46..9e9af98d8 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -225,9 +225,9 @@ struct buffer_info { : buffer_info(ptr, itemsize, format, 1, std::vector { size }, std::vector { itemsize }) { } - buffer_info(Py_buffer *view) + buffer_info(Py_buffer *view, bool ownview = true) : ptr(view->buf), itemsize((size_t) view->itemsize), size(1), format(view->format), - ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view) { + ndim((size_t) view->ndim), shape((size_t) view->ndim), strides((size_t) view->ndim), view(view), ownview(ownview) { for (size_t i = 0; i < (size_t) view->ndim; ++i) { shape[i] = (size_t) view->shape[i]; strides[i] = (size_t) view->strides[i]; @@ -236,11 +236,12 @@ struct buffer_info { } ~buffer_info() { - if (view) { PyBuffer_Release(view); delete view; } + if (view && ownview) { PyBuffer_Release(view); delete view; } } private: Py_buffer *view = nullptr; + bool ownview = false; }; NAMESPACE_BEGIN(detail) From 4764698069be4e2c10864bb3a26faf6ba7b1f5a1 Mon Sep 17 00:00:00 2001 From: Jason Newton Date: Fri, 2 Sep 2016 17:11:30 -0400 Subject: [PATCH 3/4] add move ctor and move-assignment operator --- include/pybind11/common.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 9e9af98d8..48fee4413 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -235,6 +235,23 @@ struct buffer_info { } } + buffer_info(buffer_info &&other){ + (*this) = std::move(other); + } + + buffer_info& operator=(buffer_info &&rhs){ + ptr = rhs.ptr; + itemsize = rhs.itemsize; + size = rhs.size; + format = std::move(rhs.format); + ndim = rhs.ndim; + shape = std::move(rhs.shape); + strides = std::move(rhs.strides); + std::swap(view, rhs.view); + std::swap(ownview, rhs.ownview); + return *this; + } + ~buffer_info() { if (view && ownview) { PyBuffer_Release(view); delete view; } } From 10d46e7f73c517c97fecb24a3df9917b88266e2d Mon Sep 17 00:00:00 2001 From: Jason Newton Date: Fri, 2 Sep 2016 18:39:47 -0400 Subject: [PATCH 4/4] explicitly delete copy-ctor and assignment operator --- include/pybind11/common.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/pybind11/common.h b/include/pybind11/common.h index 48fee4413..eec131c65 100644 --- a/include/pybind11/common.h +++ b/include/pybind11/common.h @@ -235,6 +235,9 @@ struct buffer_info { } } + buffer_info(const buffer_info &) = delete; + buffer_info& operator=(const buffer_info &) = delete; + buffer_info(buffer_info &&other){ (*this) = std::move(other); }