Update enum_ and bind_vector to new-style init and pickle

Fixes #1046.
This commit is contained in:
Dean Moldovan
2017-08-30 23:40:55 +02:00
parent 4c5404421f
commit 6898679270
3 changed files with 47 additions and 47 deletions

View File

@@ -78,13 +78,15 @@ TEST_SUBMODULE(buffers, m) {
py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
.def(py::init<ssize_t, ssize_t>())
/// Construct from a buffer
.def("__init__", [](Matrix &v, py::buffer b) {
.def(py::init([](py::buffer b) {
py::buffer_info info = b.request();
if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
throw std::runtime_error("Incompatible buffer format!");
new (&v) Matrix(info.shape[0], info.shape[1]);
memcpy(v.data(), info.ptr, sizeof(float) * (size_t) (v.rows() * v.cols()));
})
auto v = new Matrix(info.shape[0], info.shape[1]);
memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols()));
return v;
}))
.def("rows", &Matrix::rows)
.def("cols", &Matrix::cols)