test_buffers.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. tests/test_buffers.cpp -- supporting Pythons' buffer protocol
  3. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. TEST_SUBMODULE(buffers, m) {
  10. // test_from_python / test_to_python:
  11. class Matrix {
  12. public:
  13. Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) {
  14. print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  15. m_data = new float[(size_t) (rows*cols)];
  16. memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
  17. }
  18. Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
  19. print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  20. m_data = new float[(size_t) (m_rows * m_cols)];
  21. memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
  22. }
  23. Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
  24. print_move_created(this);
  25. s.m_rows = 0;
  26. s.m_cols = 0;
  27. s.m_data = nullptr;
  28. }
  29. ~Matrix() {
  30. print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  31. delete[] m_data;
  32. }
  33. Matrix &operator=(const Matrix &s) {
  34. print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  35. delete[] m_data;
  36. m_rows = s.m_rows;
  37. m_cols = s.m_cols;
  38. m_data = new float[(size_t) (m_rows * m_cols)];
  39. memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
  40. return *this;
  41. }
  42. Matrix &operator=(Matrix &&s) {
  43. print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  44. if (&s != this) {
  45. delete[] m_data;
  46. m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
  47. s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
  48. }
  49. return *this;
  50. }
  51. float operator()(ssize_t i, ssize_t j) const {
  52. return m_data[(size_t) (i*m_cols + j)];
  53. }
  54. float &operator()(ssize_t i, ssize_t j) {
  55. return m_data[(size_t) (i*m_cols + j)];
  56. }
  57. float *data() { return m_data; }
  58. ssize_t rows() const { return m_rows; }
  59. ssize_t cols() const { return m_cols; }
  60. private:
  61. ssize_t m_rows;
  62. ssize_t m_cols;
  63. float *m_data;
  64. };
  65. py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
  66. .def(py::init<ssize_t, ssize_t>())
  67. /// Construct from a buffer
  68. .def(py::init([](py::buffer b) {
  69. py::buffer_info info = b.request();
  70. if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
  71. throw std::runtime_error("Incompatible buffer format!");
  72. auto v = new Matrix(info.shape[0], info.shape[1]);
  73. memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols()));
  74. return v;
  75. }))
  76. .def("rows", &Matrix::rows)
  77. .def("cols", &Matrix::cols)
  78. /// Bare bones interface
  79. .def("__getitem__", [](const Matrix &m, std::pair<ssize_t, ssize_t> i) {
  80. if (i.first >= m.rows() || i.second >= m.cols())
  81. throw py::index_error();
  82. return m(i.first, i.second);
  83. })
  84. .def("__setitem__", [](Matrix &m, std::pair<ssize_t, ssize_t> i, float v) {
  85. if (i.first >= m.rows() || i.second >= m.cols())
  86. throw py::index_error();
  87. m(i.first, i.second) = v;
  88. })
  89. /// Provide buffer access
  90. .def_buffer([](Matrix &m) -> py::buffer_info {
  91. return py::buffer_info(
  92. m.data(), /* Pointer to buffer */
  93. { m.rows(), m.cols() }, /* Buffer dimensions */
  94. { sizeof(float) * size_t(m.rows()), /* Strides (in bytes) for each index */
  95. sizeof(float) }
  96. );
  97. })
  98. ;
  99. // test_inherited_protocol
  100. class SquareMatrix : public Matrix {
  101. public:
  102. SquareMatrix(ssize_t n) : Matrix(n, n) { }
  103. };
  104. // Derived classes inherit the buffer protocol and the buffer access function
  105. py::class_<SquareMatrix, Matrix>(m, "SquareMatrix")
  106. .def(py::init<ssize_t>());
  107. // test_pointer_to_member_fn
  108. // Tests that passing a pointer to member to the base class works in
  109. // the derived class.
  110. struct Buffer {
  111. int32_t value = 0;
  112. py::buffer_info get_buffer_info() {
  113. return py::buffer_info(&value, sizeof(value),
  114. py::format_descriptor<int32_t>::format(), 1);
  115. }
  116. };
  117. py::class_<Buffer>(m, "Buffer", py::buffer_protocol())
  118. .def(py::init<>())
  119. .def_readwrite("value", &Buffer::value)
  120. .def_buffer(&Buffer::get_buffer_info);
  121. class ConstBuffer {
  122. std::unique_ptr<int32_t> value;
  123. public:
  124. int32_t get_value() const { return *value; }
  125. void set_value(int32_t v) { *value = v; }
  126. py::buffer_info get_buffer_info() const {
  127. return py::buffer_info(value.get(), sizeof(*value),
  128. py::format_descriptor<int32_t>::format(), 1);
  129. }
  130. ConstBuffer() : value(new int32_t{0}) { };
  131. };
  132. py::class_<ConstBuffer>(m, "ConstBuffer", py::buffer_protocol())
  133. .def(py::init<>())
  134. .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value)
  135. .def_buffer(&ConstBuffer::get_buffer_info);
  136. struct DerivedBuffer : public Buffer { };
  137. py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol())
  138. .def(py::init<>())
  139. .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value)
  140. .def_buffer(&DerivedBuffer::get_buffer_info);
  141. }