buffer_info.h
Go to the documentation of this file.
1 /*
2  pybind11/buffer_info.h: Python buffer object interface
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
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 
15 
16 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 bytes between adjacent entries (for each per dimension)
25  bool readonly = false; // flag to indicate if the underlying storage may be written to
26 
27  buffer_info() = default;
28 
29  buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim,
30  detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in, bool readonly=false)
31  : ptr(ptr), itemsize(itemsize), size(1), format(format), ndim(ndim),
32  shape(std::move(shape_in)), strides(std::move(strides_in)), readonly(readonly) {
33  if (ndim != (ssize_t) shape.size() || ndim != (ssize_t) strides.size())
34  pybind11_fail("buffer_info: ndim doesn't match shape and/or strides length");
35  for (size_t i = 0; i < (size_t) ndim; ++i)
36  size *= shape[i];
37  }
38 
39  template <typename T>
40  buffer_info(T *ptr, detail::any_container<ssize_t> shape_in, detail::any_container<ssize_t> strides_in, bool readonly=false)
41  : 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) { }
42 
43  buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size, bool readonly=false)
44  : buffer_info(ptr, itemsize, format, 1, {size}, {itemsize}, readonly) { }
45 
46  template <typename T>
47  buffer_info(T *ptr, ssize_t size, bool readonly=false)
48  : buffer_info(ptr, sizeof(T), format_descriptor<T>::format(), size, readonly) { }
49 
50  template <typename T>
51  buffer_info(const T *ptr, ssize_t size, bool readonly=true)
52  : buffer_info(const_cast<T*>(ptr), sizeof(T), format_descriptor<T>::format(), size, readonly) { }
53 
54  explicit buffer_info(Py_buffer *view, bool ownview = true)
55  : buffer_info(view->buf, view->itemsize, view->format, view->ndim,
56  {view->shape, view->shape + view->ndim}, {view->strides, view->strides + view->ndim}, view->readonly) {
57  this->m_view = view;
58  this->ownview = ownview;
59  }
60 
61  buffer_info(const buffer_info &) = delete;
62  buffer_info& operator=(const buffer_info &) = delete;
63 
64  buffer_info(buffer_info &&other) {
65  (*this) = std::move(other);
66  }
67 
68  buffer_info& operator=(buffer_info &&rhs) {
69  ptr = rhs.ptr;
70  itemsize = rhs.itemsize;
71  size = rhs.size;
72  format = std::move(rhs.format);
73  ndim = rhs.ndim;
74  shape = std::move(rhs.shape);
75  strides = std::move(rhs.strides);
76  std::swap(m_view, rhs.m_view);
77  std::swap(ownview, rhs.ownview);
78  readonly = rhs.readonly;
79  return *this;
80  }
81 
83  if (m_view && ownview) { PyBuffer_Release(m_view); delete m_view; }
84  }
85 
86  Py_buffer *view() const { return m_view; }
87  Py_buffer *&view() { return m_view; }
88 private:
89  struct private_ctr_tag { };
90 
91  buffer_info(private_ctr_tag, void *ptr, ssize_t itemsize, const std::string &format, ssize_t ndim,
92  detail::any_container<ssize_t> &&shape_in, detail::any_container<ssize_t> &&strides_in, bool readonly)
93  : buffer_info(ptr, itemsize, format, ndim, std::move(shape_in), std::move(strides_in), readonly) { }
94 
95  Py_buffer *m_view = nullptr;
96  bool ownview = false;
97 };
98 
100 
101 template <typename T, typename SFINAE = void> struct compare_buffer_info {
102  static bool compare(const buffer_info& b) {
103  return b.format == format_descriptor<T>::format() && b.itemsize == (ssize_t) sizeof(T);
104  }
105 };
106 
107 template <typename T> struct compare_buffer_info<T, detail::enable_if_t<std::is_integral<T>::value>> {
108  static bool compare(const buffer_info& b) {
109  return (size_t) b.itemsize == sizeof(T) && (b.format == format_descriptor<T>::value ||
110  ((sizeof(T) == sizeof(long)) && b.format == (std::is_unsigned<T>::value ? "L" : "l")) ||
111  ((sizeof(T) == sizeof(size_t)) && b.format == (std::is_unsigned<T>::value ? "N" : "n")));
112  }
113 };
114 
void * ptr
Definition: buffer_info.h:18
Py_buffer *& view()
Definition: buffer_info.h:87
Py_buffer * view() const
Definition: buffer_info.h:86
Scalar * b
Definition: benchVecAdd.cpp:17
std::vector< ssize_t > strides
Definition: buffer_info.h:24
static bool compare(const buffer_info &b)
Definition: buffer_info.h:102
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)
Definition: buffer_info.h:91
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set view
Definition: Half.h:150
buffer_info(buffer_info &&other)
Definition: buffer_info.h:64
PyExc_RuntimeError[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Used internally.
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1756
buffer_info & operator=(buffer_info &&rhs)
Definition: buffer_info.h:68
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
ssize_t itemsize
Definition: buffer_info.h:19
std::string format
Definition: buffer_info.h:21
float * ptr
buffer_info(T *ptr, detail::any_container< ssize_t > shape_in, detail::any_container< ssize_t > strides_in, bool readonly=false)
Definition: buffer_info.h:40
Eigen::Triplet< double > T
buffer_info(Py_buffer *view, bool ownview=true)
Definition: buffer_info.h:54
std::vector< ssize_t > shape
Definition: buffer_info.h:23
buffer_info(const T *ptr, ssize_t size, bool readonly=true)
Definition: buffer_info.h:51
buffer_info(T *ptr, ssize_t size, bool readonly=false)
Definition: buffer_info.h:47
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
Information record describing a Python buffer object.
Definition: buffer_info.h:17
buffer_info(void *ptr, ssize_t itemsize, const std::string &format, ssize_t size, bool readonly=false)
Definition: buffer_info.h:43
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)
Definition: buffer_info.h:29
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
Definition: mpreal.h:2986
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:41:43