Savarese Software Research Corporation

sava::spread::detail::ByteBuffer Class Reference

#include <ByteBuffer.h>

Inheritance diagram for sava::spread::detail::ByteBuffer:

Inheritance graph
[legend]

List of all members.


Detailed Description

ByteBuffer provides the ability to write and read bytes to and from a buffer and reuse the buffer for input or output.

The class is not meant to be subclassed by library users, but it needs to be documented publicly so you can use its methods in its subclasses.

ByteBuffer maintains a current offset into the buffer where reads and writes will start. The offset is not updated when the buffer is resized.

Definition at line 44 of file ByteBuffer.h.


Public Types

typedef char value_type
typedef value_typereference
typedef const reference const_reference
typedef value_typepointer
typedef const pointer const_pointer

Public Member Functions

 ByteBuffer (const unsigned int capacity)
 Creates a buffer with the specified capacity and sets its size and offset to zero.
 ByteBuffer (const ByteBuffer &buffer)
 Creates a copy of a ByteBuffer, including its contents, its read/write position, size, and its capacity.
ByteBufferoperator= (const ByteBuffer &buffer)
 Copies the contents of a ByteBuffer, its read/write position, and its size, but does not necessarily copy its capacity.
unsigned int offset () const throw ()
 Returns the current read/write position.
unsigned int seek (const unsigned int offset)
 Sets the read/write position to the specified value if the value is less than or equal to the size of the buffer.
void rewind ()
 Same as seek(0).
void clear ()
 Rewinds the read/write position befor resizing the buffer to zero.
void write (const void *data, const unsigned int size)
 Writes the specified number of bytes from the provided data into the buffer at the current read/write position.
unsigned int read (void *data, unsigned int size)
 Reads the specified number of bytes from the buffer into the provided destination starting from the current read/write position.
unsigned int capacity () const throw ()
 Returns the maximum number of elements that can be written to the buffer without reallocating memory.
reference operator[] (const unsigned int index)
 Returns the element at the specified index.
const_reference operator[] (const unsigned int index) const
 Returns the element at the specified index.
void reserve (const unsigned int capacity)
 Increases the buffer capacity to the specified size.
void resize (const unsigned int size)
 Resizes the buffer to the specified size.
unsigned int size () const throw ()
 Returns the number of elements contained in the buffer.
void add (const char &value)
 Appends a value to the end of the buffer and increments the size by one.

Member Typedef Documentation

typedef char sava::spread::detail::Buffer< char >::value_type [inherited]

Definition at line 58 of file Buffer.h.

typedef value_type& sava::spread::detail::Buffer< char >::reference [inherited]

Definition at line 59 of file Buffer.h.

typedef const reference sava::spread::detail::Buffer< char >::const_reference [inherited]

Definition at line 60 of file Buffer.h.

typedef value_type* sava::spread::detail::Buffer< char >::pointer [inherited]

Definition at line 61 of file Buffer.h.

typedef const pointer sava::spread::detail::Buffer< char >::const_pointer [inherited]

Definition at line 62 of file Buffer.h.


Constructor & Destructor Documentation

sava::spread::detail::ByteBuffer::ByteBuffer ( const unsigned int  capacity  )  [inline, explicit]

Creates a buffer with the specified capacity and sets its size and offset to zero.

Parameters:
capacity The initial capacity of the buffer.

Definition at line 57 of file ByteBuffer.h.

sava::spread::detail::ByteBuffer::ByteBuffer ( const ByteBuffer buffer  )  [inline]

Creates a copy of a ByteBuffer, including its contents, its read/write position, size, and its capacity.

Parameters:
buffer The ByteBuffer to copy.

Definition at line 67 of file ByteBuffer.h.

References sava::spread::detail::Buffer< type >::capacity(), offset(), and sava::spread::detail::Buffer< type >::size().


Member Function Documentation

ByteBuffer& sava::spread::detail::ByteBuffer::operator= ( const ByteBuffer buffer  )  [inline]

Copies the contents of a ByteBuffer, its read/write position, and its size, but does not necessarily copy its capacity.

Parameters:
buffer The ByteBuffer to copy.
Returns:
*this

Definition at line 80 of file ByteBuffer.h.

References offset(), and sava::spread::detail::Buffer< type >::size().

unsigned int sava::spread::detail::ByteBuffer::offset (  )  const throw () [inline]

