设为首页 加入收藏

TOP

C++版的循环缓冲区类
2014-11-24 01:01:09 来源: 作者: 【 】 浏览:1
Tags:循环 缓冲区

C++版的循环缓冲区类(实际测试可用!)


/*
* CCycleBuffer.h
*
* Created on: 2013-5-27
* Author: shiguang
*/


#ifndef __test__CCycleBuffer__
#define __test__CCycleBuffer__
#include
#include
#include
#include
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__) */



/*
* main.cpp
*
* Created on: 2013-5-27
* Author: shiguang
*/


#include "CCycleBuffer.h"


int main()
{
CCycleBuffer buffer(100);
char* datachar = new char[40];
char* reschar = new char[100];


int res = 0;


for (int i = 0; i < 40; ++i)
{
datachar[i] = i + 1;
}
res = buffer.Write(datachar, 40);
printf("写入数据个数:%d\n", res);
res = buffer.Write(datachar, 40);
printf("写入数据个数:%d\n", res);
res = buffer.Write(datachar, 40);
printf("写入数据个数:%d\n", res);
res = buffer.Write(datachar, 40);
printf("写入数据个数:%d\n", res);


res = buffer.Read(reschar, 100);
printf("读取数据个数:%d\n", res);
for (int i = 0; i < 100; ++i)
{
if (i % 10 == 0)
{
printf("\n");
}
printf("%2d ", reschar[i]);
}


return 0;
}


输出结果:


写入数据个数:40
写入数据个数:40
写入数据个数:20
写入数据个数:0
读取数据个数:100


1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20


源程序来自网络资料!


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇TinyOS生成docs时报UnicodeDecode.. 下一篇C++学习之继承:点和圆

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: