Buffer< T > Class Template Reference

A buffer for storing a contiguous sequence of elements. More...

#include <Buffer.h++>

Inheritance diagram for Buffer< T >:

Inheritance graph
[legend]
Collaboration diagram for Buffer< T >:

Collaboration graph
[legend]

List of all members.

Public Member Functions

 Buffer (size_t size)
 Construct a new Buffer with the given size.
virtual ~Buffer () throw ()
 Destructor.
virtual void clear () throw ()
 Clear the buffer.
void flip () throw ()
 Flip the buffer.
void rewind () throw ()
 Rewind the buffer.
void setLimit (size_t limit) throw ()
 Set the limit.
void setPosition (size_t pos) throw ()
 Set the position.
size_t bump (size_t delta) throw ()
 Bump (advance) the position by the given number of elements, or to the limit, whichever occurs first.
size_t skip (size_t delta) throw ()
 Advance the position by the given number of elements, or to the limit, whichever occurs first, filling the "skipped" elements with zeroes.
bool put (const T &item) throw ()
 Copy an item into the buffer at the current position and bump the position by 1.
bool get (T *item) throw ()
 Copy an item from the buffer at the current position and bump the position by 1.
bool put (const T *items, size_t count) throw ()
 Copy an array of items into the buffer starting at the current position, and bump the position by the number of items.
void fill (const T &item, size_t count=0) throw ()
 Fill the buffer with a given item.
bool get (T *items, size_t count) throw ()
 Copy an array of items from the buffer starting at the current position, and bump the position by the number of items.
int peek (const T &item) const throw ()
 Scan forward from the current position for an element equal to the given value.
T * getPointer () throw ()
 Get a pointer to the element at the current position.
const T * getPointer () const throw ()
 Get a pointer to the next element to be read or written.
size_t getLimit () const throw ()
 Get the limit.
size_t getRemaining () const throw ()
 Get the number of elements available to be read or written.
bool hasRemaining () const throw ()
 Test if there are any elements available to be read or written.
size_t getPosition () const throw ()
 Get the position of the next element to be read or written.
T & operator[] (int index) throw (OutOfBoundsException)
 Get a reference to the element at the specified index.
operator[] (int index) const throw (OutOfBoundsException)
 Get a copy of the element at the specified index.
virtual void setSize (size_t newSize)
 Resize the buffer.
size_t getSize () const throw ()
 Get the size of the buffer.
T * getBase () throw ()
 Get a pointer to the base of the buffer.
const T * getBase () const throw ()
 Get a pointer to the base of the buffer.

Protected Attributes

size_t _limit
 The limit.
size_t _pos
 The position.
T * _data
 A pointer to the raw buffer.
size_t _size
 The size of the buffer.


Detailed Description

template<typename T>
class ccxx::Buffer< T >

A buffer for storing a contiguous sequence of elements.

The buffer has a limit, which defines a subrange of the entire buffer that is available for reading or writing, and a position, which is the offset of the next element to be read or written.

Author:
Mark Lindner

Constructor & Destructor Documentation

Buffer ( size_t  size  )  [inline]

Construct a new Buffer with the given size.

Parameters:
size The capacity of the buffer, in elements.

~Buffer (  )  throw () [inline, virtual]

Destructor.


Member Function Documentation

size_t bump ( size_t  delta  )  throw () [inline]

Bump (advance) the position by the given number of elements, or to the limit, whichever occurs first.

Parameters:
delta The number of elements.
Returns:
The new position.

void clear (  )  throw () [inline, virtual]

Clear the buffer.

Sets the position to 0 and the limit to the end of the buffer.

Reimplemented from AbstractBuffer< T >.

void fill ( const T &  item,
size_t  count = 0 
) throw () [inline]

Fill the buffer with a given item.

Parameters:
item The item to fill with.
count The number of elements to fill. If 0 or greater than the number of elements before the limit, the buffer is filled up to the limit.

void flip (  )  throw () [inline]

Flip the buffer.

Sets the limit to the current position, and the position to 0.

bool get ( T *  items,
size_t  count 
) throw () [inline]

Copy an array of items from the buffer starting at the current position, and bump the position by the number of items.

Parameters:
items The items to copy into.
count The number of items.
Returns:
true if the buffer produced the items, false if there was not enough data available.

bool get ( T *  item  )  throw () [inline]

Copy an item from the buffer at the current position and bump the position by 1.

Parameters:
item The item.
Returns:
true if the buffer produced the item, false if there was not enough data available.

const T * getBase (  )  const throw () [inline, inherited]

Get a pointer to the base of the buffer.

T * getBase (  )  throw () [inline, inherited]

Get a pointer to the base of the buffer.

size_t getLimit (  )  const throw () [inline]

Get the limit.

const T* getPointer (  )  const throw () [inline]

Get a pointer to the next element to be read or written.

T* getPointer (  )  throw () [inline]

Get a pointer to the element at the current position.

size_t getPosition (  )  const throw () [inline]

Get the position of the next element to be read or written.

size_t getRemaining (  )  const throw () [inline, virtual]

Get the number of elements available to be read or written.

Returns:
The number of elements between the position and the limit.

Implements AbstractBuffer< T >.

size_t getSize (  )  const throw () [inline, inherited]

Get the size of the buffer.

Returns:
The size, in elements.

bool hasRemaining (  )  const throw () [inline, virtual]

Test if there are any elements available to be read or written.

Returns:
true if there are elements available, false otherwise.

Reimplemented from AbstractBuffer< T >.

T operator[] ( int  index  )  const throw (OutOfBoundsException) [inline]

Get a copy of the element at the specified index.

Parameters:
index The index of the element.
Exceptions:
OutOfBoundsException If the index is out of bounds.

T & operator[] ( int  index  )  throw (OutOfBoundsException) [inline]

Get a reference to the element at the specified index.

Parameters:
index The index of the element.
Exceptions:
OutOfBoundsException If the index is out of bounds.

int peek ( const T &  item  )  const throw () [inline]

Scan forward from the current position for an element equal to the given value.

Parameters:
item The item to scan for.
Returns:
The number of elements between the current position and the matching item, if found; otherwise -1.

bool put ( const T *  items,
size_t  count 
) throw () [inline]

Copy an array of items into the buffer starting at the current position, and bump the position by the number of items.

Parameters:
items The items to place in the buffer.
count The number of items.
Returns:
true if the buffer accepted the items, false if there was not enough room.

bool put ( const T &  item  )  throw () [inline]

Copy an item into the buffer at the current position and bump the position by 1.

Parameters:
item The item.
Returns:
true if the buffer accepted the item, false if there was not enough room.

void rewind (  )  throw () [inline]

Rewind the buffer.

Sets the position to 0.

void setLimit ( size_t  limit  )  throw () [inline]

Set the limit.

void setPosition ( size_t  pos  )  throw () [inline]

Set the position.

virtual void setSize ( size_t  newSize  )  [virtual, inherited]

Resize the buffer.

Parameters:
newSize The new size, in elements.

Reimplemented in CircularBuffer< T >, and JavaBuffer.

size_t skip ( size_t  delta  )  throw () [inline]

Advance the position by the given number of elements, or to the limit, whichever occurs first, filling the "skipped" elements with zeroes.

Parameters:
delta The number of elements.
Returns:
The new position.


Member Data Documentation

T * _data [protected, inherited]

A pointer to the raw buffer.

size_t _limit [protected]

The limit.

size_t _pos [protected]

The position.

size_t _size [protected, inherited]

The size of the buffer.


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

Generated on Sat Apr 17 23:03:07 2010 for libcommonc++ by  doxygen 1.5.9