diff --git a/include/wx/stream.h b/include/wx/stream.h index e4838d263c..3f34c95212 100644 --- a/include/wx/stream.h +++ b/include/wx/stream.h @@ -416,7 +416,23 @@ public: read_write }; - wxStreamBuffer(wxStreamBase& stream, BufMode mode); + wxStreamBuffer(wxStreamBase& stream, BufMode mode) + { + InitWithStream(stream, mode); + } + + wxStreamBuffer(wxInputStream& stream, size_t bufsize) + { + InitWithStream(stream, read); + SetBufferIO(bufsize); + } + + wxStreamBuffer(wxOutputStream& stream, size_t bufsize) + { + InitWithStream(stream, write); + SetBufferIO(bufsize); + } + wxStreamBuffer(const wxStreamBuffer& buf); virtual ~wxStreamBuffer(); @@ -487,6 +503,9 @@ protected: // common part of several ctors void Init(); + // common part of ctors taking wxStreamBase parameter + void InitWithStream(wxStreamBase& stream, BufMode mode); + // init buffer variables to be empty void InitBuffer(); @@ -510,13 +529,8 @@ protected: m_fixed, m_flushable; -private: -// Cannot use -// DECLARE_NO_COPY_CLASS(wxStreamBuffer) -// because copy constructor is explicitly declared above; -// but no copy assignment operator is defined, so declare -// it private to prevent the compiler from defining it: - wxStreamBuffer& operator=(const wxStreamBuffer&); + + DECLARE_NO_ASSIGN_CLASS(wxStreamBuffer) }; // --------------------------------------------------------------------------- @@ -526,9 +540,19 @@ private: class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream { public: - // if a non NULL buffer is given to the stream, it will be deleted by it + // create a buffered stream on top of the specified low-level stream + // + // if a non NULL buffer is given to the stream, it will be deleted by it, + // otherwise a default 1KB buffer will be used wxBufferedInputStream(wxInputStream& stream, wxStreamBuffer *buffer = NULL); + + // ctor allowing to specify the buffer size, it's just a more convenient + // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize) + // and using the ctor above + wxBufferedInputStream(wxInputStream& stream, size_t bufsize); + + virtual ~wxBufferedInputStream(); char Peek(); @@ -565,9 +589,18 @@ protected: class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream { public: - // if a non NULL buffer is given to the stream, it will be deleted by it + // create a buffered stream on top of the specified low-level stream + // + // if a non NULL buffer is given to the stream, it will be deleted by it, + // otherwise a default 1KB buffer will be used wxBufferedOutputStream(wxOutputStream& stream, wxStreamBuffer *buffer = NULL); + + // ctor allowing to specify the buffer size, it's just a more convenient + // alternative to creating wxStreamBuffer, calling its SetBufferIO(bufsize) + // and using the ctor above + wxBufferedOutputStream(wxOutputStream& stream, size_t bufsize); + virtual ~wxBufferedOutputStream(); wxOutputStream& Write(const void *buffer, size_t size); diff --git a/interface/wx/stream.h b/interface/wx/stream.h index 2b96f1a96a..f301b70314 100644 --- a/interface/wx/stream.h +++ b/interface/wx/stream.h @@ -60,12 +60,34 @@ class wxBufferedInputStream : public wxFilterInputStream { public: /** - Constructor. - If a non @NULL buffer is given to the stream, it will be deleted by it. + Constructor using the provided buffer or default. + + @param stream + The associated low-level stream. + @param buffer + The buffer to use if non-@NULL. Notice that the ownership of this + buffer is taken by the stream, i.e. it will delete it. If this + parameter is @NULL a default 1KB buffer is used. */ wxBufferedInputStream(wxInputStream& stream, wxStreamBuffer *buffer = NULL); + /** + Constructor allowing to specify the size of the buffer. + + This is just a more convenient alternative to creating a wxStreamBuffer + of the given size and using the other overloaded constructor of this + class. + + @param stream + The associated low-level stream. + @param bufsize + The size of the buffer, in bytes. + + @since 2.9.0 + */ + wxBufferedInputStream(wxInputStream& stream, size_t bufsize); + /** Destructor. */ @@ -112,6 +134,26 @@ public: */ wxStreamBuffer(wxStreamBase& stream, BufMode mode); + /** + Constructor for an input buffer of the specified size. + + Using it is equivalent to using the constructor above with read mode + and calling SetBufferIO() but is more convenient. + + @since 2.9.0 + */ + wxStreamBuffer(wxInputStream& stream, size_t bufsize); + + /** + Constructor for an output buffer of the specified size. + + Using it is equivalent to using the constructor above with write mode + and calling SetBufferIO() but is more convenient. + + @since 2.9.0 + */ + wxStreamBuffer(wxOutputStream& stream, size_t bufsize); + /** Constructor; creates a new empty stream buffer which won't flush any data to a stream. mode specifies the type of the buffer (read, write, read_write). @@ -130,7 +172,7 @@ public: wxStreamBuffer(BufMode mode); /** - Constructor. + Copy constructor. This method initializes the stream buffer with the data of the specified stream buffer. The new stream buffer has the same attributes, size, position @@ -670,10 +712,34 @@ class wxBufferedOutputStream : public wxFilterOutputStream { public: /** - @todo WRITE DESCRIPTION + Constructor using the provided buffer or default. + + @param stream + The associated low-level stream. + @param buffer + The buffer to use if non-@NULL. Notice that the ownership of this + buffer is taken by the stream, i.e. it will delete it. If this + parameter is @NULL a default 1KB buffer is used. */ wxBufferedOutputStream(wxOutputStream& stream, wxStreamBuffer *buffer = NULL); + + /** + Constructor allowing to specify the size of the buffer. + + This is just a more convenient alternative to creating a wxStreamBuffer + of the given size and using the other overloaded constructor of this + class. + + @param stream + The associated low-level stream. + @param bufsize + The size of the buffer, in bytes. + + @since 2.9.0 + */ + wxBufferedOutputStream(wxOutputStream& stream, size_t bufsize); + /** Destructor. Calls Sync() and destroys the internal buffer. */ diff --git a/src/common/stream.cpp b/src/common/stream.cpp index d26e307163..657e6675d4 100644 --- a/src/common/stream.cpp +++ b/src/common/stream.cpp @@ -75,6 +75,16 @@ void wxStreamBuffer::Init() m_fixed = true; } +void wxStreamBuffer::InitWithStream(wxStreamBase& stream, BufMode mode) +{ + Init(); + + m_stream = &stream; + m_mode = mode; + + m_flushable = true; +} + wxStreamBuffer::wxStreamBuffer(BufMode mode) { Init(); @@ -85,16 +95,6 @@ wxStreamBuffer::wxStreamBuffer(BufMode mode) m_flushable = false; } -wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode) -{ - Init(); - - m_stream = &stream; - m_mode = mode; - - m_flushable = true; -} - wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer) { // doing this has big chances to lead to a crash when the source buffer is @@ -1204,21 +1204,33 @@ void wxFilterClassFactory::Remove() // wxBufferedInputStream // ---------------------------------------------------------------------------- -wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s, - wxStreamBuffer *buffer) - : wxFilterInputStream(s) +namespace { - if ( buffer ) - { - // use the buffer provided by the user - m_i_streambuf = buffer; - } - else // create a default buffer - { - m_i_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::read); - m_i_streambuf->SetBufferIO(1024); - } +// helper function used for initializing the buffer used by +// wxBufferedInput/OutputStream: it simply returns the provided buffer if it's +// not NULL or creates a buffer of the given size otherwise +template +wxStreamBuffer * +CreateBufferIfNeeded(T& stream, wxStreamBuffer *buffer, size_t bufsize = 1024) +{ + return buffer ? buffer : new wxStreamBuffer(stream, bufsize); +} + +} // anonymous namespace + +wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream, + wxStreamBuffer *buffer) + : wxFilterInputStream(stream) +{ + m_i_streambuf = CreateBufferIfNeeded(*this, buffer); +} + +wxBufferedInputStream::wxBufferedInputStream(wxInputStream& stream, + size_t bufsize) + : wxFilterInputStream(stream) +{ + m_i_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize); } wxBufferedInputStream::~wxBufferedInputStream() @@ -1320,20 +1332,18 @@ void wxBufferedInputStream::SetInputStreamBuffer(wxStreamBuffer *buffer) // wxBufferedOutputStream // ---------------------------------------------------------------------------- -wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s, +wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream, wxStreamBuffer *buffer) - : wxFilterOutputStream(s) + : wxFilterOutputStream(stream) { - if ( buffer ) - { - m_o_streambuf = buffer; - } - else // create a default one - { - m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write); + m_o_streambuf = CreateBufferIfNeeded(*this, buffer); +} - m_o_streambuf->SetBufferIO(1024); - } +wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream, + size_t bufsize) + : wxFilterOutputStream(stream) +{ + m_o_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize); } wxBufferedOutputStream::~wxBufferedOutputStream()