code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
//
// +++Date last modified: 05-Jul-1997
//
// CIRCBUF.HPP - Header file for circula
// r buffer C++ functions
//
// by Bob Stout, originally published in
// C/C++ Users Journal
// Use freely
//
#ifndef _CIRCBUF_DEFINED_
#define _CIRCBUF_DEFINED_
#include
#include
typedef enum {ASSIGNED = -1, ERROR = -1, SUCCESS, false = 0, TRUE} Boolean_T;
template
{
private:
size_t next;// Wrap-around pointer to next datum
size_t current;// Wrap-around pointer to current datum
Boolean_Tfull;// true when buffer has filled
Boolean_Tready; // true after init() call...
// ASSIGNED after import() call...
//else FALSE
protected:
size_t size;// Number of elements
T * buf;// Pointer to circular buffer
public:
cbuf_t(){ buf = NULL; next = current = 0;
full = ready = FALSE; };
cbuf_t(size_t len) { init(len); };
~cbuf_t() { if (TRUE == ready) delete buf; };
voidinit(size_t);
Boolean_Tadd(T);
Boolean_Tfetch(T *);
Boolean_Tdata_ready() { return full; };
Boolean_Tdata_avail() { return (next != current); };
voiddata_used() { current = next; };
Boolean_Tclear();
Boolean_Texport(T **, size_t *);
voidimport(T *, size_t);
};
/************************************************************************/
/* */
/* init() Circular buffer initializer. */
/* */
/* Arguments: 1 - Number of elements in the circular buffer.*/
/* */
/************************************************************************/
template
{
if (TRUE == ready)
delete buf;
else ready = TRUE;
size = len;
buf = new T[size];
clear();
}
/************************************************************************/
/* */
/* clear() - Clear a circular buffer. */
/* */
/* Returns: ERROR if buffer has not been initialized, else SUCCESS.*/
/* */
/************************************************************************/
template
{
if (!ready)
return ERROR;
memset(buf, 0, size * sizeof(T));
next = 0;
full = FALSE;
return SUCCESS;
}
/************************************************************************/
/* */
/* add() - Function to add data to a circular buffer. */
/* */
/* Arguments: 1 - Data to add. */
/* */
/* Returns: true if buffer has been filled,*/
/*FALSE if buffer has not filled,*/
/*ERROR if buffer has not been initialized. */
/* */
/************************************************************************/
template
{
if (!ready)
return ERROR;
buf[next] = data;
if (size <= ++next)
{
next = 0;
full = TRUE;
}
if (next == current)
++current;
return full;
}
/************************************************************************/
/* */
/* fetch() - Function to retrieve data from a circular buffer. */
/* */
/* Arguments: 1 - Pointer to data storage. */
/* */
/* Returns: SUCCESS if datum retrieved, else ERROR.*/
/* */
/************************************************************************/
template
{
if (!ready || next == current)
return ERROR;
*data = buf[current];
if (size <= ++current)
current = 0;
return SUCCESS;
}
/************************************************************************/
/* */
/* export() - Publish a circular buffers location and length. */
/* */
/* Arguments: 1 - Storage for buffer pointer. */
/* 2 - Storage for size of buffer. */
/* */
/* Returns: ERROR if buffer has not been initialized, else SUCCESS.*/
/* */
/************************************************************************/
template
{
if (!ready)
return ERROR;
*buffer = buf;
*len= size;
return SUCCESS;
}
/************************************************************************/
/* */
/* import() - Assign an existing circular buffer to a cbuf_t object.*/
/* */
/* Arguments: 1 - Buffer to assign.*/
/* 2 - Size of buffer. */
/* */
/************************************************************************/
template
{
if (TRUE == ready)
delete buf;
buf= buffer;
size = len;
ready = ASSIGNED;
}
#endif // _CIRCBUF_DEFINED_