Commit 95f750a8 by Omar Awile Committed by Wenzel Jakob

Add optional buffer size to pythonbuf::d_buffer constructor (#1687)

In some cases the user of pythonbuf needs to allocate the internal
buffer to a specific size e.g. for performance or to enable synchronous
writes to the buffer.
By changing `pythonbuf::d_buffer` to be dynamically allocated we can now
enable these use-cases while still providing the default behavior of
allocating a 1024 byte internal buffer (through a default parameter).
parent 38f408fc
......@@ -25,7 +25,8 @@ class pythonbuf : public std::streambuf {
private:
using traits_type = std::streambuf::traits_type;
char d_buffer[1024];
const size_t buf_size;
std::unique_ptr<char[]> d_buffer;
object pywrite;
object pyflush;
......@@ -51,10 +52,13 @@ private:
}
public:
pythonbuf(object pyostream)
: pywrite(pyostream.attr("write")),
pythonbuf(object pyostream, size_t buffer_size = 1024)
: buf_size(buffer_size),
d_buffer(new char[buf_size]),
pywrite(pyostream.attr("write")),
pyflush(pyostream.attr("flush")) {
setp(d_buffer, d_buffer + sizeof(d_buffer) - 1);
setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
}
/// Sync before destroy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment