provide ctors allowing to specify the non-default buffer size for buffered streams and wxStreamBuffer itself

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56583 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-10-29 16:42:45 +00:00
parent 187c2f81bc
commit f42c1512dd
3 changed files with 157 additions and 48 deletions

View File

@@ -416,7 +416,23 @@ public:
read_write 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); wxStreamBuffer(const wxStreamBuffer& buf);
virtual ~wxStreamBuffer(); virtual ~wxStreamBuffer();
@@ -487,6 +503,9 @@ protected:
// common part of several ctors // common part of several ctors
void Init(); void Init();
// common part of ctors taking wxStreamBase parameter
void InitWithStream(wxStreamBase& stream, BufMode mode);
// init buffer variables to be empty // init buffer variables to be empty
void InitBuffer(); void InitBuffer();
@@ -510,13 +529,8 @@ protected:
m_fixed, m_fixed,
m_flushable; m_flushable;
private:
// Cannot use DECLARE_NO_ASSIGN_CLASS(wxStreamBuffer)
// 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&);
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -526,9 +540,19 @@ private:
class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream class WXDLLIMPEXP_BASE wxBufferedInputStream : public wxFilterInputStream
{ {
public: 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, wxBufferedInputStream(wxInputStream& stream,
wxStreamBuffer *buffer = NULL); 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(); virtual ~wxBufferedInputStream();
char Peek(); char Peek();
@@ -565,9 +589,18 @@ protected:
class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream class WXDLLIMPEXP_BASE wxBufferedOutputStream : public wxFilterOutputStream
{ {
public: 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, wxBufferedOutputStream(wxOutputStream& stream,
wxStreamBuffer *buffer = NULL); 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(); virtual ~wxBufferedOutputStream();
wxOutputStream& Write(const void *buffer, size_t size); wxOutputStream& Write(const void *buffer, size_t size);

View File

@@ -60,12 +60,34 @@ class wxBufferedInputStream : public wxFilterInputStream
{ {
public: public:
/** /**
Constructor. Constructor using the provided buffer or default.
If a non @NULL buffer is given to the stream, it will be deleted by it.
@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, wxBufferedInputStream(wxInputStream& stream,
wxStreamBuffer *buffer = NULL); 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. Destructor.
*/ */
@@ -112,6 +134,26 @@ public:
*/ */
wxStreamBuffer(wxStreamBase& stream, BufMode mode); 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 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). to a stream. mode specifies the type of the buffer (read, write, read_write).
@@ -130,7 +172,7 @@ public:
wxStreamBuffer(BufMode mode); wxStreamBuffer(BufMode mode);
/** /**
Constructor. Copy constructor.
This method initializes the stream buffer with the data of the specified This method initializes the stream buffer with the data of the specified
stream buffer. The new stream buffer has the same attributes, size, position stream buffer. The new stream buffer has the same attributes, size, position
@@ -670,10 +712,34 @@ class wxBufferedOutputStream : public wxFilterOutputStream
{ {
public: 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, wxBufferedOutputStream(wxOutputStream& stream,
wxStreamBuffer *buffer = NULL); 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. Destructor. Calls Sync() and destroys the internal buffer.
*/ */

View File

@@ -75,6 +75,16 @@ void wxStreamBuffer::Init()
m_fixed = true; m_fixed = true;
} }
void wxStreamBuffer::InitWithStream(wxStreamBase& stream, BufMode mode)
{
Init();
m_stream = &stream;
m_mode = mode;
m_flushable = true;
}
wxStreamBuffer::wxStreamBuffer(BufMode mode) wxStreamBuffer::wxStreamBuffer(BufMode mode)
{ {
Init(); Init();
@@ -85,16 +95,6 @@ wxStreamBuffer::wxStreamBuffer(BufMode mode)
m_flushable = false; m_flushable = false;
} }
wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
{
Init();
m_stream = &stream;
m_mode = mode;
m_flushable = true;
}
wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer) wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
{ {
// doing this has big chances to lead to a crash when the source buffer is // 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::wxBufferedInputStream(wxInputStream& s, namespace
wxStreamBuffer *buffer)
: wxFilterInputStream(s)
{ {
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 <typename T>
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() wxBufferedInputStream::~wxBufferedInputStream()
@@ -1320,20 +1332,18 @@ void wxBufferedInputStream::SetInputStreamBuffer(wxStreamBuffer *buffer)
// wxBufferedOutputStream // wxBufferedOutputStream
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& s, wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
wxStreamBuffer *buffer) wxStreamBuffer *buffer)
: wxFilterOutputStream(s) : wxFilterOutputStream(stream)
{ {
if ( buffer ) m_o_streambuf = CreateBufferIfNeeded(*this, buffer);
{ }
m_o_streambuf = buffer;
}
else // create a default one
{
m_o_streambuf = new wxStreamBuffer(*this, wxStreamBuffer::write);
m_o_streambuf->SetBufferIO(1024); wxBufferedOutputStream::wxBufferedOutputStream(wxOutputStream& stream,
} size_t bufsize)
: wxFilterOutputStream(stream)
{
m_o_streambuf = CreateBufferIfNeeded(*this, NULL, bufsize);
} }
wxBufferedOutputStream::~wxBufferedOutputStream() wxBufferedOutputStream::~wxBufferedOutputStream()