ReadWriteBuffer Class Reference

Basic buffer with read/write cursors. More...

#include <ReadWriteBuffer.h>

List of all members.

Public Member Functions

 ReadWriteBuffer ()
 constructor
 ReadWriteBuffer (const ReadWriteBuffer &)
 Create an internal buffer copy of the supplied buffer.
ReadWriteBufferoperator= (const ReadWriteBuffer &)
 Copy the supplied buffer to an internal buffer.
 ~ReadWriteBuffer ()
 destructor
void Deallocate ()
 Deallocate and clear out any existing buffer.
void SetEmpty ()
 Clear the existing buffer to zero lengths for read and write buffers.
void SetExternalBuffer (void *a_pBuf, size_t a_nBufSiz, size_t a_nBufLen=0)
 Set the buffer as an alias to an external buffer.
void SetInternalBuffer (size_t a_nInitialSize, size_t a_nGrowBy=1024)
 Replace the current buffer with an internal growable buffer.
char * GetWriteBuffer (size_t a_nMinBytes=0)
 Get a write pointer into the buffer guaranteeing that the specified number of bytes are available.
void CommitWriteBytes (size_t a_nBytes)
 Commit the specified number of bytes to the buffer.
size_t GetWriteSize () const
 Get the available buffer size for writing.
void WriteBytes (const void *a_pBuf, size_t a_nBufLen)
 Write the supplied data to the write buffer.
const char * GetReadBuffer () const
 Get the read buffer.
void CommitReadBytes (size_t a_nBytes)
 Update the read bytes available.
size_t GetReadSize () const
 Get the available buffer size for reading.
void Compact ()
 Compact the buffer.
bool operator== (const ReadWriteBuffer &) const
 Do a memcmp on the read buffers.

Private Attributes

size_t mBufSiz
 max buf size
size_t mBufLen
 committed writes
size_t mBufIdx
 committed reads
size_t mGrowBy
 external buffer = 0, internal buffer = growby rate
char * mBuf
 buffer


Detailed Description

Basic buffer with read/write cursors.

The buffer may be backed by either an internally allocated and managed growable buffer, or a statically sized external buffer may be used.

Example:

        ReadWriteBuffer buf;

        char * p = buf.GetWriteBuffer(sizeof(data1));
        memcpy(p, data1, sizeof(data1));
        buf.CommitWriteBytes(sizeof(data1));

        p = buf.GetWriteBuffer(sizeof(data2));
        memcpy(p, data2, sizeof(data2));
        buf.CommitWriteBytes(sizeof(data2));

        while (buf.GetReadSize() > 0) {
            fputc(*buf.GetReadBuffer(), fp);
            buf.CommitReadBytes(1);
        }
    

Definition at line 38 of file ReadWriteBuffer.h.


Constructor & Destructor Documentation

ReadWriteBuffer::ReadWriteBuffer ( const ReadWriteBuffer rhs  ) 

Create an internal buffer copy of the supplied buffer.

Only the committed data will be copied. The available bytes may be different to the source buffer.

Definition at line 23 of file ReadWriteBuffer.cpp.

References operator=().


Member Function Documentation

ReadWriteBuffer & ReadWriteBuffer::operator= ( const ReadWriteBuffer rhs  ) 

Copy the supplied buffer to an internal buffer.

Only the committed data will be copied. The available bytes may be different to the source buffer.

Definition at line 36 of file ReadWriteBuffer.cpp.

References Deallocate(), GetWriteBuffer(), mBuf, mBufIdx, mBufLen, mBufSiz, mGrowBy, and SetInternalBuffer().

Referenced by ReadWriteBuffer().

void ReadWriteBuffer::SetExternalBuffer ( void *  a_pBuf,
size_t  a_nBufSiz,
size_t  a_nBufLen = 0 
)

Set the buffer as an alias to an external buffer.

Parameters:
a_pBuf Buffer to use as external store.
a_nBufSiz Maximum size of the buffer for writing.
a_nBufLen Current length of readable data in buffer.
Exceptions:
std::invalid_argument 

Definition at line 78 of file ReadWriteBuffer.cpp.

References Deallocate(), mBuf, mBufIdx, mBufLen, mBufSiz, and mGrowBy.

void ReadWriteBuffer::SetInternalBuffer ( size_t  a_nInitialSize,
size_t  a_nGrowBy = 1024 
)

Replace the current buffer with an internal growable buffer.

Internal buffers will be grown by adding a_nMinBytes rounded up to an even multiples of a_nGrowBy.

