]> git.saurik.com Git - wxWidgets.git/commitdiff
provide ctors allowing to specify the non-default buffer size for buffered streams...
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 29 Oct 2008 16:42:45 +0000 (16:42 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 29 Oct 2008 16:42:45 +0000 (16:42 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56583 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/stream.h
interface/wx/stream.h
src/common/stream.cpp

index e4838d263c288b4b8d34012d035370abfc2f789a..3f34c95212d769ef59f8c085a919fba75129b518 100644 (file)
@@ -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);
index 2b96f1a96a2b457388e8878fdf6f75147187e650..f301b70314db01a223497b14675054166258587e 100644 (file)
@@ -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.
     */
index d26e30716347c821c025d9add3cc736838d78006..657e6675d4e7c22358aefd0bb9a792b9925242d0 100644 (file)
@@ -75,24 +75,24 @@ void wxStreamBuffer::Init()
     m_fixed = true;
 }
 
-wxStreamBuffer::wxStreamBuffer(BufMode mode)
+void wxStreamBuffer::InitWithStream(wxStreamBase& stream, BufMode mode)
 {
     Init();
 
-    m_stream = NULL;
+    m_stream = &stream;
     m_mode = mode;
 
-    m_flushable = false;
+    m_flushable = true;
 }
 
-wxStreamBuffer::wxStreamBuffer(wxStreamBase& stream, BufMode mode)
+wxStreamBuffer::wxStreamBuffer(BufMode mode)
 {
     Init();
 
-    m_stream = &stream;
+    m_stream = NULL;
     m_mode = mode;
 
-    m_flushable = true;
+    m_flushable = false;
 }
 
 wxStreamBuffer::wxStreamBuffer(const wxStreamBuffer& buffer)
@@ -1204,21 +1204,33 @@ void wxFilterClassFactory::Remove()
 // wxBufferedInputStream
 // ----------------------------------------------------------------------------
 
-wxBufferedInputStream::wxBufferedInputStream(wxInputStream& s,
+namespace
+{
+
+// 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(s)
+                     : wxFilterInputStream(stream)
 {
-    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 = CreateBufferIfNeeded(*this, buffer);
+}
 
-        m_i_streambuf->SetBufferIO(1024);
-    }
+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()