- // 声明
- 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;
- };
- // 定义
- 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;
- }
- /************************************************************************/
- /* 向缓冲区写入数据,返回实际写入的字节数 */
- /************************************************************************/
- int CCycleBuffer::Write(char* buf, int count)
- {
- if(count <= 0) return 0;
- m_bEmpty = FALSE;
- // 缓冲区已满,不能继续写入
- if(m_bFull)
- {
- return 0;
- }
- else if(m_nReadPos == m_nWritePos) // 缓冲区为空时
- {
- /* == 内存模型 ==
- (empty) m_nReadPos (empty)
- |----------------------------------|-----------------------------------------|
- m_nWritePos m_nBufSize
- */
- int leftcount = m_nBufSize - m_nWritePos;
- if(leftcount > count)
- {
- CopyMemory(m_pBuf + m_nWritePos, buf, count);
- m_nWritePos += count;
- m_bFull = (m_nWritePos == m_nReadPos);
- return count;
- }
- else
- {
- CopyMemory(m_pBuf + m_nWritePos, buf, leftcount);
- m_nWritePos = (m_nReadPos > count - leftcount) ? count - leftcount : m_nWritePos;
- CopyMemory(m_pBuf, buf + leftcount, m_nWritePos);
- m_bFull = (m_nWritePos == m_nReadPos);
- return leftcount + m_nWritePos;
- }
- }
- else if(m_nReadPos < m_nWritePos) // 有剩余空间可写入
- {
- /* == 内存模型 ==
- (empty) (data) (empty)
- |-------------------|----------------------------|---------------------------|
- m_nReadPos m_nWritePos (leftcount)
- */
- // 剩余缓冲区大小(从写入位置到缓冲区尾)
- int leftcount = m_nBufSize - m_nWritePos;
- if(leftcount > count) // 有足够的剩余空间存放
- {
- CopyMemory(m_pBuf + m_nWritePos, buf, count);
- m_nWritePos += count;
- m_bFull = (m_nReadPos == m_nWritePos);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return count;
- }
- else // 剩余空间不足
- {
- // 先填充满剩余空间,再回头找空间存放
- CopyMemory(m_pBuf + m_nWritePos, buf, leftcount);
- int m_nWritePos = (m_nReadPos >= count - leftcount) ? count - leftcount : m_nReadPos;
- CopyMemory(m_pBuf, buf + leftcount, m_nWritePos);
- m_bFull = (m_nReadPos == m_nWritePos);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return leftcount + m_nWritePos;
- }
- }
- else
- {
- /* == 内存模型 ==
- (unread) (read) (unread)
- |-------------------|----------------------------|---------------------------|
- m_nWritePos (leftcount) m_nReadPos
- */
- int leftcount = m_nReadPos - m_nWritePos;
- if(leftcount > count)
- {
- // 有足够的剩余空间存放
- CopyMemory(m_pBuf + m_nWritePos, buf, count);
- m_nWritePos += count;
- m_bFull = (m_nReadPos == m_nWritePos);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return count;
- }
- else
- {
- // 剩余空间不足时要丢弃后面的数据
- CopyMemory(m_pBuf + m_nWritePos, buf, leftcount);
- m_nWritePos += leftcount;
- m_bFull = (m_nReadPos == m_nWritePos);
- ASSERT(m_bFull);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return leftcount;
- }
- }
- }
- /************************************************************************/
- /* 从缓冲区读数据,返回实际读取的字节数 */
- /************************************************************************/
- int CCycleBuffer::Read(char* buf, int count)
- {
- if(count <= 0) return 0;
- m_bFull = FALSE;
- if(m_bEmpty) // 缓冲区空,不能继续读取数据
- {
- return 0;
- }
- else if(m_nReadPos == m_nWritePos) // 缓冲区满时
- {
- /* == 内存模型 ==
- (data) m_nReadPos (data)
- |--------------------------------|--------------------------------------------|
- m_nWritePos m_nBufSize
- */
- int leftcount = m_nBufSize - m_nReadPos;
- if(leftcount > count)
- {
- CopyMemory(buf, m_pBuf + m_nReadPos, count);
- m_nReadPos += count;
- m_bEmpty = (m_nReadPos == m_nWritePos);
- return count;
- }
- else
- {
- CopyMemory(buf, m_pBuf + m_nReadPos, leftcount);
- m_nReadPos = (m_nWritePos > count - leftcount) ? count - leftcount : m_nWritePos;
- CopyMemory(buf + leftcount, m_pBuf, m_nReadPos);
- m_bEmpty = (m_nReadPos == m_nWritePos);
- return leftcount + m_nReadPos;
- }
- }
- else if(m_nReadPos < m_nWritePos) // 写指针在前(未读数据是连接的)
- {
- /* == 内存模型 ==
- (read) (unread) (read)
- |-------------------|----------------------------|---------------------------|
- m_nReadPos m_nWritePos m_nBufSize
- */
- int leftcount = m_nWritePos - m_nReadPos;
- int c = (leftcount > count) ? count : leftcount;
- CopyMemory(buf, m_pBuf + m_nReadPos, c);
- m_nReadPos += c;
- m_bEmpty = (m_nReadPos == m_nWritePos);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return c;
- }
- else // 读指针在前(未读数据可能是不连接的)
- {
- /* == 内存模型 ==
- (unread) (read) (unread)
- |-------------------|----------------------------|---------------------------|
- m_nWritePos m_nReadPos m_nBufSize
- */
- int leftcount = m_nBufSize - m_nReadPos;
- if(leftcount > count) // 未读缓冲区够大,直接读取数据
- {
- CopyMemory(buf, m_pBuf + m_nReadPos, count);
- m_nReadPos += count;
- m_bEmpty = (m_nReadPos == m_nWritePos);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return count;
- }
- else // 未读缓冲区不足,需回到缓冲区头开始读
- {
- CopyMemory(buf, m_pBuf + m_nReadPos, leftcount);
- m_nReadPos = (m_nWritePos >= count - leftcount) ? count - leftcount : m_nWritePos;
- CopyMemory(buf, m_pBuf, m_nReadPos);
- m_bEmpty = (m_nReadPos == m_nWritePos);
- ASSERT(m_nReadPos <= m_nBufSize);
- ASSERT(m_nWritePos <= m_nBufSize);
- return leftcount + m_nReadPos;
- }
- }
- }
- /************************************************************************/
- /* 获取缓冲区有效数据长度 */
- /************************************************************************/
- int CCycleBuffer::GetLength()
- {
- if(m_bEmpty)
- {
- return 0;
- }
- else if(m_bFull)
- {
- return m_nBufSize;
- }
- else if(m_nReadPos < m_nWritePos)
- {
- return m_nWritePos - m_nReadPos;
- }
- else
- {
- return m_nBufSize - m_nReadPos + m_nWritePos;
- }
- }
- void CCycleBuffer::Empty()
- {
- m_nReadPos = 0;
- m_nWritePos = 0;
- m_bEmpty = TRUE;
- m_bFull = FALSE;
- }
- BOOL CCycleBuffer::isEmpty()
- {
- return m_bEmpty;
- }
- BOOL CCycleBuffer::isFull()
- {
- return m_bFull;
- }
上面这个版本是windows C++版,下面的是标准C++版
- #ifndef __test__CCycleBuffer__
- #define __test__CCycleBuffer__
- #include <iostream>
- #include <assert.h>
- 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;
- public:
- int GetReadPos() { return m_nReadPos; }
- int GetWritePos() { return m_nWritePos; }
- };
- #endif /* defined(__test__CCycleBuffer__) */
- #include "CCycleBuffer.h"
- // 定义
- 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;
- }
- /************************************************************************/
- /* 向缓冲区写入数据,返回实际写入的字节数 */
- /************************************************************************/
- int CCycleBuffer::Write(char* buf, int count)
- {
- if(count <= 0) return 0;
- m_bEmpty = false;
- // 缓冲区已满,不能继续写入
- if(m_bFull)
- {
- return 0;
- }
- else if(m_nReadPos == m_nWritePos) // 缓冲区为空时
- {
- /* == 内存模型 ==
- (empty) m_nReadPos (empty)
- |----------------------------------|-----------------------------------------|
- m_nWritePos m_nBufSize
- */
- int leftcount = m_nBufSize - m_nWritePos;
- if(leftcount > count)
- {
- memcpy(m_pBuf + m_nWritePos, buf, count);
- m_nWritePos += count;
- m_bFull = (m_nWritePos == m_nReadPos);
- return count;
- }
- else
- {
- memcpy(m_pBuf + m_nWritePos, buf, leftcount);
- m_nWritePos = (m_nReadPos > count - leftcount) ? count - leftcount : m_nWritePos;
- memcpy(m_pBuf, buf + leftcount, m_nWritePos);
- m_bFull = (m_nWritePos == m_nReadPos);
- return leftcount + m_nWritePos;
- }
- }
- else if(m_nReadPos < m_nWritePos) // 有剩余空间可写入
- {
- /* == 内存模型 ==
- (empty) (data) (empty)
- |-------------------|----------------------------|---------------------------|
- m_nReadPos m_nWritePos (leftcount)
- */
- // 剩余缓冲区大小(从写入位置到缓冲区尾)
- int leftcount = m_nBufSize - m_nWritePos;
- if(leftcount > count) // 有足够的剩余空间存放
- {
- memcpy(m_pBuf + m_nWritePos, buf, count);
- m_nWritePos += count;
- m_bFull = (m_nReadPos == m_nWritePos);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return count;
- }
- else // 剩余空间不足
- {
- // 先填充满剩余空间,再回头找空间存放
- memcpy(m_pBuf + m_nWritePos, buf, leftcount);
- m_nWritePos = (m_nReadPos >= count - leftcount) ? count - leftcount : m_nReadPos;
- memcpy(m_pBuf, buf + leftcount, m_nWritePos);
- m_bFull = (m_nReadPos == m_nWritePos);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return leftcount + m_nWritePos;
- }
- }
- else
- {
- /* == 内存模型 ==
- (unread) (read) (unread)
- |-------------------|----------------------------|---------------------------|
- m_nWritePos (leftcount) m_nReadPos
- */
- int leftcount = m_nReadPos - m_nWritePos;
- if(leftcount > count)
- {
- // 有足够的剩余空间存放
- memcpy(m_pBuf + m_nWritePos, buf, count);
- m_nWritePos += count;
- m_bFull = (m_nReadPos == m_nWritePos);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return count;
- }
- else
- {
- // 剩余空间不足时要丢弃后面的数据
- memcpy(m_pBuf + m_nWritePos, buf, leftcount);
- m_nWritePos += leftcount;
- m_bFull = (m_nReadPos == m_nWritePos);
- assert(m_bFull);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return leftcount;
- }
- }
- }
- /************************************************************************/
- /* 从缓冲区读数据,返回实际读取的字节数 */
- /************************************************************************/
- int CCycleBuffer::Read(char* buf, int count)
- {
- if(count <= 0) return 0;
- m_bFull = false;
- if(m_bEmpty) // 缓冲区空,不能继续读取数据
- {
- return 0;
- }
- else if(m_nReadPos == m_nWritePos) // 缓冲区满时
- {
- /* == 内存模型 ==
- (data) m_nReadPos (data)
- |--------------------------------|--------------------------------------------|
- m_nWritePos m_nBufSize
- */
- int leftcount = m_nBufSize - m_nReadPos;
- if(leftcount > count)
- {
- memcpy(buf, m_pBuf + m_nReadPos, count);
- m_nReadPos += count;
- m_bEmpty = (m_nReadPos == m_nWritePos);
- return count;
- }
- else
- {
- memcpy(buf, m_pBuf + m_nReadPos, leftcount);
- m_nReadPos = (m_nWritePos > count - leftcount) ? count - leftcount : m_nWritePos;
- memcpy(buf + leftcount, m_pBuf, m_nReadPos);
- m_bEmpty = (m_nReadPos == m_nWritePos);
- return leftcount + m_nReadPos;
- }
- }
- else if(m_nReadPos < m_nWritePos) // 写指针在前(未读数据是连接的)
- {
- /* == 内存模型 ==
- (read) (unread) (read)
- |-------------------|----------------------------|---------------------------|
- m_nReadPos m_nWritePos m_nBufSize
- */
- int leftcount = m_nWritePos - m_nReadPos;
- int c = (leftcount > count) ? count : leftcount;
- memcpy(buf, m_pBuf + m_nReadPos, c);
- m_nReadPos += c;
- m_bEmpty = (m_nReadPos == m_nWritePos);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return c;
- }
- else // 读指针在前(未读数据可能是不连接的)
- {
- /* == 内存模型 ==
- (unread) (read) (unread)
- |-------------------|----------------------------|---------------------------|
- m_nWritePos m_nReadPos m_nBufSize
- */
- int leftcount = m_nBufSize - m_nReadPos;
- if(leftcount > count) // 未读缓冲区够大,直接读取数据
- {
- memcpy(buf, m_pBuf + m_nReadPos, count);
- m_nReadPos += count;
- m_bEmpty = (m_nReadPos == m_nWritePos);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return count;
- }
- else // 未读缓冲区不足,需回到缓冲区头开始读
- {
- memcpy(buf, m_pBuf + m_nReadPos, leftcount);
- m_nReadPos = (m_nWritePos >= count - leftcount) ? count - leftcount : m_nWritePos;
- memcpy(buf, m_pBuf, m_nReadPos);
- m_bEmpty = (m_nReadPos == m_nWritePos);
- assert(m_nReadPos <= m_nBufSize);
- assert(m_nWritePos <= m_nBufSize);
- return leftcount + m_nReadPos;
- }
- }
- }
- /************************************************************************/
- /* 获取缓冲区有效数据长度 */
- /************************************************************************/
- int CCycleBuffer::GetLength()
- {
- if(m_bEmpty)
- {
- return 0;
- }
- else if(m_bFull)
- {
- return m_nBufSize;
- }
- else if(m_nReadPos < m_nWritePos)
- {
- return m_nWritePos - m_nReadPos;
- }
- else
- {
- return m_nBufSize - m_nReadPos + m_nWritePos;
- }
- }
- void CCycleBuffer::Empty()
- {
- m_nReadPos = 0;
- m_nWritePos = 0;
- m_bEmpty = true;
- m_bFull = false;
- }
- bool CCycleBuffer::isEmpty()
- {
- return m_bEmpty;
- }
- bool CCycleBuffer::isFull()
- {
- return m_bFull;
- }
增加一个Objective_c的测试用例
- int* dataint = new int[10]();
- CCycleBuffer buffer(100);
- char* datachar = (char *)dataint;
- int* resint = new int[10]();
- char* reschar = (char*)resint;
- int num = 0;
- while(1)
- {
- int a = 0;
- if(buffer.GetLength()<60)
- {
- dataint[0] = num;
- //qDebug("write 之前 m_nReadPos:%d,m_nWritePos:%d,Length:%d",buffer.getStart(),buffer.getEnd(),buffer.getLength());
- a = buffer.Write(datachar,10*4);
- if(a<=0)
- NSLog(@"写失败");
- NSLog(@"write %d之后 m_nReadPos:%d,m_nWritePos:%d,Length:%d", a, buffer.GetReadPos() , buffer.GetWritePos(),buffer.GetLength());
- num++;
- }
- if(buffer.GetLength()<60)
- {
- dataint[0] = num;
- NSLog(@"write 之前 m_nReadPos:%d,m_nWritePos:%d,Length:%d", buffer.GetReadPos() , buffer.GetWritePos(),buffer.GetLength());
- a = buffer.Write(datachar,10*4);
- if(a<=0)
- NSLog(@"写失败");
- NSLog(@"write %d之后 m_nReadPos:%d,m_nWritePos:%d,Length:%d", a, buffer.GetReadPos() , buffer.GetWritePos(),buffer.GetLength());
- num++;
- }
- if(buffer.GetLength()>=10*4)
- {
- NSLog(@"read 之前 m_nReadPos:%d,m_nWritePos:%d,Length:%d", buffer.GetReadPos() , buffer.GetWritePos(),buffer.GetLength());
- a = buffer.Read(reschar,10*4);
- NSLog(@"read %d之后 m_nReadPos:%d,m_nWritePos:%d,Length:%d", a, buffer.GetReadPos() , buffer.GetWritePos(),buffer.GetLength());
- }
- }