You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
5.7 KiB

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