|
7 | 7 |
|
8 | 8 | namespace py = pybind11;
|
9 | 9 |
|
| 10 | +class PyBBufferIO : public BBufferIO{ |
| 11 | + public: |
| 12 | + using BBufferIO::BBufferIO; |
| 13 | + ssize_t ReadAt(off_t pos, void* buffer, size_t size) override { |
| 14 | + PYBIND11_OVERLOAD(ssize_t, BBufferIO, ReadAt, pos, buffer, size); |
| 15 | + } |
| 16 | + ssize_t WriteAt(off_t pos, const void* buffer, size_t size) override { |
| 17 | + PYBIND11_OVERLOAD(ssize_t, BBufferIO, WriteAt, pos, buffer, size); |
| 18 | + } |
| 19 | + off_t Seek(off_t position, uint32 seekMode) override { |
| 20 | + PYBIND11_OVERLOAD(off_t, BBufferIO, Seek, position, seekMode); |
| 21 | + } |
| 22 | + off_t Position() const override { |
| 23 | + PYBIND11_OVERLOAD(off_t, BBufferIO, Position); |
| 24 | + } |
| 25 | + status_t SetSize(off_t size) override { |
| 26 | + PYBIND11_OVERLOAD(status_t, BBufferIO, SetSize, size); |
| 27 | + } |
| 28 | + status_t Flush() override { |
| 29 | + PYBIND11_OVERLOAD(status_t, BBufferIO, Flush); |
| 30 | + } |
| 31 | +}; |
10 | 32 |
|
11 |
| -void define_BufferIO(py::module_& m) |
| 33 | + |
| 34 | +ssize_t ReadAtWrapper(BBufferIO& self, off_t pos, py::buffer buffer) { |
| 35 | + py::buffer_info info = buffer.request(); |
| 36 | + |
| 37 | + if (info.ndim != 1) { |
| 38 | + throw std::runtime_error("Buffer should be one-dimensional"); |
| 39 | + } |
| 40 | + |
| 41 | + if (info.itemsize != 1) { |
| 42 | + throw std::runtime_error("Buffer elements should be of size 1 byte"); |
| 43 | + } |
| 44 | + |
| 45 | + return self.ReadAt(pos, info.ptr, info.size); |
| 46 | +}; |
| 47 | + |
| 48 | +ssize_t WriteAtWrapper(BBufferIO& self, off_t pos, py::buffer buffer) { |
| 49 | + py::buffer_info info = buffer.request(); |
| 50 | + |
| 51 | + if (info.ndim != 1) { |
| 52 | + throw std::runtime_error("Buffer should be one-dimensional"); |
| 53 | + } |
| 54 | + |
| 55 | + if (info.itemsize != 1) { |
| 56 | + throw std::runtime_error("Buffer elements should be of size 1 byte"); |
| 57 | + } |
| 58 | + |
| 59 | + return self.WriteAt(pos, info.ptr, info.size); |
| 60 | +}; |
| 61 | + |
| 62 | +PYBIND11_MODULE(BufferIO, m) |
12 | 63 | {
|
13 |
| -py::class_<BBufferIO, BPositionIO>(m, "BBufferIO") |
14 |
| -.def(py::init<BPositionIO *, size_t, bool>(), "", py::arg("stream"), py::arg("bufferSize")=65536 L, py::arg("ownsStream")=true) |
15 |
| -.def("ReadAt", &BBufferIO::ReadAt, "", py::arg("pos"), py::arg("buffer"), py::arg("size")) |
16 |
| -.def("WriteAt", &BBufferIO::WriteAt, "", py::arg("pos"), py::arg("buffer"), py::arg("size")) |
| 64 | +py::class_<BBufferIO, PyBBufferIO, BPositionIO>(m, "BBufferIO") |
| 65 | +.def(py::init<BPositionIO *, size_t, bool>(), "", py::arg("stream"), py::arg("bufferSize")=65536L, py::arg("ownsStream")=true) |
| 66 | +//.def("ReadAt", &BBufferIO::ReadAt, "", py::arg("pos"), py::arg("buffer"), py::arg("size")) |
| 67 | +.def("ReadAt", &ReadAtWrapper, "", py::arg("pos"), py::arg("buffer")) |
| 68 | +//.def("WriteAt", &BBufferIO::WriteAt, "", py::arg("pos"), py::arg("buffer"), py::arg("size")) |
| 69 | +.def("WriteAt", &WriteAtWrapper, "", py::arg("pos"), py::arg("buffer")) |
17 | 70 | .def("Seek", &BBufferIO::Seek, "", py::arg("position"), py::arg("seekMode"))
|
18 | 71 | .def("Position", &BBufferIO::Position, "")
|
19 | 72 | .def("SetSize", &BBufferIO::SetSize, "", py::arg("size"))
|
|
0 commit comments