Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb6062b

Browse files
author
Davide Faconti
committed
WIP
1 parent 2a2c34c commit fb6062b

27 files changed

+12724
-2
lines changed

3rdparty/pybind11/attr.h

Lines changed: 489 additions & 0 deletions
Large diffs are not rendered by default.

3rdparty/pybind11/buffer_info.h

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
pybind11/buffer_info.h: Python buffer object interface
3+
4+
Copyright (c) 2016 Wenzel Jakob <[email protected]>
5+
6+
All rights reserved. Use of this source code is governed by a
7+
BSD-style license that can be found in the LICENSE file.
8+
*/
9+
10+
#pragma once
11+
12+
#include "detail/common.h"
13+
14+
NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
15+
16+
/// Information record describing a Python buffer object
17+
struct buffer_info {
18+
void *ptr = nullptr; // Pointer to the underlying storage
19+
ssize_t itemsize = 0; // Size of individual items in bytes
20+
ssize_t size = 0; // Total number of entries
21+
std::string format; // For homogeneous buffers, this should be set to format_descriptor<T>::format()
22+
ssize_t ndim = 0; // Number of dimensions
23+
std::vector<ssize_t> shape; // Shape of the tensor (1 entry per dimension)
24+
std::vector<ssize_t> strides; // Number of entries between adjacent entries (for each per dimension)
25+
26+
buffer_info() { }
27+
28+
buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim,
29+
detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in)
30+
: ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
31+
shape(std::move(shape_in)), strides(std::move(strides_in)) {
32+
if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size())
33+
pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
34+
for (size_t i = 0; i < (size_t) ndim; ++i)
35+
size *= shape[i];
36+
}
37+
38+
template <typename T>
39+
buffer_info(T *ptr, detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in)
40+
: 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)) { }
41+
42+
buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size)
43+
: buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}) { }
44+
45+
template <typename T>
46+
buffer_info(T *ptr, ssize_t size)
47+
: buffer_info(ptr, sizeof(T), format_descriptor<T>::format(), size) { }
48+
49+
explicit buffer_info(Py_buffer *view, bool ownview = true)
50+
: buffer_info(view->buf, view->itemsize, view->format, view->ndim,
51+
{view->shape, view->shape + view->ndim}, {view->strides, view->strides + view->ndim}) {
52+
this->view = view;
53+
this->ownview = ownview;
54+
}
55+
56+
buffer_info(const buffer_info &) = delete;
57+
buffer_info& operator=(const buffer_info &) = delete;
58+
59+
buffer_info(buffer_info &&other) {
60+
(*this) = std::move(other);
61+
}
62+
63+
buffer_info& operator=(buffer_info &&rhs) {
64+
ptr = rhs.ptr;
65+
itemsize = rhs.itemsize;
66+
size = rhs.size;
67+
format = std::move(rhs.format);
68+
ndim = rhs.ndim;
69+
shape = std::move(rhs.shape);
70+
strides = std::move(rhs.strides);
71+
std::swap(view, rhs.view);
72+
std::swap(ownview, rhs.ownview);
73+
return *this;
74+
}
75+
76+
~buffer_info() {
77+
if (view && ownview) { PyBuffer_Release(view); delete view; }
78+
}
79+
80+
private:
81+
struct private_ctr_tag { };
82+
83+
buffer_info(private_ctr_tag, void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim,
84+
detail::any_container<ssize_t> &&shape_in, detail::any_container<ssize_t> &&strides_in)
85+
: buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in)) { }
86+
87+
Py_buffer *view = nullptr;
88+
bool ownview = false;
89+
};
90+
91+
NAMESPACE_BEGIN(detail)
92+
93+
template <typename T, typename SFINAE = void> struct compare_buffer_info {
94+
static bool compare(const buffer_info& b) {
95+
return b.format == format_descriptor<T>::format() && b.itemsize == (ssize_t) sizeof(T);
96+
}
97+
};
98+
99+
template <typename T> struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
100+
static bool compare(const buffer_info& b) {
101+
return (size_t) b.itemsize == sizeof(T) && (b.format == format_descriptor<T>::value ||
102+
((sizeof(T) == sizeof(long)) && b.format == (std::is_unsigned<T>::value ? "L" : "l")) ||
103+
((sizeof(T) == sizeof(size_t)) && b.format == (std::is_unsigned<T>::value ? "N" : "n")));
104+
}
105+
};
106+
107+
NAMESPACE_END(detail)
108+
NAMESPACE_END(PYBIND11_NAMESPACE)

0 commit comments

Comments
 (0)