Skip to content

Commit 0d8f934

Browse files
committed
add BufferIO
1 parent 6b433cc commit 0d8f934

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ local sourceFiles =
215215
Flattenable.cpp
216216
StringList.cpp
217217
DataIO.cpp
218+
BufferIO.cpp
218219
BufferedDataIO.cpp
219220
Url.cpp
220221
UTF8.cpp

bindings/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
from .AffineTransform import *
106106
from .StringList import *
107107
from .DataIO import *
108+
from .BufferIO import *
108109
from .BufferedDataIO import *
109110
from .Url import *
110111
from .UTF8 import *

bindings/support/BufferIO.cpp

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,66 @@
77

88
namespace py = pybind11;
99

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+
};
1032

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)
1263
{
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"))
1770
.def("Seek", &BBufferIO::Seek, "", py::arg("position"), py::arg("seekMode"))
1871
.def("Position", &BBufferIO::Position, "")
1972
.def("SetSize", &BBufferIO::SetSize, "", py::arg("size"))

0 commit comments

Comments
 (0)