|
10 | 10 | #include "pybind11_tests.h"
|
11 | 11 | #include "constructor_stats.h"
|
12 | 12 |
|
13 |
| -class Matrix { |
14 |
| -public: |
15 |
| - Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) { |
16 |
| - print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
17 |
| - m_data = new float[(size_t) (rows*cols)]; |
18 |
| - memset(m_data, 0, sizeof(float) * (size_t) (rows * cols)); |
19 |
| - } |
20 |
| - |
21 |
| - Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) { |
22 |
| - print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
23 |
| - m_data = new float[(size_t) (m_rows * m_cols)]; |
24 |
| - memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); |
25 |
| - } |
26 |
| - |
27 |
| - Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) { |
28 |
| - print_move_created(this); |
29 |
| - s.m_rows = 0; |
30 |
| - s.m_cols = 0; |
31 |
| - s.m_data = nullptr; |
32 |
| - } |
33 |
| - |
34 |
| - ~Matrix() { |
35 |
| - print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
36 |
| - delete[] m_data; |
37 |
| - } |
38 |
| - |
39 |
| - Matrix &operator=(const Matrix &s) { |
40 |
| - print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
41 |
| - delete[] m_data; |
42 |
| - m_rows = s.m_rows; |
43 |
| - m_cols = s.m_cols; |
44 |
| - m_data = new float[(size_t) (m_rows * m_cols)]; |
45 |
| - memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); |
46 |
| - return *this; |
47 |
| - } |
48 |
| - |
49 |
| - Matrix &operator=(Matrix &&s) { |
50 |
| - print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
51 |
| - if (&s != this) { |
52 |
| - delete[] m_data; |
53 |
| - m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data; |
54 |
| - s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr; |
| 13 | +TEST_SUBMODULE(buffers, m) { |
| 14 | + // test_from_python / test_to_python: |
| 15 | + class Matrix { |
| 16 | + public: |
| 17 | + Matrix(ssize_t rows, ssize_t cols) : m_rows(rows), m_cols(cols) { |
| 18 | + print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 19 | + m_data = new float[(size_t) (rows*cols)]; |
| 20 | + memset(m_data, 0, sizeof(float) * (size_t) (rows * cols)); |
55 | 21 | }
|
56 |
| - return *this; |
57 |
| - } |
58 |
| - |
59 |
| - float operator()(ssize_t i, ssize_t j) const { |
60 |
| - return m_data[(size_t) (i*m_cols + j)]; |
61 |
| - } |
62 |
| - |
63 |
| - float &operator()(ssize_t i, ssize_t j) { |
64 |
| - return m_data[(size_t) (i*m_cols + j)]; |
65 |
| - } |
66 |
| - |
67 |
| - float *data() { return m_data; } |
68 | 22 |
|
69 |
| - ssize_t rows() const { return m_rows; } |
70 |
| - ssize_t cols() const { return m_cols; } |
71 |
| -private: |
72 |
| - ssize_t m_rows; |
73 |
| - ssize_t m_cols; |
74 |
| - float *m_data; |
75 |
| -}; |
76 |
| - |
77 |
| -class SquareMatrix : public Matrix { |
78 |
| -public: |
79 |
| - SquareMatrix(ssize_t n) : Matrix(n, n) { } |
80 |
| -}; |
81 |
| - |
82 |
| -struct PTMFBuffer { |
83 |
| - int32_t value = 0; |
84 |
| - |
85 |
| - py::buffer_info get_buffer_info() { |
86 |
| - return py::buffer_info(&value, sizeof(value), |
87 |
| - py::format_descriptor<int32_t>::format(), 1); |
88 |
| - } |
89 |
| -}; |
| 23 | + Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) { |
| 24 | + print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 25 | + m_data = new float[(size_t) (m_rows * m_cols)]; |
| 26 | + memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); |
| 27 | + } |
90 | 28 |
|
91 |
| -class ConstPTMFBuffer { |
92 |
| - std::unique_ptr<int32_t> value; |
| 29 | + Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) { |
| 30 | + print_move_created(this); |
| 31 | + s.m_rows = 0; |
| 32 | + s.m_cols = 0; |
| 33 | + s.m_data = nullptr; |
| 34 | + } |
93 | 35 |
|
94 |
| -public: |
95 |
| - int32_t get_value() const { return *value; } |
96 |
| - void set_value(int32_t v) { *value = v; } |
| 36 | + ~Matrix() { |
| 37 | + print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 38 | + delete[] m_data; |
| 39 | + } |
97 | 40 |
|
98 |
| - py::buffer_info get_buffer_info() const { |
99 |
| - return py::buffer_info(value.get(), sizeof(*value), |
100 |
| - py::format_descriptor<int32_t>::format(), 1); |
101 |
| - } |
| 41 | + Matrix &operator=(const Matrix &s) { |
| 42 | + print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 43 | + delete[] m_data; |
| 44 | + m_rows = s.m_rows; |
| 45 | + m_cols = s.m_cols; |
| 46 | + m_data = new float[(size_t) (m_rows * m_cols)]; |
| 47 | + memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols)); |
| 48 | + return *this; |
| 49 | + } |
102 | 50 |
|
103 |
| - ConstPTMFBuffer() : value(new int32_t{0}) { }; |
104 |
| -}; |
| 51 | + Matrix &operator=(Matrix &&s) { |
| 52 | + print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix"); |
| 53 | + if (&s != this) { |
| 54 | + delete[] m_data; |
| 55 | + m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data; |
| 56 | + s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr; |
| 57 | + } |
| 58 | + return *this; |
| 59 | + } |
105 | 60 |
|
106 |
| -struct DerivedPTMFBuffer : public PTMFBuffer { }; |
| 61 | + float operator()(ssize_t i, ssize_t j) const { |
| 62 | + return m_data[(size_t) (i*m_cols + j)]; |
| 63 | + } |
107 | 64 |
|
108 |
| -test_initializer buffers([](py::module &m) { |
109 |
| - py::class_<Matrix> mtx(m, "Matrix", py::buffer_protocol()); |
| 65 | + float &operator()(ssize_t i, ssize_t j) { |
| 66 | + return m_data[(size_t) (i*m_cols + j)]; |
| 67 | + } |
110 | 68 |
|
111 |
| - mtx.def(py::init<ssize_t, ssize_t>()) |
| 69 | + float *data() { return m_data; } |
| 70 | + |
| 71 | + ssize_t rows() const { return m_rows; } |
| 72 | + ssize_t cols() const { return m_cols; } |
| 73 | + private: |
| 74 | + ssize_t m_rows; |
| 75 | + ssize_t m_cols; |
| 76 | + float *m_data; |
| 77 | + }; |
| 78 | + py::class_<Matrix>(m, "Matrix", py::buffer_protocol()) |
| 79 | + .def(py::init<ssize_t, ssize_t>()) |
112 | 80 | /// Construct from a buffer
|
113 | 81 | .def("__init__", [](Matrix &v, py::buffer b) {
|
114 | 82 | py::buffer_info info = b.request();
|
@@ -143,24 +111,57 @@ test_initializer buffers([](py::module &m) {
|
143 | 111 | })
|
144 | 112 | ;
|
145 | 113 |
|
| 114 | + |
| 115 | + // test_inherited_protocol |
| 116 | + class SquareMatrix : public Matrix { |
| 117 | + public: |
| 118 | + SquareMatrix(ssize_t n) : Matrix(n, n) { } |
| 119 | + }; |
146 | 120 | // Derived classes inherit the buffer protocol and the buffer access function
|
147 | 121 | py::class_<SquareMatrix, Matrix>(m, "SquareMatrix")
|
148 | 122 | .def(py::init<ssize_t>());
|
149 | 123 |
|
150 |
| - py::class_<PTMFBuffer>(m, "PTMFBuffer", py::buffer_protocol()) |
| 124 | + |
| 125 | + // test_pointer_to_member_fn |
| 126 | + // Tests that passing a pointer to member to the base class works in |
| 127 | + // the derived class. |
| 128 | + struct Buffer { |
| 129 | + int32_t value = 0; |
| 130 | + |
| 131 | + py::buffer_info get_buffer_info() { |
| 132 | + return py::buffer_info(&value, sizeof(value), |
| 133 | + py::format_descriptor<int32_t>::format(), 1); |
| 134 | + } |
| 135 | + }; |
| 136 | + py::class_<Buffer>(m, "Buffer", py::buffer_protocol()) |
151 | 137 | .def(py::init<>())
|
152 |
| - .def_readwrite("value", &PTMFBuffer::value) |
153 |
| - .def_buffer(&PTMFBuffer::get_buffer_info); |
| 138 | + .def_readwrite("value", &Buffer::value) |
| 139 | + .def_buffer(&Buffer::get_buffer_info); |
| 140 | + |
154 | 141 |
|
155 |
| - py::class_<ConstPTMFBuffer>(m, "ConstPTMFBuffer", py::buffer_protocol()) |
| 142 | + class ConstBuffer { |
| 143 | + std::unique_ptr<int32_t> value; |
| 144 | + |
| 145 | + public: |
| 146 | + int32_t get_value() const { return *value; } |
| 147 | + void set_value(int32_t v) { *value = v; } |
| 148 | + |
| 149 | + py::buffer_info get_buffer_info() const { |
| 150 | + return py::buffer_info(value.get(), sizeof(*value), |
| 151 | + py::format_descriptor<int32_t>::format(), 1); |
| 152 | + } |
| 153 | + |
| 154 | + ConstBuffer() : value(new int32_t{0}) { }; |
| 155 | + }; |
| 156 | + py::class_<ConstBuffer>(m, "ConstBuffer", py::buffer_protocol()) |
156 | 157 | .def(py::init<>())
|
157 |
| - .def_property("value", &ConstPTMFBuffer::get_value, &ConstPTMFBuffer::set_value) |
158 |
| - .def_buffer(&ConstPTMFBuffer::get_buffer_info); |
| 158 | + .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value) |
| 159 | + .def_buffer(&ConstBuffer::get_buffer_info); |
159 | 160 |
|
160 |
| - // Tests that passing a pointer to member to the base class works in |
161 |
| - // the derived class. |
162 |
| - py::class_<DerivedPTMFBuffer>(m, "DerivedPTMFBuffer", py::buffer_protocol()) |
| 161 | + struct DerivedBuffer : public Buffer { }; |
| 162 | + py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol()) |
163 | 163 | .def(py::init<>())
|
164 |
| - .def_readwrite("value", (int32_t DerivedPTMFBuffer::*) &DerivedPTMFBuffer::value) |
165 |
| - .def_buffer(&DerivedPTMFBuffer::get_buffer_info); |
166 |
| -}); |
| 164 | + .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value) |
| 165 | + .def_buffer(&DerivedBuffer::get_buffer_info); |
| 166 | + |
| 167 | +} |
0 commit comments