CircularBuffer< T > Class Template Reference

Implements a circular buffer. More...

#include <CircularBuffer.h>

List of all members.

Public Member Functions

 CircularBuffer (int nSize)
 Constructor.
 CircularBuffer ()
 Default constructor.
void Create (int nSize)
 Sets the size for this circular buffer object.
void Destroy ()
 Deallocates all of the resources taken up by this object.
int Get (T *pBlock, int n)
 Retrieves the oldest n items in the circular buffer.
bool Get (T &item)
 Retrieves the oldest item in the circular buffer.
int GetMaximumSize () const
 Tells the maximum number of items that this circular buffer can store.
int GetNumItems () const
 Tells the actual number of items stored in this circular buffer.
int GetRoom () const
 Tells the number of items which can still be stored in this circular buffer.
void Put (const T *pBlock, int nSize)
 Stores a set of items in the circular buffer.
void Put (const T &x)
 Stores a single item in the circular buffer.
void Reset ()
 Empties this circular buffer object.
 ~CircularBuffer ()
 Destructor.

Private Attributes

int m_iReadNext
 Index of next read position.
int m_iWriteNext
 Index of current feed position.
int m_nItems
 Number of elements stored in the buffer.
int m_nSize
 Allocated size (in items).
T * m_pBuffer
 Pointer to the internal memory buffer.


Detailed Description

template<typename T>
class CircularBuffer< T >

Implements a circular buffer.

Parameters:
T Data type for the contained items.

Definition at line 43 of file CircularBuffer.h.


Constructor & Destructor Documentation

template<typename T>
CircularBuffer< T >::CircularBuffer  )  [inline]
 

Default constructor.

Constructs an empty circular buffer object.

See also:
CircularBuffer(int)

Definition at line 88 of file CircularBuffer.h.

00089         : m_pBuffer(0)
00090         , m_nSize(0)
00091         , m_nItems(0)
00092         , m_iWriteNext(0)
00093         , m_iReadNext(0) {}

template<typename T>
CircularBuffer< T >::CircularBuffer int  nSize  )  [inline]
 

Constructor.

Constructs a circular buffer object of the specified size.

Parameters:
nSize Initial size (in items) of the circular buffer object.
See also:
CircularBuffer()

Definition at line 108 of file CircularBuffer.h.

References CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00109         : m_nSize(nSize)
00110         , m_nItems(0)
00111         , m_iWriteNext(0)
00112         , m_iReadNext(0)
00113     {
00114         m_pBuffer = (T*)new T[nSize];
00115         memset(m_pBuffer, 0, m_nSize * sizeof(T));
00116     }

template<typename T>
CircularBuffer< T >::~CircularBuffer  )  [inline]
 

Destructor.

Definition at line 122 of file CircularBuffer.h.

References CircularBuffer< T >::m_pBuffer.

00123     {
00124         if(m_pBuffer)
00125             delete[] m_pBuffer;
00126     }


Member Function Documentation

template<typename T>
void CircularBuffer< T >::Create int  nSize  )  [inline]
 

Sets the size for this circular buffer object.

Parameters:
nSize New size (in mumber of items) for this circular buffer object.
Remarks:
All of the previous content of the circular buffer is lost.

Definition at line 139 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_iWriteNext, CircularBuffer< T >::m_nItems, CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00140     {
00141         if(nSize != m_nSize)
00142         {
00143             m_nSize = nSize;
00144             m_nItems = 0;
00145             m_iWriteNext = 0;
00146             m_iReadNext = 0;
00147 
00148             if(m_pBuffer)
00149                 delete[] m_pBuffer;
00150 
00151             m_pBuffer = (T*)new T[m_nSize];
00152             memset(m_pBuffer, 0, m_nSize * sizeof(T));
00153         }
00154         else
00155         {
00156             Reset();
00157         }
00158     }

template<typename T>
void CircularBuffer< T >::Destroy  )  [inline]
 

Deallocates all of the resources taken up by this object.

Remarks:
All of the previous content of the circular buffer is lost.

Definition at line 167 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_iWriteNext, CircularBuffer< T >::m_nItems, CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00168     {
00169         m_nSize = 0;
00170         m_nItems = 0;
00171         m_iWriteNext = 0;
00172         m_iReadNext = 0;
00173 
00174         if(m_pBuffer) {
00175             delete[] m_pBuffer;
00176             m_pBuffer = 0;
00177         }
00178     }

template<typename T>
int CircularBuffer< T >::Get T *  pBlock,
int  n
[inline]
 