Parameters:
a_nInitialSize Initial size to create the internal buffer at
a_nGrowBy Number of bytes to grow the buffer by whenever it runs out of space.

Definition at line 109 of file ReadWriteBuffer.cpp.

References Deallocate(), mBuf, mBufSiz, mGrowBy, and roundup().

Referenced by operator=().

char * ReadWriteBuffer::GetWriteBuffer ( size_t  a_nMinBytes = 0  ) 

Get a write pointer into the buffer guaranteeing that the specified number of bytes are available.

Call CommitWriteBytes() after accessing the buffer to specify the number of bytes actually written.

Parameters:
a_nMinBytes Minimum writable size required for the returned pointer. If 0 then the currently available buffer will be returned without resizing it. The available bytes for writing can be determined via GetWriteSize().
Exceptions:
std::overflow_error when an external buffer is used and a_nMinBytes is not available.
std::bad_alloc when using an internal buffer and there was an out of memory error on resizing.
Returns:
Writable pointer to a buffer with at least the requested number of bytes available for writing. See the function GetWriteSize() to find the actual number of bytes available.

Definition at line 128 of file ReadWriteBuffer.cpp.

References mBuf, mBufLen, mBufSiz, mGrowBy, and roundup().

Referenced by MemCacheClient::HandleGetResponse(), operator=(), and WriteBytes().

void ReadWriteBuffer::CommitWriteBytes ( size_t  a_nBytes  ) 

Commit the specified number of bytes to the buffer.

This function should be called after the buffer returned by GetWriteBuffer() is written to. After calling this function, the specified bytes will then be available for reading from the GetReadBuffer() and GetReadSize() functions.

Parameters:
a_nBytes Number of bytes written
Exceptions:
std::invalid_argument if more bytes were committed then is available

Definition at line 146 of file ReadWriteBuffer.cpp.

References mBufLen, and mBufSiz.

Referenced by MemCacheClient::HandleGetResponse(), and WriteBytes().

size_t ReadWriteBuffer::GetWriteSize (  )  const

Get the available buffer size for writing.

Returns:
number of bytes available in the write buffer returned from GetWriteBuffer()

Definition at line 155 of file ReadWriteBuffer.cpp.

References mBufLen, and mBufSiz.

void ReadWriteBuffer::WriteBytes ( const void *  a_pBuf,
size_t  a_nBufLen 
)

Write the supplied data to the write buffer.

This function is a simple wrapper around calls to GetWriteBuffer, memcpy, CommitWriteBytes.

Parameters:
a_pBuf Data to copy to the buffer
a_nBufLen Length of the data in bytes to copy
Exceptions:
std::overflow_error when an external buffer is used and a_nBufLen is not available.
std::bad_alloc when using an internal buffer and there was an out of memory error on resizing.

Definition at line 161 of file ReadWriteBuffer.cpp.

References CommitWriteBytes(), and GetWriteBuffer().

const char * ReadWriteBuffer::GetReadBuffer (  )  const

Get the read buffer.

See GetReadSize() for the number of bytes available to be read from this pointer.

Returns:
Pointer to the data to be read.

Definition at line 172 of file ReadWriteBuffer.cpp.

References mBuf, and mBufIdx.

Referenced by Compact(), and operator==().

void ReadWriteBuffer::CommitReadBytes ( size_t  a_nBytes  ) 

Update the read bytes available.

Indicate that the specified number of bytes have been read and are not necessary anymore. These bytes are now available for reclaiming by Compact().

Parameters:
a_nBytes Number of bytes read
Exceptions:
std::invalid_argument if more bytes were committed then is available

Definition at line 178 of file ReadWriteBuffer.cpp.

References mBufIdx, and mBufLen.

Referenced by MemCacheClient::Store().

size_t ReadWriteBuffer::GetReadSize (  )  const

Get the available buffer size for reading.

Returns:
number of bytes available for reading

Definition at line 187 of file ReadWriteBuffer.cpp.

References mBufIdx, and mBufLen.

Referenced by Compact(), operator==(), and MemCacheClient::Store().

void ReadWriteBuffer::Compact (  ) 

Compact the buffer.

All remaining unread bytes are moved to the beginning of the buffer leaving the largest size available for writing.

Definition at line 193 of file ReadWriteBuffer.cpp.

References GetReadBuffer(), GetReadSize(), mBuf, mBufIdx, and mBufLen.


The documentation for this class was generated from the following files:

Generated on Tue Jun 3 17:24:05 2008 for MemCacheClient by  doxygen 1.5.6