libcommonc++  0.7
Buffer< T > Class Template Reference

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

#include <Buffer.h++>

Inheritance diagram for Buffer< T >:
Collaboration diagram for Buffer< T >:

Public Member Functions

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

Protected Attributes

uint_t _limit
 The limit. More...
 
uint_t _pos
 The position. More...
 
T * _data
 A pointer to the raw buffer. More...
 
uint_t _size
 The size of the buffer. More...
 
bool _owner
 Whether this object owns the raw buffer. More...
 

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() [1/2]

Buffer ( uint_t  size)

Construct a new Buffer with the given size.

Parameters
sizeThe capacity of the buffer, in elements.

◆ Buffer() [2/2]

Buffer ( T *  data,
size_t  size,
bool  takeOwnership = true 
)

Construct a new Buffer that will wrap the given array.

Parameters
dataThe array.
sizeThe number of elements in the array.
takeOwnershipA flag indicating whether the Buffer should take ownership of this array and delete[] it in its destructor.

◆ ~Buffer()

virtual ~Buffer ( )
virtual

Destructor.

Member Function Documentation

◆ bump()

uint_t bump ( uint_t  delta)

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

Parameters
deltaThe number of elements.
Returns
The new position.

◆ clear()

virtual void clear ( )
virtual

Clear the buffer.

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

Reimplemented from AbstractBuffer< T >.

◆ fill()

void fill ( const T &  item,
uint_t  count = 0 
)

Fill the buffer with a given item.

Parameters
itemThe item to fill with.
countThe 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.

◆ flip()

void flip ( )

Flip the buffer.

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

◆ get() [1/2]

bool get ( T *  item)

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

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

◆ get() [2/2]

bool get ( T *  items,
uint_t  count 
)

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

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

◆ getBase() [1/2]

T* getBase ( )
inlineinherited

Get a pointer to the base of the buffer.

◆ getBase() [2/2]

const T* getBase ( ) const
inlineinherited

Get a pointer to the base of the buffer.

◆ getLimit()

uint_t getLimit ( ) const
inline

Get the limit.

◆ getPointer() [1/2]

T* getPointer ( )
inline

Get a pointer to the element at the current position.

◆ getPointer() [2/2]

const T* getPointer ( ) const
inline

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

◆ getPosition()

uint_t getPosition ( ) const
inline

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

◆ getRemaining()

uint_t getRemaining ( ) const
inlinevirtual

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 >.

◆ getSize()

uint_t getSize ( ) const
inlineinherited

Get the size of the buffer.

Returns
The size, in elements.

◆ hasRemaining()

bool hasRemaining ( ) const
inlinevirtual

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 >.

◆ operator[]() [1/2]

T& operator[] ( int  index)

Get a reference to the element at the specified index.

Parameters
indexThe index of the element.
Exceptions
OutOfBoundsExceptionIf the index is out of bounds.

◆ operator[]() [2/2]

T operator[] ( int  index) const

Get a copy of the element at the specified index.

Parameters
indexThe index of the element.
Exceptions
OutOfBoundsExceptionIf the index is out of bounds.

◆ peek()

int peek ( const T &  item) const

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

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

◆ put() [1/2]

bool put ( const T &  item)

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

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

◆ put() [2/2]

bool put ( const T *  items,
uint_t  count 
)

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

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

◆ rewind()

void rewind ( )

Rewind the buffer.

Sets the position to 0.

◆ setLimit()

void setLimit ( uint_t  limit)

Set the limit.

◆ setPosition()

void setPosition ( uint_t  pos)

Set the position.

◆ setSize()

virtual void setSize ( uint_t  newSize)
virtualinherited

Resize the buffer.

Parameters
newSizeThe new size, in elements.
Exceptions
UnsupportedOperationExceptionIf this object does not own the underlying byte buffer.

Reimplemented in CircularBuffer< T >, CircularBuffer< C >, and CircularBuffer< byte_t >.

◆ skip()

uint_t skip ( uint_t  delta)

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

Parameters
deltaThe number of elements.
Returns
The new position.

Member Data Documentation

◆ _data

T* _data
protectedinherited

A pointer to the raw buffer.

◆ _limit

uint_t _limit
protected

The limit.

◆ _owner

bool _owner
protectedinherited

Whether this object owns the raw buffer.

◆ _pos

uint_t _pos
protected

The position.

◆ _size

uint_t _size
protectedinherited

The size of the buffer.


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