Retrieves the oldest n items in the circular buffer.

Parameters:
pBlock Pointer to a buffer which receives the wanted items.
n Number of items to be retrieved.
Returns:
The number of items which were succesfully retrieved. The returned value is always <= n, 0 if the buffer is empty.
See also:
Get(T&)

Definition at line 361 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_nItems, CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00362     {
00363         // Cannot give away more than we have!
00364         n = (n <= m_nItems) ? n : m_nItems;
00365 
00366         if((m_iReadNext + n) <= m_nSize)
00367         {
00368             memcpy(pBlock, m_pBuffer + m_iReadNext, n * sizeof(T));
00369             m_iReadNext = (m_iReadNext + n) % m_nSize;
00370         }
00371         else
00372         {
00373             int nPart1 = m_nSize - m_iReadNext;
00374             int nPart2 = n - nPart1;
00375 
00376             memcpy(pBlock,          m_pBuffer + m_iReadNext,    nPart1 * sizeof(T));    // copy tail
00377             memcpy(pBlock + nPart1, m_pBuffer,                  nPart2 * sizeof(T));    // copy from beginning
00378 
00379             m_iReadNext = nPart2;
00380         }
00381 
00382         m_nItems -= n;
00383 
00384         return n;
00385     }

template<typename T>
bool CircularBuffer< T >::Get T &  item  )  [inline]
 

Retrieves the oldest item in the circular buffer.

Parameters:
item [out] Takes the value of the retrieved item.
Returns:
true if the item was successfully retrieved, false whether the buffer is empty.
See also:
Get(T*, int)

Definition at line 269 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_nItems, CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00270     {
00271         if(m_nItems)
00272         {
00273             item = m_pBuffer[m_iReadNext];
00274             m_iReadNext = ++m_iReadNext % m_nSize;
00275             --m_nItems;
00276             return true;
00277         }
00278         else
00279         {
00280             return false;
00281         }
00282     }

template<typename T>
int CircularBuffer< T >::GetMaximumSize  )  const [inline]
 

Tells the maximum number of items that this circular buffer can store.

Returns:
Maximum number of items that this circular buffer can store.

Definition at line 201 of file CircularBuffer.h.

References CircularBuffer< T >::m_nSize.

00201                                       {
00202         return m_nSize;
00203     }

template<typename T>
int CircularBuffer< T >::GetNumItems  )  const [inline]
 

Tells the actual number of items stored in this circular buffer.

Returns:
The actual number of items stored in this circular buffer.

Definition at line 212 of file CircularBuffer.h.

References CircularBuffer< T >::m_nItems.

00212                                    {
00213         return m_nItems;
00214     }

template<typename T>
int CircularBuffer< T >::GetRoom  )  const [inline]
 

Tells the number of items which can still be stored in this circular buffer.

Returns:
The number of items which can still be stored in this circular buffer.

Definition at line 224 of file CircularBuffer.h.

References CircularBuffer< T >::m_nItems, and CircularBuffer< T >::m_nSize.

00224                                {
00225         return m_nSize - m_nItems;
00226     }

template<typename T>
void CircularBuffer< T >::Put const T *  pBlock,
int  nSize
[inline]
 

Stores a set of items in the circular buffer.

Parameters:
pBlock Pointer to a buffer containing the items to be stored.
nSize Number of items to which pBlock points to.
See also:
Put(const T&)

Definition at line 297 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_iWriteNext, CircularBuffer< T >::m_nItems, CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00298     {
00299         // If it doesn't fit, take last m_nSize samples
00300         if(nSize > m_nSize)
00301         {
00302             pBlock = &pBlock[nSize - m_nSize];  // start later, in order to get last m_nSize samples
00303             nSize = m_nSize;                    // limit the length to the buffer's length
00304         }
00305 
00306         // Wrap around buffer end
00307         if(m_iWriteNext + nSize > m_nSize)
00308         {
00309             bool bReadWasBeforeWrite = m_iReadNext < m_iWriteNext;
00310 
00311             // Fill the end of the buffer
00312             memcpy((m_pBuffer + m_iWriteNext), pBlock, (m_nSize - m_iWriteNext) * sizeof(T));
00313 
00314             // Restart filling from the beginning of the buffer
00315             memcpy(m_pBuffer, (pBlock + m_nSize - m_iWriteNext), (nSize - m_nSize + m_iWriteNext) * sizeof(T));
00316 
00317             // Update feed position
00318             m_iWriteNext = nSize + m_iWriteNext - m_nSize;
00319 
00320             // Check if we have written over read position, and move it
00321             if(bReadWasBeforeWrite && (m_iReadNext < m_iWriteNext))
00322                 m_iReadNext = m_iWriteNext;
00323         }
00324         else
00325         {
00326             bool bReadWasAfterWrite = m_iReadNext > m_iWriteNext;
00327 
00328             // Copy the entire pBlock to the first free location
00329             memcpy((m_pBuffer + m_iWriteNext), pBlock, nSize * sizeof(T));
00330             
00331             // Update feed position
00332             m_iWriteNext = (m_iWriteNext + nSize) % m_nSize;
00333 
00334             // Check if we have written over read position, and move it
00335             if(bReadWasAfterWrite && (m_iReadNext < m_iWriteNext))
00336                 m_iReadNext = m_iWriteNext;
00337         }
00338 
00339         // More items in the buffer
00340         m_nItems = m_nItems + nSize < m_nSize ? m_nItems + nSize : m_nSize;
00341     }

