blob: 5be717730a616f3ccee8d5386415a94c8efdb779 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_buffers.cpp -- supporting Pythons' buffer protocol
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02005
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
Jason Rhinelander391c7542017-07-25 16:47:36 -040013TEST_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));
Wenzel Jakob38bd7112015-07-05 20:05:44 +020021 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020022
Jason Rhinelander391c7542017-07-25 16:47:36 -040023 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 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020028
Jason Rhinelander391c7542017-07-25 16:47:36 -040029 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 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020035
Jason Rhinelander391c7542017-07-25 16:47:36 -040036 ~Matrix() {
37 print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
38 delete[] m_data;
39 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020040
Jason Rhinelander391c7542017-07-25 16:47:36 -040041 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 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020050
Jason Rhinelander391c7542017-07-25 16:47:36 -040051 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 }
Dean Moldovan427e4af2017-05-28 16:35:02 +020060
Jason Rhinelander391c7542017-07-25 16:47:36 -040061 float operator()(ssize_t i, ssize_t j) const {
62 return m_data[(size_t) (i*m_cols + j)];
63 }
Bruce Merryfe0cf8b2017-05-17 10:52:33 +020064
Jason Rhinelander391c7542017-07-25 16:47:36 -040065 float &operator()(ssize_t i, ssize_t j) {
66 return m_data[(size_t) (i*m_cols + j)];
67 }
Bruce Merryfe0cf8b2017-05-17 10:52:33 +020068
Jason Rhinelander391c7542017-07-25 16:47:36 -040069 float *data() { return m_data; }
Bruce Merryfe0cf8b2017-05-17 10:52:33 +020070
Jason Rhinelander391c7542017-07-25 16:47:36 -040071 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>())
Wenzel Jakob38bd7112015-07-05 20:05:44 +020080 /// Construct from a buffer
Dean Moldovan68986792017-08-30 23:40:55 +020081 .def(py::init([](py::buffer b) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020082 py::buffer_info info = b.request();
Ivan Smirnov5e71e172016-06-26 12:42:34 +010083 if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020084 throw std::runtime_error("Incompatible buffer format!");
Dean Moldovan68986792017-08-30 23:40:55 +020085
86 auto v = new Matrix(info.shape[0], info.shape[1]);
87 memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols()));
88 return v;
89 }))
Wenzel Jakob38bd7112015-07-05 20:05:44 +020090
91 .def("rows", &Matrix::rows)
92 .def("cols", &Matrix::cols)
93
94 /// Bare bones interface
Cris Luengo30d43c42017-04-14 14:33:44 -060095 .def("__getitem__", [](const Matrix &m, std::pair<ssize_t, ssize_t> i) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020096 if (i.first >= m.rows() || i.second >= m.cols())
97 throw py::index_error();
98 return m(i.first, i.second);
99 })
Cris Luengo30d43c42017-04-14 14:33:44 -0600100 .def("__setitem__", [](Matrix &m, std::pair<ssize_t, ssize_t> i, float v) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200101 if (i.first >= m.rows() || i.second >= m.cols())
102 throw py::index_error();
103 m(i.first, i.second) = v;
104 })
105 /// Provide buffer access
106 .def_buffer([](Matrix &m) -> py::buffer_info {
107 return py::buffer_info(
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100108 m.data(), /* Pointer to buffer */
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100109 { m.rows(), m.cols() }, /* Buffer dimensions */
Cris Luengo30d43c42017-04-14 14:33:44 -0600110 { sizeof(float) * size_t(m.rows()), /* Strides (in bytes) for each index */
111 sizeof(float) }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200112 );
Jason Rhinelander3f589372016-08-07 13:05:26 -0400113 })
114 ;
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200115
Jason Rhinelander391c7542017-07-25 16:47:36 -0400116
117 // test_inherited_protocol
118 class SquareMatrix : public Matrix {
119 public:
120 SquareMatrix(ssize_t n) : Matrix(n, n) { }
121 };
Dean Moldovan427e4af2017-05-28 16:35:02 +0200122 // Derived classes inherit the buffer protocol and the buffer access function
123 py::class_<SquareMatrix, Matrix>(m, "SquareMatrix")
124 .def(py::init<ssize_t>());
125
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200126
Jason Rhinelander391c7542017-07-25 16:47:36 -0400127 // test_pointer_to_member_fn
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200128 // Tests that passing a pointer to member to the base class works in
129 // the derived class.
Jason Rhinelander391c7542017-07-25 16:47:36 -0400130 struct Buffer {
131 int32_t value = 0;
132
133 py::buffer_info get_buffer_info() {
134 return py::buffer_info(&value, sizeof(value),
135 py::format_descriptor<int32_t>::format(), 1);
136 }
137 };
138 py::class_<Buffer>(m, "Buffer", py::buffer_protocol())
Bruce Merryfe0cf8b2017-05-17 10:52:33 +0200139 .def(py::init<>())
Jason Rhinelander391c7542017-07-25 16:47:36 -0400140 .def_readwrite("value", &Buffer::value)
141 .def_buffer(&Buffer::get_buffer_info);
142
143
144 class ConstBuffer {
145 std::unique_ptr<int32_t> value;
146
147 public:
148 int32_t get_value() const { return *value; }
149 void set_value(int32_t v) { *value = v; }
150
151 py::buffer_info get_buffer_info() const {
152 return py::buffer_info(value.get(), sizeof(*value),
153 py::format_descriptor<int32_t>::format(), 1);
154 }
155
156 ConstBuffer() : value(new int32_t{0}) { };
157 };
158 py::class_<ConstBuffer>(m, "ConstBuffer", py::buffer_protocol())
159 .def(py::init<>())
160 .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value)
161 .def_buffer(&ConstBuffer::get_buffer_info);
162
163 struct DerivedBuffer : public Buffer { };
164 py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol())
165 .def(py::init<>())
166 .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value)
167 .def_buffer(&DerivedBuffer::get_buffer_info);
168
169}