2017-04-07 19:21:53 +00:00
|
|
|
/*
|
|
|
|
pybind11/buffer_info.h: Python buffer object interface
|
|
|
|
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2017-04-07 19:49:54 +00:00
|
|
|
#pragma once
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2017-08-13 22:35:53 +00:00
|
|
|
#include "detail/common.h"
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2020-10-03 21:09:14 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
|
|
|
|
|
|
// Default, C-style strides
|
|
|
|
inline std::vector<ssize_t> c_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) {
|
|
|
|
auto ndim = shape.size();
|
|
|
|
std::vector<ssize_t> strides(ndim, itemsize);
|
2022-02-08 00:23:20 +00:00
|
|
|
if (ndim > 0) {
|
|
|
|
for (size_t i = ndim - 1; i > 0; --i) {
|
2020-10-03 21:09:14 +00:00
|
|
|
strides[i - 1] = strides[i] * shape[i];
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-03 21:09:14 +00:00
|
|
|
return strides;
|
|
|
|
}
|
|
|
|
|
|
|
|
// F-style strides; default when constructing an array_t with `ExtraFlags & f_style`
|
|
|
|
inline std::vector<ssize_t> f_strides(const std::vector<ssize_t> &shape, ssize_t itemsize) {
|
|
|
|
auto ndim = shape.size();
|
|
|
|
std::vector<ssize_t> strides(ndim, itemsize);
|
2022-02-08 00:23:20 +00:00
|
|
|
for (size_t i = 1; i < ndim; ++i) {
|
2020-10-03 21:09:14 +00:00
|
|
|
strides[i] = strides[i - 1] * shape[i - 1];
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2020-10-03 21:09:14 +00:00
|
|
|
return strides;
|
|
|
|
}
|
|
|
|
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
|
2017-04-07 19:21:53 +00:00
|
|
|
/// Information record describing a Python buffer object
|
|
|
|
struct buffer_info {
|
2017-04-14 20:33:44 +00:00
|
|
|
void *ptr = nullptr; // Pointer to the underlying storage
|
|
|
|
ssize_t itemsize = 0; // Size of individual items in bytes
|
|
|
|
ssize_t size = 0; // Total number of entries
|
2022-02-10 20:17:07 +00:00
|
|
|
std::string format; // For homogeneous buffers, this should be set to
|
|
|
|
// format_descriptor<T>::format()
|
2017-04-14 20:33:44 +00:00
|
|
|
ssize_t ndim = 0; // Number of dimensions
|
|
|
|
std::vector<ssize_t> shape; // Shape of the tensor (1 entry per dimension)
|
2022-02-10 19:42:03 +00:00
|
|
|
std::vector<ssize_t> strides; // Number of bytes between adjacent entries
|
|
|
|
// (for each per dimension)
|
2019-11-24 07:33:05 +00:00
|
|
|
bool readonly = false; // flag to indicate if the underlying storage may be written to
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2020-09-11 03:15:22 +00:00
|
|
|
buffer_info() = default;
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
buffer_info(void *ptr,
|
|
|
|
ssize_t itemsize,
|
|
|
|
const std::string &format,
|
|
|
|
ssize_t ndim,
|
|
|
|
detail::any_container<ssize_t> shape_in,
|
|
|
|
detail::any_container<ssize_t> strides_in,
|
|
|
|
bool readonly = false)
|
|
|
|
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
|
|
|
|
shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size()) {
|
2017-04-07 19:49:54 +00:00
|
|
|
pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < (size_t) ndim; ++i) {
|
2017-04-07 19:21:53 +00:00
|
|
|
size *= shape[i];
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-04-07 19:21:53 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 01:13:55 +00:00
|
|
|
template <typename T>
|
2022-02-10 20:17:07 +00:00
|
|
|
buffer_info(T *ptr,
|
|
|
|
detail::any_container<ssize_t> shape_in,
|
|
|
|
detail::any_container<ssize_t> strides_in,
|
|
|
|
bool readonly = false)
|
|
|
|
: buffer_info(private_ctr_tag(),
|
|
|
|
ptr,
|
|
|
|
sizeof(T),
|
|
|
|
format_descriptor<T>::format(),
|
|
|
|
static_cast<ssize_t>(shape_in->size()),
|
|
|
|
std::move(shape_in),
|
|
|
|
std::move(strides_in),
|
|
|
|
readonly) {}
|
|
|
|
|
|
|
|
buffer_info(void *ptr,
|
|
|
|
ssize_t itemsize,
|
|
|
|
const std::string &format,
|
|
|
|
ssize_t size,
|
|
|
|
bool readonly = false)
|
|
|
|
: buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}, readonly) {}
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2017-05-29 01:13:55 +00:00
|
|
|
template <typename T>
|
2022-02-10 20:17:07 +00:00
|
|
|
buffer_info(T *ptr, ssize_t size, bool readonly = false)
|
|
|
|
: buffer_info(ptr, sizeof(T), format_descriptor<T>::format(), size, readonly) {}
|
2019-11-24 07:33:05 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2022-02-10 20:17:07 +00:00
|
|
|
buffer_info(const T *ptr, ssize_t size, bool readonly = true)
|
|
|
|
: buffer_info(
|
|
|
|
const_cast<T *>(ptr), sizeof(T), format_descriptor<T>::format(), size, readonly) {}
|
2017-05-29 01:13:55 +00:00
|
|
|
|
2017-04-14 17:57:49 +00:00
|
|
|
explicit buffer_info(Py_buffer *view, bool ownview = true)
|
2022-02-10 20:17:07 +00:00
|
|
|
: buffer_info(
|
|
|
|
view->buf,
|
|
|
|
view->itemsize,
|
|
|
|
view->format,
|
|
|
|
view->ndim,
|
2020-10-03 21:09:14 +00:00
|
|
|
{view->shape, view->shape + view->ndim},
|
|
|
|
/* Though buffer::request() requests PyBUF_STRIDES, ctypes objects
|
|
|
|
* ignore this flag and return a view with NULL strides.
|
|
|
|
* When strides are NULL, build them manually. */
|
|
|
|
view->strides
|
2022-02-10 20:17:07 +00:00
|
|
|
? std::vector<ssize_t>(view->strides, view->strides + view->ndim)
|
|
|
|
: detail::c_strides({view->shape, view->shape + view->ndim}, view->itemsize),
|
2021-07-26 20:26:36 +00:00
|
|
|
(view->readonly != 0)) {
|
2022-02-10 17:45:46 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
|
2020-07-15 15:50:43 +00:00
|
|
|
this->m_view = view;
|
2022-02-10 17:45:46 +00:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
|
2017-04-14 17:57:49 +00:00
|
|
|
this->ownview = ownview;
|
2017-04-07 19:21:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer_info(const buffer_info &) = delete;
|
2022-02-10 20:17:07 +00:00
|
|
|
buffer_info &operator=(const buffer_info &) = delete;
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2021-06-19 17:53:27 +00:00
|
|
|
buffer_info(buffer_info &&other) noexcept { (*this) = std::move(other); }
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2021-06-19 17:53:27 +00:00
|
|
|
buffer_info &operator=(buffer_info &&rhs) noexcept {
|
2017-04-07 19:21:53 +00:00
|
|
|
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);
|
2020-07-15 15:50:43 +00:00
|
|
|
std::swap(m_view, rhs.m_view);
|
2017-04-07 19:21:53 +00:00
|
|
|
std::swap(ownview, rhs.ownview);
|
2019-11-24 07:33:05 +00:00
|
|
|
readonly = rhs.readonly;
|
2017-04-07 19:21:53 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~buffer_info() {
|
2022-02-10 20:17:07 +00:00
|
|
|
if (m_view && ownview) {
|
|
|
|
PyBuffer_Release(m_view);
|
|
|
|
delete m_view;
|
|
|
|
}
|
2017-04-07 19:21:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:50:43 +00:00
|
|
|
Py_buffer *view() const { return m_view; }
|
|
|
|
Py_buffer *&view() { return m_view; }
|
2017-05-29 01:13:55 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
private:
|
|
|
|
struct private_ctr_tag {};
|
|
|
|
|
|
|
|
buffer_info(private_ctr_tag,
|
|
|
|
void *ptr,
|
|
|
|
ssize_t itemsize,
|
|
|
|
const std::string &format,
|
|
|
|
ssize_t ndim,
|
|
|
|
detail::any_container<ssize_t> &&shape_in,
|
|
|
|
detail::any_container<ssize_t> &&strides_in,
|
|
|
|
bool readonly)
|
|
|
|
: buffer_info(
|
|
|
|
ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) {}
|
2017-05-29 01:13:55 +00:00
|
|
|
|
2020-07-15 15:50:43 +00:00
|
|
|
Py_buffer *m_view = nullptr;
|
2017-04-07 19:21:53 +00:00
|
|
|
bool ownview = false;
|
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
2017-04-07 19:21:53 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T, typename SFINAE = void>
|
|
|
|
struct compare_buffer_info {
|
|
|
|
static bool compare(const buffer_info &b) {
|
2017-04-14 20:33:44 +00:00
|
|
|
return b.format == format_descriptor<T>::format() && b.itemsize == (ssize_t) sizeof(T);
|
2017-04-07 19:21:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T>
|
|
|
|
struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
|
|
|
|
static bool compare(const buffer_info &b) {
|
|
|
|
return (size_t) b.itemsize == sizeof(T)
|
|
|
|
&& (b.format == format_descriptor<T>::value
|
|
|
|
|| ((sizeof(T) == sizeof(long))
|
|
|
|
&& b.format == (std::is_unsigned<T>::value ? "L" : "l"))
|
|
|
|
|| ((sizeof(T) == sizeof(size_t))
|
|
|
|
&& b.format == (std::is_unsigned<T>::value ? "N" : "n")));
|
2017-04-07 19:21:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-08 22:14:41 +00:00
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|