test_buffers.cpp revision 12037:d28054ac6ec9
1/*
2    tests/test_buffers.cpp -- supporting Pythons' buffer protocol
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#include "pybind11_tests.h"
11#include "constructor_stats.h"
12
13class Matrix {
14public:
15    Matrix(size_t rows, size_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[rows*cols];
18        memset(m_data, 0, sizeof(float) * 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[m_rows * m_cols];
24        memcpy(m_data, s.m_data, sizeof(float) * 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[m_rows * m_cols];
45        memcpy(m_data, s.m_data, sizeof(float) * 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;
55        }
56        return *this;
57    }
58
59    float operator()(size_t i, size_t j) const {
60        return m_data[i*m_cols + j];
61    }
62
63    float &operator()(size_t i, size_t j) {
64        return m_data[i*m_cols + j];
65    }
66
67    float *data() { return m_data; }
68
69    size_t rows() const { return m_rows; }
70    size_t cols() const { return m_cols; }
71private:
72    size_t m_rows;
73    size_t m_cols;
74    float *m_data;
75};
76
77test_initializer buffers([](py::module &m) {
78    py::class_<Matrix> mtx(m, "Matrix", py::buffer_protocol());
79
80    mtx.def(py::init<size_t, size_t>())
81        /// Construct from a buffer
82        .def("__init__", [](Matrix &v, py::buffer b) {
83            py::buffer_info info = b.request();
84            if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
85                throw std::runtime_error("Incompatible buffer format!");
86            new (&v) Matrix(info.shape[0], info.shape[1]);
87            memcpy(v.data(), info.ptr, sizeof(float) * v.rows() * v.cols());
88        })
89
90       .def("rows", &Matrix::rows)
91       .def("cols", &Matrix::cols)
92
93        /// Bare bones interface
94       .def("__getitem__", [](const Matrix &m, std::pair<size_t, size_t> i) {
95            if (i.first >= m.rows() || i.second >= m.cols())
96                throw py::index_error();
97            return m(i.first, i.second);
98        })
99       .def("__setitem__", [](Matrix &m, std::pair<size_t, size_t> i, float v) {
100            if (i.first >= m.rows() || i.second >= m.cols())
101                throw py::index_error();
102            m(i.first, i.second) = v;
103        })
104       /// Provide buffer access
105       .def_buffer([](Matrix &m) -> py::buffer_info {
106            return py::buffer_info(
107                m.data(),                               /* Pointer to buffer */
108                sizeof(float),                          /* Size of one scalar */
109                py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
110                2,                                      /* Number of dimensions */
111                { m.rows(), m.cols() },                 /* Buffer dimensions */
112                { sizeof(float) * m.rows(),             /* Strides (in bytes) for each index */
113                  sizeof(float) }
114            );
115        })
116        ;
117});
118