12#include "detail/common.h"
20 auto ndim = shape.size();
21 std::vector<ssize_t> strides(ndim, itemsize);
23 for (
size_t i = ndim - 1; i > 0; --i) {
24 strides[i - 1] = strides[i] * shape[i];
31inline std::vector<ssize_t>
f_strides(
const std::vector<ssize_t> &shape,
ssize_t itemsize) {
32 auto ndim = shape.size();
33 std::vector<ssize_t> strides(ndim, itemsize);
34 for (
size_t i = 1; i < ndim; ++i) {
35 strides[i] = strides[i - 1] * shape[i - 1];
53 bool readonly =
false;
59 const std::string &format,
61 detail::any_container<ssize_t> shape_in,
62 detail::any_container<ssize_t> strides_in,
63 bool readonly =
false)
64 : ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
65 shape(
std::
move(shape_in)), strides(
std::
move(strides_in)), readonly(readonly) {
66 if (ndim != (
ssize_t) shape.size() || ndim != (
ssize_t) strides.size()) {
67 pybind11_fail(
"buffer_info: ndim doesn't match shape and/or strides length");
69 for (
size_t i = 0; i < (
size_t) ndim; ++i) {
76 detail::any_container<ssize_t> shape_in,
77 detail::any_container<ssize_t> strides_in,
78 bool readonly =
false)
83 static_cast<
ssize_t>(shape_in->size()),
90 const std::string &format,
92 bool readonly =
false)
93 :
buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}, readonly) {}
102 const_cast<T *>(ptr), sizeof(T),
format_descriptor<T>::format(), size, readonly) {}
110 {view->shape, view->shape + view->ndim},
115 ? std::vector<ssize_t>(view->strides, view->strides + view->ndim)
117 (view->readonly != 0)) {
121 this->ownview = ownview;
131 itemsize = rhs.itemsize;
133 format = std::move(rhs.format);
135 shape = std::move(rhs.shape);
136 strides = std::move(rhs.strides);
137 std::swap(m_view, rhs.m_view);
138 std::swap(ownview, rhs.ownview);
139 readonly = rhs.readonly;
144 if (m_view && ownview) {
145 PyBuffer_Release(m_view);
150 Py_buffer *
view()
const {
return m_view; }
151 Py_buffer *&
view() {
return m_view; }
159 const std::string &format,
161 detail::any_container<ssize_t> &&shape_in,
162 detail::any_container<ssize_t> &&strides_in,
165 ptr, itemsize, format, ndim,
std::
move(shape_in),
std::
move(strides_in), readonly) {}
167 Py_buffer *m_view =
nullptr;
168 bool ownview =
false;
173template <
typename T,
typename SFINAE =
void>
183 return (
size_t) b.
itemsize ==
sizeof(T)
185 || ((
sizeof(T) ==
sizeof(
long))
186 && b.
format == (std::is_unsigned<T>::value ?
"L" :
"l"))
187 || ((
sizeof(T) ==
sizeof(
size_t))
188 && b.
format == (std::is_unsigned<T>::value ?
"N" :
"n")));
std::vector< ssize_t > f_strides(const std::vector< ssize_t > &shape, ssize_t itemsize)
std::vector< ssize_t > c_strides(const std::vector< ssize_t > &shape, ssize_t itemsize)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
@ move
Use std::move to move the return value contents into a new instance that will be owned by Python.
Information record describing a Python buffer object.
buffer_info(const T *ptr, ssize_t size, bool readonly=true)
buffer_info(T *ptr, ssize_t size, bool readonly=false)
buffer_info(Py_buffer *view, bool ownview=true)
std::vector< ssize_t > shape
std::vector< ssize_t > strides
buffer_info(buffer_info &&other) noexcept
buffer_info & operator=(const buffer_info &)=delete
buffer_info(const buffer_info &)=delete
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)
buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size, bool readonly=false)
buffer_info(T *ptr, detail::any_container< ssize_t > shape_in, detail::any_container< ssize_t > strides_in, bool readonly=false)
buffer_info & operator=(buffer_info &&rhs) noexcept
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)
static bool compare(const buffer_info &b)
static bool compare(const buffer_info &b)