Returns the current read/write position.

Returns:
The current read/write position.

Definition at line 94 of file ByteBuffer.h.

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

unsigned int sava::spread::detail::ByteBuffer::seek ( const unsigned int  offset  )  [inline]

Sets the read/write position to the specified value if the value is less than or equal to the size of the buffer.

Otherwise, the current offset is left unchanged.

Parameters:
offset The buffer offset of the new read/write position.
Returns:
The new read/write position.

Definition at line 106 of file ByteBuffer.h.

void sava::spread::detail::ByteBuffer::rewind (  )  [inline]

Same as seek(0).

Definition at line 113 of file ByteBuffer.h.

void sava::spread::detail::ByteBuffer::clear (  )  [inline]

Rewinds the read/write position befor resizing the buffer to zero.

Reimplemented from sava::spread::detail::Buffer< char >.

Reimplemented in sava::spread::Message.

Definition at line 120 of file ByteBuffer.h.

void sava::spread::detail::ByteBuffer::write ( const void *  data,
const unsigned int  size 
) [inline]

Writes the specified number of bytes from the provided data into the buffer at the current read/write position.

If the buffer is not large enough to hold the data, it is resized automatically to the current offset plus the size of the data to be written. After writing the data, the read/write position is incremented by the size of the data written.

Parameters:
data A pointer to the data to be written.
size The number of bytes to write.

Definition at line 136 of file ByteBuffer.h.

unsigned int sava::spread::detail::ByteBuffer::read ( void *  data,
unsigned int  size 
) [inline]

Reads the specified number of bytes from the buffer into the provided destination starting from the current read/write position.

If the number of bytes to be read would result in running off the end of the buffer, the read is truncated at the end of the buffer.

Parameters:
data A pointer to the read destination.
size The number of bytes to read.
Returns:
The number of bytes read.

Definition at line 158 of file ByteBuffer.h.

unsigned int sava::spread::detail::Buffer< char >::capacity (  )  const throw () [inline, inherited]

Returns the maximum number of elements that can be written to the buffer without reallocating memory.

Returns:
The buffer capacity.

Definition at line 84 of file Buffer.h.

reference sava::spread::detail::Buffer< char >::operator[] ( const unsigned int  index  )  [inline, inherited]

Returns the element at the specified index.

Parameters:
index The index of the element to retrieve.
Returns:
The element at the specified index.

Definition at line 94 of file Buffer.h.

const_reference sava::spread::detail::Buffer< char >::operator[] ( const unsigned int  index  )  const [inline, inherited]

Returns the element at the specified index.

Parameters:
index The index of the element to retrieve.
Returns:
The element at the specified index.

Definition at line 104 of file Buffer.h.

void sava::spread::detail::Buffer< char >::reserve ( const unsigned int  capacity  )  [inline, inherited]

Increases the buffer capacity to the specified size.

If the new size is less than or equal to the buffer capacity, no action is performed. If the size is greater than the capacity, new memory is reserved, increasing the capacity while preserving the existing buffer contents.

Parameters:
capacity The new buffer capacity.

Definition at line 117 of file Buffer.h.

void sava::spread::detail::Buffer< char >::resize ( const unsigned int  size  )  [inline, inherited]

Resizes the buffer to the specified size.

If the new size is less than or equal to the buffer capacity, the size is simply set to the new value. If the size is greater than the capacity, new memory is reserved—preserving the existing buffer contents—before setting the size to the new value.

Parameters:
size The new buffer size.

Definition at line 136 of file Buffer.h.

unsigned int sava::spread::detail::Buffer< char >::size (  )  const throw () [inline, inherited]

Returns the number of elements contained in the buffer.

Returns:
The number of elements contained in the buffer.

Reimplemented in sava::spread::Message.

Definition at line 145 of file Buffer.h.

void sava::spread::detail::Buffer< char >::add ( const char &  value  )  [inline, inherited]

Appends a value to the end of the buffer and increments the size by one.

If the buffer is at full capacity, its capacity is increased to be able to hold more data. Currently, this is done by doubling the current capacity, but you should not rely on that behavior.

Parameters:
value The value to append to the buffer.

Definition at line 163 of file Buffer.h.


The documentation for this class was generated from the following file:
Savarese Software Research Corporation
Copyright © 2006-2008 Savarese Software Research Corporation. All rights reserved.