// Statement
Class CCycleBuffer
{
Public:
BOOL isFull ();
BOOL isEmpty ();
Void Empty ();
Int GetLength ();
CCycleBuffer (int size );
Virtual ~ CCycleBuffer ();
Int Write (char * buf, int count );
Int Read (char * buf, int count );
Private:
BOOL m_bEmpty, m_bFull;
Char * m_pBuf;
Int m_nBufSize;
Int m_nReadPos;
Int m_nWritePos;
};
// Define
CCycleBuffer: CCycleBuffer (int size)
{
M_nBufSize = size;
M_nReadPos = 0;
M_nWritePos = 0;
M_pBuf = new char [m_nBufSize];
M_bEmpty = TRUE;
M_bFull = FALSE;
}
CCycleBuffer ::~ CCycleBuffer ()
{
Delete [] m_pBuf;
}
/*************************************** *********************************/
/* Write data to the buffer and return the actual number of bytes written */
/*************************************** *********************************/
Int CCycleBuffer: Write (char * buf, int count)
{
If (count <= 0) return 0;
M_bEmpty = FALSE;
// The buffer is full and cannot be written.
If (m_bFull)
{
Return 0;
}
Else if (m_nReadPos = m_nWritePos) // when the buffer is empty
{
/* = Memory Model =
(Empty) m_nReadPos (empty)
| ---------------------------------- | ----------------------------------------- |
&
<