template<typename T>
void CircularBuffer< T >::Put const T &  x  )  [inline]
 

Stores a single item in the circular buffer.

Parameters:
x Item to be stored.
See also:
Put(const T*, int)

Definition at line 239 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_iWriteNext, CircularBuffer< T >::m_nItems, CircularBuffer< T >::m_nSize, and CircularBuffer< T >::m_pBuffer.

00240     {
00241         // Copy the sample to the first free location
00242         m_pBuffer[m_iWriteNext] = x;
00243         ++m_nItems;
00244 
00245         // Update write index
00246         m_iWriteNext = ++m_iWriteNext % m_nSize;
00247 
00248         // If we have filled the buffer, move to next read position
00249         if(m_nItems > m_nSize)
00250         {
00251             m_nItems = m_nSize;
00252             ++m_iReadNext;
00253         }
00254     }

template<typename T>
void CircularBuffer< T >::Reset  )  [inline]
 

Empties this circular buffer object.

Remarks:
The size of this buffer and the allocated memory remain untouched.

Definition at line 187 of file CircularBuffer.h.

References CircularBuffer< T >::m_iReadNext, CircularBuffer< T >::m_iWriteNext, and CircularBuffer< T >::m_nItems.

00188     {
00189         m_nItems = 0;
00190         m_iWriteNext = 0;
00191         m_iReadNext = 0;
00192     }


Member Data Documentation

template<typename T>
int CircularBuffer< T >::m_iReadNext [private]
 

Index of next read position.

Definition at line 74 of file CircularBuffer.h.

Referenced by CircularBuffer< T >::Create(), CircularBuffer< T >::Destroy(), CircularBuffer< T >::Get(), CircularBuffer< T >::Put(), and CircularBuffer< T >::Reset().

template<typename T>
int CircularBuffer< T >::m_iWriteNext [private]
 

Index of current feed position.

Definition at line 68 of file CircularBuffer.h.

Referenced by CircularBuffer< T >::Create(), CircularBuffer< T >::Destroy(), CircularBuffer< T >::Put(), and CircularBuffer< T >::Reset().

template<typename T>
int CircularBuffer< T >::m_nItems [private]
 

Number of elements stored in the buffer.

Definition at line 62 of file CircularBuffer.h.

Referenced by CircularBuffer< T >::Create(), CircularBuffer< T >::Destroy(), CircularBuffer< T >::Get(), CircularBuffer< T >::GetNumItems(), CircularBuffer< T >::GetRoom(), CircularBuffer< T >::Put(), and CircularBuffer< T >::Reset().

template<typename T>
int CircularBuffer< T >::m_nSize [private]
 

Allocated size (in items).

Definition at line 56 of file CircularBuffer.h.

Referenced by CircularBuffer< T >::CircularBuffer(), CircularBuffer< T >::Create(), CircularBuffer< T >::Destroy(), CircularBuffer< T >::Get(), CircularBuffer< T >::GetMaximumSize(), CircularBuffer< T >::GetRoom(), and CircularBuffer< T >::Put().

template<typename T>
T* CircularBuffer< T >::m_pBuffer [private]
 

Pointer to the internal memory buffer.

Definition at line 50 of file CircularBuffer.h.

Referenced by CircularBuffer< T >::CircularBuffer(), CircularBuffer< T >::Create(), CircularBuffer< T >::Destroy(), CircularBuffer< T >::Get(), CircularBuffer< T >::Put(), and CircularBuffer< T >::~CircularBuffer().


Generated on Mon Jan 15 17:13:41 2007 for "ag-works goodies" by  doxygen 1.4.5